Skip to content

Commit

Permalink
Add tests for 400 and 404 response handling in stateful stub
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshnikam671 committed Nov 19, 2024
1 parent 8c11bc2 commit f545a4f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,80 @@ class StatefulHttpStubTest {
assertThat(getResponse.status).isEqualTo(404)
}

@Test
@Order(6)
fun `should get a 400 response in a structured manner for an invalid post request`() {
val response = httpStub.client.execute(
HttpRequest(
method = "POST",
path = "/products",
body = parsedJSONObject(
"""
{
"name": "Product A",
"description": "A detailed description of Product A.",
"price": 19.99,
"inStock": "true"
}
""".trimIndent()
)
)
)

assertThat(response.status).isEqualTo(400)
val responseBody = response.body as JSONObjectValue
val error = responseBody.getStringValue("error")
assertThat(error).contains(">> REQUEST.BODY.inStock")
assertThat(error).contains("Contract expected boolean but request contained \"true\"")
}

@Order(7)
@Test
fun `should get a 400 response as a string for an invalid get request where 400 sceham is not defined for the same in the spec`() {
val response = httpStub.client.execute(
HttpRequest(
method = "GET",
path = "/products/invalid-id"
)
)

assertThat(response.status).isEqualTo(400)
val responseBody = (response.body as StringValue).toStringLiteral()
assertThat(responseBody).contains(">> REQUEST.PATH.id")
assertThat(responseBody).contains("Contract expected number but request contained \"invalid-id\"")
}

@Test
@Order(8)
fun `should get a 404 response in a structured manner for a get request where the entry with requested id is not present in the cache`() {
val response = httpStub.client.execute(
HttpRequest(
method = "GET",
path = "/products/0"
)
)

assertThat(response.status).isEqualTo(404)
val responseBody = response.body as JSONObjectValue
val error = responseBody.getStringValue("error")
assertThat(error).isEqualTo("Resource with resourceId '0' not found")
}

@Test
@Order(9)
fun `should get a 404 response as a string for a delete request with missing id where 404 schema is not defined for the same in the spec`() {
val response = httpStub.client.execute(
HttpRequest(
method = "DELETE",
path = "/products/0"
)
)

assertThat(response.status).isEqualTo(404)
val responseBody = response.body as StringValue
assertThat(responseBody.toStringLiteral()).isEqualTo("Resource with resourceId '0' not found")
}

}

@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Product'
'400':
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/BadRequestError'

/products/{id}:
get:
Expand All @@ -56,7 +62,7 @@ paths:
name: id
required: true
schema:
type: string
type: integer
description: The ID of the product to retrieve
- in: query
name: columns
Expand All @@ -72,6 +78,10 @@ paths:
$ref: '#/components/schemas/Product'
'404':
description: Product not found
content:
application/json:
schema:
$ref: '#/components/schemas/NotFoundError'

patch:
summary: Update a product partially
Expand Down Expand Up @@ -184,3 +194,19 @@ components:
inStock:
type: boolean
example: false

NotFoundError:
type: object
properties:
error:
type: string
message:
type: string

BadRequestError:
type: object
properties:
error:
type: string
message:
type: string

0 comments on commit f545a4f

Please sign in to comment.