@@ -13,6 +13,7 @@ import (
1313 _http "net/http"
1414 "testing"
1515
16+ "github.com/go-faker/faker/v4"
1617 "github.com/stretchr/testify/assert"
1718
1819 "github.com/ARM-software/golang-utils/utils/commonerrors"
@@ -123,3 +124,64 @@ func TestCallAndCheckSuccess(t *testing.T) {
123124 errortest .AssertError (t , err , commonerrors .ErrMarshalling )
124125 })
125126}
127+
128+ func TestGenericCallAndCheckSuccess (t * testing.T ) {
129+ t .Run ("context cancelled" , func (t * testing.T ) {
130+ errMessage := "context cancelled"
131+ parentCtx := context .Background ()
132+ ctx , cancelCtx := context .WithCancel (parentCtx )
133+ cancelCtx ()
134+ _ , actualErr := GenericCallAndCheckSuccess (ctx , errMessage ,
135+ func (ctx context.Context ) (* struct {}, * _http.Response , error ) {
136+ return nil , & _http.Response {Body : io .NopCloser (bytes .NewReader (nil ))}, errors .New (errMessage )
137+ })
138+ errortest .AssertError (t , actualErr , commonerrors .ErrCancelled )
139+ })
140+
141+ t .Run ("api call not successful" , func (t * testing.T ) {
142+ errMessage := "client error"
143+ parentCtx := context .Background ()
144+ _ , actualErr := GenericCallAndCheckSuccess (parentCtx , errMessage ,
145+ func (ctx context.Context ) (* struct {}, * _http.Response , error ) {
146+ resp := _http.Response {StatusCode : 400 , Body : io .NopCloser (bytes .NewReader ([]byte ("{\" message\" : \" client error\" ,\" requestId\" : \" 761761721\" }" )))}
147+ return nil , & resp , errors .New (errMessage )
148+ })
149+ expectedErr := "client error (400): API call error [request-id: 761761721] client error; client error"
150+ assert .Equal (t , actualErr .Error (), expectedErr )
151+ })
152+
153+ t .Run ("no context error, api call successful" , func (t * testing.T ) {
154+ errMessage := "no error"
155+ parentCtx := context .Background ()
156+ _ , err := GenericCallAndCheckSuccess (parentCtx , errMessage ,
157+ func (ctx context.Context ) (any , * _http.Response , error ) {
158+ tmp := struct {
159+ test string
160+ }{
161+ test : faker .Word (),
162+ }
163+ return & tmp , & _http.Response {StatusCode : 200 }, errors .New (errMessage )
164+ })
165+ assert .NoError (t , err )
166+ })
167+
168+ t .Run ("api call successful, empty response" , func (t * testing.T ) {
169+ errMessage := "response error"
170+ parentCtx := context .Background ()
171+ _ , err := GenericCallAndCheckSuccess (parentCtx , errMessage ,
172+ func (ctx context.Context ) (* struct {}, * _http.Response , error ) {
173+ return & struct {}{}, & _http.Response {StatusCode : 200 }, errors .New (errMessage )
174+ })
175+ errortest .AssertError (t , err , commonerrors .ErrMarshalling )
176+ })
177+
178+ t .Run ("api call successful, incorrect response" , func (t * testing.T ) {
179+ errMessage := "response error"
180+ parentCtx := context .Background ()
181+ _ , err := GenericCallAndCheckSuccess (parentCtx , errMessage ,
182+ func (ctx context.Context ) (struct {}, * _http.Response , error ) {
183+ return struct {}{}, & _http.Response {StatusCode : 200 }, errors .New (errMessage )
184+ })
185+ errortest .AssertError (t , err , commonerrors .ErrConflict )
186+ })
187+ }
0 commit comments