-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from aboutbits/aic-1195-be-improve-validation-t…
…ests add bean validation for ScaledBigDecimal
- Loading branch information
Showing
10 changed files
with
376 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...va/it/aboutbits/springboot/toolbox/validation/validator/MaxScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Max, ScaledBigDecimal> { | ||
|
||
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...va/it/aboutbits/springboot/toolbox/validation/validator/MinScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Min, ScaledBigDecimal> { | ||
|
||
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...bits/springboot/toolbox/validation/validator/NegativeOrZeroScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<NegativeOrZero, ScaledBigDecimal> { | ||
@Override | ||
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return value.compareTo(ScaledBigDecimal.ZERO) < 1; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../aboutbits/springboot/toolbox/validation/validator/NegativeScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Negative, ScaledBigDecimal> { | ||
@Override | ||
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return value.compareTo(ScaledBigDecimal.ZERO) < 0; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...bits/springboot/toolbox/validation/validator/PositiveOrZeroScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PositiveOrZero, ScaledBigDecimal> { | ||
@Override | ||
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return value.compareTo(ScaledBigDecimal.ZERO) > -1; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../aboutbits/springboot/toolbox/validation/validator/PositiveScaledBigDecimalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Positive, ScaledBigDecimal> { | ||
@Override | ||
public boolean isValid(ScaledBigDecimal value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
return value.compareTo(ScaledBigDecimal.ZERO) > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<validation-config | ||
xmlns="https://jakarta.ee/xml/ns/validation/configuration" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://jakarta.ee/xml/ns/validation/configuration | ||
https://jakarta.ee/xml/ns/validation/configuration/validation-configuration-3.0.xsd" | ||
version="3.0"> | ||
<constraint-mapping> | ||
META-INF/validation/scaled-big-decimal.xml | ||
</constraint-mapping> | ||
</validation-config> |
46 changes: 46 additions & 0 deletions
46
src/main/resources/META-INF/validation/scaled-big-decimal.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<constraint-mappings | ||
xmlns="https://jakarta.ee/xml/ns/validation/mapping" | ||
version="3.0"> | ||
<constraint-definition annotation="jakarta.validation.constraints.Max"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.MaxScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
<constraint-definition annotation="jakarta.validation.constraints.Min"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.MinScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
<constraint-definition annotation="jakarta.validation.constraints.Negative"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.NegativeScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
<constraint-definition annotation="jakarta.validation.constraints.NegativeOrZero"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.NegativeOrZeroScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
<constraint-definition annotation="jakarta.validation.constraints.Positive"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.PositiveScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
<constraint-definition annotation="jakarta.validation.constraints.PositiveOrZero"> | ||
<validated-by include-existing-validators="true"> | ||
<value> | ||
it.aboutbits.springboot.toolbox.validation.validator.PositiveOrZeroScaledBigDecimalValidator | ||
</value> | ||
</validated-by> | ||
</constraint-definition> | ||
</constraint-mappings> |
200 changes: 200 additions & 0 deletions
200
...a/it/aboutbits/springboot/toolbox/validation/validator/ScaledBigDecimalValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |