Skip to content

Commit

Permalink
Update binding richclient application methods
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed May 3, 2023
1 parent 6916d3b commit 96c9669
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
33 changes: 21 additions & 12 deletions binding/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,41 @@ type Application struct {
client *Client
}

//
// Create a Application.
func (h *Application) Create(r *api.Application) (err error) {
err = h.client.Post(api.ApplicationsRoot, &r)
return
}

// Retrieve the Application.
func (h *Application) Get(r *api.Application) (err error) {
err = h.client.Get(Path(api.ApplicationRoot).Inject(Params{api.ID: r.ID}), &r)
//
// Get an application by ID.
func (h *Application) Get(id uint) (r *api.Application, err error) {
r = &api.Application{}
path := Path(api.ApplicationRoot).Inject(Params{api.ID: id})
err = h.client.Get(path, r)
return
}

// Update the Application.
func (h *Application) Update(r *api.Application) (err error) {
err = h.client.Put(Path(api.ApplicationRoot).Inject(Params{api.ID: r.ID}), &r)
//
// List applications.
func (h *Application) List() (list []api.Application, err error) {
list = []api.Application{}
err = h.client.Get(api.ApplicationsRoot, &list)
return
}

// Delete the Application.
func (h *Application) Delete(r *api.Application) (err error) {
err = h.client.Delete(Path(api.ApplicationRoot).Inject(Params{api.ID: r.ID}))
//
// Update an application.
func (h *Application) Update(r *api.Application) (err error) {
path := Path(api.ApplicationRoot).Inject(Params{api.ID: r.ID})
err = h.client.Put(path, r)
return
}

// List Applications.
func (h *Application) List(r []*api.Application) (err error) {
err = h.client.Get(api.ApplicationsRoot, &r)
//
// Delete the Application.
func (h *Application) Delete(id uint) (err error) {
err = h.client.Delete(Path(api.ApplicationRoot).Inject(Params{api.ID: id}))
return
}
10 changes: 5 additions & 5 deletions binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ var (
)

func init() {
err := Settings.Load()
if err != nil {
panic(err)
}
err := Settings.Load()
if err != nil {
panic(err)
}
}

// The RichClient provides API integration.
Expand Down Expand Up @@ -103,4 +103,4 @@ func (r *RichClient) Login(user, password string) (err error) {
}
r.client.SetToken(login.Token)
return
}
}
2 changes: 1 addition & 1 deletion test/api/application/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func TestApplicationBucket(t *testing.T) {
}

// Clean the application.
assert.Must(t, Application.Delete(&application))
assert.Must(t, Application.Delete(application.ID))
}
21 changes: 10 additions & 11 deletions test/api/application/create_get_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ func TestApplicationCreateGetDelete(t *testing.T) {
assert.Should(t, Application.Create(&r))

// Try get.
got := api.Application{}
got.ID = r.ID
assert.Should(t, Application.Get(&got))
got, err := Application.Get(r.ID)
assert.Should(t, err)

// Assert the get response.
if assert.FlatEqual(got, r) {
t.Errorf("Different response error. Got %v, expected %v", got, r)
}

// Try list.
gotList := []*api.Application{}
assert.Should(t, Application.List(gotList))
gotList, err := Application.List()
assert.Should(t, err)

// Assert the list response.
foundR := api.Application{}
for _, listR := range gotList {
if listR.Name == r.Name && listR.ID == r.ID {
foundR = *listR
foundR = listR
break
}
}
Expand All @@ -40,10 +39,10 @@ func TestApplicationCreateGetDelete(t *testing.T) {
}

// Try delete.
assert.Should(t, Application.Delete(&got))
assert.Should(t, Application.Delete(got.ID))

// Check the created application was deleted.
err := Application.Get(&r)
_, err = Application.Get(r.ID)
if err == nil {
t.Fatalf("Exits, but should be deleted: %v", r)
}
Expand All @@ -68,11 +67,11 @@ func TestApplicationNotCreateDuplicates(t *testing.T) {
t.Errorf("Created duplicate application: %v", dup)

// Clean the duplicate.
assert.Must(t, Application.Delete(dup))
assert.Must(t, Application.Delete(dup.ID))
}

// Clean.
assert.Must(t, Application.Delete(&r))
assert.Must(t, Application.Delete(r.ID))
}

func TestApplicationNotCreateWithoutName(t *testing.T) {
Expand All @@ -87,6 +86,6 @@ func TestApplicationNotCreateWithoutName(t *testing.T) {
t.Errorf("Created empty application: %v", r)

// Clean.
assert.Must(t, Application.Delete(r))
assert.Must(t, Application.Delete(r.ID))
}
}
8 changes: 4 additions & 4 deletions test/api/application/facts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestApplicationFactCRUD(t *testing.T) {
factPath := binding.Path(api.ApplicationFactRoot).Inject(binding.Params{api.ID: application.ID, api.Key: r.Key})

// Create.
err := Client.Post(factPath, &r)
err := Client.Post(binding.Path(api.ApplicationFactRoot).Inject(binding.Params{api.ID: application.ID}), &r)
if err != nil {
t.Errorf(err.Error())
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestApplicationFactCRUD(t *testing.T) {
}

// Clean the application.
assert.Must(t, Application.Delete(&application))
assert.Must(t, Application.Delete(application.ID))
}

func TestApplicationFactsList(t *testing.T) {
Expand All @@ -97,7 +97,7 @@ func TestApplicationFactsList(t *testing.T) {
for _, r := range SampleFacts {
err := Client.Post(binding.Path(api.ApplicationFactRoot).Inject(binding.Params{api.ID: application.ID, api.Key: r.Key}), &r)
if err != nil {
t.Fatalf(err.Error())
t.Errorf(err.Error())
}
}

Expand All @@ -118,5 +118,5 @@ func TestApplicationFactsList(t *testing.T) {
}

// Clean the application.
assert.Must(t, Application.Delete(&application))
assert.Must(t, Application.Delete(application.ID))
}
8 changes: 3 additions & 5 deletions test/api/application/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"
"testing"

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

Expand All @@ -21,15 +20,14 @@ func TestApplicationUpdateName(t *testing.T) {
assert.Should(t, Application.Update(&update))

// Check the updated.
got := api.Application{}
got.ID = r.ID
assert.Should(t, Application.Get(&got))
got, err := Application.Get(r.ID)
assert.Should(t, err)
if !reflect.DeepEqual(got.Name, update.Name) {
t.Errorf("Different updated name error. Got %v, expected %v", got.Name, update.Name)
}

// Clean.
assert.Must(t, Application.Delete(&r))
assert.Must(t, Application.Delete(r.ID))
})
}
}
Expand Down

0 comments on commit 96c9669

Please sign in to comment.