From 6424ac55bc986a342646ff6571160d7c91a116fb Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Tue, 21 Mar 2023 12:55:50 +0100 Subject: [PATCH] Add StakeHolder group API test Signed-off-by: Marek Aufart --- test/api/stakeholdergroup/api_test.go | 86 +++++++++++++++++++++++++++ test/api/stakeholdergroup/fixtures.go | 57 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 test/api/stakeholdergroup/api_test.go create mode 100644 test/api/stakeholdergroup/fixtures.go diff --git a/test/api/stakeholdergroup/api_test.go b/test/api/stakeholdergroup/api_test.go new file mode 100644 index 000000000..9a9edb9cd --- /dev/null +++ b/test/api/stakeholdergroup/api_test.go @@ -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) + } + +} diff --git a/test/api/stakeholdergroup/fixtures.go b/test/api/stakeholdergroup/fixtures.go new file mode 100644 index 000000000..a827bbe5e --- /dev/null +++ b/test/api/stakeholdergroup/fixtures.go @@ -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()) + } +}