Deserializer: BadRequestAlertException("Unknown property") with test code coverage

This commit is contained in:
Michael Hoennig 2019-05-15 07:41:21 +02:00
parent 60612f6c41
commit f9b68df901
5 changed files with 38 additions and 3 deletions

View File

@ -163,7 +163,7 @@ public abstract class JsonDeserializerWithAccessFilter<T extends AccessMappings>
jsonFieldReader(node, field).readInto(dto);
updatingFields.add(field);
} catch (NoSuchFieldException e) {
throw new RuntimeException("setting field " + fieldName + " failed", e);
throw new BadRequestAlertException("Unknown property", fieldName, "unknownProperty");
}
});
}

View File

@ -137,9 +137,24 @@ public class ReflectionUtil {
T get() throws Exception;
}
public static <T> T unchecked(final ThrowingSupplier<T> supplier) {
/**
* Catches checked exceptions and wraps these into an unchecked RuntimeException.
* <p>
* Rationale: Checked exceptions are a controversial Java feature to begin with.
* They often mix error handling code into the normal flow of domain rules
* or other technical aspects which is not only hard to read but also violates
* the Single Responsibility Principle. Often this is even worse for expressions
* than it is for statements.
* </p>
*
* @param expression an expresion which returns a T and may throw a checked exception
* @param <T> the result type of the expression
* @return the result of the expression
* @throws RuntimeException which wraps a checked exception thrown by the expression
*/
public static <T> T unchecked(final ThrowingSupplier<T> expression) {
try {
return supplier.get();
return expression.get();
} catch (final Exception e) {
throw new RuntimeException(e);
}

View File

@ -1,6 +1,7 @@
{
"error": {
"idNotFound": "Technische Datensatz-ID nicht gefunden",
"unknownProperty": "Unbekannte Eigenschaft",
"shareSubscriptionPositiveQuantity": "Zeichnungen von Geschäftsanteilen erfordern eine positive Stückzahl",
"shareCancellationNegativeQuantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl",
"shareTransactionImmutable": "Transaktionen mit Geschäftsanteilen sind unveränderlich",

View File

@ -1,6 +1,7 @@
{
"error": {
"idNotFound": "Technical record-ID not found",
"unknownProperty": "Unknown Property",
"shareSubscriptionPositiveQuantity": "Share subscriptions require a positive quantity",
"shareCancellationNegativeQuantity": "Share cancellations require a negative quantity",
"shareTransactionImmutable": "Share transactions are immutable",

View File

@ -399,6 +399,24 @@ public class JSonDeserializationWithAccessFilterUnitTest {
.hasMessageContaining("GivenDtoWithUnknownFieldType.unknown");
}
@Test
public void shouldDetectUnknownPropertyName() throws IOException {
// given
securityContext.havingAuthenticatedUser().withAuthority(AuthoritiesConstants.ADMIN);
givenJSonTree(asJSon(ImmutablePair.of("somePropWhichDoesNotExist", "Some Value")));
// when
final Throwable exception = catchThrowable(
() -> deserializerForGivenDto().deserialize(jsonParser, null));
// then
assertThat(exception).isInstanceOfSatisfying(BadRequestAlertException.class, (exc) -> {
assertThat(exc).hasMessageStartingWith("Unknown property");
assertThat(exc.getParam()).isEqualTo("somePropWhichDoesNotExist");
assumeThat(exc.getErrorKey()).isEqualTo("unknownProperty");
});
}
// --- only fixture code below ---
private void givenJSonTree(String givenJSon) throws IOException {