Testsuite PoC - RealmRef for Client and User
Signed-off-by: Lukas Hanusovsky <lhanusov@redhat.com>
This commit is contained in:
parent
704383fc65
commit
11595e2349
8 changed files with 138 additions and 8 deletions
|
@ -53,6 +53,12 @@
|
||||||
<groupId>com.microsoft.sqlserver</groupId>
|
<groupId>com.microsoft.sqlserver</groupId>
|
||||||
<artifactId>mssql-jdbc</artifactId>
|
<artifactId>mssql-jdbc</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Temporary dependency until we figure out how we want to support OAuth -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.nimbusds</groupId>
|
||||||
|
<artifactId>oauth2-oidc-sdk</artifactId>
|
||||||
|
<version>11.13</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -5,9 +5,11 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.keycloak.representations.idm.RealmRepresentation;
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
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.KeycloakIntegrationTest;
|
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
|
||||||
import org.keycloak.test.framework.realm.ManagedClient;
|
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.RealmConfig;
|
import org.keycloak.test.framework.realm.RealmConfig;
|
||||||
|
|
||||||
@KeycloakIntegrationTest
|
@KeycloakIntegrationTest
|
||||||
|
@ -22,12 +24,33 @@ public class MultipleInstancesTest {
|
||||||
@InjectRealm(ref = "another", config = CustomRealmConfig.class)
|
@InjectRealm(ref = "another", config = CustomRealmConfig.class)
|
||||||
ManagedRealm realm3;
|
ManagedRealm realm3;
|
||||||
|
|
||||||
|
@InjectRealm(ref = "anotherOne")
|
||||||
|
ManagedRealm realm4;
|
||||||
|
|
||||||
@InjectClient(ref = "client1")
|
@InjectClient(ref = "client1")
|
||||||
ManagedClient client;
|
ManagedClient client;
|
||||||
|
|
||||||
@InjectClient
|
@InjectClient
|
||||||
ManagedClient client2;
|
ManagedClient client2;
|
||||||
|
|
||||||
|
@InjectUser(realmRef = "default")
|
||||||
|
ManagedUser user1;
|
||||||
|
|
||||||
|
@InjectUser(realmRef = "another")
|
||||||
|
ManagedUser user2;
|
||||||
|
|
||||||
|
@InjectUser(ref = "another", realmRef = "another")
|
||||||
|
ManagedUser user3;
|
||||||
|
|
||||||
|
@InjectUser(ref = "anotherOne", realmRef = "anotherOne")
|
||||||
|
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", realm1.getName());
|
||||||
|
@ -38,6 +61,17 @@ public class MultipleInstancesTest {
|
||||||
|
|
||||||
Assertions.assertEquals("client1", client.getClientId());
|
Assertions.assertEquals("client1", client.getClientId());
|
||||||
Assertions.assertEquals("default", client2.getClientId());
|
Assertions.assertEquals("default", client2.getClientId());
|
||||||
|
|
||||||
|
Assertions.assertEquals("client1", client.getClientId());
|
||||||
|
Assertions.assertEquals("default", client2.getClientId());
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,9 @@ public @interface InjectClient {
|
||||||
|
|
||||||
Class<? extends ClientConfig> config() default DefaultClientConfig.class;
|
Class<? extends ClientConfig> config() default DefaultClientConfig.class;
|
||||||
|
|
||||||
String ref() default "default";
|
|
||||||
|
|
||||||
LifeCycle lifecycle() default LifeCycle.CLASS;
|
LifeCycle lifecycle() default LifeCycle.CLASS;
|
||||||
|
|
||||||
|
String ref() default "default";
|
||||||
|
|
||||||
|
String realmRef() default "default";
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,4 +18,6 @@ public @interface InjectUser {
|
||||||
LifeCycle lifecycle() default LifeCycle.CLASS;
|
LifeCycle lifecycle() default LifeCycle.CLASS;
|
||||||
|
|
||||||
String ref() default "default";
|
String ref() default "default";
|
||||||
|
|
||||||
|
String realmRef() default "default";
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ public class InstanceContext<T, A extends Annotation> {
|
||||||
private Class<? extends T> requestedValueType;
|
private Class<? extends T> requestedValueType;
|
||||||
private LifeCycle lifeCycle;
|
private LifeCycle lifeCycle;
|
||||||
private final String ref;
|
private final String ref;
|
||||||
|
private final String realmRef;
|
||||||
private final Map<String, Object> notes = new HashMap<>();
|
private final Map<String, Object> notes = new HashMap<>();
|
||||||
|
|
||||||
public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation, Class<? extends T> requestedValueType) {
|
public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation, Class<? extends T> requestedValueType) {
|
||||||
|
@ -25,6 +26,17 @@ public class InstanceContext<T, A extends Annotation> {
|
||||||
this.requestedValueType = requestedValueType;
|
this.requestedValueType = requestedValueType;
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation, Class<? extends T> requestedValueType, String ref) {
|
||||||
|
this.registry = registry;
|
||||||
|
this.supplier = supplier;
|
||||||
|
this.annotation = annotation;
|
||||||
|
this.requestedValueType = requestedValueType;
|
||||||
|
this.lifeCycle = supplier.getLifeCycle(annotation);
|
||||||
|
this.ref = ref;
|
||||||
|
this.realmRef = supplier.getRealmRef(annotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <D> D getDependency(Class<D> typeClazz) {
|
public <D> D getDependency(Class<D> typeClazz) {
|
||||||
|
@ -59,6 +71,10 @@ public class InstanceContext<T, A extends Annotation> {
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRealmRef() {
|
||||||
|
return realmRef;
|
||||||
|
}
|
||||||
|
|
||||||
public A getAnnotation() {
|
public A getAnnotation() {
|
||||||
return annotation;
|
return annotation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.keycloak.test.framework.injection;
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
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.config.Config;
|
import org.keycloak.test.framework.config.Config;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
@ -14,7 +15,6 @@ import java.util.Optional;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
public class Registry {
|
public class Registry {
|
||||||
|
@ -39,7 +39,33 @@ public class Registry {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getDependency(Class<T> typeClass, InstanceContext dependent) {
|
public <T> T getDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
InstanceContext dependency = getDeployedInstance(typeClass);
|
T dependency;
|
||||||
|
dependency = getDeployedDependency(typeClass, dependent);
|
||||||
|
if (dependency != null) {
|
||||||
|
return dependency;
|
||||||
|
} else {
|
||||||
|
dependency = getRequestedDependency(typeClass, dependent);
|
||||||
|
if(dependency != null) {
|
||||||
|
return dependency;
|
||||||
|
} else {
|
||||||
|
dependency = getUnConfiguredDependency(typeClass, dependent);
|
||||||
|
if(dependency != null) {
|
||||||
|
return dependency;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("Dependency not found: " + typeClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
|
InstanceContext dependency;
|
||||||
|
if(!dependent.getRealmRef().equals("")) {
|
||||||
|
dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
|
||||||
|
} else {
|
||||||
|
dependency = getDeployedInstance(typeClass);
|
||||||
|
}
|
||||||
|
|
||||||
if (dependency != null) {
|
if (dependency != null) {
|
||||||
dependency.registerDependency(dependent);
|
dependency.registerDependency(dependent);
|
||||||
|
|
||||||
|
@ -51,8 +77,17 @@ public class Registry {
|
||||||
|
|
||||||
return (T) dependency.getValue();
|
return (T) dependency.getValue();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
RequestedInstance requestedDependency = getRequestedInstance(typeClass);
|
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
|
InstanceContext dependency;
|
||||||
|
RequestedInstance requestedDependency;
|
||||||
|
if(!dependent.getRealmRef().equals("")) {
|
||||||
|
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
|
||||||
|
} else {
|
||||||
|
requestedDependency = getRequestedInstance(typeClass);
|
||||||
|
}
|
||||||
if (requestedDependency != null) {
|
if (requestedDependency != null) {
|
||||||
dependency = new InstanceContext<Object, Annotation>(this, requestedDependency.getSupplier(), requestedDependency.getAnnotation(), requestedDependency.getValueType());
|
dependency = new InstanceContext<Object, Annotation>(this, requestedDependency.getSupplier(), requestedDependency.getAnnotation(), requestedDependency.getValueType());
|
||||||
dependency.setValue(requestedDependency.getSupplier().getValue(dependency));
|
dependency.setValue(requestedDependency.getSupplier().getValue(dependency));
|
||||||
|
@ -69,11 +104,20 @@ public class Registry {
|
||||||
|
|
||||||
return (T) dependency.getValue();
|
return (T) dependency.getValue();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getUnConfiguredDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
|
InstanceContext dependency;
|
||||||
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("")) {
|
||||||
|
dependency = new InstanceContext(this, supplier, supplier.getAnnotationClass().getAnnotation(InjectRealm.class), typeClass, dependent.getRealmRef());
|
||||||
|
} else {
|
||||||
dependency = new InstanceContext(this, supplier, null, typeClass);
|
dependency = new InstanceContext(this, supplier, null, typeClass);
|
||||||
|
}
|
||||||
|
|
||||||
dependency.registerDependency(dependent);
|
dependency.registerDependency(dependent);
|
||||||
dependency.setValue(supplier.getValue(dependency));
|
dependency.setValue(supplier.getValue(dependency));
|
||||||
|
|
||||||
|
@ -87,8 +131,7 @@ public class Registry {
|
||||||
|
|
||||||
return (T) dependency.getValue();
|
return (T) dependency.getValue();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
throw new RuntimeException("Dependency not found: " + typeClass);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void beforeEach(Object testInstance) {
|
public void beforeEach(Object testInstance) {
|
||||||
|
@ -302,8 +345,16 @@ public class Registry {
|
||||||
return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null);
|
return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private InstanceContext getDeployedInstance(Class typeClass, String realmRef) {
|
||||||
|
return deployedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).filter(j -> j.getRef().equals(realmRef)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
private RequestedInstance getRequestedInstance(Class typeClass) {
|
private RequestedInstance getRequestedInstance(Class typeClass) {
|
||||||
return requestedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null);
|
return requestedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private RequestedInstance getRequestedInstance(Class typeClass, String realmRef) {
|
||||||
|
return requestedInstances.stream().filter(i -> i.getSupplier().getValueType().equals(typeClass)).filter(j -> j.getRef().equals(realmRef)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ public class RequestedInstance<T, A extends Annotation> {
|
||||||
private final Class<? extends T> valueType;
|
private final Class<? extends T> valueType;
|
||||||
private final LifeCycle lifeCycle;
|
private final LifeCycle lifeCycle;
|
||||||
private final String ref;
|
private final String ref;
|
||||||
|
private final String realmRef;
|
||||||
|
|
||||||
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
|
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
|
@ -16,6 +17,7 @@ public class RequestedInstance<T, A extends Annotation> {
|
||||||
this.valueType = valueType;
|
this.valueType = valueType;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Supplier<T, A> getSupplier() {
|
public Supplier<T, A> getSupplier() {
|
||||||
|
@ -37,4 +39,8 @@ public class RequestedInstance<T, A extends Annotation> {
|
||||||
public String getRef() {
|
public String getRef() {
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRealmRef() {
|
||||||
|
return realmRef;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,20 @@ public interface Supplier<T, S extends Annotation> {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default String getRealmRef(S annotation) {
|
||||||
|
if (annotation != null) {
|
||||||
|
Optional<Method> realmRef = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("realmRef")).findFirst();
|
||||||
|
if (realmRef.isPresent()) {
|
||||||
|
try {
|
||||||
|
return (String) realmRef.get().invoke(annotation);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
default LifeCycle getDefaultLifecycle() {
|
default LifeCycle getDefaultLifecycle() {
|
||||||
return LifeCycle.CLASS;
|
return LifeCycle.CLASS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue