From fd3efdeaaeba92115ed8de4b13f79c8be050164a Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Tue, 21 Mar 2023 09:32:03 +0100 Subject: [PATCH] Add TagCategory and Tag API tests Signed-off-by: Marek Aufart --- test/api/client/assertion.go | 7 +++ test/api/tag/api_test.go | 94 ++++++++++++++++++++++++++++++++ test/api/tag/fixtures.go | 78 ++++++++++++++++++++++++++ test/api/tagcategory/api_test.go | 86 +++++++++++++++++++++++++++++ test/api/tagcategory/fixtures.go | 59 ++++++++++++++++++++ 5 files changed, 324 insertions(+) create mode 100644 test/api/client/assertion.go create mode 100644 test/api/tag/api_test.go create mode 100644 test/api/tag/fixtures.go create mode 100644 test/api/tagcategory/api_test.go create mode 100644 test/api/tagcategory/fixtures.go diff --git a/test/api/client/assertion.go b/test/api/client/assertion.go new file mode 100644 index 000000000..2d1c97429 --- /dev/null +++ b/test/api/client/assertion.go @@ -0,0 +1,7 @@ +package client + +import "fmt" + +func FlatEqual(got, expected interface{}) bool { + return fmt.Sprintf("%v", got) == fmt.Sprintf("%v", expected) +} diff --git a/test/api/tag/api_test.go b/test/api/tag/api_test.go new file mode 100644 index 000000000..dc3e73b9e --- /dev/null +++ b/test/api/tag/api_test.go @@ -0,0 +1,94 @@ +package tag + +import ( + "fmt" + "testing" + + "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/test/api/client" + "github.com/konveyor/tackle2-hub/test/api/tagcategory" +) + +func TestTagCRUD(t *testing.T) { + samples := CloneSamples() + category := prepareCategory(t, samples) + + for _, tag := range samples { + t.Run(tag.Name, func(t *testing.T) { + // Create. + err := Client.Post(api.TagsRoot, &tag) + if err != nil { + t.Errorf(err.Error()) + } + tagPath := fmt.Sprintf("%s/%d", api.TagsRoot, tag.ID) + + // Get. + gotTag := api.Tag{} + err = Client.Get(tagPath, &gotTag) + if err != nil { + t.Errorf(err.Error()) + } + if client.FlatEqual(gotTag, tag) { + t.Errorf("Different response error. Got %v, expected %v", gotTag, tag) + } + + // Update. + updatedTag := api.Tag{ + Name: "Updated " + tag.Name, + Category: tag.Category, + } + err = Client.Put(tagPath, updatedTag) + if err != nil { + t.Errorf(err.Error()) + } + + err = Client.Get(tagPath, &gotTag) + if err != nil { + t.Errorf(err.Error()) + } + if gotTag.Name != updatedTag.Name { + t.Errorf("Different response error. Got %s, expected %s", gotTag.Name, updatedTag.Name) + } + + // Delete. + err = Client.Delete(tagPath) + if err != nil { + t.Errorf(err.Error()) + } + + err = Client.Get(tagPath, &tag) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", tag) + } + + // Cleanup. + Delete(t, tag) + }) + } + // Category cleanup. + tagcategory.Delete(t, category) + +} + +func TestTagList(t *testing.T) { + samples := CloneSamples() + category := prepareCategory(t, samples) + + for _, tag := range samples { + Create(t, tag) + } + + gotTag := []api.Tag{} + err := Client.Get(api.TagsRoot, &gotTag) + if err != nil { + t.Errorf(err.Error()) + } + if client.FlatEqual(gotTag, samples) { + t.Errorf("Different response error. Got %v, expected %v", gotTag, samples) + } + + for _, tag := range samples { + Delete(t, tag) + } + tagcategory.Delete(t, category) +} diff --git a/test/api/tag/fixtures.go b/test/api/tag/fixtures.go new file mode 100644 index 000000000..aa2547e91 --- /dev/null +++ b/test/api/tag/fixtures.go @@ -0,0 +1,78 @@ +package tag + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/test/api/client" + "github.com/konveyor/tackle2-hub/test/api/tagcategory" +) + +var ( + // Setup Hub API client + Client = client.Client +) + +// +// Set of valid resources for tests and reuse. +var Samples = []*api.Tag{ + { + Name: "Test Linux", + Category: api.Ref{ + //ID: 99901, // or not hardcode ID here + //Name: "Test OS categories", + }, + }, + { + Name: "Test RHEL", + Category: api.Ref{ + //ID: 99902, // or not hardcode ID here + //Name: "Test Linux distros", + }, + }, +} + +// +// Creates a copy of Samples for given test (copy is there to avoid tests inflence each other using the same object ref). +func CloneSamples() (samples []*api.Tag) { + raw, err := json.Marshal(Samples) + if err != nil { + fmt.Print("ERROR cloning samples") + } + json.Unmarshal(raw, &samples) + + return +} + +// +// Creates category for Tag test from tagcategory package samples. +func prepareCategory(t *testing.T, tags []*api.Tag) (category *api.TagCategory) { + category = tagcategory.CloneSamples()[0] + tagcategory.Create(t, category) + + // Assign category to Tag. + for _, tag := range tags { + tag.Category = api.Ref{ID: category.ID} + } + return +} + +// +// Create a Tag. +func Create(t *testing.T, r *api.Tag) { + err := Client.Post(api.TagsRoot, &r) + if err != nil { + t.Fatalf("Create fatal error: %v", err.Error()) // Fatal here, Error for standard test failure or failed assertion. + } +} + +// +// Delete the Tag. +func Delete(t *testing.T, r *api.Tag) { + err := Client.Delete(fmt.Sprintf("%s/%d", api.TagsRoot, r.ID)) + if err != nil { + t.Fatalf("Delete fatal error: %v", err.Error()) + } +} diff --git a/test/api/tagcategory/api_test.go b/test/api/tagcategory/api_test.go new file mode 100644 index 000000000..cfbc30517 --- /dev/null +++ b/test/api/tagcategory/api_test.go @@ -0,0 +1,86 @@ +package tagcategory + +import ( + "fmt" + "testing" + + "github.com/konveyor/tackle2-hub/api" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +func TestTagCategoriesCRUD(t *testing.T) { + samples := CloneSamples() + + for _, tagCategory := range samples { + t.Run(tagCategory.Name, func(t *testing.T) { + // Create. + err := Client.Post(api.TagCategoriesRoot, &tagCategory) + if err != nil { + t.Errorf(err.Error()) + } + tagCategoryPath := fmt.Sprintf("%s/%d", api.TagCategoriesRoot, tagCategory.ID) + + // Get. + gotTagCategory := api.TagCategory{} + err = Client.Get(tagCategoryPath, &gotTagCategory) + if err != nil { + t.Errorf(err.Error()) + } + if client.FlatEqual(gotTagCategory, tagCategory) { + t.Errorf("Different response error. Got %v, expected %v", gotTagCategory, tagCategory) + } + + // Update. + updatedTagCategory := api.TagCategory{ + Name: "Updated " + tagCategory.Name, + } + err = Client.Put(tagCategoryPath, updatedTagCategory) + if err != nil { + t.Errorf(err.Error()) + } + + err = Client.Get(tagCategoryPath, &gotTagCategory) + if err != nil { + t.Errorf(err.Error()) + } + if gotTagCategory.Name != updatedTagCategory.Name { + t.Errorf("Different response error. Got %s, expected %s", gotTagCategory.Name, updatedTagCategory.Name) + } + + // Delete. + err = Client.Delete(tagCategoryPath) + if err != nil { + t.Errorf(err.Error()) + } + + err = Client.Get(tagCategoryPath, &tagCategory) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", tagCategory) + } + + // Cleanup. + Delete(t, tagCategory) + }) + } +} + +func TestTagCategoriesList(t *testing.T) { + samples := CloneSamples() + for _, tagCategory := range samples { + Create(t, tagCategory) + } + + gotTagCategories := []api.TagCategory{} + err := Client.Get(api.TagCategoriesRoot, &gotTagCategories) + if err != nil { + t.Errorf(err.Error()) + } + if client.FlatEqual(gotTagCategories, samples) { + t.Errorf("Different response error. Got %v, expected %v", gotTagCategories, samples) + } + + for _, tagCategory := range samples { + Delete(t, tagCategory) + } + +} diff --git a/test/api/tagcategory/fixtures.go b/test/api/tagcategory/fixtures.go new file mode 100644 index 000000000..f4eaa5561 --- /dev/null +++ b/test/api/tagcategory/fixtures.go @@ -0,0 +1,59 @@ +package tagcategory + +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 TagCategories resources for tests and reuse. +var SampleTagCategories = []*api.TagCategory{ + { + Name: "Test OS", + Color: "#dd0000", + //Tags: []api.Ref{}, + }, + { + Name: "Test Language", + Color: "#0000dd", + //Tags: []api.Ref{}, + }, +} + +// +// Creates a copy of Samples for given test (copy is there to avoid tests inflence each other using the same object ref). +func CloneSamples() (applications []*api.TagCategory) { + raw, err := json.Marshal(SampleTagCategories) + if err != nil { + fmt.Print("ERROR cloning samples") + } + json.Unmarshal(raw, &applications) + return +} + +// +// Create a TagCategory. +func Create(t *testing.T, tagCategory *api.TagCategory) { + err := Client.Post(api.TagCategoriesRoot, &tagCategory) + if err != nil { + t.Fatalf("Create fatal error: %v", err.Error()) // Fatal here, Error for standard test failure or failed assertion. + } +} + +// +// Delete the TagCategory. +func Delete(t *testing.T, tagCategory *api.TagCategory) { + err := Client.Delete(fmt.Sprintf("%s/%d", api.TagCategoriesRoot, tagCategory.ID)) + if err != nil { + t.Fatalf("Delete fatal error: %v", err.Error()) + } +}