diff --git a/test-poc/examples/src/test/java/org/keycloak/test/examples/GlobalManagedResourcesTest.java b/test-poc/examples/src/test/java/org/keycloak/test/examples/GlobalManagedResourcesTest.java index c389b01bc3..de9d0b6bdc 100644 --- a/test-poc/examples/src/test/java/org/keycloak/test/examples/GlobalManagedResourcesTest.java +++ b/test-poc/examples/src/test/java/org/keycloak/test/examples/GlobalManagedResourcesTest.java @@ -28,17 +28,17 @@ public class GlobalManagedResourcesTest { @Test public void testCreatedRealm() { - Assertions.assertEquals(DefaultRealmConfig.class.getSimpleName(), realmResource.toRepresentation().getRealm()); + Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); } @Test public void testCreatedClient() { - Assertions.assertEquals(DefaultClientConfig.class.getSimpleName(), clientResource.toRepresentation().getClientId()); + Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); } @Test public void testCreatedUser() { - Assertions.assertEquals(DefaultUserConfig.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername()); + Assertions.assertEquals("default", userResource.toRepresentation().getUsername()); } } diff --git a/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResources2Test.java b/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResources2Test.java index 31f9975636..663c0fa1b2 100644 --- a/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResources2Test.java +++ b/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResources2Test.java @@ -23,14 +23,14 @@ public class ManagedResources2Test { @Test public void testCreatedRealm() { - Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), realmResource.toRepresentation().getRealm()); + Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); } @Test public void testCreatedClient() { - Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), clientResource.toRepresentation().getClientId()); + Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); - List clients = realmResource.clients().findByClientId(ManagedResources2Test.class.getSimpleName()); + List clients = realmResource.clients().findByClientId("default"); Assertions.assertEquals(1, clients.size()); } diff --git a/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResourcesTest.java b/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResourcesTest.java index 169f60a21a..a755ffafe4 100644 --- a/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResourcesTest.java +++ b/test-poc/examples/src/test/java/org/keycloak/test/examples/ManagedResourcesTest.java @@ -28,20 +28,20 @@ public class ManagedResourcesTest { @Test public void testCreatedRealm() { - Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), realmResource.toRepresentation().getRealm()); + Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); } @Test public void testCreatedClient() { - Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), clientResource.toRepresentation().getClientId()); + Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); - List clients = realmResource.clients().findByClientId(ManagedResourcesTest.class.getSimpleName()); + List clients = realmResource.clients().findByClientId("default"); Assertions.assertEquals(1, clients.size()); } @Test public void testCreatedUser() { - Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername()); + Assertions.assertEquals("default", userResource.toRepresentation().getUsername()); } } diff --git a/test-poc/examples/src/test/java/org/keycloak/test/examples/MultipleInstancesTest.java b/test-poc/examples/src/test/java/org/keycloak/test/examples/MultipleInstancesTest.java new file mode 100644 index 0000000000..bde1cbe666 --- /dev/null +++ b/test-poc/examples/src/test/java/org/keycloak/test/examples/MultipleInstancesTest.java @@ -0,0 +1,52 @@ +package org.keycloak.test.examples; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.keycloak.admin.client.resource.ClientResource; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; +import org.keycloak.test.framework.annotations.TestClient; +import org.keycloak.test.framework.annotations.TestRealm; +import org.keycloak.test.framework.realm.RealmConfig; + +@KeycloakIntegrationTest +public class MultipleInstancesTest { + + @TestRealm + RealmResource realm1; + + @TestRealm + RealmResource realm2; + + @TestRealm(ref = "another", config = CustomRealmConfig.class) + RealmResource realm3; + + @TestClient(ref = "client1") + ClientResource client; + + @TestClient + ClientResource client2; + + @Test + public void testMultipleInstances() { + Assertions.assertEquals("default", realm1.toRepresentation().getRealm()); + Assertions.assertEquals("default", realm2.toRepresentation().getRealm()); + Assertions.assertEquals(realm1, realm2); + + Assertions.assertEquals("another", realm3.toRepresentation().getRealm()); + + Assertions.assertEquals("client1", client.toRepresentation().getClientId()); + Assertions.assertEquals("default", client2.toRepresentation().getClientId()); + } + + + public static class CustomRealmConfig implements RealmConfig { + @Override + public RealmRepresentation getRepresentation() { + return new RealmRepresentation(); + } + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestClient.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestClient.java index f60a888cbf..3e76c6f1eb 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestClient.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestClient.java @@ -15,6 +15,8 @@ public @interface TestClient { Class config() default DefaultClientConfig.class; + String ref() default "default"; + LifeCycle lifecycle() default LifeCycle.CLASS; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestRealm.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestRealm.java index 4176722093..c7be82877d 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestRealm.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestRealm.java @@ -17,4 +17,5 @@ public @interface TestRealm { LifeCycle lifecycle() default LifeCycle.CLASS; + String ref() default "default"; } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestUser.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestUser.java index 3b5ba5f46b..5c879042e0 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestUser.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/annotations/TestUser.java @@ -17,4 +17,5 @@ public @interface TestUser { LifeCycle lifecycle() default LifeCycle.CLASS; + String ref() default "default"; } 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 871bb70159..4c6c04ce72 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 @@ -15,6 +15,7 @@ public class InstanceContext { private T value; private Class requestedValueType; private LifeCycle lifeCycle; + private final String ref; private final Map notes = new HashMap<>(); public InstanceContext(Registry registry, Supplier supplier, A annotation, Class requestedValueType) { @@ -23,6 +24,7 @@ public class InstanceContext { this.annotation = annotation; this.requestedValueType = requestedValueType; this.lifeCycle = supplier.getLifeCycle(annotation); + this.ref = supplier.getRef(annotation); } public D getDependency(Class typeClazz) { @@ -53,6 +55,10 @@ public class InstanceContext { return lifeCycle; } + public String getRef() { + return ref; + } + 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 02484b8dc4..fa54efda98 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 @@ -14,6 +14,7 @@ import java.util.Optional; import java.util.ServiceLoader; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; @SuppressWarnings({"rawtypes", "unchecked"}) public class Registry { @@ -146,13 +147,15 @@ public class Registry { while (!requestedInstances.isEmpty()) { RequestedInstance requestedInstance = requestedInstances.remove(0); - InstanceContext instance = new InstanceContext(this, requestedInstance.getSupplier(), requestedInstance.getAnnotation(), requestedInstance.getValueType()); - instance.setValue(requestedInstance.getSupplier().getValue(instance)); - deployedInstances.add(instance); + if (getDeployedInstance(requestedInstance) == null) { + InstanceContext instance = new InstanceContext(this, requestedInstance.getSupplier(), requestedInstance.getAnnotation(), requestedInstance.getValueType()); + instance.setValue(requestedInstance.getSupplier().getValue(instance)); + deployedInstances.add(instance); - if (LOGGER.isTraceEnabled()) { - LOGGER.tracev("Created instance: {0}", - requestedInstance.getSupplier().getClass().getSimpleName()); + if (LOGGER.isTraceEnabled()) { + LOGGER.tracev("Created instance: {0}", + requestedInstance.getSupplier().getClass().getSimpleName()); + } } } } @@ -205,8 +208,10 @@ public class Registry { private InstanceContext getDeployedInstance(Class valueType, Annotation[] annotations) { for (Annotation a : annotations) { for (InstanceContext i : deployedInstances) { - Supplier supplier = i.getSupplier(); - if (supplier.getAnnotationClass().equals(a.annotationType()) && valueType.isAssignableFrom(i.getValue().getClass())) { + Supplier supplier = i.getSupplier(); + if (supplier.getAnnotationClass().equals(a.annotationType()) + && valueType.isAssignableFrom(i.getValue().getClass()) + && supplier.getRef(a).equals(i.getRef()) ) { return i; } } @@ -229,8 +234,13 @@ public class Registry { } private InstanceContext getDeployedInstance(RequestedInstance requestedInstance) { + String requestedRef = requestedInstance.getRef(); Class requestedValueType = requestedInstance.getValueType(); for (InstanceContext i : deployedInstances) { + if(!i.getRef().equals(requestedRef)) { + continue; + } + if (requestedValueType != null) { if (requestedValueType.isAssignableFrom(i.getValue().getClass())) { 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 a5e45d6e0b..3861311799 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 @@ -8,12 +8,14 @@ public class RequestedInstance { private final A annotation; private final Class valueType; private final LifeCycle lifeCycle; + private final String ref; 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); } public Supplier getSupplier() { @@ -31,4 +33,8 @@ public class RequestedInstance { public LifeCycle getLifeCycle() { return lifeCycle; } + + public String getRef() { + return ref; + } } 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 f55e375e22..1eba1783dd 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 @@ -27,6 +27,20 @@ public interface Supplier { 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 LifeCycle getDefaultLifecycle() { return LifeCycle.CLASS; } 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 0004b3eb63..99b779a485 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 @@ -33,7 +33,7 @@ public class ClientSupplier implements Supplier { ClientRepresentation clientRepresentation = config.getRepresentation(); if (clientRepresentation.getClientId() == null) { - String clientId = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName(); + String clientId = instanceContext.getRef(); 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 a091b31958..3131f41dcd 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 @@ -32,7 +32,7 @@ public class RealmSupplier implements Supplier { RealmRepresentation realmRepresentation = config.getRepresentation(); if (realmRepresentation.getRealm() == null) { - String realmName = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName(); + String realmName = instanceContext.getRef(); 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 9aacc1cf7f..75899080d5 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 @@ -33,7 +33,7 @@ public class UserSupplier implements Supplier { UserRepresentation userRepresentation = config.getRepresentation(); if (userRepresentation.getUsername() == null) { - String username = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName(); + String username = instanceContext.getRef(); userRepresentation.setUsername(username); } 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 30ce31334b..dcac3520af 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 @@ -208,6 +208,35 @@ public class RegistryTest { assertRunning(test.child, test.child.getParent()); } + @Test + public void testMultiplRef() { + MultipleRefTest refTest = new MultipleRefTest(); + registry.beforeEach(refTest); + + MockParentValue def1 = refTest.def; + MockParentValue a1 = refTest.a; + + Assertions.assertNotSame(refTest.def, refTest.a); + Assertions.assertNotSame(refTest.def, refTest.b); + Assertions.assertNotSame(refTest.a, refTest.b); + + assertRunning(refTest.def, refTest.a, refTest.b); + + registry.afterEach(); + + registry.beforeEach(refTest); + assertRunning(refTest.def, refTest.a, refTest.b); + + Assertions.assertSame(def1, refTest.def); + Assertions.assertSame(a1, refTest.a); + + registry.afterEach(); + assertRunning(refTest.def, refTest.a, refTest.b); + + registry.afterAll(); + assertClosed(refTest.def, refTest.a, refTest.b); + } + public static void assertRunning(Object... values) { MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values)); MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length)); @@ -243,4 +272,15 @@ public class RegistryTest { @MockParentAnnotation MockParentValue parent; } + + public static final class MultipleRefTest { + @MockParentAnnotation() + MockParentValue def; + + @MockParentAnnotation(ref = "a") + MockParentValue a; + + @MockParentAnnotation(ref = "b") + MockParentValue b; + } } diff --git a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockParentAnnotation.java b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockParentAnnotation.java index f032ec2f60..c75fe1d1f2 100644 --- a/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockParentAnnotation.java +++ b/test-poc/framework/src/test/java/org/keycloak/test/framework/injection/mocks/MockParentAnnotation.java @@ -8,4 +8,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MockParentAnnotation { + + String ref() default ""; + }