Skip to content

Commit

Permalink
test: Added test for a path variable validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz.gryzbon committed Mar 2, 2022
1 parent 9f217ca commit eef93b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class PathVariableIntegrationShould(
approver.assertApproved(asString(responseEntity.body!!))
}

@Test
fun return400ForConstraintViolationException_PathVariableTooLong(approver: Approver) {
val responseEntity = testRestTemplate.getForEntity("/objects/pathVariableValidation/12345", ErrorResponse::class.java)

assertThat(responseEntity.statusCode, `is`(BAD_REQUEST))
approver.assertApproved(asString(responseEntity.body!!))
}

@Test
fun return400ForTypeMismatchException(approver: Approver) {
val responseEntity = testRestTemplate.getForEntity("/objects/some_value/", ErrorResponse::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import javax.validation.Valid
import javax.validation.constraints.Max
import javax.validation.constraints.Min
import javax.validation.constraints.Positive
import javax.validation.constraints.Size

@Validated
@RestController
Expand Down Expand Up @@ -45,6 +46,13 @@ class TestController {
id = objectId,
created = ZonedDateTime.now())

@GetMapping("/pathVariableValidation/{objectId}")
fun getObjectByPathVariable(@PathVariable @Size(max = 3) objectId: String) = TestResponse(
message = "message",
number = 5L,
id = objectId.toLong(),
created = ZonedDateTime.now())

@GetMapping
fun getObjectBy(@RequestParam @Max(3) someParamName: String) = listOf(
TestResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"errorCode" : "INVALID_PARAMETER",
"messages" : [ "Validation failed" ],
"dataErrors" : [ {
"code" : "Size",
"name" : "objectId",
"value" : "12345",
"message" : "size must be between 0 and 3"
} ],
"attributes" : {
"traceId" : "40e1488ed0001adc",
"exception" : "ConstraintViolationException",
"startTime" : "2020-11-27T11:17:40.095818Z"
}
}

0 comments on commit eef93b9

Please sign in to comment.