From 5a6ac58107e9fecf8664fda3fe5f5d26c010123d Mon Sep 17 00:00:00 2001 From: Simon Vacek Date: Wed, 31 Jul 2024 13:12:05 +0200 Subject: [PATCH] Refactor annotation element retrieval Signed-off-by: Simon Vacek --- test-poc/examples/pom.xml | 6 -- .../framework/injection/InstanceContext.java | 8 +-- .../test/framework/injection/Registry.java | 7 +-- .../injection/RequestedInstance.java | 9 +-- .../test/framework/injection/Supplier.java | 58 +++++-------------- .../framework/injection/SupplierHelpers.java | 6 +- .../framework/oauth/OAuthClientSupplier.java | 2 +- .../webdriver/AbstractWebDriverSupplier.java | 2 +- 8 files changed, 31 insertions(+), 67 deletions(-) diff --git a/test-poc/examples/pom.xml b/test-poc/examples/pom.xml index 714c874c7b..09bc2cbf3b 100755 --- a/test-poc/examples/pom.xml +++ b/test-poc/examples/pom.xml @@ -53,12 +53,6 @@ com.microsoft.sqlserver mssql-jdbc - - - com.nimbusds - oauth2-oidc-sdk - 11.13 - 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 0fd2a7161b..85a83032d6 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 @@ -25,10 +25,10 @@ public class InstanceContext { this.supplier = supplier; this.annotation = annotation; this.requestedValueType = requestedValueType; - this.config = supplier.getConfig(annotation); - this.lifeCycle = supplier.getLifeCycle(annotation); - this.ref = supplier.getRef(annotation); - this.realmRef = supplier.getRealmRef(annotation); + 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) { 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 3dfdb8b02b..2ab5216715 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 @@ -2,7 +2,6 @@ package org.keycloak.test.framework.injection; import org.jboss.logging.Logger; import org.junit.jupiter.api.extension.ExtensionContext; -import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.config.Config; import org.keycloak.test.framework.realm.DefaultRealmConfig; import org.keycloak.test.framework.realm.ManagedRealm; @@ -62,7 +61,7 @@ public class Registry { private T getDeployedDependency(Class typeClass, InstanceContext dependent) { InstanceContext dependency; - if(!dependent.getRealmRef().equals("")) { + if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { dependency = getDeployedInstance(typeClass, dependent.getRealmRef()); } else { dependency = getDeployedInstance(typeClass); @@ -85,7 +84,7 @@ public class Registry { private T getRequestedDependency(Class typeClass, InstanceContext dependent) { InstanceContext dependency; RequestedInstance requestedDependency; - if(!dependent.getRealmRef().equals("")) { + if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef()); } else { requestedDependency = getRequestedInstance(typeClass); @@ -256,7 +255,7 @@ public class Registry { Supplier supplier = i.getSupplier(); if (supplier.getAnnotationClass().equals(a.annotationType()) && valueType.isAssignableFrom(i.getValue().getClass()) - && supplier.getRef(a).equals(i.getRef()) ) { + && supplier.getAnnotationElementValue(a, SupplierHelpers.REF).equals(i.getRef()) ) { return i; } } 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 bd4afa770a..bb3dac6eda 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 @@ -9,15 +9,13 @@ public class RequestedInstance { private final Class valueType; private final LifeCycle lifeCycle; private final String ref; - private final String realmRef; public RequestedInstance(Supplier supplier, A annotation, Class valueType) { this.supplier = supplier; this.annotation = annotation; this.valueType = valueType; - this.lifeCycle = supplier.getLifeCycle(annotation); - this.ref = supplier.getRef(annotation); - this.realmRef = supplier.getRealmRef(annotation); + this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE); + this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF); } public Supplier getSupplier() { @@ -40,7 +38,4 @@ public class RequestedInstance { return ref; } - public String getRealmRef() { - return realmRef; - } } 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 7f23b11327..e48090b712 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 @@ -13,60 +13,32 @@ public interface Supplier { T getValue(InstanceContext instanceContext); - default Class getConfig(S annotation) { + default Object getAnnotationElementValue(S annotation, String annotationAttribute) { if (annotation != null) { - Optional config = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("config")).findFirst(); - if (config.isPresent()) { + Optional annotationMethod = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals(annotationAttribute)).findFirst(); + if (annotationMethod.isPresent()) { try { - return (Class) config.get().invoke(annotation); + return annotationMethod.get().invoke(annotation); } catch (Exception e) { throw new RuntimeException(e); } } } - return null; + return getAnnotationElementValue(annotationAttribute); } - default LifeCycle getLifeCycle(S annotation) { - if (annotation != null) { - Optional lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst(); - if (lifecycle.isPresent()) { - try { - return (LifeCycle) lifecycle.get().invoke(annotation); - } catch (Exception e) { - throw new RuntimeException(e); - } + default Object getAnnotationElementValue(String annotationAttribute) { + switch (annotationAttribute) { + case SupplierHelpers.LIFECYCLE -> { + return this.getDefaultLifecycle(); + } + case SupplierHelpers.REF, SupplierHelpers.REALM_REF -> { + return ""; + } + default -> { + return null; } } - return getDefaultLifecycle(); - } - - default String getRef(S annotation) { - if (annotation != null) { - Optional ref = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("ref")).findFirst(); - if (ref.isPresent()) { - try { - return (String) ref.get().invoke(annotation); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - return ""; - } - - default String getRealmRef(S annotation) { - if (annotation != null) { - Optional realmRef = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("realmRef")).findFirst(); - if (realmRef.isPresent()) { - try { - return (String) realmRef.get().invoke(annotation); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - return ""; } 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 46402f6709..13bcd5e058 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 @@ -4,6 +4,11 @@ import java.lang.reflect.Constructor; 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(); @@ -13,5 +18,4 @@ public class SupplierHelpers { throw new RuntimeException(e); } } - } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/oauth/OAuthClientSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/oauth/OAuthClientSupplier.java index 183e5f938b..66b7b6b64b 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/oauth/OAuthClientSupplier.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/oauth/OAuthClientSupplier.java @@ -34,7 +34,7 @@ public class OAuthClientSupplier implements Supplier