Skip to content

Commit

Permalink
Add: Unit test for openSearchClient.GetResponseError (#69)
Browse files Browse the repository at this point in the history
## What

Add unit test for openSearchClient.GetResponseError

## Why

To show that the implementation works with real example responses from
opensearch and make sure that the behavior of the function does not
change in case we refactor it. Also for documentation purposes.

## References

--

## Checklist

- [x] Tests
  • Loading branch information
HollererJ committed May 21, 2024
2 parents b3c8334 + 2dd8c3a commit 45850ed
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/openSearch/openSearchClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func GetResponseError(statusCode int, responseString []byte, indexName string) e
}

if errorResponse.HasError {
return errors.Errorf("request error %v", errorResponse)
return errors.Errorf("request error %s", string(responseString))
}

return nil
Expand Down
88 changes: 88 additions & 0 deletions pkg/openSearch/openSearchClient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,91 @@ func searchAllVulnerabilities(c *assert.CollectT, client *Client) *SearchRespons
require.NoError(c, err)
return searchResponse
}

func TestGetResponseError(t *testing.T) {
//given
testCases := map[string]struct {
givenStatusCode int
givenResponse string
expectedErrorMessage *string
}{
"no error": {
givenStatusCode: 200,
givenResponse: `{"took": 1, "errors": false, "items": []}`,
expectedErrorMessage: nil,
},
"broken json": {
givenStatusCode: 200,
givenResponse: `{"error": {`,
expectedErrorMessage: strPtr(`expect " after {`),
},
"error in body": {
givenStatusCode: 200,
givenResponse: `{"took": 1, "errors": true, "items": [{"index": {}}]}`,
expectedErrorMessage: strPtr(`{"took": 1, "errors": true, "items": [{"index": {}}]}`),
},
"index exists": {
givenStatusCode: 400,
givenResponse: `{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [test/CafPswH_Q5mGXcKgBN3TNg] already exists",
"index": "test",
"index_uuid": "CafPswH_Q5mGXcKgBN3TNg"
}
],
"type": "resource_already_exists_exception",
"reason": "index [test/CafPswH_Q5mGXcKgBN3TNg] already exists",
"index": "test",
"index_uuid": "CafPswH_Q5mGXcKgBN3TNg"
},
"status": 400
}`,
expectedErrorMessage: strPtr(`Resource 'test' already exists`),
},
"bad request with broken json": {
givenStatusCode: 400,
givenResponse: `{"error": {`,
expectedErrorMessage: strPtr(`openSearchClient.OpenSearchErrors.ReadString`),
},
"some other bad request": {
givenStatusCode: 400,
givenResponse: `{
"error": {
"type": "some_bad_request",
"reason": "some reason",
"index": "test",
"index_uuid": "CafPswH_Q5mGXcKgBN3TNg"
},
"status": 400
}`,
expectedErrorMessage: strPtr(`some reason`),
},
"internal server error": {
givenStatusCode: 500,
givenResponse: `some error message`,
expectedErrorMessage: strPtr(`some error message`),
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// when
err := GetResponseError(tc.givenStatusCode, []byte(tc.givenResponse), indexName)

// then
if tc.expectedErrorMessage != nil {
require.Error(t, err)
require.Contains(t, err.Error(), *tc.expectedErrorMessage)
} else {
require.NoError(t, err)
}
})
}
}

func strPtr(s string) *string {
return &s
}

0 comments on commit 45850ed

Please sign in to comment.