Skip to content

Commit 882ed90

Browse files
committed
Add shouldReturnBadRequestForInvalidTodoUpdate testcase
1 parent def4bae commit 882ed90

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

todo-api/src/test/java/lol/maki/dev/todo/TodoApiApplicationTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void shouldReturnNotFoundForNonExistentTodo() {
226226
}
227227

228228
@Test
229-
void shouldReturnBadRequestForInvalidTodo() {
229+
void shouldReturnBadRequestForInvalidTodoCreate() {
230230
String accessToken = this.accessTokenSupplier.apply(Set.of("todo:write"));
231231
ResponseEntity<JsonNode> response = this.restClient.post()
232232
.uri("/todos")
@@ -290,6 +290,28 @@ void shouldUpdateTodoWithSufficientScope() {
290290
}
291291
}
292292

293+
@Test
294+
@Order(4)
295+
void shouldReturnBadRequestForInvalidTodoUpdate() {
296+
String accessToken = this.accessTokenSupplier.apply(Set.of("todo:write"));
297+
ResponseEntity<JsonNode> response = this.restClient.patch()
298+
.uri("/todos/{todoId}", "00000000-0000-0000-0000-000000000001")
299+
.contentType(MediaType.APPLICATION_JSON)
300+
.body("""
301+
{"finished": true, "todoTitle": "%s"}
302+
""".formatted("a".repeat(256)))
303+
.headers(httpHeaders -> httpHeaders.setBearerAuth(accessToken))
304+
.retrieve()
305+
.toEntity(JsonNode.class);
306+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
307+
assertThat(response.getBody()).isNotNull();
308+
assertThat(response.getBody().get("message")).isEqualTo(new TextNode("Validation failed"));
309+
assertThat(response.getBody().has("violations")).isTrue();
310+
assertThat(response.getBody().get("violations").size()).isEqualTo(1);
311+
assertThat(response.getBody().get("violations").get(0).get("defaultMessage")).isEqualTo(
312+
new TextNode("The size of \"todoTitle\" must be less than or equal to 255. The given size is 256"));
313+
}
314+
293315
@Test
294316
void shouldNotUpdateTodoWithInSufficientScope() {
295317
String accessToken = this.accessTokenSupplier.apply(Set.of("todo:read"));

0 commit comments

Comments
 (0)