From e8d29462ff848fb9eea0c9b626b01ae2e30572f0 Mon Sep 17 00:00:00 2001 From: Simon Vacek Date: Mon, 5 Aug 2024 15:58:23 +0200 Subject: [PATCH] Aded Registry test for realmRef Signed-off-by: Simon Vacek --- .../test/examples/MultipleInstancesTest.java | 48 +++++++------------ .../test/framework/injection/Registry.java | 9 ++-- .../framework/injection/RegistryTest.java | 27 ++++++++++- .../injection/mocks/MockChildAnnotation.java | 3 ++ 4 files changed, 52 insertions(+), 35 deletions(-) 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 index d960cc230269..0954f35288be 100644 --- 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 @@ -5,11 +5,9 @@ import org.keycloak.admin.client.Keycloak; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.test.framework.annotations.InjectAdminClient; -import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectUser; import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; -import org.keycloak.test.framework.realm.ManagedClient; import org.keycloak.test.framework.realm.ManagedRealm; import org.keycloak.test.framework.realm.ManagedUser; import org.keycloak.test.framework.realm.RealmConfig; @@ -17,51 +15,39 @@ @KeycloakIntegrationTest public class MultipleInstancesTest { + private final String REALM_A_REF = "realm"; + private final String USER_A_REF = "user"; + @InjectAdminClient Keycloak adminClient; @InjectRealm - ManagedRealm realmDef1; - - @InjectRealm - ManagedRealm realmDef2; + ManagedRealm realmDef; - @InjectRealm(ref = "A", config = CustomRealmConfig.class) + @InjectRealm(ref = REALM_A_REF, config = CustomRealmConfig.class) ManagedRealm realmA; - @InjectClient(ref = "client1") - ManagedClient client1; - - @InjectUser(ref = "user1", realmRef = "default") - ManagedUser user1; + @InjectUser + ManagedUser userDef; - @InjectUser(ref = "user2", realmRef = "A") - ManagedUser user2; - - @InjectUser(ref = "user3", realmRef = "A") - ManagedUser user3; - - @InjectUser(ref = "user4", realmRef = "B") - ManagedUser user4; + @InjectUser(ref = USER_A_REF, realmRef = REALM_A_REF) + ManagedUser userA; @Test public void testMultipleInstances() { - Assertions.assertEquals("default", realmDef1.getName()); - Assertions.assertEquals("default", realmDef2.getName()); - Assertions.assertSame(realmDef1, realmDef2); - - Assertions.assertEquals("A", realmA.getName()); + Assertions.assertEquals("default", realmDef.getName()); + Assertions.assertEquals(REALM_A_REF, realmA.getName()); } @Test public void testRealmRef() { - Assertions.assertFalse(realmDef1.admin().clients().findByClientId("client1").isEmpty()); - - Assertions.assertEquals(1, realmDef1.admin().users().count()); - Assertions.assertEquals(2, realmA.admin().users().count()); + var realmDefUsers = adminClient.realm("default").users().search("default"); + var realmAUsers = adminClient.realm(REALM_A_REF).users().search(USER_A_REF); + Assertions.assertEquals(1, realmDefUsers.size()); + Assertions.assertEquals(1, realmAUsers.size()); - Assertions.assertNotNull(adminClient.realm("B")); - Assertions.assertNotNull(adminClient.realm("B").users().get("user4")); + Assertions.assertEquals("default", realmDefUsers.get(0).getUsername()); + Assertions.assertEquals(USER_A_REF, realmAUsers.get(0).getUsername()); } 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 2ab5216715a2..8401f920ca29 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 @@ -61,7 +61,7 @@ public T getDependency(Class typeClass, InstanceContext dependent) { private T getDeployedDependency(Class typeClass, InstanceContext dependent) { InstanceContext dependency; - if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { + if(!dependent.getRealmRef().equals("")) { dependency = getDeployedInstance(typeClass, dependent.getRealmRef()); } else { dependency = getDeployedInstance(typeClass); @@ -84,7 +84,7 @@ private T getDeployedDependency(Class typeClass, InstanceContext dependen private T getRequestedDependency(Class typeClass, InstanceContext dependent) { InstanceContext dependency; RequestedInstance requestedDependency; - if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { + if(!dependent.getRealmRef().equals("")) { requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef()); } else { requestedDependency = getRequestedInstance(typeClass); @@ -113,7 +113,7 @@ private T getUnConfiguredDependency(Class typeClass, InstanceContext depe Optional> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst(); if (supplied.isPresent()) { Supplier supplier = (Supplier) supplied.get(); - if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { + if(!dependent.getRealmRef().equals("")) { dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class); } else { dependency = new InstanceContext(this, supplier, null, typeClass); @@ -207,6 +207,9 @@ private void deployRequestedInstances() { private void injectFields(Object testInstance) { for (Field f : testInstance.getClass().getDeclaredFields()) { InstanceContext instance = getDeployedInstance(f.getType(), f.getAnnotations()); + if(instance == null) { // a test class might have fields not meant for injection + continue; + } try { f.setAccessible(true); f.set(testInstance, instance.getValue()); 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 dcac3520af30..3fb638c527ff 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 @@ -221,11 +221,12 @@ public void testMultiplRef() { Assertions.assertNotSame(refTest.a, refTest.b); assertRunning(refTest.def, refTest.a, refTest.b); + Assertions.assertSame(refTest.a, refTest.a2); registry.afterEach(); registry.beforeEach(refTest); - assertRunning(refTest.def, refTest.a, refTest.b); + assertRunning(refTest.def, refTest.a2, refTest.b); Assertions.assertSame(def1, refTest.def); Assertions.assertSame(a1, refTest.a); @@ -237,6 +238,16 @@ public void testMultiplRef() { assertClosed(refTest.def, refTest.a, refTest.b); } + @Test + public void testRealmRef() { + RealmRefTest test = new RealmRefTest(); + registry.beforeEach(test); + + assertRunning(test.childABC, test.childABC.getParent(), test.child123, test.parent123); + Assertions.assertNotSame(test.childABC.getParent(), test.parent123); + Assertions.assertSame(test.child123.getParent(), test.parent123); + } + public static void assertRunning(Object... values) { MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values)); MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length)); @@ -280,7 +291,21 @@ public static final class MultipleRefTest { @MockParentAnnotation(ref = "a") MockParentValue a; + @MockParentAnnotation(ref = "a") + MockParentValue a2; + @MockParentAnnotation(ref = "b") MockParentValue b; } + + public static final class RealmRefTest { + @MockParentAnnotation(ref = "123") + MockParentValue parent123; + + @MockChildAnnotation(ref = "123", realmRef = "123") + MockChildValue child123; + + @MockChildAnnotation(ref = "ABC", realmRef = "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 0158c6efe688..812f653d2a25 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 @@ -8,4 +8,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MockChildAnnotation { + + String ref() default ""; + String realmRef() default ""; }