Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 add dependency package in test/api #375

Merged
merged 10 commits into from
Jun 9, 2023
5 changes: 3 additions & 2 deletions api/dependency.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package api

import (
"net/http"

khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/model"
"gorm.io/gorm/clause"
"net/http"
)

//
Expand Down Expand Up @@ -115,7 +116,7 @@ func (h DependencyHandler) Create(ctx *gin.Context) {
_ = ctx.Error(err)
return
}

r.With(m)
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
h.Respond(ctx, http.StatusCreated, r)
}

Expand Down
43 changes: 43 additions & 0 deletions binding/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
}

//
// 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
103 changes: 103 additions & 0 deletions test/api/dependency/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package dependency

import (
"fmt"
"testing"

"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/test/assert"
)

func TestDependencyCRUD(t *testing.T) {
for _, sample := range Samples {
t.Run(fmt.Sprintf("Dependency from %s -> %s", sample.ApplicationFrom.Name, sample.ApplicationTo.Name), func(t *testing.T) {

assert.Must(t, Application.Create(&sample.ApplicationFrom))
assert.Must(t, Application.Create(&sample.ApplicationTo))

// Create.
dependency := api.Dependency{
From: api.Ref{
ID: sample.ApplicationFrom.ID,
},
To: api.Ref{
ID: sample.ApplicationTo.ID,
},
}
assert.Should(t, Dependency.Create(&dependency))

// Get.
got, err := Dependency.Get(dependency.ID)
if err != nil {
t.Errorf(err.Error())
}
if assert.FlatEqual(got, dependency.ID) {
t.Errorf("Different response error. Got %v, expected %v", got, dependency.ID)
}

// Delete dependency.
assert.Should(t, Dependency.Delete(dependency.ID))
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

_, err = Dependency.Get(dependency.ID)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", dependency)
}

//Delete Applications
assert.Must(t, Application.Delete(sample.ApplicationFrom.ID))
assert.Must(t, Application.Delete(sample.ApplicationTo.ID))
})
}
}

func TestDependencyList(t *testing.T) {

// an array of created dependencies to track them later
createdDependencies := []api.Dependency{}

for _, r := range Samples {

// Create applications.
assert.Must(t, Application.Create(&r.ApplicationFrom))
assert.Must(t, Application.Create(&r.ApplicationTo))

// Create dependencies.
dependency := api.Dependency{
From: api.Ref{
ID: r.ApplicationFrom.ID,
},
To: api.Ref{
ID: r.ApplicationTo.ID,
},
}
assert.Should(t, Dependency.Create(&dependency))
createdDependencies = append(createdDependencies, dependency)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a Dependency creation and condition below should check if got is an array with the created dependency. Sorry, I missed this part before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I just compare the ID's of dependency created and the ApplicationFrom/To ID's will it suffice?

// List dependencies.
got, err := Dependency.List()
if err != nil {
t.Errorf(err.Error())
}

// check if created Dependencies are in the list we got from Dependency.List()
for _, createdDependency := range createdDependencies {
found := false
for _, retrievedDependency := range got {
if assert.FlatEqual(createdDependency.ID, retrievedDependency.ID) {
found = true
break
}
}
if !found {
t.Errorf("Expected dependency not found in the list: %v", createdDependency)
}
}

// Delete Dependencies and Applications.
for _, dependency := range createdDependencies {
assert.Should(t, Dependency.Delete(dependency.ID))
assert.Must(t, Application.Delete(dependency.From.ID))
assert.Must(t, Application.Delete(dependency.To.ID))
}
}
23 changes: 23 additions & 0 deletions test/api/dependency/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dependency

import (
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/test/api/client"
)

var (
RichClient *binding.RichClient
Dependency binding.Dependency
Application binding.Application
)

func init() {
// Prepare RichClient and login to Hub API (configured from env variables).
RichClient = client.PrepareRichClient()

// Shortcut for Dependency related RichClient methods.
Dependency = RichClient.Dependency
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved

//Shortcut for Application related RichClient methods.
Application = RichClient.Application
}
24 changes: 24 additions & 0 deletions test/api/dependency/samples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dependency

import (
"github.com/konveyor/tackle2-hub/api"
)

// Struct to hold dependency sample data.
type DependencySample struct {
ApplicationFrom api.Application
ApplicationTo api.Application
}

var Samples = []DependencySample{
{
ApplicationFrom: api.Application{
Name: "Gateway",
Description: "Gateway application",
},
ApplicationTo: api.Application{
Name: "Inventory",
Description: "Inventory application",
},
},
}