Skip to content

Commit

Permalink
Add verifyExistence methods that accept varargs in KiwiResources (#664)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sleberknight authored Jan 28, 2022
1 parent 0685d29 commit 9b47279
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/java/org/kiwiproject/jaxrs/KiwiResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -108,6 +109,24 @@ public static <T> 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 <T> the object type
* @throws JaxrsNotFoundException if the entity is null
*/
public static <T> 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}.
*
Expand All @@ -123,6 +142,23 @@ public static <T> T verifyExistence(Optional<T> 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 <T> the object type
* @throws JaxrsNotFoundException if the entity is empty
*/
public static <T> T verifyExistence(Optional<T> resourceEntity, String notFoundMessageTemplate, Object... args) {
verifyExistence(resourceEntity.orElse(null), notFoundMessageTemplate, args);

return resourceEntity.orElseThrow();
}

/**
* Builds a {@link Response} having the given status and entity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public JaxrsNotFoundException(String type, Object itemId) {
/**
* Build a generic "not found" message using the given type and key.
* <p>
* Format: [type] [key] wsa not found.
* Format: [type] [key] was not found.
* <p>
* Example: User 42 was not found.
*
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -59,6 +61,8 @@ class KiwiResourcesTest {
private static final Optional<MyEntity> ENTITY_OPTIONAL = Optional.of(ENTITY);
private static final Optional<MyEntity> 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();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9b47279

Please sign in to comment.