Skip to content

Commit

Permalink
Add TagCategory and Tag API tests
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 a68ce37 commit fd3efde
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/api/client/assertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package client

import "fmt"

func FlatEqual(got, expected interface{}) bool {
return fmt.Sprintf("%v", got) == fmt.Sprintf("%v", expected)
}
94 changes: 94 additions & 0 deletions test/api/tag/api_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
78 changes: 78 additions & 0 deletions test/api/tag/fixtures.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
86 changes: 86 additions & 0 deletions test/api/tagcategory/api_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
59 changes: 59 additions & 0 deletions test/api/tagcategory/fixtures.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit fd3efde

Please sign in to comment.