From 841e2790ad3d58c2ae12cb5325908e6477d9b2ed Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 19 Aug 2024 08:10:54 +0200 Subject: [PATCH] Testsuite PoC - Refactor registry and suppliers around annotations, and make lookup of dependency by ref more generic (#32161) Signed-off-by: stianst --- .../test/examples/OAuthClientTest.java | 3 - .../framework/annotations/InjectClient.java | 4 +- .../framework/annotations/InjectRealm.java | 2 +- .../framework/annotations/InjectUser.java | 4 +- .../framework/injection/AnnotationFields.java | 11 ++++ .../injection/DefaultAnnotationProxy.java | 28 ++++++++ .../framework/injection/InstanceContext.java | 33 ++-------- .../test/framework/injection/Registry.java | 65 +++++++------------ .../injection/RequestedInstance.java | 4 +- .../test/framework/injection/StringUtil.java | 9 +++ .../test/framework/injection/Supplier.java | 31 ++------- .../framework/injection/SupplierHelpers.java | 32 +++++++-- .../test/framework/realm/ClientSupplier.java | 4 +- .../test/framework/realm/RealmSupplier.java | 4 +- .../test/framework/realm/UserSupplier.java | 4 +- .../injection/DefaultAnnotationProxyTest.java | 49 ++++++++++++++ .../framework/injection/RegistryTest.java | 4 +- .../injection/mocks/MockChildAnnotation.java | 2 +- .../injection/mocks/MockChildSupplier.java | 2 +- 19 files changed, 176 insertions(+), 119 deletions(-) create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/injection/AnnotationFields.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/injection/DefaultAnnotationProxy.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/injection/StringUtil.java create mode 100644 test-poc/framework/src/test/java/org/keycloak/test/framework/injection/DefaultAnnotationProxyTest.java diff --git a/test-poc/examples/src/test/java/org/keycloak/test/examples/OAuthClientTest.java b/test-poc/examples/src/test/java/org/keycloak/test/examples/OAuthClientTest.java index f84db974e9..192aa36980 100644 --- a/test-poc/examples/src/test/java/org/keycloak/test/examples/OAuthClientTest.java +++ b/test-poc/examples/src/test/java/org/keycloak/test/examples/OAuthClientTest.java @@ -25,9 +25,6 @@ import java.net.URL; @KeycloakIntegrationTest public class OAuthClientTest { - @InjectRealm - ManagedRealm realm; // Need to specify realm as otherwise there's a bug when annotation is not present - @InjectUser(config = UserConfig.class) ManagedUser user; diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectClient.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectClient.java index b6094ec9c1..67351e2780 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectClient.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectClient.java @@ -17,7 +17,7 @@ public @interface InjectClient { LifeCycle lifecycle() default LifeCycle.CLASS; - String ref() default "default"; + String ref() default ""; - String realmRef() default "default"; + String realmRef() default ""; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectRealm.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectRealm.java index e104140e08..61afc73bd3 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectRealm.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectRealm.java @@ -17,5 +17,5 @@ public @interface InjectRealm { LifeCycle lifecycle() default LifeCycle.CLASS; - String ref() default "default"; + String ref() default ""; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectUser.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectUser.java index 98886d783c..92d122f189 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectUser.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/InjectUser.java @@ -17,7 +17,7 @@ public @interface InjectUser { LifeCycle lifecycle() default LifeCycle.CLASS; - String ref() default "default"; + String ref() default ""; - String realmRef() default "default"; + String realmRef() default ""; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/AnnotationFields.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/AnnotationFields.java new file mode 100644 index 0000000000..8fda4bb172 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/AnnotationFields.java @@ -0,0 +1,11 @@ +package org.keycloak.test.framework.injection; + +public interface AnnotationFields { + + String CONFIG = "config"; + String LIFECYCLE = "lifecycle"; + String REF = "ref"; + + String REALM_REF = "realmRef"; + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/DefaultAnnotationProxy.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/DefaultAnnotationProxy.java new file mode 100644 index 0000000000..43b433e55a --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/DefaultAnnotationProxy.java @@ -0,0 +1,28 @@ +package org.keycloak.test.framework.injection; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class DefaultAnnotationProxy implements InvocationHandler { + + private final Class annotationClass; + + public DefaultAnnotationProxy(Class annotationClass) { + this.annotationClass = annotationClass; + } + + public static S proxy(Class annotationClass) { + return (S) Proxy.newProxyInstance(DefaultAnnotationProxy.class.getClassLoader(), new Class[]{annotationClass}, new DefaultAnnotationProxy(annotationClass)); + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if (method.getName().equals("annotationType")) { + return annotationClass; + } else { + return annotationClass.getMethod(method.getName()).getDefaultValue(); + } + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/InstanceContext.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/InstanceContext.java index 85a83032d6..efd56ecc22 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/InstanceContext.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/InstanceContext.java @@ -14,10 +14,8 @@ public class InstanceContext { private final Set> dependencies = new HashSet<>(); private T value; private Class requestedValueType; - private final Class config; private LifeCycle lifeCycle; private final String ref; - private final String realmRef; private final Map notes = new HashMap<>(); public InstanceContext(Registry registry, Supplier supplier, A annotation, Class requestedValueType) { @@ -25,25 +23,16 @@ public class InstanceContext { this.supplier = supplier; this.annotation = annotation; this.requestedValueType = requestedValueType; - this.config = (Class) supplier.getAnnotationElementValue(annotation, SupplierHelpers.CONFIG); - this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE); - this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF); - this.realmRef = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REALM_REF); - } - - public InstanceContext(Registry registry, Supplier supplier, Class requestedValueType, String ref, Class config) { - this.registry = registry; - this.supplier = supplier; - this.annotation = null; - this.requestedValueType = requestedValueType; - this.config = config; - this.lifeCycle = supplier.getDefaultLifecycle(); - this.ref = ref; - this.realmRef = ""; + this.lifeCycle = supplier.getLifeCycle(annotation); + this.ref = StringUtil.convertEmptyToNull(supplier.getRef(annotation)); } public D getDependency(Class typeClazz) { - return registry.getDependency(typeClazz, this); + return getDependency(typeClazz, null); + } + + public D getDependency(Class typeClazz, String ref) { + return registry.getDependency(typeClazz, ref, this); } public Registry getRegistry() { @@ -66,10 +55,6 @@ public class InstanceContext { return requestedValueType; } - public Class getConfig() { - return config; - } - public LifeCycle getLifeCycle() { return lifeCycle; } @@ -78,10 +63,6 @@ public class InstanceContext { return ref; } - public String getRealmRef() { - return realmRef; - } - public A getAnnotation() { return annotation; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java index 8401f920ca..9b03bbc618 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Registry.java @@ -12,6 +12,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.ServiceLoader; import java.util.Set; @@ -39,17 +40,18 @@ public class Registry { this.currentContext = currentContext; } - public T getDependency(Class typeClass, InstanceContext dependent) { + public T getDependency(Class typeClass, String ref, InstanceContext dependent) { + ref = StringUtil.convertEmptyToNull(ref); T dependency; - dependency = getDeployedDependency(typeClass, dependent); + dependency = getDeployedDependency(typeClass, ref, dependent); if (dependency != null) { return dependency; } else { - dependency = getRequestedDependency(typeClass, dependent); + dependency = getRequestedDependency(typeClass, ref, dependent); if(dependency != null) { return dependency; } else { - dependency = getUnConfiguredDependency(typeClass, dependent); + dependency = getUnConfiguredDependency(typeClass, ref, dependent); if(dependency != null) { return dependency; } @@ -59,14 +61,8 @@ public class Registry { throw new RuntimeException("Dependency not found: " + typeClass); } - private T getDeployedDependency(Class typeClass, InstanceContext dependent) { - InstanceContext dependency; - if(!dependent.getRealmRef().equals("")) { - dependency = getDeployedInstance(typeClass, dependent.getRealmRef()); - } else { - dependency = getDeployedInstance(typeClass); - } - + private T getDeployedDependency(Class typeClass, String ref, InstanceContext dependent) { + InstanceContext dependency = getDeployedInstance(typeClass, ref); if (dependency != null) { dependency.registerDependency(dependent); @@ -81,16 +77,10 @@ public class Registry { return null; } - private T getRequestedDependency(Class typeClass, InstanceContext dependent) { - InstanceContext dependency; - RequestedInstance requestedDependency; - if(!dependent.getRealmRef().equals("")) { - requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef()); - } else { - requestedDependency = getRequestedInstance(typeClass); - } + private T getRequestedDependency(Class typeClass, String ref, InstanceContext dependent) { + RequestedInstance requestedDependency = getRequestedInstance(typeClass, ref); if (requestedDependency != null) { - dependency = new InstanceContext(this, requestedDependency.getSupplier(), requestedDependency.getAnnotation(), requestedDependency.getValueType()); + InstanceContext dependency = new InstanceContext(this, requestedDependency.getSupplier(), requestedDependency.getAnnotation(), requestedDependency.getValueType()); dependency.setValue(requestedDependency.getSupplier().getValue(dependency)); dependency.registerDependency(dependent); deployedInstances.add(dependency); @@ -108,16 +98,13 @@ public class Registry { return null; } - private T getUnConfiguredDependency(Class typeClass, InstanceContext dependent) { + private T getUnConfiguredDependency(Class typeClass, String ref, InstanceContext dependent) { InstanceContext dependency; Optional> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst(); if (supplied.isPresent()) { Supplier supplier = (Supplier) supplied.get(); - if(!dependent.getRealmRef().equals("")) { - dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class); - } else { - dependency = new InstanceContext(this, supplier, null, typeClass); - } + Annotation defaultAnnotation = DefaultAnnotationProxy.proxy(supplier.getAnnotationClass()); + dependency = new InstanceContext(this, supplier, defaultAnnotation, typeClass); dependency.registerDependency(dependent); dependency.setValue(supplier.getValue(dependency)); @@ -258,7 +245,7 @@ public class Registry { Supplier supplier = i.getSupplier(); if (supplier.getAnnotationClass().equals(a.annotationType()) && valueType.isAssignableFrom(i.getValue().getClass()) - && supplier.getAnnotationElementValue(a, SupplierHelpers.REF).equals(i.getRef()) ) { + && Objects.equals(supplier.getRef(a), i.getRef()) ) { return i; } } @@ -284,7 +271,7 @@ public class Registry { String requestedRef = requestedInstance.getRef(); Class requestedValueType = requestedInstance.getValueType(); for (InstanceContext i : deployedInstances) { - if(!i.getRef().equals(requestedRef)) { + if(!Objects.equals(i.getRef(), requestedRef)) { continue; } @@ -345,20 +332,16 @@ public class Registry { } } - private InstanceContext getDeployedInstance(Class typeClass) { - return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null); + private InstanceContext getDeployedInstance(Class typeClass, String ref) { + return deployedInstances.stream() + .filter(i -> i.getSupplier().getValueType().equals(typeClass) && Objects.equals(i.getRef(), ref)) + .findFirst().orElse(null); } - private InstanceContext getDeployedInstance(Class typeClass, String realmRef) { - return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).filter(j -> j.getRef().equals(realmRef)).findFirst().orElse(null); - } - - private RequestedInstance getRequestedInstance(Class typeClass) { - return requestedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null); - } - - private RequestedInstance getRequestedInstance(Class typeClass, String realmRef) { - return requestedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).filter(j -> j.getRef().equals(realmRef)).findFirst().orElse(null); + private RequestedInstance getRequestedInstance(Class typeClass, String ref) { + return requestedInstances.stream() + .filter(i -> i.getSupplier().getValueType().equals(typeClass) && Objects.equals(i.getRef(), ref)) + .findFirst().orElse(null); } } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/RequestedInstance.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/RequestedInstance.java index bb3dac6eda..c105415594 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/RequestedInstance.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/RequestedInstance.java @@ -14,8 +14,8 @@ public class RequestedInstance { this.supplier = supplier; this.annotation = annotation; this.valueType = valueType; - this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE); - this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF); + this.lifeCycle = supplier.getLifeCycle(annotation); + this.ref = StringUtil.convertEmptyToNull(supplier.getRef(annotation)); } public Supplier getSupplier() { diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/StringUtil.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/StringUtil.java new file mode 100644 index 0000000000..ca4ec854d5 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/StringUtil.java @@ -0,0 +1,9 @@ +package org.keycloak.test.framework.injection; + +public class StringUtil { + + public static String convertEmptyToNull(String s) { + return s != null && s.isEmpty() ? null : s; + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java index e48090b712..4832960e31 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/Supplier.java @@ -1,9 +1,6 @@ package org.keycloak.test.framework.injection; import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Optional; public interface Supplier { @@ -13,32 +10,12 @@ public interface Supplier { T getValue(InstanceContext instanceContext); - default Object getAnnotationElementValue(S annotation, String annotationAttribute) { - if (annotation != null) { - Optional annotationMethod = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals(annotationAttribute)).findFirst(); - if (annotationMethod.isPresent()) { - try { - return annotationMethod.get().invoke(annotation); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - return getAnnotationElementValue(annotationAttribute); + default String getRef(S annotation) { + return StringUtil.convertEmptyToNull(SupplierHelpers.getAnnotationField(annotation, AnnotationFields.REF)); } - default Object getAnnotationElementValue(String annotationAttribute) { - switch (annotationAttribute) { - case SupplierHelpers.LIFECYCLE -> { - return this.getDefaultLifecycle(); - } - case SupplierHelpers.REF, SupplierHelpers.REALM_REF -> { - return ""; - } - default -> { - return null; - } - } + default LifeCycle getLifeCycle(S annotation) { + return SupplierHelpers.getAnnotationField(annotation, AnnotationFields.LIFECYCLE, getDefaultLifecycle()); } default LifeCycle getDefaultLifecycle() { diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/SupplierHelpers.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/SupplierHelpers.java index 13bcd5e058..2c7e3e19a1 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/SupplierHelpers.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/injection/SupplierHelpers.java @@ -1,14 +1,11 @@ package org.keycloak.test.framework.injection; +import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; public class SupplierHelpers { - public static final String CONFIG = "config"; - public static final String LIFECYCLE = "lifecycle"; - public static final String REF = "ref"; - public static final String REALM_REF = "realmRef"; - public static T getInstance(Class clazz) { try { Constructor declaredConstructor = clazz.getDeclaredConstructor(); @@ -18,4 +15,29 @@ public class SupplierHelpers { throw new RuntimeException(e); } } + + public static T getAnnotationField(Annotation annotation, String name, T defaultValue) { + T value = getAnnotationField(annotation, name); + return value != null ? value : defaultValue; + } + + public static T getAnnotationField(Annotation annotation, String name) { + if (annotation != null) { + for (Method m : annotation.annotationType().getMethods()) { + if (m.getName().equals(name)) { + try { + return (T) m.invoke(annotation); + } catch (Exception e){ + throw new RuntimeException(e); + } + } + } + } + return null; + } + + public static String createName(InstanceContext instanceContext) { + return instanceContext.getRef() != null ? instanceContext.getRef() : "default"; + } + } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/ClientSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/ClientSupplier.java index ba7d149bb3..3bddc0a067 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/ClientSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/ClientSupplier.java @@ -23,13 +23,13 @@ public class ClientSupplier implements Supplier { @Override public ManagedClient getValue(InstanceContext instanceContext) { - ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class); + ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class, instanceContext.getAnnotation().realmRef()); ClientConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); ClientRepresentation clientRepresentation = config.getRepresentation(); if (clientRepresentation.getClientId() == null) { - String clientId = instanceContext.getRef(); + String clientId = SupplierHelpers.createName(instanceContext); clientRepresentation.setClientId(clientId); } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/RealmSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/RealmSupplier.java index 03aa8b8b7a..4111256a16 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/RealmSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/RealmSupplier.java @@ -29,11 +29,11 @@ public class RealmSupplier implements Supplier { KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.class); Keycloak adminClient = instanceContext.getDependency(Keycloak.class); - RealmConfig config = (RealmConfig) SupplierHelpers.getInstance(instanceContext.getConfig()); + RealmConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); RealmRepresentation realmRepresentation = config.getRepresentation(); if (realmRepresentation.getRealm() == null) { - String realmName = instanceContext.getRef(); + String realmName = SupplierHelpers.createName(instanceContext); realmRepresentation.setRealm(realmName); } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/UserSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/UserSupplier.java index b2d193c4c7..831d90938d 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/UserSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/realm/UserSupplier.java @@ -25,13 +25,13 @@ public class UserSupplier implements Supplier { @Override public ManagedUser getValue(InstanceContext instanceContext) { - ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class); + ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class, instanceContext.getAnnotation().realmRef()); UserConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); UserRepresentation userRepresentation = config.getRepresentation(); if (userRepresentation.getUsername() == null) { - String username = instanceContext.getRef(); + String username = SupplierHelpers.createName(instanceContext); userRepresentation.setUsername(username); } diff --git a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/DefaultAnnotationProxyTest.java b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/DefaultAnnotationProxyTest.java new file mode 100644 index 0000000000..5e940ab2b9 --- /dev/null +++ b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/DefaultAnnotationProxyTest.java @@ -0,0 +1,49 @@ +package org.keycloak.test.framework.injection; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.keycloak.test.framework.annotations.InjectRealm; +import org.keycloak.test.framework.realm.DefaultRealmConfig; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.LinkedList; +import java.util.List; + +public class DefaultAnnotationProxyTest { + + @Test + public void testGetField() { + MockAnnotation proxy = DefaultAnnotationProxy.proxy(MockAnnotation.class); + Assertions.assertEquals(LifeCycle.CLASS, proxy.lifecycle()); + Assertions.assertEquals(LinkedList.class, proxy.config()); + Assertions.assertEquals("", proxy.ref()); + Assertions.assertEquals("else", proxy.something()); + } + + @Test + public void testAnnotationReflection() { + MockAnnotation proxy = DefaultAnnotationProxy.proxy(MockAnnotation.class); + Assertions.assertEquals(LifeCycle.CLASS, SupplierHelpers.getAnnotationField(proxy, "lifecycle")); + Assertions.assertEquals(LinkedList.class, SupplierHelpers.getAnnotationField(proxy, "config")); + Assertions.assertEquals("", SupplierHelpers.getAnnotationField(proxy, "ref")); + Assertions.assertEquals("else", SupplierHelpers.getAnnotationField(proxy, "something")); + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface MockAnnotation { + + Class config() default LinkedList.class; + + LifeCycle lifecycle() default LifeCycle.CLASS; + + String ref() default ""; + + String something() default "else"; + + } + +} diff --git a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/RegistryTest.java b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/RegistryTest.java index 3fb638c527..030cf26a8e 100644 --- a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/RegistryTest.java +++ b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/RegistryTest.java @@ -302,10 +302,10 @@ public class RegistryTest { @MockParentAnnotation(ref = "123") MockParentValue parent123; - @MockChildAnnotation(ref = "123", realmRef = "123") + @MockChildAnnotation(ref = "123", parentRef = "123") MockChildValue child123; - @MockChildAnnotation(ref = "ABC", realmRef = "ABC") + @MockChildAnnotation(ref = "ABC", parentRef = "ABC") MockChildValue childABC; } } diff --git a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildAnnotation.java b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildAnnotation.java index 812f653d2a..bbb678eabf 100644 --- a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildAnnotation.java +++ b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildAnnotation.java @@ -10,5 +10,5 @@ import java.lang.annotation.Target; public @interface MockChildAnnotation { String ref() default ""; - String realmRef() default ""; + String parentRef() default ""; } diff --git a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildSupplier.java b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildSupplier.java index d714886677..357f1b4c37 100644 --- a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildSupplier.java +++ b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockChildSupplier.java @@ -25,7 +25,7 @@ public class MockChildSupplier implements Supplier instanceContext) { - MockParentValue mockParentValue = instanceContext.getDependency(MockParentValue.class); + MockParentValue mockParentValue = instanceContext.getDependency(MockParentValue.class, instanceContext.getAnnotation().parentRef()); return new MockChildValue(mockParentValue); }