Skip to content

Commit

Permalink
Fix nullpointer and provide unit test for Validators
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Sep 18, 2024
1 parent e15b2fd commit 16297ac
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 15 deletions.
11 changes: 11 additions & 0 deletions plume-web-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> T checkRequired(T parameter) {
public static<T> @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> T checkRequired(String fieldName, T fieldValue) {
public static<T> @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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> statusArguments;
Expand All @@ -23,6 +24,7 @@ public WsException(WsError error, String... statusArguments) {
}

public WsException(WsError error, Iterable<String> statusArguments) {
super(error.name());
this.error = error;
this.statusArguments = statusArguments;
}
Expand All @@ -34,6 +36,5 @@ public WsError getError() {
public Iterable<String> getStatusArguments() {
return statusArguments;
}

}

Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 16297ac

Please sign in to comment.