diff --git a/src/main/java/it/aboutbits/springboot/toolbox/reflection/util/ClassScannerUtil.java b/src/main/java/it/aboutbits/springboot/toolbox/reflection/util/ClassScannerUtil.java index 653dd52..7bfa48c 100644 --- a/src/main/java/it/aboutbits/springboot/toolbox/reflection/util/ClassScannerUtil.java +++ b/src/main/java/it/aboutbits/springboot/toolbox/reflection/util/ClassScannerUtil.java @@ -19,11 +19,10 @@ public static final class ClassScanner implements AutoCloseable { private final ScanResult scanResult; private ClassScanner(String... packages) { - var result = new ClassGraph() + this.scanResult = new ClassGraph() .enableAllInfo() .acceptPackages(packages) .scan(); - this.scanResult = result; } @SuppressWarnings("unchecked") diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java new file mode 100644 index 0000000..024d124 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java @@ -0,0 +1,25 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Max; + +public class MaxScaledBigDecimalValidator implements ConstraintValidator { + + private long upperBound; + + @Override + public void initialize(Max constraintAnnotation) { + this.upperBound = constraintAnnotation.value(); + } + + @Override + public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) { + if (scaledBigDecimal == null) { + return true; + } + + return scaledBigDecimal.compareTo(new ScaledBigDecimal(upperBound)) <= 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java new file mode 100644 index 0000000..1cffd2a --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java @@ -0,0 +1,25 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Min; + +public class MinScaledBigDecimalValidator implements ConstraintValidator { + + private Long lowerBound; + + @Override + public void initialize(Min constraintAnnotation) { + this.lowerBound = constraintAnnotation.value(); + } + + @Override + public boolean isValid(ScaledBigDecimal scaledBigDecimal, ConstraintValidatorContext context) { + if (scaledBigDecimal == null) { + return true; + } + + return scaledBigDecimal.compareTo(new ScaledBigDecimal(lowerBound)) >= 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java new file mode 100644 index 0000000..31ac388 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.NegativeOrZero; + +public class NegativeOrZeroScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) < 1; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java new file mode 100644 index 0000000..bdd3229 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Negative; + +public class NegativeScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) < 0; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java new file mode 100644 index 0000000..b1a0b85 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.PositiveOrZero; + +public class PositiveOrZeroScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) > -1; + } +} diff --git a/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java new file mode 100644 index 0000000..9202d83 --- /dev/null +++ b/src/main/java/it/aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java @@ -0,0 +1,17 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.Positive; + +public class PositiveScaledBigDecimalValidator implements ConstraintValidator { + @Override + public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + + return value.compareTo(ScaledBigDecimal.ZERO) > 0; + } +} diff --git a/src/main/resources/META-INF/validation.xml b/src/main/resources/META-INF/validation.xml new file mode 100644 index 0000000..73f7a36 --- /dev/null +++ b/src/main/resources/META-INF/validation.xml @@ -0,0 +1,11 @@ + + + + META-INF/validation/scaled-big-decimal.xml + + diff --git a/src/main/resources/META-INF/validation/scaled-big-decimal.xml b/src/main/resources/META-INF/validation/scaled-big-decimal.xml new file mode 100644 index 0000000..9d80254 --- /dev/null +++ b/src/main/resources/META-INF/validation/scaled-big-decimal.xml @@ -0,0 +1,46 @@ + + + + + it.aboutbits.springboot.toolbox.validation.validator.MaxScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.MinScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.NegativeScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.NegativeOrZeroScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.PositiveScaledBigDecimalValidator + + + + + + + it.aboutbits.springboot.toolbox.validation.validator.PositiveOrZeroScaledBigDecimalValidator + + + + diff --git a/src/test/java/it/aboutbits/springboot/toolbox/validation/validator/ScaledBigDecimalValidatorTest.java b/src/test/java/it/aboutbits/springboot/toolbox/validation/validator/ScaledBigDecimalValidatorTest.java new file mode 100644 index 0000000..814ca56 --- /dev/null +++ b/src/test/java/it/aboutbits/springboot/toolbox/validation/validator/ScaledBigDecimalValidatorTest.java @@ -0,0 +1,200 @@ +package it.aboutbits.springboot.toolbox.validation.validator; + +import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal; +import jakarta.validation.Validation; +import jakarta.validation.ValidatorFactory; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.Negative; +import jakarta.validation.constraints.NegativeOrZero; +import jakarta.validation.constraints.Positive; +import jakarta.validation.constraints.PositiveOrZero; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ScaledBigDecimalValidatorTest { + private static final ValidatorFactory VALIDATOR_FACTORY = Validation.buildDefaultValidatorFactory(); + + @Nested + class PositiveConstraint { + record Sut(@Positive ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {0.0000001, 1, 100_000_000.9999}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {-1, -0.0000001, 0}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } + + @Nested + class PositiveOrZeroConstraint { + record Sut(@PositiveOrZero ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {0, 0.0000001, 1, 100_000_000.9999}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {-1, -0.0000001}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } + + @Nested + class NegativeConstraint { + record Sut(@Negative ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {-1, -0.0000001}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {0, 0.0000001, 1, 100_000_000.9999}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } + + @Nested + class NegativeOrZeroConstraint { + record Sut(@NegativeOrZero ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {-1, -0.0000001, 0}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {0.0000001, 1, 100_000_000.9999}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } + + @Nested + class MinConstraint { + record Sut(@Min(50) ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {50, 50.0000001, 100_000_000.9999}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {-1, -0.0000001, 0, 49, 49.999999}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } + + @Nested + class MaxConstraint { + record Sut(@Max(-50) ScaledBigDecimal value) { + } + + @ParameterizedTest + @ValueSource(doubles = {-100_000_000.99999, -50.000001, -50}) + void valid_shouldSucceed(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isEmpty(); + } + + @ParameterizedTest + @ValueSource(doubles = {-49.999999, -49, 0, 100_000_000}) + void inValid_shouldFail(double doubleValue) { + var validator = VALIDATOR_FACTORY.getValidator(); + + var instance = new Sut(ScaledBigDecimal.valueOf(doubleValue)); + + var violations = validator.validate(instance); + + assertThat(violations).isNotEmpty(); + } + } +}