-
Notifications
You must be signed in to change notification settings - Fork 0
Aic 1195 be improve validation tests #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5894045
add initial validation implementation
SirCotare 3730f7b
cleanup implementation
SirCotare b822d67
add dependency to toolbox
SirCotare cbe67a6
add ScaledBigDecimal and reorder methods
SirCotare 133447f
add bean validation for ScaledBigDecimal
SirCotare 188f27a
remove jetbrains annotations and lock toolbox lib to release candidat…
SirCotare 1c7a1bb
add documentation and extract some methods
SirCotare 5cb4ea8
fix workflow
SirCotare 19862c8
fix workflow
SirCotare 2090064
fix workflow
SirCotare 92273f7
fix review issues
SirCotare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
src/main/java/it/aboutbits/springboot/testing/validation/core/BaseRuleBuilder.java
This file contains hidden or 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,59 @@ | ||
| package it.aboutbits.springboot.testing.validation.core; | ||
|
|
||
| import it.aboutbits.springboot.testing.validation.rule.BetweenRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.FutureRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.MaxRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.MinRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NegativeOrZeroRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NegativeRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NotBlankRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NotEmptyRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NotNullRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NotValidatedRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.NullableRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.PastRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.PositiveOrZeroRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.PositiveRule; | ||
| import it.aboutbits.springboot.testing.validation.rule.ValidBeanRule; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NonNull; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public abstract class BaseRuleBuilder<R extends BaseRuleBuilder<?>> implements | ||
| ValidationRulesData, | ||
| BetweenRule<R>, | ||
| FutureRule<R>, | ||
| MaxRule<R>, | ||
| MinRule<R>, | ||
| NegativeOrZeroRule<R>, | ||
| NegativeRule<R>, | ||
| NotBlankRule<R>, | ||
| NotEmptyRule<R>, | ||
| NotNullRule<R>, | ||
| NullableRule<R>, | ||
| PastRule<R>, | ||
| PositiveOrZeroRule<R>, | ||
| PositiveRule<R>, | ||
| NotValidatedRule<R>, | ||
| ValidBeanRule<R> { | ||
| @Getter(AccessLevel.PACKAGE) | ||
| private final List<Rule> rules = new ArrayList<>(); | ||
|
|
||
| @Setter(AccessLevel.PACKAGE) | ||
| private Runnable triggerValidation; | ||
|
|
||
| @Override | ||
| public void addRule(@NonNull Rule rule) { | ||
| rules.add(rule); | ||
| } | ||
|
|
||
| public void isCompliant() { | ||
| triggerValidation.run(); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/main/java/it/aboutbits/springboot/testing/validation/core/BaseValidationAssert.java
This file contains hidden or 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,76 @@ | ||
| package it.aboutbits.springboot.testing.validation.core; | ||
|
|
||
| import it.aboutbits.springboot.toolbox.type.CustomType; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NonNull; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Consumer; | ||
|
|
||
| @RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public abstract class BaseValidationAssert<R extends BaseRuleBuilder<?>> { | ||
| @Getter(AccessLevel.PROTECTED) | ||
| private final R ruleBuilder; | ||
|
|
||
| protected static final Set<Class<?>> NON_BEAN_TYPES = new HashSet<>( | ||
| Set.of( | ||
| CustomType.class | ||
| ) | ||
| ); | ||
|
|
||
| private Object parameterUnderTest; | ||
|
|
||
| @Setter(AccessLevel.PRIVATE) | ||
| private Consumer<?> functionToCallWithParameter = null; | ||
|
|
||
| public static void registerNonBeanType(Class<?> type) { | ||
| NON_BEAN_TYPES.add(type); | ||
| } | ||
|
|
||
| public <P> CallBuilder<R, P> of(@NonNull P parameterUnderTest) { | ||
| this.parameterUnderTest = parameterUnderTest; | ||
| ruleBuilder.setTriggerValidation(this::assertValidation); | ||
| return new CallBuilder<>(this); | ||
| } | ||
|
|
||
| @RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public static final class CallBuilder<R extends BaseRuleBuilder<?>, P> { | ||
| private final BaseValidationAssert<R> parent; | ||
|
|
||
| public R calling(@NonNull Consumer<P> functionToCallWithParameter) { | ||
| parent.setFunctionToCallWithParameter(functionToCallWithParameter); | ||
| return parent.ruleBuilder; | ||
| } | ||
|
|
||
| public R usingBeanValidation() { | ||
| return parent.ruleBuilder; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public <ID> R calling( | ||
| @NonNull BiConsumer<ID, P> functionToCallWithParameter, | ||
| @NonNull ID id | ||
| ) { | ||
| parent.setFunctionToCallWithParameter( | ||
| p -> functionToCallWithParameter.accept(id, (P) p) | ||
| ); | ||
| return parent.ruleBuilder; | ||
| } | ||
| } | ||
|
|
||
| private void assertValidation() { | ||
| new RuleValidator<>().assertValidation( | ||
| new RuleValidator.AssertionParameter<>( | ||
| parameterUnderTest, | ||
| functionToCallWithParameter, | ||
| ruleBuilder.getRules(), | ||
| NON_BEAN_TYPES | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
src/main/java/it/aboutbits/springboot/testing/validation/core/Rule.java
This file contains hidden or 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,52 @@ | ||
| package it.aboutbits.springboot.testing.validation.core; | ||
|
|
||
| import it.aboutbits.springboot.testing.validation.source.InertValueSource; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
|
|
||
| public final class Rule { | ||
| @Getter(AccessLevel.PACKAGE) | ||
| private final String property; | ||
|
|
||
| @Getter(AccessLevel.PACKAGE) | ||
| private final Class<? extends ValueSource> valueSource; | ||
|
|
||
| @Getter(AccessLevel.PACKAGE) | ||
| private final Object[] args; | ||
|
|
||
| @Getter(AccessLevel.PACKAGE) | ||
| private boolean requireValid = false; | ||
|
|
||
| @Getter(AccessLevel.PACKAGE) | ||
| private boolean requireNullable = false; | ||
|
|
||
| public Rule(String property, Class<? extends ValueSource> valueSource, Object... args) { | ||
| checkPropertyName(property); | ||
|
|
||
| this.property = property; | ||
| this.valueSource = valueSource; | ||
| this.args = args; | ||
| } | ||
|
|
||
| public static Rule validAnnotated(String property) { | ||
| checkPropertyName(property); | ||
|
|
||
| var rule = new Rule(property, InertValueSource.class); | ||
| rule.requireValid = true; | ||
| return rule; | ||
| } | ||
|
|
||
| public static Rule nullableAnnotated(String property) { | ||
| checkPropertyName(property); | ||
|
|
||
| var rule = new Rule(property, InertValueSource.class); | ||
| rule.requireNullable = true; | ||
| return rule; | ||
| } | ||
|
|
||
| private static void checkPropertyName(String property) { | ||
| if (property.contains(".")) { | ||
| throw new IllegalArgumentException("Referencing sub-objects using dot notation is not supported."); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/it/aboutbits/springboot/testing/validation/core/RuleValidationException.java
This file contains hidden or 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 @@ | ||
| package it.aboutbits.springboot.testing.validation.core; | ||
|
|
||
| class RuleValidationException extends RuntimeException { | ||
| RuleValidationException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| RuleValidationException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.