Register hints for @WithSecurityContext on class level

Issue gh-12215
This commit is contained in:
Marcus Da Coregio
2022-11-17 10:08:40 -03:00
parent 222f8ae1a5
commit 1648151dd2
3 changed files with 24 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.security.test.aot.hint;
import java.util.Arrays;
import java.util.stream.Stream;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
@@ -38,13 +39,21 @@ class WithSecurityContextTestRuntimeHints implements TestRuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, Class<?> testClass, ClassLoader classLoader) {
Arrays.stream(testClass.getDeclaredMethods())
.map((method) -> MergedAnnotations.from(method, SUPERCLASS).get(WithSecurityContext.class))
Stream.concat(getClassAnnotations(testClass), getMethodAnnotations(testClass))
.filter(MergedAnnotation::isPresent)
.map((withSecurityContext) -> withSecurityContext.getClass("factory"))
.forEach((factory) -> registerDeclaredConstructors(hints, factory));
}
private Stream<MergedAnnotation<WithSecurityContext>> getClassAnnotations(Class<?> testClass) {
return MergedAnnotations.search(SUPERCLASS).from(testClass).stream(WithSecurityContext.class);
}
private Stream<MergedAnnotation<WithSecurityContext>> getMethodAnnotations(Class<?> testClass) {
return Arrays.stream(testClass.getDeclaredMethods())
.map((method) -> MergedAnnotations.from(method, SUPERCLASS).get(WithSecurityContext.class));
}
private void registerDeclaredConstructors(RuntimeHints hints, Class<?> factory) {
hints.reflection().registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}