Skip to content

Commit

Permalink
🌱 add Review API Test (#456)
Browse files Browse the repository at this point in the history
Add review API Test

Work to be Done
1. Check for Review.List()
2. Check for Copy Root

---------

Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
  • Loading branch information
khareyash05 authored Aug 4, 2023
1 parent a51c0eb commit b05ee5e
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 1 deletion.
61 changes: 61 additions & 0 deletions binding/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package binding

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

//
// Review API.
type Review struct {
client *Client
}

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

//
// Get a Review by ID.
func (h *Review) Get(id uint) (r *api.Review, err error) {
r = &api.Review{}
path := Path(api.ReviewRoot).Inject(Params{api.ID: id})
err = h.client.Get(path, r)
return
}

//
// List Reviews.
func (h *Review) List() (list []api.Review, err error) {
list = []api.Review{}
err = h.client.Get(api.ReviewsRoot, &list)
return
}

//
// Update a Review.
func (h *Review) Update(r *api.Review) (err error) {
path := Path(api.ReviewRoot).Inject(Params{api.ID: r.ID})
err = h.client.Put(path, r)
return
}

//
// Delete a Review.
func (h *Review) Delete(id uint) (err error) {
err = h.client.Delete(Path(api.ReviewRoot).Inject(Params{api.ID: id}))
return
}

//
// Copy a Review.
func (h *Review) Copy(reviewID uint, appID uint) (err error) {
copyRequest := api.CopyRequest{
SourceReview: reviewID,
TargetApplications: []uint{appID},
}
err = h.client.Post(api.CopyRoot, copyRequest)
return
}
4 changes: 4 additions & 0 deletions binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RichClient struct {
Identity Identity
JobFunction JobFunction
Proxy Proxy
Review Review
RuleSet RuleSet
Setting Setting
Stakeholder Stakeholder
Expand Down Expand Up @@ -74,6 +75,9 @@ func New(baseUrl string) (r *RichClient) {
Proxy: Proxy{
client: client,
},
Review: Review{
client: client,
},
RuleSet: RuleSet{
client: client,
},
Expand Down
2 changes: 1 addition & 1 deletion docs/test-api-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bucket||||partially within application
dependency|:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|
file|:heavy_check_mark:|:heavy_check_mark:||
import||:heavy_check_mark:||
review||||
review|:heavy_check_mark|:heavy_check_mark||
**Controls**||||
businessservice|:heavy_check_mark:|:heavy_check_mark:||
group|:heavy_check_mark:|:heavy_check_mark:||aka StakeholderGroup
Expand Down
164 changes: 164 additions & 0 deletions test/api/review/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package review

import (
"fmt"
"testing"

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

func TestReviewCRUD(t *testing.T) {
for _, r := range Samples {
t.Run("Review CRUD", func(t *testing.T) {

// Create Application and Review.
app := api.Application{
Name: r.Application.Name,
Description: "Application for Review",
}
assert.Must(t, Application.Create(&app))

// Update Application ID with the Sample Id.
r.Application.ID = app.ID

assert.Must(t, Review.Create(&r))

// Get.
got, err := Review.Get(r.ID)
if err != nil {
t.Errorf(err.Error())
}

// Compare got values with expected values.
AssertEqualReviews(t, got, r)

// Update Comments and Effort Estimate.
r.Comments = "Updated Comment " + r.Comments
assert.Should(t, Review.Update(&r))

// Find Review and check its parameters with the got(On Updation).
got, err = Review.Get(r.ID)
if err != nil {
t.Errorf(err.Error())
}

// Check if the unchanged values remain same or not.
AssertEqualReviews(t, got, r)

// Delete Related Applications.
assert.Must(t, Application.Delete(app.ID))
// Delete Review.
assert.Must(t, Review.Delete(r.ID))

// Check if the review is present even after deletion or not.
_, err = Review.Get(r.ID)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", r)
}
})

t.Run("Copy Review", func(t *testing.T) {

// Create Application and Review.
srcApp := api.Application{
Name: r.Application.Name,
Description: "Application for Review",
}
assert.Must(t, Application.Create(&srcApp))

// Update Application ID with the Sample Id.
r.Application.ID = srcApp.ID

assert.Must(t, Review.Create(&r))

// Create another application to copy
destApp := api.Application{
Name: "New Application",
Description: "Application for Review",
Review: &api.Ref{
ID: r.ID,
},
}
assert.Must(t, Application.Create(&destApp))

err := Review.Copy(r.ID, destApp.ID)
if err != nil {
t.Errorf(err.Error())
}

gotReview, err := Review.Get(destApp.Review.ID)
if err != nil {
fmt.Println(err.Error())
t.Errorf(err.Error())
}

// Check if the expcted review and got review is same.
AssertEqualReviews(t, gotReview, r)

// Delete Review.
assert.Must(t, Review.Delete(r.ID))

// Delete Applications.
assert.Must(t, Application.Delete(srcApp.ID))
assert.Must(t, Application.Delete(destApp.ID))
})
}
}

func TestReviewList(t *testing.T) {
createdReviews := []api.Review{}

for _, r := range Samples {
app := api.Application{
Name: r.Application.Name,
Description: "Application for Review",
}
assert.Must(t, Application.Create(&app))

r.Application.ID = app.ID
assert.Should(t, Review.Create(&r))
createdReviews = append(createdReviews, r)
}

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

// check if created Reviews are in the list we got from Review.List()
for _, createdReview := range createdReviews {
found := false
for _, retrievedReview := range got {
if assert.FlatEqual(createdReview.ID, retrievedReview.ID) {
found = true
break
}
}
if !found {
t.Errorf("Expected review not found in the list: %v", createdReview)
}
}

// Delete related reviews and applications.
for _, review := range createdReviews {
assert.Must(t, Application.Delete(review.ID))
assert.Must(t, Review.Delete(review.ID))
}
}

func AssertEqualReviews(t *testing.T, got *api.Review, expected api.Review) {
if got.BusinessCriticality != expected.BusinessCriticality {
t.Errorf("Different Business Criticality Got %v, expected %v", got.BusinessCriticality, expected.BusinessCriticality)
}
if got.EffortEstimate != expected.EffortEstimate {
t.Errorf("Different Effort Estimate Got %v, expected %v", got.EffortEstimate, expected.EffortEstimate)
}
if got.ProposedAction != expected.ProposedAction {
t.Errorf("Different Proposed Action Got %v, expected %v", got.ProposedAction, expected.ProposedAction)
}
if got.WorkPriority != expected.WorkPriority {
t.Errorf("Different Work Priority Got %v, expected %v", got.WorkPriority, expected.WorkPriority)
}
}
23 changes: 23 additions & 0 deletions test/api/review/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package review

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

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

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

// Shortcut for Review-related RichClient methods.
Review = RichClient.Review

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

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

var Samples = []api.Review{
{
BusinessCriticality: 1,
EffortEstimate: "min",
ProposedAction: "run",
WorkPriority: 1,
Comments: "nil",
Application: api.Ref{
ID: 1,
Name: "Sample Review 1",
},
},
{
BusinessCriticality: 2,
EffortEstimate: "max",
ProposedAction: "stop",
WorkPriority: 2,
Comments: "nil",
Application: api.Ref{
ID: 2,
Name: "Sample Review 2",
},
},
}

0 comments on commit b05ee5e

Please sign in to comment.