refactor: renamed Validation to ValidationUtility

This commit is contained in:
Yannik Bretschneider 2021-06-05 16:01:06 +02:00
parent e42bce7917
commit e9987b4d4f
2 changed files with 10 additions and 9 deletions

View File

@ -1,26 +1,23 @@
package uulm.teamname.marvelous.gamelibrary.json;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class Validation {
private Validation() {}
public class ValidationUtility {
private ValidationUtility() {}
private static final Validator validator;
static {
var factory = jakarta.validation.Validation.buildDefaultValidatorFactory();
var factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
public static Set<ConstraintViolation<Object>> validate(Object object) {
return validator.validate(object);
}
/**
* Validates the given object by using a {@link Validator}, and generates
* a {@link String} from the violations. If there aren't any violations, and the object perfectly
@ -29,7 +26,7 @@ public class Validation {
* @return an {@link Optional}<{@link String}> describing the violations the object commits,
* or an empty {@link Optional} if there aren't any
*/
public static Optional<String> validateAndGetString(Object object) {
public static Optional<String> validate(Object object) {
var violations = validator.validate(object);
if (violations.isEmpty()) return Optional.empty();
else {

View File

@ -4,6 +4,7 @@ import jakarta.validation.ConstraintViolation;
import jakarta.validation.ValidatorFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility;
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
import jakarta.validation.Validation;
@ -15,7 +16,7 @@ import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class MessageValidationTest {
class MessageValidationUtilityTest {
ValidatorFactory factory;
Validator validator;
@ -49,6 +50,9 @@ class MessageValidationTest {
assertThat(violations).isNotEmpty();
assertThat(violationsString)
.isEqualTo("characters doesn't have twelve elements");
assertThat(ValidationUtility.validate(message).get())
.isEqualTo("characters: doesn't have twelve elements");
}
}