From 81422c11b723c7e2ade213dce209205098bb321f Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Mon, 2 Oct 2023 09:59:43 +0200 Subject: [PATCH] :seedling: Add Questionnaire to binding and API test (#492) Adding Questionnaire endpoint to binding and basic API test. Started looking on Pathfinder export/Assessment import (via CLI), adding basic API test as a side-effect. Related to https://github.com/konveyor/tackle2-hub/pull/492 --------- Signed-off-by: Marek Aufart --- binding/questionnaire.go | 50 +++++++++++++++++++ binding/richclient.go | 6 ++- docs/test-api-matrix.md | 4 ++ test/api/questionnaire/api_test.go | 80 ++++++++++++++++++++++++++++++ test/api/questionnaire/pkg.go | 20 ++++++++ test/api/questionnaire/samples.go | 49 ++++++++++++++++++ 6 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 binding/questionnaire.go create mode 100644 test/api/questionnaire/api_test.go create mode 100644 test/api/questionnaire/pkg.go create mode 100644 test/api/questionnaire/samples.go diff --git a/binding/questionnaire.go b/binding/questionnaire.go new file mode 100644 index 000000000..1dd529b8c --- /dev/null +++ b/binding/questionnaire.go @@ -0,0 +1,50 @@ +package binding + +import ( + "github.com/konveyor/tackle2-hub/api" +) + +// +// Questionnaire API. +type Questionnaire struct { + client *Client +} + +// +// Create a Questionnaire. +func (h *Questionnaire) Create(r *api.Questionnaire) (err error) { + err = h.client.Post(api.QuestionnairesRoot, &r) + return +} + +// +// Get a Questionnaire by ID. +func (h *Questionnaire) Get(id uint) (r *api.Questionnaire, err error) { + r = &api.Questionnaire{} + path := Path(api.QuestionnaireRoot).Inject(Params{api.ID: id}) + err = h.client.Get(path, r) + return +} + +// +// List Questionnaires. +func (h *Questionnaire) List() (list []api.Questionnaire, err error) { + list = []api.Questionnaire{} + err = h.client.Get(api.QuestionnairesRoot, &list) + return +} + +// +// Update a Questionnaire. +func (h *Questionnaire) Update(r *api.Questionnaire) (err error) { + path := Path(api.QuestionnaireRoot).Inject(Params{api.ID: r.ID}) + err = h.client.Put(path, r) + return +} + +// +// Delete a Questionnaire. +func (h *Questionnaire) Delete(id uint) (err error) { + err = h.client.Delete(Path(api.QuestionnaireRoot).Inject(Params{api.ID: id})) + return +} diff --git a/binding/richclient.go b/binding/richclient.go index 547e54d2e..6302ed07a 100644 --- a/binding/richclient.go +++ b/binding/richclient.go @@ -24,11 +24,13 @@ type RichClient struct { Application Application Bucket Bucket BusinessService BusinessService + Dependency Dependency File File Identity Identity JobFunction JobFunction MigrationWave MigrationWave Proxy Proxy + Questionnaire Questionnaire Review Review RuleSet RuleSet Setting Setting @@ -39,7 +41,6 @@ type RichClient struct { Target Target Task Task Tracker Tracker - Dependency Dependency // A REST client. Client *Client @@ -81,6 +82,9 @@ func New(baseUrl string) (r *RichClient) { Proxy: Proxy{ client: client, }, + Questionnaire: Questionnaire{ + client: client, + }, Review: Review{ client: client, }, diff --git a/docs/test-api-matrix.md b/docs/test-api-matrix.md index 4e4a3205d..1c4a1e635 100644 --- a/docs/test-api-matrix.md +++ b/docs/test-api-matrix.md @@ -27,6 +27,10 @@ batch|||| migrationwave|:heavy_check_mark:|:heavy_check_mark:|| ticket|||| tracker|:heavy_check_mark:|:heavy_check_mark:|| +**Assessments**|||| +archetype|||| +assessment|||| +questionnaire|:heavy_check_mark:|:heavy_check_mark:|| **Other**|||| addon || | | auth|||| diff --git a/test/api/questionnaire/api_test.go b/test/api/questionnaire/api_test.go new file mode 100644 index 000000000..b323df35e --- /dev/null +++ b/test/api/questionnaire/api_test.go @@ -0,0 +1,80 @@ +package questionnaire + +import ( + "testing" + + "github.com/konveyor/tackle2-hub/test/assert" +) + +func TestQuestionnaireCRUD(t *testing.T) { + for _, r := range Samples { + t.Run(r.Name, func(t *testing.T) { + // Create. + err := Questionnaire.Create(&r) + if err != nil { + t.Errorf(err.Error()) + } + + // Get. + got, err := Questionnaire.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 = Questionnaire.Update(&r) + if err != nil { + t.Errorf(err.Error()) + } + + got, err = Questionnaire.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 = Questionnaire.Delete(r.ID) + if err != nil { + t.Errorf(err.Error()) + } + + _, err = Questionnaire.Get(r.ID) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", r) + } + }) + } +} + +func TestQuestionnaireList(t *testing.T) { + samples := Samples + + for name := range samples { + sample := samples[name] + assert.Must(t, Questionnaire.Create(&sample)) + samples[name] = sample + } + + got, err := Questionnaire.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, Questionnaire.Delete(r.ID)) + } +} diff --git a/test/api/questionnaire/pkg.go b/test/api/questionnaire/pkg.go new file mode 100644 index 000000000..479a880a6 --- /dev/null +++ b/test/api/questionnaire/pkg.go @@ -0,0 +1,20 @@ +package questionnaire + +import ( + "github.com/konveyor/tackle2-hub/binding" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +var ( + RichClient *binding.RichClient + Questionnaire binding.Questionnaire +) + + +func init() { + // Prepare RichClient and login to Hub API (configured from env variables). + RichClient = client.PrepareRichClient() + + // Shortcut for Questionnaire-related RichClient methods. + Questionnaire = RichClient.Questionnaire +} diff --git a/test/api/questionnaire/samples.go b/test/api/questionnaire/samples.go new file mode 100644 index 000000000..eb76ea150 --- /dev/null +++ b/test/api/questionnaire/samples.go @@ -0,0 +1,49 @@ +package questionnaire + +import ( + "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/assessment" +) + +// Set of valid resources for tests and reuse. +var ( + Questionnaire1 = api.Questionnaire{ + Name: "Questionnaire1", + Description: "Questionnaire minimal sample 1", + Required: true, + Thresholds: assessment.Thresholds{}, + RiskMessages: assessment.RiskMessages{}, + 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.Questionnaire{Questionnaire1} +)