From 03b021e599b6bf0a5b25954cfd07580402a1271d Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Tue, 6 Jun 2023 11:14:31 +0530 Subject: [PATCH] add dependency directory necessary files --- binding/dependency.go | 51 ++++++++++++++++++++++ binding/richclient.go | 6 ++- test/api/dependency/api_test.go | 76 +++++++++++++++++++++++++++++++++ test/api/dependency/pkg.go | 19 +++++++++ test/api/dependency/samples.go | 32 ++++++++++++++ 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 binding/dependency.go create mode 100644 test/api/dependency/api_test.go create mode 100644 test/api/dependency/pkg.go create mode 100644 test/api/dependency/samples.go diff --git a/binding/dependency.go b/binding/dependency.go new file mode 100644 index 000000000..f3953f900 --- /dev/null +++ b/binding/dependency.go @@ -0,0 +1,51 @@ +package binding + +import ( + "github.com/konveyor/tackle2-hub/api" +) + +// +// Dependency API. +type Dependency struct { + // hub API client. + client *Client +} + +// +// Create a Dependency. +func (h *Dependency) Create(r *api.Dependency) (err error) { + err = h.client.Post(api.DependenciesRoot, &r) + return +} + +// +// Get a Dependency by ID. +func (h *Dependency) Get(id uint) (r *api.Dependency, err error) { + r = &api.Dependency{} + path := Path(api.DependencyRoot).Inject(Params{api.ID: id}) + err = h.client.Get(path, r) + return +} + +// +// List Dependencies. +func (h *Dependency) List() (list []api.Dependency, err error) { + list = []api.Dependency{} + err = h.client.Get(api.DependenciesRoot, &list) + return +} + +// +// Update a Dependency. +func (h *Dependency) Update(r *api.Dependency) (err error) { + path := Path(api.DependencyRoot).Inject(Params{api.ID: r.From.ID}) + err = h.client.Put(path, r) + return +} + +// +// Delete a Dependency. +func (h *Dependency) Delete(id uint) (err error) { + err = h.client.Delete(Path(api.DependencyRoot).Inject(Params{api.ID: id})) + return +} diff --git a/binding/richclient.go b/binding/richclient.go index 6bc8fa513..f5a0f1d85 100644 --- a/binding/richclient.go +++ b/binding/richclient.go @@ -22,7 +22,7 @@ func init() { type RichClient struct { // Resources APIs. Application Application - Bucket Bucket + Bucket Bucket BusinessService BusinessService Identity Identity JobFunction JobFunction @@ -32,6 +32,7 @@ type RichClient struct { Tag Tag TagCategory TagCategory Task Task + Dependency Dependency // A REST client. client *Client @@ -60,6 +61,9 @@ func New(baseUrl string) (r *RichClient) { BusinessService: BusinessService{ client: client, }, + Dependency: Dependency{ + client: client, + }, Identity: Identity{ client: client, }, diff --git a/test/api/dependency/api_test.go b/test/api/dependency/api_test.go new file mode 100644 index 000000000..fe30c40fc --- /dev/null +++ b/test/api/dependency/api_test.go @@ -0,0 +1,76 @@ +package dependency + +import ( + "testing" + + "github.com/konveyor/tackle2-hub/test/assert" +) + +func TestDependencyCRUD(t *testing.T) { + for _, r := range Samples { + t.Run("Dependency CRUD", func(t *testing.T) { + // Create. + err := Dependency.Create(&r) + if err != nil { + t.Errorf(err.Error()) + } + + // Get. + got, err := Dependency.Get(r.From.ID) + if err != nil { + t.Errorf(err.Error()) + } + if assert.FlatEqual(got, r) { + t.Errorf("Different response error. Got %v, expected %v", got, r) + } + + // Update. + r.From.Name = "Updated " + r.From.Name + err = Dependency.Update(&r) + if err != nil { + t.Errorf(err.Error()) + } + + got, err = Dependency.Get(r.From.ID) + if err != nil { + t.Errorf(err.Error()) + } + if got.From.Name != r.From.Name { + t.Errorf("Different response error. Got %s, expected %s", got.From.Name, r.From.Name) + } + + // Delete. + err = Dependency.Delete(r.From.ID) + if err != nil { + t.Errorf(err.Error()) + } + + _, err = Dependency.Get(r.From.ID) + if err == nil { + t.Errorf("Resource exits, but should be deleted: %v", r) + } + }) + } +} + +func TestDependencyList(t *testing.T) { + samples := Samples + + for name := range samples { + sample := samples[name] + assert.Must(t, Dependency.Create(&sample)) + samples[name] = sample + } + + got, err := Dependency.List() + if err != nil { + t.Errorf(err.Error()) + } + if assert.FlatEqual(got, &samples) { + t.Errorf("Different response error. Got %v, expected %v", got, samples) + } + + for _, r := range samples { + assert.Must(t, Dependency.Delete(r.ID)) + } +} diff --git a/test/api/dependency/pkg.go b/test/api/dependency/pkg.go new file mode 100644 index 000000000..e72aa9900 --- /dev/null +++ b/test/api/dependency/pkg.go @@ -0,0 +1,19 @@ +package dependency + +import ( + "github.com/konveyor/tackle2-hub/binding" + "github.com/konveyor/tackle2-hub/test/api/client" +) + +var ( + RichClient *binding.RichClient + Dependency binding.Dependency +) + +func init() { + // Prepare RichClient and login to Hub API (configured from env variables). + RichClient = client.PrepareRichClient() + + // Shortcut for Dependencyrelated RichClient methods. + Dependency = RichClient.Dependency +} diff --git a/test/api/dependency/samples.go b/test/api/dependency/samples.go new file mode 100644 index 000000000..209a00628 --- /dev/null +++ b/test/api/dependency/samples.go @@ -0,0 +1,32 @@ +package dependency + +import ( + "github.com/konveyor/tackle2-hub/api" +) + +// Set of valid resources for tests and reuse. +var ( + firstDependency = api.Dependency{ + To: api.Ref{ + ID: uint(1473), + Name: "Alice", + }, + From: api.Ref{ + ID: uint(2), + Name: "Bob", + }, + } + + secondDependency = api.Dependency{ + To: api.Ref{ + ID: uint(2123), + Name: "Bob", + }, + From: api.Ref{ + ID: uint(1), + Name: "Alice", + }, + } + + Samples = []api.Dependency{firstDependency, secondDependency} +)