diff --git a/api/dependency.go b/api/dependency.go index 25879a569..0623adc71 100644 --- a/api/dependency.go +++ b/api/dependency.go @@ -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" ) // @@ -115,7 +116,7 @@ func (h DependencyHandler) Create(ctx *gin.Context) { _ = ctx.Error(err) return } - + r.With(m) h.Respond(ctx, http.StatusCreated, r) } diff --git a/binding/dependency.go b/binding/dependency.go new file mode 100644 index 000000000..a5b1459fa --- /dev/null +++ b/binding/dependency.go @@ -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 +} 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..08a8816de --- /dev/null +++ b/test/api/dependency/api_test.go @@ -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)) + } +} diff --git a/test/api/dependency/pkg.go b/test/api/dependency/pkg.go new file mode 100644 index 000000000..9a86a8501 --- /dev/null +++ b/test/api/dependency/pkg.go @@ -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 +} diff --git a/test/api/dependency/samples.go b/test/api/dependency/samples.go new file mode 100644 index 000000000..ee9230a4b --- /dev/null +++ b/test/api/dependency/samples.go @@ -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", + }, + }, +}