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.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 @@ import org.keycloak.test.framework.realm.RealmConfig;
@KeycloakIntegrationTest
public class MultipleInstancesTest {
private final String REALM_A_REF = "realm";
private final String USER_A_REF = "user";
@InjectAdminClient
Keycloak adminClient;
@InjectRealm
ManagedRealm realmDef1;
ManagedRealm realmDef;
@InjectRealm
ManagedRealm realmDef2;
@InjectRealm(ref = "A", config = CustomRealmConfig.class)
@InjectRealm(ref = REALM_A_REF, config = CustomRealmConfig.class)
ManagedRealm realmA;
@InjectClient(ref = "client1")
ManagedClient client1;
@InjectUser
ManagedUser userDef;
@InjectUser(ref = "user1", realmRef = "default")
ManagedUser user1;
@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());
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(2, realmA.admin().users().count());
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());
}

View file

@ -61,7 +61,7 @@ public class Registry {
private <T> T getDeployedDependency(Class<T> 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 @@ public class Registry {
private <T> T getRequestedDependency(Class<T> 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 @@ public class Registry {
Optional<Supplier<?, ?>> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst();
if (supplied.isPresent()) {
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);
} else {
dependency = new InstanceContext(this, supplier, null, typeClass);
@ -207,6 +207,9 @@ public class Registry {
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());

View file

@ -221,11 +221,12 @@ public class RegistryTest {
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 class RegistryTest {
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 class RegistryTest {
@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;
}
}

View file

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