Skip to content

Commit

Permalink
add dependency directory necessary files
Browse files Browse the repository at this point in the history
  • Loading branch information
khareyash05 committed Jun 6, 2023
1 parent 171b155 commit 03b021e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 1 deletion.
51 changes: 51 additions & 0 deletions binding/dependency.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
type RichClient struct {
// Resources APIs.
Application Application
Bucket Bucket
Bucket Bucket
BusinessService BusinessService
Identity Identity
JobFunction JobFunction
Expand All @@ -32,6 +32,7 @@ type RichClient struct {
Tag Tag
TagCategory TagCategory
Task Task
Dependency Dependency

// A REST client.
client *Client
Expand Down Expand Up @@ -60,6 +61,9 @@ func New(baseUrl string) (r *RichClient) {
BusinessService: BusinessService{
client: client,
},
Dependency: Dependency{
client: client,
},
Identity: Identity{
client: client,
},
Expand Down
76 changes: 76 additions & 0 deletions test/api/dependency/api_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
19 changes: 19 additions & 0 deletions test/api/dependency/pkg.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions test/api/dependency/samples.go
Original file line number Diff line number Diff line change
@@ -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}
)

0 comments on commit 03b021e

Please sign in to comment.