Skip to content

Commit

Permalink
Improve generics in KiwiResources validation methods (#665)
Browse files Browse the repository at this point in the history
* Change validateIntParameter and validateLongParameter so that the
  map values can be any type, e.g. Map<String, V>. The keys are still
  restricted to String since they are request parameter names, and
  anything else doesn't make sense.
* Fix the names of the validateLongParameter tests and also rename the
  incorrectly named tests that expect exceptions

Closes #603
  • Loading branch information
sleberknight authored Jan 31, 2022
1 parent 9b47279 commit 05e4b74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/kiwiproject/jaxrs/KiwiResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ public static Response.ResponseBuilder newResponseBuilderBufferingEntityFrom(Res
*
* @param parameters the parameters to check
* @param parameterName name of the parameter which should be present
* @param <V> the type of values in the map
* @return the int value of the validated parameter
* @throws JaxrsBadRequestException if the specified parameter is not present, or is not an integer
*/
public static int validateIntParameter(Map<String, Object> parameters, String parameterName) {
public static <V> int validateIntParameter(Map<String, V> parameters, String parameterName) {
assertNotNull(parameterName, parameters);

var value = parameters.get(parameterName);
Expand All @@ -397,10 +398,11 @@ public static int validateIntParameter(Map<String, Object> parameters, String pa
*
* @param parameters the parameters to check
* @param parameterName name of the parameter which should be present
* @param <V> the type of values in the map
* @return the long value of the validated parameter
* @throws JaxrsBadRequestException if the specified parameter is not present, or is not long
*/
public static long validateLongParameter(Map<String, Object> parameters, String parameterName) {
public static <V> long validateLongParameter(Map<String, V> parameters, String parameterName) {
assertNotNull(parameterName, parameters);

var value = parameters.get(parameterName);
Expand Down
25 changes: 14 additions & 11 deletions src/test/java/org/kiwiproject/jaxrs/KiwiResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
Expand Down Expand Up @@ -494,28 +495,29 @@ class ValidateIntParameter {

@ParameterizedTest
@MethodSource("org.kiwiproject.jaxrs.KiwiResourcesTest#validIntParameterMaps")
void shouldReturnTheValue_WhenIsOrCanBecomeAnInt(Map<String, Object> parameters) {
<V> void shouldReturnTheValue_WhenIsOrCanBecomeAnInt(Map<String, V> parameters) {
assertThat(KiwiResources.validateIntParameter(parameters, "id"))
.isEqualTo(42);
}

@ParameterizedTest
@MethodSource("org.kiwiproject.jaxrs.KiwiResourcesTest#invalidIntParameterMaps")
void shouldReturnTheValue_WhenCannotBecomeAnInt(Map<String, Object> parameters) {
<V> void shouldThrow_WhenCannotBecomeAnInt(Map<String, V> parameters) {
assertThatThrownBy(() -> KiwiResources.validateIntParameter(parameters, "id"))
.isExactlyInstanceOf(JaxrsBadRequestException.class);
}
}

private static Stream<Map<String, Object>> validIntParameterMaps() {
private static Stream<Map<String, ?>> validIntParameterMaps() {
return Stream.of(
Map.of("id", 42),
Map.of("id", 42L),
Map.of("id", "42")
Map.of("id", "42"),
Map.of("id", BigInteger.valueOf(42))
);
}

private static Stream<Map<String, Object>> invalidIntParameterMaps() {
private static Stream<Map<String, ?>> invalidIntParameterMaps() {
return Stream.of(
null,
Map.of(),
Expand All @@ -534,28 +536,29 @@ class ValidateLongParameter {

@ParameterizedTest
@MethodSource("org.kiwiproject.jaxrs.KiwiResourcesTest#validLongParameterMaps")
void shouldReturnTheValue_WhenIsOrCanBecomeAnInt(Map<String, Object> parameters) {
<V> void shouldReturnTheValue_WhenIsOrCanBecomeLong(Map<String, V> parameters) {
assertThat(KiwiResources.validateLongParameter(parameters, "id"))
.isEqualTo(42);
.isEqualTo(42L);
}

@ParameterizedTest
@MethodSource("org.kiwiproject.jaxrs.KiwiResourcesTest#invalidLongParameterMaps")
void shouldReturnTheValue_WhenCannotBecomeAnInt(Map<String, Object> parameters) {
<V> void shouldThrow_WhenCannotBecomeLong(Map<String, V> parameters) {
assertThatThrownBy(() -> KiwiResources.validateLongParameter(parameters, "id"))
.isExactlyInstanceOf(JaxrsBadRequestException.class);
}
}

private static Stream<Map<String, Object>> validLongParameterMaps() {
private static Stream<Map<String, ?>> validLongParameterMaps() {
return Stream.of(
Map.of("id", 42),
Map.of("id", 42L),
Map.of("id", "42")
Map.of("id", "42"),
Map.of("id", BigInteger.valueOf(42))
);
}

private static Stream<Map<String, Object>> invalidLongParameterMaps() {
private static Stream<Map<String, ?>> invalidLongParameterMaps() {
return Stream.of(
null,
Map.of(),
Expand Down

0 comments on commit 05e4b74

Please sign in to comment.