From 31e3f51638ceb217655b29db5927e7e949490a37 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Thu, 5 Oct 2023 11:31:53 +0200 Subject: [PATCH 1/7] Initial Asessment API test Signed-off-by: Marek Aufart --- binding/assessment.go | 50 ++++++++++++++++++++ binding/richclient.go | 4 ++ test/api/assessment/api_test.go | 81 +++++++++++++++++++++++++++++++++ test/api/assessment/pkg.go | 20 ++++++++ test/api/assessment/samples.go | 53 +++++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 binding/assessment.go create mode 100644 test/api/assessment/api_test.go create mode 100644 test/api/assessment/pkg.go create mode 100644 test/api/assessment/samples.go diff --git a/binding/assessment.go b/binding/assessment.go new file mode 100644 index 000000000..ebe4cf6a9 --- /dev/null +++ b/binding/assessment.go @@ -0,0 +1,50 @@ +package binding + +import ( + "github.com/konveyor/tackle2-hub/api" +) + +// +// Assessment API. +type Assessment struct { + client *Client +} + +// +// Create a Assessment. +func (h *Assessment) Create(r *api.Assessment) (err error) { + err = h.client.Post(api.AssessmentsRoot, &r) + return +} + +// +// Get a Assessment by ID. +func (h *Assessment) Get(id uint) (r *api.Assessment, err error) { + r = &api.Assessment{} + path := Path(api.AssessmentRoot).Inject(Params{api.ID: id}) + err = h.client.Get(path, r) + return +} + +// +// List Assessments. +func (h *Assessment) List() (list []api.Assessment, err error) { + list = []api.Assessment{} + err = h.client.Get(api.AssessmentsRoot, &list) + return +} + +// +// Update a Assessment. +func (h *Assessment) Update(r *api.Assessment) (err error) { + path := Path(api.AssessmentRoot).Inject(Params{api.ID: r.ID}) + err = h.client.Put(path, r) + return +} + +// +// Delete a Assessment. +func (h *Assessment) Delete(id uint) (err error) { + err = h.client.Delete(Path(api.AssessmentRoot).Inject(Params{api.ID: id})) + return +} diff --git a/binding/richclient.go b/binding/richclient.go index 6302ed07a..a586e4856 100644 --- a/binding/richclient.go +++ b/binding/richclient.go @@ -22,6 +22,7 @@ func init() { type RichClient struct { // Resources APIs. Application Application + Assessment Assessment Bucket Bucket BusinessService BusinessService Dependency Dependency @@ -58,6 +59,9 @@ func New(baseUrl string) (r *RichClient) { Application: Application{ client: client, }, + Assessment: Assessment{ + client: client, + }, Bucket: Bucket{ client: client, }, diff --git a/test/api/assessment/api_test.go b/test/api/assessment/api_test.go new file mode 100644 index 000000000..1445021e8 --- /dev/null +++ b/test/api/assessment/api_test.go @@ -0,0 +1,81 @@ +package assessment + +import ( + "fmt" + "testing" + + "github.com/konveyor/tackle2-hub/test/assert" +) + +func TestAssessmentCRUD(t *testing.T) { + for _, r := range Samples { + t.Run(fmt.Sprintf("%s for application %s", r.Questionnaire.Name, r.Application.Name), func(t *testing.T) { + // Create. + err := Assessment.Create(&r) + if err != nil { + t.Errorf(err.Error()) + } + + // Get. + got, err := Assessment.Get(r.ID) + if err != nil { + t.Errorf(err.Error()) + } + if assert.FlatEqual(got, r) { + t.Errorf("Different response error. Got %v, expected %v", got, r) + } + + // Update. + r.Name = "Updated " + r.Name + r.Required = false + err = Assessment.Update(&r) + if err != nil { + t.Errorf(err.Error()) + } + + got, err = Assessment.Get(r.ID) + if err != nil { + t.Errorf(err.Error()) + } + if got.Name != r.Name { + t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name) + } + if got.Required != false { + t.Errorf("Required should be false after update. Got %+v, expected %+v", got, r) + } + + // Delete. + err = Assessment.Delete(r.ID) + if err != nil { + t.Errorf(err.Error()) + } + + _, err = Assessment.Get(r.ID) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", r) + } + }) + } +} + +func TestAssessmentList(t *testing.T) { + samples := Samples + + for name := range samples { + sample := samples[name] + assert.Must(t, Assessment.Create(&sample)) + samples[name] = sample + } + + got, err := Assessment.List() + if err != nil { + t.Errorf(err.Error()) + } + if assert.FlatEqual(got, &samples) { + t.Errorf("Different response error. Got %v, expected %v", got, samples) + } + + for _, r := range samples { + assert.Must(t, Assessment.Delete(r.ID)) + } +} diff --git a/test/api/assessment/pkg.go b/test/api/assessment/pkg.go new file mode 100644 index 000000000..eb8c06323 --- /dev/null +++ b/test/api/assessment/pkg.go @@ -0,0 +1,20 @@ +package assessment + +import ( + "github.com/konveyor/tackle2-hub/binding" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +var ( + RichClient *binding.RichClient + Assessment binding.Assessment +) + + +func init() { + // Prepare RichClient and login to Hub API (configured from env variables). + RichClient = client.PrepareRichClient() + + // Shortcut for Assessment-related RichClient methods. + Assessment = RichClient.Assessment +} diff --git a/test/api/assessment/samples.go b/test/api/assessment/samples.go new file mode 100644 index 000000000..7fce20351 --- /dev/null +++ b/test/api/assessment/samples.go @@ -0,0 +1,53 @@ +package assessment + +import ( + "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/assessment" + "github.com/konveyor/tackle2-hub/test/api/application" + "github.com/konveyor/tackle2-hub/test/api/questionnaire" +) + +// Set of valid resources for tests and reuse. +var ( + Assessment1 = api.Assessment{ + // Ref resource are created by the test. + Application: &api.Ref{ + Name: application.Minimal.Name, + }, + Questionnaire: api.Ref{ + Name: questionnaire.Questionnaire1.Name, + }, + Sections: []assessment.Section{ + { + Order: 1, + Name: "Section 1", + Questions: []assessment.Question{ + { + Order: 1, + Text: "What is your favorite color?", + Explanation: "Please tell us your favorite color.", + Answers: []assessment.Answer{ + { + Order: 1, + Text: "Red", + Risk: "red", + }, + { + Order: 2, + Text: "Green", + Risk: "green", + }, + { + Order: 3, + Text: "Blue", + Risk: "yellow", + Selected: true, + }, + }, + }, + }, + }, + }, + } + Samples = []api.Assessment{Assessment1} +) From f1692f5298d2c9391bda411471e20f931e37c74f Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Wed, 13 Dec 2023 15:15:09 +0100 Subject: [PATCH 2/7] Update Application Assessment test Signed-off-by: Marek Aufart --- binding/application.go | 17 +++++++++++ test/api/assessment/api_test.go | 53 +++++++++++++-------------------- test/api/assessment/samples.go | 5 ++-- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/binding/application.go b/binding/application.go index 8927b62fa..881d991b0 100644 --- a/binding/application.go +++ b/binding/application.go @@ -319,6 +319,23 @@ func (h *Application) Analysis(id uint) (a Analysis) { return } +// +// Create an Application Assessment. +func (h *Application) CreateAssesment(id uint, r *api.Assessment) (err error) { + path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: id}) + err = h.client.Post(path, &r) + return +} + +// +// Get Application Assessments. +func (h *Application) GetAssesments(id uint) (list []api.Assessment, err error) { + list = []api.Assessment{} + path := Path(api.AppAssessmentsRoot).Inject(Params{api.ID: id}) + err = h.client.Get(path, &list) + return +} + // // Analysis API. type Analysis struct { diff --git a/test/api/assessment/api_test.go b/test/api/assessment/api_test.go index 1445021e8..643d1dc88 100644 --- a/test/api/assessment/api_test.go +++ b/test/api/assessment/api_test.go @@ -4,16 +4,23 @@ import ( "fmt" "testing" + "github.com/konveyor/tackle2-hub/api" "github.com/konveyor/tackle2-hub/test/assert" ) func TestAssessmentCRUD(t *testing.T) { for _, r := range Samples { t.Run(fmt.Sprintf("%s for application %s", r.Questionnaire.Name, r.Application.Name), func(t *testing.T) { - // Create. - err := Assessment.Create(&r) - if err != nil { - t.Errorf(err.Error()) + // Create via parent resource. + if r.Application.Name != "" { + app := api.Application{Name: r.Application.Name} + assert.Must(t, RichClient.Application.Create(&app)) + r.Application.ID = app.ID + err := RichClient.Application.CreateAssesment(app.ID, &r) + if err != nil { + t.Errorf(err.Error()) + } + } // Get. @@ -25,9 +32,9 @@ func TestAssessmentCRUD(t *testing.T) { t.Errorf("Different response error. Got %v, expected %v", got, r) } - // Update. - r.Name = "Updated " + r.Name - r.Required = false + // Update example - select green instead of blue. + r.Sections[0].Questions[0].Answers[2].Selected = false // blue (default) + r.Sections[0].Questions[0].Answers[1].Selected = true // green err = Assessment.Update(&r) if err != nil { t.Errorf(err.Error()) @@ -37,11 +44,11 @@ func TestAssessmentCRUD(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - if got.Name != r.Name { - t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name) + if got.Sections[0].Questions[0].Answers[2].Selected { // blue not selected + t.Errorf("Different response error. Blue should not be selected.") } - if got.Required != false { - t.Errorf("Required should be false after update. Got %+v, expected %+v", got, r) + if !got.Sections[0].Questions[0].Answers[1].Selected { // green selected + t.Errorf("Different response error. Green should be selected.") } // Delete. @@ -54,28 +61,8 @@ func TestAssessmentCRUD(t *testing.T) { if err == nil { t.Errorf("Resource exits, but should be deleted: %v", r) } - }) - } -} - -func TestAssessmentList(t *testing.T) { - samples := Samples - for name := range samples { - sample := samples[name] - assert.Must(t, Assessment.Create(&sample)) - samples[name] = sample - } - - got, err := Assessment.List() - if err != nil { - t.Errorf(err.Error()) - } - if assert.FlatEqual(got, &samples) { - t.Errorf("Different response error. Got %v, expected %v", got, samples) - } - - for _, r := range samples { - assert.Must(t, Assessment.Delete(r.ID)) + assert.Must(t, RichClient.Application.Delete(r.Application.ID)) + }) } } diff --git a/test/api/assessment/samples.go b/test/api/assessment/samples.go index 7fce20351..961562bcb 100644 --- a/test/api/assessment/samples.go +++ b/test/api/assessment/samples.go @@ -9,12 +9,13 @@ import ( // Set of valid resources for tests and reuse. var ( - Assessment1 = api.Assessment{ + ApplicationAssessment1 = api.Assessment{ // Ref resource are created by the test. Application: &api.Ref{ Name: application.Minimal.Name, }, Questionnaire: api.Ref{ + ID: 1, Name: questionnaire.Questionnaire1.Name, }, Sections: []assessment.Section{ @@ -49,5 +50,5 @@ var ( }, }, } - Samples = []api.Assessment{Assessment1} + Samples = []api.Assessment{ApplicationAssessment1} ) From 3dd131c8a3ae61678126a657fcb37447a17bbc9f Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Wed, 13 Dec 2023 15:17:28 +0100 Subject: [PATCH 3/7] Remove not existing assessment create Signed-off-by: Marek Aufart --- binding/assessment.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/binding/assessment.go b/binding/assessment.go index ebe4cf6a9..184d987f7 100644 --- a/binding/assessment.go +++ b/binding/assessment.go @@ -10,13 +10,6 @@ type Assessment struct { client *Client } -// -// Create a Assessment. -func (h *Assessment) Create(r *api.Assessment) (err error) { - err = h.client.Post(api.AssessmentsRoot, &r) - return -} - // // Get a Assessment by ID. func (h *Assessment) Get(id uint) (r *api.Assessment, err error) { From ec8daa68a2843ca719ec3af4a7dcaa50cb143ff7 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Thu, 14 Dec 2023 10:41:54 +0100 Subject: [PATCH 4/7] Add Application Assessment Get Signed-off-by: Marek Aufart --- test/api/assessment/api_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/api/assessment/api_test.go b/test/api/assessment/api_test.go index 643d1dc88..16e719a7b 100644 --- a/test/api/assessment/api_test.go +++ b/test/api/assessment/api_test.go @@ -32,6 +32,21 @@ func TestAssessmentCRUD(t *testing.T) { t.Errorf("Different response error. Got %v, expected %v", got, r) } + // Get via parent object Application. + gotList, err := RichClient.Application.GetAssesments(r.Application.ID) + if err != nil { + t.Errorf(err.Error()) + } + found := false + for _, gotItem := range gotList { + if gotItem.ID == r.ID { + found = true + } + } + if !found { + t.Errorf("Cannot find Assessment ID:%d on parent Application ID:%d", r.ID, r.Application.ID) + } + // Update example - select green instead of blue. r.Sections[0].Questions[0].Answers[2].Selected = false // blue (default) r.Sections[0].Questions[0].Answers[1].Selected = true // green From 8dc495d0e1c24fd63ebe6dc527f17545f7e6e527 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Thu, 14 Dec 2023 13:01:02 +0100 Subject: [PATCH 5/7] Fix uints in Section order Signed-off-by: Marek Aufart --- test/api/assessment/samples.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/api/assessment/samples.go b/test/api/assessment/samples.go index 961562bcb..526247be7 100644 --- a/test/api/assessment/samples.go +++ b/test/api/assessment/samples.go @@ -15,31 +15,31 @@ var ( Name: application.Minimal.Name, }, Questionnaire: api.Ref{ - ID: 1, + ID: 1, Name: questionnaire.Questionnaire1.Name, }, Sections: []assessment.Section{ { - Order: 1, + Order: uint(1), Name: "Section 1", Questions: []assessment.Question{ { - Order: 1, + Order: uint(1), Text: "What is your favorite color?", Explanation: "Please tell us your favorite color.", Answers: []assessment.Answer{ { - Order: 1, + Order: uint(1), Text: "Red", Risk: "red", }, { - Order: 2, + Order: uint(2), Text: "Green", Risk: "green", }, { - Order: 3, + Order: uint(3), Text: "Blue", Risk: "yellow", Selected: true, From 6e388661ae3847dca52a9798ba78f65de9258243 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Thu, 14 Dec 2023 13:33:48 +0100 Subject: [PATCH 6/7] Use uintptr for Order Signed-off-by: Marek Aufart --- test/api/assessment/pkg.go | 4 ++++ test/api/assessment/samples.go | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/test/api/assessment/pkg.go b/test/api/assessment/pkg.go index eb8c06323..dedc942e3 100644 --- a/test/api/assessment/pkg.go +++ b/test/api/assessment/pkg.go @@ -18,3 +18,7 @@ func init() { // Shortcut for Assessment-related RichClient methods. Assessment = RichClient.Assessment } + +func uint2ptr(u uint) *uint { + return &u +} \ No newline at end of file diff --git a/test/api/assessment/samples.go b/test/api/assessment/samples.go index 526247be7..90a09a0cb 100644 --- a/test/api/assessment/samples.go +++ b/test/api/assessment/samples.go @@ -20,26 +20,26 @@ var ( }, Sections: []assessment.Section{ { - Order: uint(1), + Order: uint2ptr(1), Name: "Section 1", Questions: []assessment.Question{ { - Order: uint(1), + Order: uint2ptr(1), Text: "What is your favorite color?", Explanation: "Please tell us your favorite color.", Answers: []assessment.Answer{ { - Order: uint(1), + Order: uint2ptr(1), Text: "Red", Risk: "red", }, { - Order: uint(2), + Order: uint2ptr(2), Text: "Green", Risk: "green", }, { - Order: uint(3), + Order: uint2ptr(3), Text: "Blue", Risk: "yellow", Selected: true, From 4f5b3590a3ff58d068b797ef6850c83f46d84405 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Mon, 18 Dec 2023 11:10:12 +0100 Subject: [PATCH 7/7] Create questionnaire by test itself Signed-off-by: Marek Aufart --- test/api/assessment/api_test.go | 7 +++++++ test/api/assessment/samples.go | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/test/api/assessment/api_test.go b/test/api/assessment/api_test.go index 16e719a7b..33815dcfe 100644 --- a/test/api/assessment/api_test.go +++ b/test/api/assessment/api_test.go @@ -5,12 +5,18 @@ import ( "testing" "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/test/api/questionnaire" "github.com/konveyor/tackle2-hub/test/assert" ) func TestAssessmentCRUD(t *testing.T) { for _, r := range Samples { t.Run(fmt.Sprintf("%s for application %s", r.Questionnaire.Name, r.Application.Name), func(t *testing.T) { + // Prepare questionnaire + questionnaire := questionnaire.Questionnaire1 + assert.Must(t, RichClient.Questionnaire.Create(&questionnaire)) + r.Questionnaire.ID = questionnaire.ID + // Create via parent resource. if r.Application.Name != "" { app := api.Application{Name: r.Application.Name} @@ -78,6 +84,7 @@ func TestAssessmentCRUD(t *testing.T) { } assert.Must(t, RichClient.Application.Delete(r.Application.ID)) + assert.Must(t, RichClient.Questionnaire.Delete(r.Questionnaire.ID)) }) } } diff --git a/test/api/assessment/samples.go b/test/api/assessment/samples.go index 90a09a0cb..1a1ec5493 100644 --- a/test/api/assessment/samples.go +++ b/test/api/assessment/samples.go @@ -15,7 +15,6 @@ var ( Name: application.Minimal.Name, }, Questionnaire: api.Ref{ - ID: 1, Name: questionnaire.Questionnaire1.Name, }, Sections: []assessment.Section{