Fix creating unconfigured realm dependency

Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
Simon Vacek 2024-07-29 18:21:19 +02:00 committed by Pedro Igor
parent 11595e2349
commit ef54d20be1
5 changed files with 56 additions and 43 deletions

View file

@ -2,7 +2,9 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
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.InjectClient; 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;
@ -15,63 +17,51 @@ import org.keycloak.test.framework.realm.RealmConfig;
@KeycloakIntegrationTest @KeycloakIntegrationTest
public class MultipleInstancesTest { public class MultipleInstancesTest {
@InjectRealm @InjectAdminClient
ManagedRealm realm1; Keycloak adminClient;
@InjectRealm @InjectRealm
ManagedRealm realm2; ManagedRealm realmDef1;
@InjectRealm(ref = "another", config = CustomRealmConfig.class) @InjectRealm
ManagedRealm realm3; ManagedRealm realmDef2;
@InjectRealm(ref = "anotherOne") @InjectRealm(ref = "A", config = CustomRealmConfig.class)
ManagedRealm realm4; ManagedRealm realmA;
@InjectClient(ref = "client1") @InjectClient(ref = "client1")
ManagedClient client; ManagedClient client1;
@InjectClient @InjectUser(ref = "user1", realmRef = "default")
ManagedClient client2;
@InjectUser(realmRef = "default")
ManagedUser user1; ManagedUser user1;
@InjectUser(realmRef = "another") @InjectUser(ref = "user2", realmRef = "A")
ManagedUser user2; ManagedUser user2;
@InjectUser(ref = "another", realmRef = "another") @InjectUser(ref = "user3", realmRef = "A")
ManagedUser user3; ManagedUser user3;
@InjectUser(ref = "anotherOne", realmRef = "anotherOne") @InjectUser(ref = "user4", realmRef = "B")
ManagedUser user4; ManagedUser user4;
@InjectUser(realmRef = "anotherTwo")
ManagedUser user5;
@InjectUser(ref = "anotherTwo", realmRef = "anotherTwo")
ManagedUser user6;
@Test @Test
public void testMultipleInstances() { public void testMultipleInstances() {
Assertions.assertEquals("default", realm1.getName()); Assertions.assertEquals("default", realmDef1.getName());
Assertions.assertEquals("default", realm2.getName()); Assertions.assertEquals("default", realmDef2.getName());
Assertions.assertEquals(realm1, realm2); Assertions.assertSame(realmDef1, realmDef2);
Assertions.assertEquals("another", realm3.getName()); Assertions.assertEquals("A", realmA.getName());
}
Assertions.assertEquals("client1", client.getClientId()); @Test
Assertions.assertEquals("default", client2.getClientId()); public void testRealmRef() {
Assertions.assertFalse(realmDef1.admin().clients().findByClientId("client1").isEmpty());
Assertions.assertEquals("client1", client.getClientId()); Assertions.assertEquals(1, realmDef1.admin().users().count());
Assertions.assertEquals("default", client2.getClientId()); Assertions.assertEquals(2, realmA.admin().users().count());
Assertions.assertEquals("default", realm1.admin().toRepresentation().user(user1.getUsername()).getUsername()); Assertions.assertNotNull(adminClient.realm("B"));
Assertions.assertEquals("default", realm3.admin().toRepresentation().user(user2.getUsername()).getUsername()); Assertions.assertNotNull(adminClient.realm("B").users().get("user4"));
Assertions.assertEquals("another", realm3.admin().toRepresentation().user(user3.getUsername()).getUsername());
Assertions.assertNotNull(user4);
Assertions.assertNotNull(user5);
Assertions.assertNotNull(user6);
} }

View file

@ -14,6 +14,7 @@ public class InstanceContext<T, A extends Annotation> {
private final Set<InstanceContext<T, A>> dependencies = new HashSet<>(); private final Set<InstanceContext<T, A>> dependencies = new HashSet<>();
private T value; private T value;
private Class<? extends T> requestedValueType; private Class<? extends T> requestedValueType;
private final Class<?> config;
private LifeCycle lifeCycle; private LifeCycle lifeCycle;
private final String ref; private final String ref;
private final String realmRef; private final String realmRef;
@ -24,19 +25,21 @@ public class InstanceContext<T, A extends Annotation> {
this.supplier = supplier; this.supplier = supplier;
this.annotation = annotation; this.annotation = annotation;
this.requestedValueType = requestedValueType; this.requestedValueType = requestedValueType;
this.config = supplier.getConfig(annotation);
this.lifeCycle = supplier.getLifeCycle(annotation); this.lifeCycle = supplier.getLifeCycle(annotation);
this.ref = supplier.getRef(annotation); this.ref = supplier.getRef(annotation);
this.realmRef = supplier.getRealmRef(annotation); this.realmRef = supplier.getRealmRef(annotation);
} }
public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation, Class<? extends T> requestedValueType, String ref) { public InstanceContext(Registry registry, Supplier<T, A> supplier, Class<? extends T> requestedValueType, String ref, Class<?> config) {
this.registry = registry; this.registry = registry;
this.supplier = supplier; this.supplier = supplier;
this.annotation = annotation; this.annotation = null;
this.requestedValueType = requestedValueType; this.requestedValueType = requestedValueType;
this.lifeCycle = supplier.getLifeCycle(annotation); this.config = config;
this.lifeCycle = supplier.getDefaultLifecycle();
this.ref = ref; this.ref = ref;
this.realmRef = supplier.getRealmRef(annotation); this.realmRef = "";
} }
public <D> D getDependency(Class<D> typeClazz) { public <D> D getDependency(Class<D> typeClazz) {
@ -63,6 +66,10 @@ public class InstanceContext<T, A extends Annotation> {
return requestedValueType; return requestedValueType;
} }
public Class<?> getConfig() {
return config;
}
public LifeCycle getLifeCycle() { public LifeCycle getLifeCycle() {
return lifeCycle; return lifeCycle;
} }

View file

@ -4,6 +4,8 @@ import org.jboss.logging.Logger;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.config.Config; import org.keycloak.test.framework.config.Config;
import org.keycloak.test.framework.realm.DefaultRealmConfig;
import org.keycloak.test.framework.realm.ManagedRealm;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -112,8 +114,8 @@ 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(!dependent.getRealmRef().equals("")) { if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
dependency = new InstanceContext(this, supplier, supplier.getAnnotationClass().getAnnotation(InjectRealm.class), typeClass, dependent.getRealmRef()); 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);
} }

View file

@ -13,6 +13,20 @@ public interface Supplier<T, S extends Annotation> {
T getValue(InstanceContext<T, S> instanceContext); T getValue(InstanceContext<T, S> instanceContext);
default Class<?> getConfig(S annotation) {
if (annotation != null) {
Optional<Method> config = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("config")).findFirst();
if (config.isPresent()) {
try {
return (Class<?>) config.get().invoke(annotation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return null;
}
default LifeCycle getLifeCycle(S annotation) { default LifeCycle getLifeCycle(S annotation) {
if (annotation != null) { if (annotation != null) {
Optional<Method> lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst(); Optional<Method> lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst();

View file

@ -29,7 +29,7 @@ public class RealmSupplier implements Supplier<ManagedRealm, InjectRealm> {
KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.class); KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.class);
Keycloak adminClient = instanceContext.getDependency(Keycloak.class); Keycloak adminClient = instanceContext.getDependency(Keycloak.class);
RealmConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); RealmConfig config = (RealmConfig) SupplierHelpers.getInstance(instanceContext.getConfig());
RealmRepresentation realmRepresentation = config.getRepresentation(); RealmRepresentation realmRepresentation = config.getRepresentation();
if (realmRepresentation.getRealm() == null) { if (realmRepresentation.getRealm() == null) {