From d31e05e9e6377d1d21818e67d91905482b168c2e Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Tue, 19 Dec 2023 08:31:12 +0100 Subject: [PATCH] :seedling: Add Setting, Schema basic API tests (#580) Adding basic API test for following resources: - Setting - basic CRUD - Schema - Get --------- Signed-off-by: Marek Aufart --- binding/setting.go | 31 +++++++++++++++++ test/api/schema/api_test.go | 19 +++++++++++ test/api/schema/pkg.go | 16 +++++++++ test/api/setting/api_test.go | 64 ++++++++++++++++++++++++++++++++++++ test/api/setting/pkg.go | 20 +++++++++++ test/api/setting/samples.go | 15 +++++++++ 6 files changed, 165 insertions(+) create mode 100644 test/api/schema/api_test.go create mode 100644 test/api/schema/pkg.go create mode 100644 test/api/setting/api_test.go create mode 100644 test/api/setting/pkg.go create mode 100644 test/api/setting/samples.go diff --git a/binding/setting.go b/binding/setting.go index 0a9eaef0e..012f8f0fc 100644 --- a/binding/setting.go +++ b/binding/setting.go @@ -38,3 +38,34 @@ func (h *Setting) Int(key string) (n int, err error) { err = h.Get(key, &n) return } + +// +// Create a Setting. +func (h *Setting) Create(r *api.Setting) (err error) { + err = h.client.Post(api.SettingsRoot, &r) + return +} + +// +// List Settings. +func (h *Setting) List() (list []api.Setting, err error) { + list = []api.Setting{} + err = h.client.Get(api.SettingsRoot, &list) + return +} + +// +// Update a Setting. +func (h *Setting) Update(r *api.Setting) (err error) { + path := Path(api.SettingRoot).Inject(Params{api.Key: r.Key}) + err = h.client.Put(path, r) + return +} + +// +// Delete a Setting. +func (h *Setting) Delete(key string) (err error) { + path := Path(api.SettingRoot).Inject(Params{api.Key: key}) + err = h.client.Delete(path) + return +} diff --git a/test/api/schema/api_test.go b/test/api/schema/api_test.go new file mode 100644 index 000000000..f97f4f23d --- /dev/null +++ b/test/api/schema/api_test.go @@ -0,0 +1,19 @@ +package schema + +import ( + "testing" + + "github.com/konveyor/tackle2-hub/api" +) + +func TestGetSchema(t *testing.T) { + // Get. + schema := api.Schema{} + err := RichClient.Client.Get("/schema", &schema) + if err != nil { + t.Errorf(err.Error()) + } + if len(schema.Paths) < 1 { + t.Errorf("Got empty Paths from /schema.") + } +} diff --git a/test/api/schema/pkg.go b/test/api/schema/pkg.go new file mode 100644 index 000000000..938d6cd83 --- /dev/null +++ b/test/api/schema/pkg.go @@ -0,0 +1,16 @@ +package schema + +import ( + "github.com/konveyor/tackle2-hub/binding" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +var ( + RichClient *binding.RichClient +) + + +func init() { + // Prepare RichClient and login to Hub API (configured from env variables). + RichClient = client.PrepareRichClient() +} diff --git a/test/api/setting/api_test.go b/test/api/setting/api_test.go new file mode 100644 index 000000000..735aa9ca8 --- /dev/null +++ b/test/api/setting/api_test.go @@ -0,0 +1,64 @@ +package setting + +import ( + "testing" +) + +func TestSettingCRUD(t *testing.T) { + for _, r := range Samples { + t.Run(r.Key, func(t *testing.T) { + // Create. + err := Setting.Create(&r) + if err != nil { + t.Errorf(err.Error()) + } + + // Get. + gotValue := "" + err = Setting.Get(r.Key, &gotValue) + if err != nil { + t.Errorf(err.Error()) + } + if gotValue != r.Value { + t.Errorf("Different response error. Got %v, expected %v", gotValue, r) + } + + // Update. + updateValue := "data-updated" + r.Value = updateValue + err = Setting.Update(&r) + if err != nil { + t.Errorf(err.Error()) + } + + err = Setting.Get(r.Key, &r) + if err != nil { + t.Errorf(err.Error()) + } + if r.Value != updateValue { + t.Errorf("Different Setting Value error. Got %s, expected %s", gotValue, updateValue) + } + + // Delete. + err = Setting.Delete(r.Key) + if err != nil { + t.Errorf(err.Error()) + } + + err = Setting.Get(r.Key, gotValue) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", r) + } + }) + } +} + +func TestSettingList(t *testing.T) { + got, err := Setting.List() + if err != nil { + t.Errorf(err.Error()) + } + if len(got) < 1 { + t.Errorf("Got empty Settings list.") + } +} diff --git a/test/api/setting/pkg.go b/test/api/setting/pkg.go new file mode 100644 index 000000000..05b9acfef --- /dev/null +++ b/test/api/setting/pkg.go @@ -0,0 +1,20 @@ +package setting + +import ( + "github.com/konveyor/tackle2-hub/binding" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +var ( + RichClient *binding.RichClient + Setting binding.Setting +) + + +func init() { + // Prepare RichClient and login to Hub API (configured from env variables). + RichClient = client.PrepareRichClient() + + // Shortcut for Setting-related RichClient methods. + Setting = RichClient.Setting +} diff --git a/test/api/setting/samples.go b/test/api/setting/samples.go new file mode 100644 index 000000000..f49a73768 --- /dev/null +++ b/test/api/setting/samples.go @@ -0,0 +1,15 @@ +package setting + +import ( + "github.com/konveyor/tackle2-hub/api" +) + +// Set of valid resources for tests and reuse. +var ( + SampleSetting = api.Setting{ + Key: "sample.setting.1", + Value: "data-123", + } + + Samples = []api.Setting{SampleSetting} +)