refactor: renamed Validation to ValidationUtility
This commit is contained in:
parent
e42bce7917
commit
e9987b4d4f
@ -1,26 +1,23 @@
|
|||||||
package uulm.teamname.marvelous.gamelibrary.json;
|
package uulm.teamname.marvelous.gamelibrary.json;
|
||||||
|
|
||||||
import jakarta.validation.ConstraintViolation;
|
import jakarta.validation.ConstraintViolation;
|
||||||
|
import jakarta.validation.Validation;
|
||||||
import jakarta.validation.Validator;
|
import jakarta.validation.Validator;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Validation {
|
public class ValidationUtility {
|
||||||
private Validation() {}
|
private ValidationUtility() {}
|
||||||
|
|
||||||
private static final Validator validator;
|
private static final Validator validator;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
var factory = jakarta.validation.Validation.buildDefaultValidatorFactory();
|
var factory = Validation.buildDefaultValidatorFactory();
|
||||||
validator = factory.getValidator();
|
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
|
* 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
|
* 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,
|
* @return an {@link Optional}<{@link String}> describing the violations the object commits,
|
||||||
* or an empty {@link Optional} if there aren't any
|
* 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);
|
var violations = validator.validate(object);
|
||||||
if (violations.isEmpty()) return Optional.empty();
|
if (violations.isEmpty()) return Optional.empty();
|
||||||
else {
|
else {
|
@ -4,6 +4,7 @@ import jakarta.validation.ConstraintViolation;
|
|||||||
import jakarta.validation.ValidatorFactory;
|
import jakarta.validation.ValidatorFactory;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.ValidationUtility;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
|
||||||
|
|
||||||
import jakarta.validation.Validation;
|
import jakarta.validation.Validation;
|
||||||
@ -15,7 +16,7 @@ import static org.mockito.Mockito.*;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
class MessageValidationTest {
|
class MessageValidationUtilityTest {
|
||||||
|
|
||||||
ValidatorFactory factory;
|
ValidatorFactory factory;
|
||||||
Validator validator;
|
Validator validator;
|
||||||
@ -49,6 +50,9 @@ class MessageValidationTest {
|
|||||||
assertThat(violations).isNotEmpty();
|
assertThat(violations).isNotEmpty();
|
||||||
assertThat(violationsString)
|
assertThat(violationsString)
|
||||||
.isEqualTo("characters doesn't have twelve elements");
|
.isEqualTo("characters doesn't have twelve elements");
|
||||||
|
|
||||||
|
assertThat(ValidationUtility.validate(message).get())
|
||||||
|
.isEqualTo("characters: doesn't have twelve elements");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user