From 9b47279888fb621440cb7e0e2bcfc212a686bfc5 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Fri, 28 Jan 2022 16:21:56 -0500 Subject: [PATCH] Add verifyExistence methods that accept varargs in KiwiResources (#664) * Add verifyExistence methods in KiwiResources that accept a message template and a variable number of arguments; they use the KiwiStrings#format method to perform the argument substitution * Fix typo in javadoc of JaxrsNotFoundException#buildMessage Closes #663 --- .../org/kiwiproject/jaxrs/KiwiResources.java | 36 +++++++++++++++ .../exception/JaxrsNotFoundException.java | 2 +- .../kiwiproject/jaxrs/KiwiResourcesTest.java | 44 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java b/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java index 6ca4cc0b..fa69dee3 100644 --- a/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java +++ b/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java @@ -8,6 +8,7 @@ import com.google.common.primitives.Longs; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; +import org.kiwiproject.base.KiwiStrings; import org.kiwiproject.jaxrs.exception.JaxrsBadRequestException; import org.kiwiproject.jaxrs.exception.JaxrsNotFoundException; @@ -108,6 +109,24 @@ public static void verifyExistence(T resourceEntity, String notFoundMessage) } } + /** + * Verifies that {@code resourceEntity} is not null, otherwise throws {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param notFoundMessageTemplate template for the error message to include in the response entity; uses + * {@link KiwiStrings#format(String, Object...) KiwiStrings.format} + * to construct the message + * @param args the arguments to be substituted into the message template + * @param the object type + * @throws JaxrsNotFoundException if the entity is null + */ + public static void verifyExistence(T resourceEntity, String notFoundMessageTemplate, Object... args) { + if (isNull(resourceEntity)) { + var message = f(notFoundMessageTemplate, args); + throw new JaxrsNotFoundException(message); + } + } + /** * Verifies that {@code resourceEntity} contains a value, otherwise throws {@link JaxrsNotFoundException}. * @@ -123,6 +142,23 @@ public static T verifyExistence(Optional resourceEntity, String notFoundM return resourceEntity.orElseThrow(); } + /** + * Verifies that {@code resourceEntity} contains a value, otherwise throws {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param notFoundMessageTemplate template for the error message to include in the response entity; uses + * {@link KiwiStrings#format(String, Object...) KiwiStrings.format} + * to construct the message + * @param args the arguments to be substituted into the message template + * @param the object type + * @throws JaxrsNotFoundException if the entity is empty + */ + public static T verifyExistence(Optional resourceEntity, String notFoundMessageTemplate, Object... args) { + verifyExistence(resourceEntity.orElse(null), notFoundMessageTemplate, args); + + return resourceEntity.orElseThrow(); + } + /** * Builds a {@link Response} having the given status and entity. * diff --git a/src/main/java/org/kiwiproject/jaxrs/exception/JaxrsNotFoundException.java b/src/main/java/org/kiwiproject/jaxrs/exception/JaxrsNotFoundException.java index 8a3f4337..5bb3e90d 100644 --- a/src/main/java/org/kiwiproject/jaxrs/exception/JaxrsNotFoundException.java +++ b/src/main/java/org/kiwiproject/jaxrs/exception/JaxrsNotFoundException.java @@ -61,7 +61,7 @@ public JaxrsNotFoundException(String type, Object itemId) { /** * Build a generic "not found" message using the given type and key. *

- * Format: [type] [key] wsa not found. + * Format: [type] [key] was not found. *

* Example: User 42 was not found. * diff --git a/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java b/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java index b11ec033..f9989e14 100644 --- a/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java +++ b/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java @@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.kiwiproject.base.KiwiStrings.f; import static org.kiwiproject.jaxrs.JaxrsTestHelper.assertCreatedResponseWithLocation; import static org.kiwiproject.jaxrs.JaxrsTestHelper.assertCustomHeaderFirstValue; import static org.kiwiproject.jaxrs.JaxrsTestHelper.assertOkResponse; @@ -25,6 +26,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.kiwiproject.jaxrs.exception.ErrorMessage; import org.kiwiproject.jaxrs.exception.JaxrsBadRequestException; import org.kiwiproject.jaxrs.exception.JaxrsNotFoundException; @@ -59,6 +61,8 @@ class KiwiResourcesTest { private static final Optional ENTITY_OPTIONAL = Optional.of(ENTITY); private static final Optional EMPTY_ENTITY_OPTIONAL = Optional.empty(); private static final String ENTITY_NOT_FOUND_MESSAGE = "MyEntity not found"; + private static final String ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1 = "MyEntity {} not found"; + private static final String ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_2 = "MyEntity %s not found"; private static final JsonHelper JSON_HELPER = JsonHelper.newDropwizardJsonHelper(); @@ -163,6 +167,46 @@ void shouldThrow_WhenOptionalIsEmpty() { .hasMessage(ENTITY_NOT_FOUND_MESSAGE); } } + + @Nested + class EntityAndNotFoundMessageTemplateWithArgs { + + @Test + void shouldNotThrow_WhenEntityNotNull() { + assertThatCode(() -> KiwiResources.verifyExistence(ENTITY, ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1, 42)) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = { + ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1, + ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_2 + }) + void shouldThrow_WhenEntityIsNull(String template) { + var arg = 84; + assertThatThrownBy(() -> KiwiResources.verifyExistence(NULL_ENTITY, template, arg)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(f(template, arg)); + } + + @Test + void shouldNotThrow_WhenOptionalContainsValue() { + var verifiedEntity = KiwiResources.verifyExistence(ENTITY_OPTIONAL, ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1, 24); + assertThat(verifiedEntity).isSameAs(ENTITY); + } + + @ParameterizedTest + @ValueSource(strings = { + ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1, + ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_2 + }) + void shouldThrow_WhenOptionalIsEmpty(String template) { + var arg = 126; + assertThatThrownBy(() -> KiwiResources.verifyExistence(EMPTY_ENTITY_OPTIONAL, template, arg)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(f(template, arg)); + } + } } @Nested