Skip to content

Commit

Permalink
🌱 add dependency package in test/api (#375)
Browse files Browse the repository at this point in the history
Upstream issue
[https://github.com/konveyor/tackle2-operator/issues/220](https://github.com/konveyor/tackle2-operator/issues/220)

Issue in the repo #370 

added api_test.go,pkg.go,samples.go in `dependency` directory. Also
added `dependency.go` in binding package and added dependency client in
`binding/richclient.go`

---------

Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
  • Loading branch information
khareyash05 authored Jun 9, 2023
1 parent 709437f commit 69ca6ac
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 3 deletions.
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"

"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)
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))

_, 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)
}

// 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

//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",
},
},
}

0 comments on commit 69ca6ac

Please sign in to comment.