Skip to content

Commit

Permalink
🌱 Add Assessment to binding and API test (#562)
Browse files Browse the repository at this point in the history
Adding basic API test for Assessments.

Related to #494

---------

Signed-off-by: Marek Aufart <[email protected]>
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi authored Dec 19, 2023
1 parent 2427494 commit 377ab76
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 1 deletion.
17 changes: 17 additions & 0 deletions binding/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions binding/assessment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package binding

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

//
// Assessment API.
type Assessment struct {
client *Client
}

//
// 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
}
6 changes: 5 additions & 1 deletion binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type RichClient struct {
// Resources APIs.
Application Application
Archetype Archetype
Assessment Assessment
Bucket Bucket
BusinessService BusinessService
Dependency Dependency
Expand Down Expand Up @@ -60,9 +61,12 @@ func New(baseUrl string) (r *RichClient) {
Application: Application{
client: client,
},
Archetype: Archetype{
Archetype: Archetype{
client: client,
},
Assessment: Assessment{
client: client,
},
Bucket: Bucket{
client: client,
},
Expand Down
90 changes: 90 additions & 0 deletions test/api/assessment/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package assessment

import (
"fmt"
"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}
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.
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)
}

// 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
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.Sections[0].Questions[0].Answers[2].Selected { // blue not selected
t.Errorf("Different response error. Blue should not be selected.")
}
if !got.Sections[0].Questions[0].Answers[1].Selected { // green selected
t.Errorf("Different response error. Green should be selected.")
}

// 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)
}

assert.Must(t, RichClient.Application.Delete(r.Application.ID))
assert.Must(t, RichClient.Questionnaire.Delete(r.Questionnaire.ID))
})
}
}
24 changes: 24 additions & 0 deletions test/api/assessment/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
}

func uint2ptr(u uint) *uint {
return &u
}
53 changes: 53 additions & 0 deletions test/api/assessment/samples.go
Original file line number Diff line number Diff line change
@@ -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 (
ApplicationAssessment1 = 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: uint2ptr(1),
Name: "Section 1",
Questions: []assessment.Question{
{
Order: uint2ptr(1),
Text: "What is your favorite color?",
Explanation: "Please tell us your favorite color.",
Answers: []assessment.Answer{
{
Order: uint2ptr(1),
Text: "Red",
Risk: "red",
},
{
Order: uint2ptr(2),
Text: "Green",
Risk: "green",
},
{
Order: uint2ptr(3),
Text: "Blue",
Risk: "yellow",
Selected: true,
},
},
},
},
},
},
}
Samples = []api.Assessment{ApplicationAssessment1}
)

0 comments on commit 377ab76

Please sign in to comment.