From 16297ac142251aa3275ff2c03a761510df7f646e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Manteaux?= Date: Wed, 18 Sep 2024 15:24:35 +0200 Subject: [PATCH] Fix nullpointer and provide unit test for Validators --- plume-web-jersey/pom.xml | 11 ++ .../plume/jersey/errors/Validators.java | 26 +++-- .../plume/jersey/errors/WsException.java | 7 +- .../plume/jersey/errors/ValidatorsTest.java | 106 ++++++++++++++++++ 4 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 plume-web-jersey/src/test/java/com/coreoz/plume/jersey/errors/ValidatorsTest.java diff --git a/plume-web-jersey/pom.xml b/plume-web-jersey/pom.xml index 69add61..4c3cc60 100644 --- a/plume-web-jersey/pom.xml +++ b/plume-web-jersey/pom.xml @@ -99,6 +99,17 @@ 3.1.0 provided + + + junit + junit + test + + + org.assertj + assertj-core + test + diff --git a/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/Validators.java b/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/Validators.java index 766f22a..c5c1f88 100644 --- a/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/Validators.java +++ b/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/Validators.java @@ -4,54 +4,56 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Provides common validators that will throw {@link WsException} * when the input data does not pass the validator. */ public class Validators { - - public static T checkRequired(T parameter) { + public static @NotNull T checkRequired(@Nullable T parameter) { if(parameter == null) { throw new WsException(WsError.FIELD_REQUIRED); } return parameter; } - public static String checkRequired(String fieldName, String fieldValue) { + public static @NotNull String checkRequired(@NotNull String fieldName, @Nullable String fieldValue) { if(Strings.isNullOrEmpty(fieldValue)) { throw new WsException(WsError.FIELD_REQUIRED, ImmutableList.of(fieldName)); } return fieldValue; } - public static T checkRequired(String fieldName, T fieldValue) { + public static @NotNull T checkRequired(@NotNull String fieldName, @Nullable T fieldValue) { if(fieldValue == null) { throw new WsException(WsError.FIELD_REQUIRED, ImmutableList.of(fieldName)); } return fieldValue; } - public static String checkEmail(String fieldName, String fieldValue) { + public static @Nullable String checkEmail(@NotNull String fieldName, @Nullable String fieldValue) { if(!EmailValidator.getInstance().isValid(fieldValue)) { throw new WsException(WsError.EMAIL_INVALID, ImmutableList.of(fieldName)); } return fieldValue; } - public static String checkHexaColor(String fieldName, String fieldValue) { + public static @Nullable String checkHexaColor(@NotNull String fieldName, @Nullable String fieldValue) { if(fieldValue != null && !fieldValue.matches("[0-9a-fA-F]{6}")) { throw new WsException(WsError.COLOR_INVALID, ImmutableList.of(fieldName)); } return fieldValue; } - public static String checkHexaColorWithStartingHash(String fieldName, String fieldValue) { - if(fieldValue != null && !fieldValue.startsWith("#")) { - throw new WsException(WsError.COLOR_INVALID, ImmutableList.of(fieldName)); + public static @Nullable String checkHexaColorWithStartingHash(@NotNull String fieldName, @Nullable String fieldValue) { + if(fieldValue != null) { + if (!fieldValue.startsWith("#")) { + throw new WsException(WsError.COLOR_INVALID, ImmutableList.of(fieldName)); + } + return "#" + checkHexaColor(fieldName, fieldValue.substring(1)); } - checkHexaColor(fieldName, fieldValue.substring(1)); - return fieldValue; + return null; } - } diff --git a/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsException.java b/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsException.java index 2bc2179..769af87 100644 --- a/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsException.java +++ b/plume-web-jersey/src/main/java/com/coreoz/plume/jersey/errors/WsException.java @@ -2,14 +2,15 @@ import com.google.common.collect.ImmutableList; +import java.io.Serial; + /** * A {@link RuntimeException} that stops the execution of the web-service * and provides at the same time information of the error to the web-service consumer. * @see WsResultExceptionMapper */ public class WsException extends RuntimeException { - - private static final long serialVersionUID = -5694734210679299708L; + private static final long serialVersionUID = -5694734210679299708L; private final WsError error; private final Iterable statusArguments; @@ -23,6 +24,7 @@ public WsException(WsError error, String... statusArguments) { } public WsException(WsError error, Iterable statusArguments) { + super(error.name()); this.error = error; this.statusArguments = statusArguments; } @@ -34,6 +36,5 @@ public WsError getError() { public Iterable getStatusArguments() { return statusArguments; } - } diff --git a/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/errors/ValidatorsTest.java b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/errors/ValidatorsTest.java new file mode 100644 index 0000000..56918b2 --- /dev/null +++ b/plume-web-jersey/src/test/java/com/coreoz/plume/jersey/errors/ValidatorsTest.java @@ -0,0 +1,106 @@ +package com.coreoz.plume.jersey.errors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +public class ValidatorsTest { + @Test + public void checkRequired_whenParameterIsNotNull_shouldReturnParameter() { + String input = "test"; + String result = Validators.checkRequired(input); + assertThat(result).isEqualTo(input); + } + + @Test + public void checkRequired_whenParameterIsNull_shouldThrowWsException() { + assertThatThrownBy(() -> Validators.checkRequired(null)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.FIELD_REQUIRED.name()); + } + + @Test + public void checkRequired_withFieldName_whenFieldValueIsNotNull_shouldReturnFieldValue() { + String fieldName = "field"; + String fieldValue = "value"; + String result = Validators.checkRequired(fieldName, fieldValue); + assertThat(result).isEqualTo(fieldValue); + } + + @Test + public void checkRequired_withFieldName_whenFieldValueIsNull_shouldThrowWsException() { + String fieldName = "field"; + assertThatThrownBy(() -> Validators.checkRequired(fieldName, (String) null)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.FIELD_REQUIRED.name()); + } + + @Test + public void checkEmail_whenEmailIsValid_shouldReturnEmail() { + String fieldName = "email"; + String fieldValue = "test@example.com"; + String result = Validators.checkEmail(fieldName, fieldValue); + assertThat(result).isEqualTo(fieldValue); + } + + @Test + public void checkEmail_whenEmailIsInvalid_shouldThrowWsException() { + String fieldName = "email"; + String invalidEmail = "invalid-email"; + assertThatThrownBy(() -> Validators.checkEmail(fieldName, invalidEmail)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.EMAIL_INVALID.name()); + } + + @Test + public void checkHexaColor_whenValidHexColor_shouldReturnFieldValue() { + String fieldName = "color"; + String validHexColor = "a1b2c3"; + String result = Validators.checkHexaColor(fieldName, validHexColor); + assertThat(result).isEqualTo(validHexColor); + } + + @Test + public void checkHexaColor_whenInvalidHexColor_shouldThrowWsException() { + String fieldName = "color"; + String invalidHexColor = "zzzzzz"; + assertThatThrownBy(() -> Validators.checkHexaColor(fieldName, invalidHexColor)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.COLOR_INVALID.name()); + } + + @Test + public void checkHexaColorWithStartingHash_whenValidHexColorWithHash_shouldReturnFieldValueWithoutHash() { + String fieldName = "color"; + String hexColorWithHash = "#a1b2c3"; + String result = Validators.checkHexaColorWithStartingHash(fieldName, hexColorWithHash); + assertThat(result).isEqualTo("#a1b2c3"); + } + + @Test + public void checkHexaColorWithStartingHash_whenNullProvider_shouldReturnNull() { + String fieldName = "color"; + String hexColorWithHash = null; + String result = Validators.checkHexaColorWithStartingHash(fieldName, hexColorWithHash); + assertThat(result).isNull(); + } + + @Test + public void checkHexaColorWithStartingHash_whenNoHash_shouldThrowWsException() { + String fieldName = "color"; + String hexColorWithoutHash = "a1b2c3"; + assertThatThrownBy(() -> Validators.checkHexaColorWithStartingHash(fieldName, hexColorWithoutHash)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.COLOR_INVALID.name()); + } + + @Test + public void checkHexaColorWithStartingHash_whenInvalidHexColorAfterHash_shouldThrowWsException() { + String fieldName = "color"; + String invalidHexColor = "#zzzzzz"; + assertThatThrownBy(() -> Validators.checkHexaColorWithStartingHash(fieldName, invalidHexColor)) + .isInstanceOf(WsException.class) + .hasMessage(WsError.COLOR_INVALID.name()); + } +}