Skip to content

Commit

Permalink
Add StakeHolder group API test
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Mar 21, 2023
1 parent fd3efde commit 6424ac5
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/api/stakeholdergroup/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package stakeholdergroup

import (
"fmt"
"testing"

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

func TestStakeholderGroupCRUD(t *testing.T) {
samples := CloneSamples()

for _, r := range samples {
t.Run(r.Name, func(t *testing.T) {
// Create.
err := Client.Post(api.StakeholderGroupsRoot, &r)
if err != nil {
t.Errorf(err.Error())
}
rPath := fmt.Sprintf("%s/%d", api.StakeholderGroupsRoot, r.ID)

// Get.
gotR := api.StakeholderGroup{}
err = Client.Get(rPath, &gotR)
if err != nil {
t.Errorf(err.Error())
}
if client.FlatEqual(gotR, r) {
t.Errorf("Different response error. Got %v, expected %v", gotR, r)
}

// Update.
updatedR := api.StakeholderGroup{
Name: "Updated " + r.Name,
}
err = Client.Put(rPath, updatedR)
if err != nil {
t.Errorf(err.Error())
}

err = Client.Get(rPath, &gotR)
if err != nil {
t.Errorf(err.Error())
}
if gotR.Name != updatedR.Name {
t.Errorf("Different response error. Got %s, expected %s", gotR.Name, updatedR.Name)
}

// Delete.
err = Client.Delete(rPath)
if err != nil {
t.Errorf(err.Error())
}

err = Client.Get(rPath, &r)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", r)
}

// Cleanup.
Delete(t, r)
})
}
}

func TestStakeholderGroupList(t *testing.T) {
samples := CloneSamples()
for _, r := range samples {
Create(t, r)
}

gotList := []api.StakeholderGroup{}
err := Client.Get(api.TagCategoriesRoot, &gotList)
if err != nil {
t.Errorf(err.Error())
}
if client.FlatEqual(gotList, samples) {
t.Errorf("Different response error. Got %v, expected %v", gotList, samples)
}

for _, r := range samples {
Delete(t, r)
}

}
57 changes: 57 additions & 0 deletions test/api/stakeholdergroup/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package stakeholdergroup

import (
"encoding/json"
"fmt"
"testing"

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

var (
// Setup Hub API client
Client = client.Client
)

//
// Set of valid resources for tests and reuse.
var Samples = []*api.StakeholderGroup{
{
Name: "Mgmt",
Description: "Management stakeholder group.",
},
{
Name: "Engineering",
Description: "Engineering team.",
},
}

//
// Creates a copy of Samples for a test.
func CloneSamples() (samples []*api.StakeholderGroup) {
raw, err := json.Marshal(Samples)
if err != nil {
fmt.Print("ERROR cloning samples")
}
json.Unmarshal(raw, &samples)
return
}

//
// Create.
func Create(t *testing.T, r *api.StakeholderGroup) {
err := Client.Post(api.StakeholderGroupsRoot, &r)
if err != nil {
t.Fatalf("Create fatal error: %v", err.Error())
}
}

//
// Delete.
func Delete(t *testing.T, r *api.StakeholderGroup) {
err := Client.Delete(fmt.Sprintf("%s/%d", api.StakeholderGroupsRoot, r.ID))
if err != nil {
t.Fatalf("Delete fatal error: %v", err.Error())
}
}

0 comments on commit 6424ac5

Please sign in to comment.