Fix creating unconfigured realm dependency
Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
parent
11595e2349
commit
ef54d20be1
5 changed files with 56 additions and 43 deletions
|
@ -2,7 +2,9 @@ package org.keycloak.test.examples;
|
|||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
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;
|
||||
|
@ -15,63 +17,51 @@ import org.keycloak.test.framework.realm.RealmConfig;
|
|||
@KeycloakIntegrationTest
|
||||
public class MultipleInstancesTest {
|
||||
|
||||
@InjectRealm
|
||||
ManagedRealm realm1;
|
||||
@InjectAdminClient
|
||||
Keycloak adminClient;
|
||||
|
||||
@InjectRealm
|
||||
ManagedRealm realm2;
|
||||
ManagedRealm realmDef1;
|
||||
|
||||
@InjectRealm(ref = "another", config = CustomRealmConfig.class)
|
||||
ManagedRealm realm3;
|
||||
@InjectRealm
|
||||
ManagedRealm realmDef2;
|
||||
|
||||
@InjectRealm(ref = "anotherOne")
|
||||
ManagedRealm realm4;
|
||||
@InjectRealm(ref = "A", config = CustomRealmConfig.class)
|
||||
ManagedRealm realmA;
|
||||
|
||||
@InjectClient(ref = "client1")
|
||||
ManagedClient client;
|
||||
ManagedClient client1;
|
||||
|
||||
@InjectClient
|
||||
ManagedClient client2;
|
||||
|
||||
@InjectUser(realmRef = "default")
|
||||
@InjectUser(ref = "user1", realmRef = "default")
|
||||
ManagedUser user1;
|
||||
|
||||
@InjectUser(realmRef = "another")
|
||||
@InjectUser(ref = "user2", realmRef = "A")
|
||||
ManagedUser user2;
|
||||
|
||||
@InjectUser(ref = "another", realmRef = "another")
|
||||
@InjectUser(ref = "user3", realmRef = "A")
|
||||
ManagedUser user3;
|
||||
|
||||
@InjectUser(ref = "anotherOne", realmRef = "anotherOne")
|
||||
@InjectUser(ref = "user4", realmRef = "B")
|
||||
ManagedUser user4;
|
||||
|
||||
@InjectUser(realmRef = "anotherTwo")
|
||||
ManagedUser user5;
|
||||
|
||||
@InjectUser(ref = "anotherTwo", realmRef = "anotherTwo")
|
||||
ManagedUser user6;
|
||||
|
||||
@Test
|
||||
public void testMultipleInstances() {
|
||||
Assertions.assertEquals("default", realm1.getName());
|
||||
Assertions.assertEquals("default", realm2.getName());
|
||||
Assertions.assertEquals(realm1, realm2);
|
||||
Assertions.assertEquals("default", realmDef1.getName());
|
||||
Assertions.assertEquals("default", realmDef2.getName());
|
||||
Assertions.assertSame(realmDef1, realmDef2);
|
||||
|
||||
Assertions.assertEquals("another", realm3.getName());
|
||||
Assertions.assertEquals("A", realmA.getName());
|
||||
}
|
||||
|
||||
Assertions.assertEquals("client1", client.getClientId());
|
||||
Assertions.assertEquals("default", client2.getClientId());
|
||||
@Test
|
||||
public void testRealmRef() {
|
||||
Assertions.assertFalse(realmDef1.admin().clients().findByClientId("client1").isEmpty());
|
||||
|
||||
Assertions.assertEquals("client1", client.getClientId());
|
||||
Assertions.assertEquals("default", client2.getClientId());
|
||||
Assertions.assertEquals(1, realmDef1.admin().users().count());
|
||||
Assertions.assertEquals(2, realmA.admin().users().count());
|
||||
|
||||
Assertions.assertEquals("default", realm1.admin().toRepresentation().user(user1.getUsername()).getUsername());
|
||||
Assertions.assertEquals("default", realm3.admin().toRepresentation().user(user2.getUsername()).getUsername());
|
||||
Assertions.assertEquals("another", realm3.admin().toRepresentation().user(user3.getUsername()).getUsername());
|
||||
|
||||
Assertions.assertNotNull(user4);
|
||||
Assertions.assertNotNull(user5);
|
||||
Assertions.assertNotNull(user6);
|
||||
Assertions.assertNotNull(adminClient.realm("B"));
|
||||
Assertions.assertNotNull(adminClient.realm("B").users().get("user4"));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ public class InstanceContext<T, A extends Annotation> {
|
|||
private final Set<InstanceContext<T, A>> dependencies = new HashSet<>();
|
||||
private T value;
|
||||
private Class<? extends T> requestedValueType;
|
||||
private final Class<?> config;
|
||||
private LifeCycle lifeCycle;
|
||||
private final String ref;
|
||||
private final String realmRef;
|
||||
|
@ -24,19 +25,21 @@ public class InstanceContext<T, A extends Annotation> {
|
|||
this.supplier = supplier;
|
||||
this.annotation = annotation;
|
||||
this.requestedValueType = requestedValueType;
|
||||
this.config = supplier.getConfig(annotation);
|
||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
||||
this.ref = supplier.getRef(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.supplier = supplier;
|
||||
this.annotation = annotation;
|
||||
this.annotation = null;
|
||||
this.requestedValueType = requestedValueType;
|
||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
||||
this.config = config;
|
||||
this.lifeCycle = supplier.getDefaultLifecycle();
|
||||
this.ref = ref;
|
||||
this.realmRef = supplier.getRealmRef(annotation);
|
||||
this.realmRef = "";
|
||||
}
|
||||
|
||||
public <D> D getDependency(Class<D> typeClazz) {
|
||||
|
@ -63,6 +66,10 @@ public class InstanceContext<T, A extends Annotation> {
|
|||
return requestedValueType;
|
||||
}
|
||||
|
||||
public Class<?> getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public LifeCycle getLifeCycle() {
|
||||
return lifeCycle;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import org.jboss.logging.Logger;
|
|||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.keycloak.test.framework.annotations.InjectRealm;
|
||||
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.reflect.Field;
|
||||
|
@ -112,8 +114,8 @@ 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(!dependent.getRealmRef().equals("")) {
|
||||
dependency = new InstanceContext(this, supplier, supplier.getAnnotationClass().getAnnotation(InjectRealm.class), typeClass, dependent.getRealmRef());
|
||||
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
|
||||
dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class);
|
||||
} else {
|
||||
dependency = new InstanceContext(this, supplier, null, typeClass);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,20 @@ public interface Supplier<T, S extends Annotation> {
|
|||
|
||||
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) {
|
||||
if (annotation != null) {
|
||||
Optional<Method> lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst();
|
||||
|
|
|
@ -29,7 +29,7 @@ public class RealmSupplier implements Supplier<ManagedRealm, InjectRealm> {
|
|||
KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.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();
|
||||
|
||||
if (realmRepresentation.getRealm() == null) {
|
||||
|
|
Loading…
Reference in a new issue