diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/Validation.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ValidationUtility.java similarity index 77% rename from src/main/java/uulm/teamname/marvelous/gamelibrary/json/Validation.java rename to src/main/java/uulm/teamname/marvelous/gamelibrary/json/ValidationUtility.java index cb6832a..5dbaca4 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/Validation.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ValidationUtility.java @@ -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> 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 validateAndGetString(Object object) { + public static Optional validate(Object object) { var violations = validator.validate(object); if (violations.isEmpty()) return Optional.empty(); else { diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationUtilityTest.java similarity index 86% rename from src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationTest.java rename to src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationUtilityTest.java index 4c58b8d..373abf9 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/messages/MessageValidationUtilityTest.java @@ -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"); } }