1
0

improved REST-API error handling for broken request body JSON

This commit is contained in:
Michael Hoennig
2022-10-16 10:08:42 +02:00
parent e305c3c935
commit 77ef126a7e
5 changed files with 94 additions and 47 deletions

View File

@@ -136,7 +136,7 @@ class HsOfficeBankAccountControllerAcceptanceTest {
.port(port)
.when()
.post("http://localhost/api/hs/office/bankaccounts")
.then().assertThat()
.then().log().all().assertThat()
.statusCode(201)
.contentType(ContentType.JSON)
.body("uuid", isUuidValid())
@@ -346,7 +346,10 @@ class HsOfficeBankAccountControllerAcceptanceTest {
jpaAttempt.transacted(() -> {
context.define("superuser-alex@hostsharing.net", null);
tempBankAccountUuids.addAll(
bankAccountRepo.findByOptionalHolderLike("some temp acc").stream().map(HsOfficeBankAccountEntity::getUuid).toList()
bankAccountRepo.findByOptionalHolderLike("some temp acc")
.stream()
.map(HsOfficeBankAccountEntity::getUuid)
.toList()
);
});
tempBankAccountUuids.forEach(uuid -> {

View File

@@ -247,6 +247,26 @@ class TestCustomerControllerAcceptanceTest {
context.define("superuser-fran@hostsharing.net");
assertThat(testCustomerRepository.findCustomerByOptionalPrefixLike("uuu")).hasSize(0);
}
@Test
void invalidRequestBodyJson_raisesClientError() {
RestAssured // @formatter:off
.given()
.header("current-user", "superuser-alex@hostsharing.net")
.contentType(ContentType.JSON)
.body("{]") // deliberately invalid JSON
.port(port)
.when()
.post("http://localhost/api/test/customers")
.then().assertThat()
.statusCode(400)
.contentType(ContentType.JSON)
.body("message", containsString("JSON parse error: Unexpected close marker ']': expected '}'"))
.body("message", containsString("line: 1, column: 1"));
// @formatter:on
}
}
private UUID toCleanup(final UUID tempPartnerUuid) {
@@ -262,7 +282,9 @@ class TestCustomerControllerAcceptanceTest {
System.out.println("DELETING temporary partner: " + uuid);
final var entity = testCustomerRepository.findByUuid(uuid);
final var count = testCustomerRepository.deleteByUuid(uuid);
System.out.println("DELETED temporary partner: " + uuid + (count > 0 ? " successful" : " failed") + " (" + entity.map(TestCustomerEntity::getPrefix).orElse("???") + ")");
System.out.println(
"DELETED temporary partner: " + uuid + (count > 0 ? " successful" : " failed") + " (" + entity.map(
TestCustomerEntity::getPrefix).orElse("???") + ")");
}).assertSuccessful();
});
}