From 1edc506c7c04480091690fb0e315a4b3b93d22b7 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Fri, 9 Aug 2024 21:20:42 +0000 Subject: [PATCH] Add verifyExistenceAndReturn methods to KiwiResources * Add new verifyExistenceAndReturn methods to KiwiResources to return non-null objects for method chaining * Rearrange method order so they are grouped by error message (no message, static message, message template and args). Clsoes #1166 --- .../org/kiwiproject/jaxrs/KiwiResources.java | 80 ++++++++++++++++--- .../kiwiproject/jaxrs/KiwiResourcesTest.java | 75 +++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java b/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java index 46b516ed..876c9815 100644 --- a/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java +++ b/src/main/java/org/kiwiproject/jaxrs/KiwiResources.java @@ -60,6 +60,20 @@ public static void verifyExistence(T resourceEntity) { verifyExistence(resourceEntity, null); } + /** + * Verifies that {@code resourceEntity} is not null and returns it, + * otherwise throws a {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param the object type + * @return the entity, if it is not null + * @throws JaxrsNotFoundException if the entity is null + */ + @NonNull + public static T verifyExistenceAndReturn(T resourceEntity) { + return verifyExistence(Optional.ofNullable(resourceEntity)); + } + /** * Verifies that {@code resourceEntity} contains a value, otherwise throws a {@link JaxrsNotFoundException}. * @@ -89,6 +103,21 @@ public static void verifyExistence(T resourceEntity, Class entityType, Ob verifyExistence(resourceEntity, notFoundMessage); } + /** + * Verifies that {@code resourceEntity} is not null and returns it, + * otherwise throws a {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param entityType a Class representing the entity type, used in error messages + * @param identifier the unique identifier of the resource identity + * @param the object type + * @return the entity, if it is not null + * @throws JaxrsNotFoundException if the entity is null + */ + public static T verifyExistenceAndReturn(T resourceEntity, Class entityType, Object identifier) { + return verifyExistence(Optional.ofNullable(resourceEntity), entityType, identifier); + } + /** * Verifies that {@code resourceEntity} contains a value, otherwise throws {@link JaxrsNotFoundException}. * @@ -121,6 +150,37 @@ public static void verifyExistence(T resourceEntity, String notFoundMessage) } } + /** + * Verifies that {@code resourceEntity} is not null and returns it, + * otherwise throws {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param notFoundMessage the error message to include in the response entity + * @param the object type + * @return the entity, if it is not null + * @throws JaxrsNotFoundException if the entity is null + */ + @NonNull + public static T verifyExistenceAndReturn(T resourceEntity, String notFoundMessage) { + return verifyExistence(Optional.ofNullable(resourceEntity), notFoundMessage); + } + + /** + * Verifies that {@code resourceEntity} contains a value, otherwise throws {@link JaxrsNotFoundException}. + * + * @param resourceEntity the resource entity to verify + * @param notFoundMessage the error message to include in the response entity + * @param the object type + * @return the entity if the Optional contains a value + * @throws JaxrsNotFoundException if the entity is empty + */ + @NonNull + public static T verifyExistence(Optional resourceEntity, String notFoundMessage) { + verifyExistence(resourceEntity.orElse(null), notFoundMessage); + + return resourceEntity.orElseThrow(); + } + /** * Verifies that {@code resourceEntity} is not null, otherwise throws {@link JaxrsNotFoundException}. * @@ -140,19 +200,21 @@ public static void verifyExistence(T resourceEntity, String notFoundMessageT } /** - * Verifies that {@code resourceEntity} contains a value, otherwise throws {@link JaxrsNotFoundException}. + * Verifies that {@code resourceEntity} is not null and returns it, + * otherwise throws {@link JaxrsNotFoundException}. * - * @param resourceEntity the resource entity to verify - * @param notFoundMessage the error message to include in the response entity - * @param the object type - * @return the entity if the Optional contains a value + * @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 + * @return the entity, if it is not null * @throws JaxrsNotFoundException if the entity is empty */ @NonNull - public static T verifyExistence(Optional resourceEntity, String notFoundMessage) { - verifyExistence(resourceEntity.orElse(null), notFoundMessage); - - return resourceEntity.orElseThrow(); + public static T verifyExistenceAndReturn(T resourceEntity, String notFoundMessageTemplate, Object... args) { + return verifyExistence(Optional.ofNullable(resourceEntity), notFoundMessageTemplate, args); } /** diff --git a/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java b/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java index 24e6f597..3ba76b7d 100644 --- a/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java +++ b/src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java @@ -216,6 +216,81 @@ void shouldThrow_WhenOptionalIsEmpty(String template) { } } + @Nested + class VerifyExistenceAndReturn { + + @Nested + class EntityArgument { + + @Test + void shouldReturnEntity_WhenEntityNotNull() { + assertThat(KiwiResources.verifyExistenceAndReturn(ENTITY)).isSameAs(ENTITY); + } + + @Test + void shouldThrow_WhenEntityIsNull() { + assertThatThrownBy(() -> KiwiResources.verifyExistenceAndReturn(NULL_ENTITY)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(defaultNotFoundMessage()); + } + } + + @Nested + class EntityTypeAndIdentifier { + + @Test + void shouldReturnEntity_WhenEntityNotNull() { + assertThat(KiwiResources.verifyExistenceAndReturn(ENTITY, MyEntity.class, ID)).isSameAs(ENTITY); + } + + @Test + void shouldThrow_WhenEntityIsNull() { + var message = JaxrsNotFoundException.buildMessage("MyEntity", ID); + + assertThatThrownBy(() -> KiwiResources.verifyExistenceAndReturn((MyEntity) null, MyEntity.class, ID)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(message); + } + } + + @Nested + class EntityAndNotFoundMessage { + + @Test + void shouldReturnEntity_WhenEntityNotNull() { + assertThat(KiwiResources.verifyExistenceAndReturn(ENTITY, ENTITY_NOT_FOUND_MESSAGE)).isSameAs(ENTITY); + } + + @Test + void shouldThrow_WhenEntityIsNull() { + assertThatThrownBy(() -> KiwiResources.verifyExistenceAndReturn(NULL_ENTITY, ENTITY_NOT_FOUND_MESSAGE)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(ENTITY_NOT_FOUND_MESSAGE); + } + } + + @Nested + class EntityAndNotFoundMessageTemplateWithArgs { + + @Test + void shouldReturnEntity_WhenEntityNotNull() { + assertThat(KiwiResources.verifyExistenceAndReturn(ENTITY, ENTITY_NOT_FOUND_MESSAGE_TEMPLATE_1, 42)).isSameAs(ENTITY); + } + + @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.verifyExistenceAndReturn(NULL_ENTITY, template, arg)) + .isExactlyInstanceOf(JaxrsNotFoundException.class) + .hasMessage(f(template, arg)); + } + } + } + @Nested class NewResponse {