Aded Registry test for realmRef

Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
Simon Vacek 2024-08-05 15:58:23 +02:00 committed by Pedro Igor
parent 5a6ac58107
commit e8d29462ff
4 changed files with 52 additions and 35 deletions

View file

@ -5,11 +5,9 @@ import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.InjectAdminClient; 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.InjectRealm;
import org.keycloak.test.framework.annotations.InjectUser; import org.keycloak.test.framework.annotations.InjectUser;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; 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.ManagedRealm;
import org.keycloak.test.framework.realm.ManagedUser; import org.keycloak.test.framework.realm.ManagedUser;
import org.keycloak.test.framework.realm.RealmConfig; import org.keycloak.test.framework.realm.RealmConfig;
@ -17,51 +15,39 @@ import org.keycloak.test.framework.realm.RealmConfig;
@KeycloakIntegrationTest @KeycloakIntegrationTest
public class MultipleInstancesTest { public class MultipleInstancesTest {
private final String REALM_A_REF = "realm";
private final String USER_A_REF = "user";
@InjectAdminClient @InjectAdminClient
Keycloak adminClient; Keycloak adminClient;
@InjectRealm @InjectRealm
ManagedRealm realmDef1; ManagedRealm realmDef;
@InjectRealm @InjectRealm(ref = REALM_A_REF, config = CustomRealmConfig.class)
ManagedRealm realmDef2;
@InjectRealm(ref = "A", config = CustomRealmConfig.class)
ManagedRealm realmA; ManagedRealm realmA;
@InjectClient(ref = "client1") @InjectUser
ManagedClient client1; ManagedUser userDef;
@InjectUser(ref = "user1", realmRef = "default") @InjectUser(ref = USER_A_REF, realmRef = REALM_A_REF)
ManagedUser user1; ManagedUser userA;
@InjectUser(ref = "user2", realmRef = "A")
ManagedUser user2;
@InjectUser(ref = "user3", realmRef = "A")
ManagedUser user3;
@InjectUser(ref = "user4", realmRef = "B")
ManagedUser user4;
@Test @Test
public void testMultipleInstances() { public void testMultipleInstances() {
Assertions.assertEquals("default", realmDef1.getName()); Assertions.assertEquals("default", realmDef.getName());
Assertions.assertEquals("default", realmDef2.getName()); Assertions.assertEquals(REALM_A_REF, realmA.getName());
Assertions.assertSame(realmDef1, realmDef2);
Assertions.assertEquals("A", realmA.getName());
} }
@Test @Test
public void testRealmRef() { public void testRealmRef() {
Assertions.assertFalse(realmDef1.admin().clients().findByClientId("client1").isEmpty()); 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.assertEquals(1, realmDef1.admin().users().count()); Assertions.assertEquals("default", realmDefUsers.get(0).getUsername());
Assertions.assertEquals(2, realmA.admin().users().count()); Assertions.assertEquals(USER_A_REF, realmAUsers.get(0).getUsername());
Assertions.assertNotNull(adminClient.realm("B"));
Assertions.assertNotNull(adminClient.realm("B").users().get("user4"));
} }

View file

@ -61,7 +61,7 @@ public class Registry {
private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) { private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
InstanceContext dependency; InstanceContext dependency;
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { if(!dependent.getRealmRef().equals("")) {
dependency = getDeployedInstance(typeClass, dependent.getRealmRef()); dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
} else { } else {
dependency = getDeployedInstance(typeClass); dependency = getDeployedInstance(typeClass);
@ -84,7 +84,7 @@ public class Registry {
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) { private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
InstanceContext dependency; InstanceContext dependency;
RequestedInstance requestedDependency; RequestedInstance requestedDependency;
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { if(!dependent.getRealmRef().equals("")) {
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef()); requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
} else { } else {
requestedDependency = getRequestedInstance(typeClass); requestedDependency = getRequestedInstance(typeClass);
@ -113,7 +113,7 @@ public class Registry {
Optional<Supplier<?, ?>> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst(); Optional<Supplier<?, ?>> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst();
if (supplied.isPresent()) { if (supplied.isPresent()) {
Supplier<T, ?> supplier = (Supplier<T, ?>) supplied.get(); Supplier<T, ?> supplier = (Supplier<T, ?>) supplied.get();
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) { if(!dependent.getRealmRef().equals("")) {
dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class); dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class);
} else { } else {
dependency = new InstanceContext(this, supplier, null, typeClass); dependency = new InstanceContext(this, supplier, null, typeClass);
@ -207,6 +207,9 @@ public class Registry {
private void injectFields(Object testInstance) { private void injectFields(Object testInstance) {
for (Field f : testInstance.getClass().getDeclaredFields()) { for (Field f : testInstance.getClass().getDeclaredFields()) {
InstanceContext<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations()); InstanceContext<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations());
if(instance == null) { // a test class might have fields not meant for injection
continue;
}
try { try {
f.setAccessible(true); f.setAccessible(true);
f.set(testInstance, instance.getValue()); f.set(testInstance, instance.getValue());

View file

@ -221,11 +221,12 @@ public class RegistryTest {
Assertions.assertNotSame(refTest.a, refTest.b); Assertions.assertNotSame(refTest.a, refTest.b);
assertRunning(refTest.def, refTest.a, refTest.b); assertRunning(refTest.def, refTest.a, refTest.b);
Assertions.assertSame(refTest.a, refTest.a2);
registry.afterEach(); registry.afterEach();
registry.beforeEach(refTest); registry.beforeEach(refTest);
assertRunning(refTest.def, refTest.a, refTest.b); assertRunning(refTest.def, refTest.a2, refTest.b);
Assertions.assertSame(def1, refTest.def); Assertions.assertSame(def1, refTest.def);
Assertions.assertSame(a1, refTest.a); Assertions.assertSame(a1, refTest.a);
@ -237,6 +238,16 @@ public class RegistryTest {
assertClosed(refTest.def, refTest.a, refTest.b); 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) { public static void assertRunning(Object... values) {
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values)); MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values));
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length)); MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length));
@ -280,7 +291,21 @@ public class RegistryTest {
@MockParentAnnotation(ref = "a") @MockParentAnnotation(ref = "a")
MockParentValue a; MockParentValue a;
@MockParentAnnotation(ref = "a")
MockParentValue a2;
@MockParentAnnotation(ref = "b") @MockParentAnnotation(ref = "b")
MockParentValue 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;
}
} }

View file

@ -8,4 +8,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface MockChildAnnotation { public @interface MockChildAnnotation {
String ref() default "";
String realmRef() default "";
} }