Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 add csv import test #390

Merged
merged 22 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions binding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (r *Client) Get(path string, object interface{}, params ...Param) (err erro
err = liberr.Wrap(err)
return
}
err = json.Unmarshal(body, object)
err = json.Unmarshal(body, &object)
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err = liberr.Wrap(err)
return
Expand Down Expand Up @@ -361,7 +361,7 @@ func (r *Client) BucketPut(source, destination string) (err error) {
request.Header.Set(api.Directory, api.DirectoryExpand)
err = r.putDir(part, source)
} else {
err = r.putFile(part, source)
err = r.loadFile(part, source)
}
return
}
Expand Down Expand Up @@ -449,7 +449,7 @@ func (r *Client) FilePut(path, source string, object interface{}) (err error) {
request.Header.Add(
api.ContentType,
writer.FormDataContentType())
err = r.putFile(part, source)
err = r.loadFile(part, source)
return
}
reply, err := r.send(request)
Expand Down Expand Up @@ -479,6 +479,69 @@ func (r *Client) FilePut(path, source string, object interface{}) (err error) {
return
}

//
// FilePost uploads a file.
// Returns the created File resource.
func (r *Client) FilePost(path, source string, object interface{}) (err error) {
isDir, err := r.isDir(source, true)
if err != nil {
return
}
if isDir {
err = liberr.New("Source cannot be directory.")
return
}
request := func() (request *http.Request, err error) {
buf := new(bytes.Buffer)
request = &http.Request{
Header: http.Header{},
Method: http.MethodPost,
Body: io.NopCloser(buf),
URL: r.join(path),
}
request.Header.Set(api.Accept, binding.MIMEJSON)
writer := multipart.NewWriter(buf)
defer func() {
_ = writer.Close()
}()
part, nErr := writer.CreateFormFile(api.FileField, pathlib.Base(source))
if err != nil {
err = liberr.Wrap(nErr)
return
}
request.Header.Add(
api.ContentType,
writer.FormDataContentType())
err = r.loadFile(part, source)
return
}
reply, err := r.send(request)
if err != nil {
return
}
status := reply.StatusCode
switch status {
case http.StatusOK,
http.StatusCreated:
var body []byte
body, err = io.ReadAll(reply.Body)
if err != nil {
err = liberr.Wrap(err)
return
}
err = json.Unmarshal(body, &object)
if err != nil {
err = liberr.Wrap(err)
return
}
case http.StatusConflict:
err = &Conflict{Path: path}
default:
err = liberr.New(http.StatusText(status))
}
return
}

//
// getDir downloads and expands a directory.
func (r *Client) getDir(body io.Reader, output string) (err error) {
Expand Down Expand Up @@ -610,8 +673,8 @@ func (r *Client) getFile(body io.Reader, path, output string) (err error) {
}

//
// putFile uploads plain file.
func (r *Client) putFile(writer io.Writer, input string) (err error) {
// loadFile uploads a file.
func (r *Client) loadFile(writer io.Writer, input string) (err error) {
file, err := os.Open(input)
if err != nil {
err = liberr.Wrap(err)
Expand Down
29 changes: 29 additions & 0 deletions test/api/importCSV/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package importCSV
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

import (
"strconv"
"testing"
)

func TestImportCRUD(t *testing.T) {
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
for _, r := range Samples {
t.Run("CSV_Import", func(t *testing.T) {
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

// Upload CSV.
inputData := make(map[string]interface{})
err := Client.FilePost("/importsummaries/upload", r.fileName, &inputData)
if err != nil {
t.Errorf(err.Error())
}

// Get summaries.
id := uint64(inputData["id"].(float64))
var inputID = strconv.FormatUint(id, 10)
outputImport := make(map[string]interface{})
err = Client.Get("/importsummaries/"+inputID, &outputImport)
if err != nil {
t.Errorf("CSV import failed")
}
})
}
}
19 changes: 19 additions & 0 deletions test/api/importCSV/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package importCSV

import (
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/test/api/client"
)

var (
RichClient *binding.RichClient
Client *binding.Client
)

func init() {
// Prepare RichClient and login to Hub API (configured from env variables).
RichClient = client.PrepareRichClient()

// Access REST client directly
Client = RichClient.Client()
}
49 changes: 49 additions & 0 deletions test/api/importCSV/samples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package importCSV

import (
"github.com/konveyor/tackle2-hub/api"
)

type TestCase struct {
fileName string
ExpectedApplications []api.Application
ExpectedDependencies []api.Dependency
}

var (
Applications = []api.Application{
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
{
Name: "Gateway",
Description: "Gateway application",
},
{
Name: "Inventory",
Description: "Inventory application",
},
}
Dependencies = []api.Dependency{
{
To: api.Ref{
Name: "Gateway",
},
From: api.Ref{
Name: "Inventory",
},
},
{
To: api.Ref{
Name: "Inventory",
},
From: api.Ref{
Name: "Customers",
},
},
}
Samples = []TestCase{
{
fileName: "template_application_import.csv",
ExpectedApplications: Applications,
ExpectedDependencies: Dependencies,
},
}
)
6 changes: 6 additions & 0 deletions test/api/importCSV/template_application_import.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Record Type 1,Application Name,Description,Comments,Business Service,Dependency,Dependency Direction,Binary Group,Binary Artifact,Binary Version,Binary Packaging,Repository Type,Repository URL,Repository Branch,Repository Path,Tag Category 1,Tag 1,Tag Category 2,Tag 2,Tag Category 3,Tag 3,Tag Category 4,Tag 4,Tag Category 5,Tag 5,Tag Category 6,Tag 6,Tag Category 7,Tag 7,Tag Category 8,Tag 8,Tag Category 9,Tag 9,Tag Category 10,Tag 10,Tag Category 11,Tag 11,Tag Category 12,Tag 12,Tag Category 13,Tag 13,Tag Category 14,Tag 14,Tag Category 15,Tag 15,Tag Category 16,Tag 16,Tag Category 17,Tag 17,Tag Category 18,Tag 18,Tag Category 19,Tag 19,Tag Category 20,Tag 20
1,Customers,Legacy Customers management service,,Retail,,,corp.acme.demo,customers-tomcat,0.0.1-SNAPSHOT,war,git,https://git-acme.local/customers.git,,,Operating System,RHEL 8,Database,Oracle,Language,Java,Runtime,Tomcat,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1,Inventory,Inventory service,,Retail,,,corp.acme.demo,inventory,0.1.1-SNAPSHOT,war,git,https://git-acme.local/inventory.git,,,Operating System,RHEL 8,Database,Postgresql,Language,Java,Runtime,Quarkus,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1,Gateway,API Gateway,,Retail,,,corp.acme.demo,gateway,0.1.1-SNAPSHOT,war,git,https://git-acme.local/gateway.git,,,Operating System,RHEL 8,,,Language,Java,Runtime,Spring Boot,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2,Gateway,,,,Inventory,southbound
2,Gateway,,,,Customers,southbound