|
1 | 1 | package it.aboutbits.springboot.testing.validation.core; |
2 | 2 |
|
3 | 3 | import it.aboutbits.springboot.toolbox.type.CustomType; |
| 4 | +import jakarta.validation.Valid; |
4 | 5 | import lombok.AccessLevel; |
5 | 6 | import lombok.Getter; |
6 | 7 | import lombok.NonNull; |
7 | 8 | import lombok.RequiredArgsConstructor; |
8 | 9 | import lombok.Setter; |
9 | 10 | import org.apache.logging.log4j.util.TriConsumer; |
| 11 | +import org.springframework.validation.annotation.Validated; |
10 | 12 |
|
| 13 | +import java.util.Arrays; |
11 | 14 | import java.util.HashSet; |
12 | 15 | import java.util.Set; |
13 | 16 | import java.util.function.BiConsumer; |
14 | 17 | import java.util.function.Consumer; |
15 | 18 |
|
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
16 | 21 | @RequiredArgsConstructor(access = AccessLevel.PROTECTED) |
17 | 22 | public abstract class BaseValidationAssert<R extends BaseRuleBuilder<?>> { |
18 | 23 | @Getter(AccessLevel.PROTECTED) |
@@ -46,6 +51,43 @@ public <P> CallBuilder<R, P> of(@NonNull P parameterUnderTest) { |
46 | 51 | return new CallBuilder<>(this); |
47 | 52 | } |
48 | 53 |
|
| 54 | + @SuppressWarnings("unused") |
| 55 | + public AnnotationChecker calling( |
| 56 | + @NonNull Class<?> classUnderTest, |
| 57 | + @NonNull String methodName, |
| 58 | + @NonNull Class<?>... methodParameterTypes |
| 59 | + ) { |
| 60 | + return new AnnotationChecker(classUnderTest, methodName, methodParameterTypes); |
| 61 | + } |
| 62 | + |
| 63 | + @RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
| 64 | + public static final class AnnotationChecker { |
| 65 | + private final Class<?> classUnderTest; |
| 66 | + private final String methodName; |
| 67 | + private final Class<?>[] methodParameterTypes; |
| 68 | + |
| 69 | + public void isEnabled() { |
| 70 | + assertThat(classUnderTest.isAnnotationPresent(Validated.class)).isTrue(); |
| 71 | + |
| 72 | + try { |
| 73 | + var method = classUnderTest.getMethod(methodName, methodParameterTypes); |
| 74 | + var parameter = method.getParameters()[method.getParameterCount() - 1]; |
| 75 | + assertThat(parameter.isAnnotationPresent(Valid.class)).isTrue(); |
| 76 | + } catch (NoSuchMethodException e) { |
| 77 | + throw new AssertionError( |
| 78 | + "Method \"%s(%s)\" not found in class \"%s\"".formatted( |
| 79 | + methodName, |
| 80 | + String.join( |
| 81 | + ", ", |
| 82 | + Arrays.stream(methodParameterTypes).map(Class::getCanonicalName).toList() |
| 83 | + ), |
| 84 | + classUnderTest.getCanonicalName() |
| 85 | + ), e |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
49 | 91 | @RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
50 | 92 | public static final class CallBuilder<R extends BaseRuleBuilder<?>, P> { |
51 | 93 | private final BaseValidationAssert<R> parent; |
|
0 commit comments