-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 add dependency package in test/api (#375)
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
1 parent
709437f
commit 69ca6ac
Showing
6 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
} |