Skip to content

Commit

Permalink
🌱 Add Questionnaire to binding and API test (#492)
Browse files Browse the repository at this point in the history
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 #492

---------

Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi authored Oct 2, 2023
1 parent 546f14b commit 81422c1
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
50 changes: 50 additions & 0 deletions binding/questionnaire.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +41,6 @@ type RichClient struct {
Target Target
Task Task
Tracker Tracker
Dependency Dependency

// A REST client.
Client *Client
Expand Down Expand Up @@ -81,6 +82,9 @@ func New(baseUrl string) (r *RichClient) {
Proxy: Proxy{
client: client,
},
Questionnaire: Questionnaire{
client: client,
},
Review: Review{
client: client,
},
Expand Down
4 changes: 4 additions & 0 deletions docs/test-api-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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||||
Expand Down
80 changes: 80 additions & 0 deletions test/api/questionnaire/api_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
20 changes: 20 additions & 0 deletions test/api/questionnaire/pkg.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions test/api/questionnaire/samples.go
Original file line number Diff line number Diff line change
@@ -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}
)

0 comments on commit 81422c1

Please sign in to comment.