|
| 1 | +package it.aboutbits.springboot.testing.validation.core; |
| 2 | + |
| 3 | +import lombok.AccessLevel; |
| 4 | +import lombok.Getter; |
| 5 | +import lombok.NonNull; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import lombok.Setter; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.function.BiConsumer; |
| 12 | +import java.util.function.Consumer; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +@RequiredArgsConstructor |
| 17 | +public abstract class BaseValidationAssert<R extends BaseRuleBuilder<?>> implements AutoCloseable { |
| 18 | + @Getter(AccessLevel.PROTECTED) |
| 19 | + private final R ruleBuilder; |
| 20 | + |
| 21 | + protected static final Set<Class<?>> NON_BEAN_TYPES = new HashSet<>(); |
| 22 | + |
| 23 | + private Object parameterUnderTest; |
| 24 | + |
| 25 | + @Setter(AccessLevel.PRIVATE) |
| 26 | + private Consumer<Object> functionToCallWithParameter = null; |
| 27 | + |
| 28 | + protected boolean hasBeenCalled = false; |
| 29 | + |
| 30 | + public static void registerNonBeanType(Class<?> type) { |
| 31 | + NON_BEAN_TYPES.add(type); |
| 32 | + } |
| 33 | + |
| 34 | + public <P> CallBuilder<R, P> that(@NonNull P parameterUnderTest) { |
| 35 | + this.parameterUnderTest = parameterUnderTest; |
| 36 | + ruleBuilder.setTriggerValidation(this::assertValidation); |
| 37 | + return new CallBuilder<>(this, parameterUnderTest); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void close() throws RuntimeException { |
| 42 | + assertThat(hasBeenCalled) |
| 43 | + .withFailMessage("Validation was never invoked by calling 'isCompliant()'.") |
| 44 | + .isTrue(); |
| 45 | + } |
| 46 | + |
| 47 | + @RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
| 48 | + public static final class CallBuilder<R extends BaseRuleBuilder<?>, P> { |
| 49 | + private final BaseValidationAssert<R> parent; |
| 50 | + private final P parameterUnderTest; |
| 51 | + |
| 52 | + public R calling(@NonNull Consumer<P> functionToCallWithParameter) { |
| 53 | + parent.setFunctionToCallWithParameter((Consumer<Object>) functionToCallWithParameter); |
| 54 | + return parent.ruleBuilder; |
| 55 | + } |
| 56 | + |
| 57 | + public R usingBeanValidation() { |
| 58 | + return parent.ruleBuilder; |
| 59 | + } |
| 60 | + |
| 61 | + public <ID> R calling( |
| 62 | + @NonNull BiConsumer<ID, P> functionToCallWithParameter, |
| 63 | + @NonNull ID id |
| 64 | + ) { |
| 65 | + parent.setFunctionToCallWithParameter( |
| 66 | + p -> functionToCallWithParameter.accept(id, (P) p) |
| 67 | + ); |
| 68 | + return parent.ruleBuilder; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void assertValidation() { |
| 73 | + hasBeenCalled = true; |
| 74 | + new RuleValidator<>().assertValidation( |
| 75 | + new RuleValidator.AssertionParameter<>( |
| 76 | + parameterUnderTest, |
| 77 | + functionToCallWithParameter, |
| 78 | + ruleBuilder.getRules(), |
| 79 | + NON_BEAN_TYPES |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments