Testsuite PoC - RealmRef for Client and User

Signed-off-by: Lukas Hanusovsky <lhanusov@redhat.com>
This commit is contained in:
Lukas Hanusovsky 2024-07-26 15:38:44 +02:00 committed by Pedro Igor
parent 704383fc65
commit 11595e2349
8 changed files with 138 additions and 8 deletions

View file

@ -53,6 +53,12 @@
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</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>
<build>

View file

@ -5,9 +5,11 @@ import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.RealmRepresentation;
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;
@KeycloakIntegrationTest
@ -22,12 +24,33 @@ public class MultipleInstancesTest {
@InjectRealm(ref = "another", config = CustomRealmConfig.class)
ManagedRealm realm3;
@InjectRealm(ref = "anotherOne")
ManagedRealm realm4;
@InjectClient(ref = "client1")
ManagedClient client;
@InjectClient
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
public void testMultipleInstances() {
Assertions.assertEquals("default", realm1.getName());
@ -38,6 +61,17 @@ public class MultipleInstancesTest {
Assertions.assertEquals("client1", client.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);
}

View file

@ -15,8 +15,9 @@ public @interface InjectClient {
Class<? extends ClientConfig> config() default DefaultClientConfig.class;
String ref() default "default";
LifeCycle lifecycle() default LifeCycle.CLASS;
String ref() default "default";
String realmRef() default "default";
}

View file

@ -18,4 +18,6 @@ public @interface InjectUser {
LifeCycle lifecycle() default LifeCycle.CLASS;
String ref() default "default";
String realmRef() default "default";
}

View file

@ -16,6 +16,7 @@ public class InstanceContext<T, A extends Annotation> {
private Class<? extends T> requestedValueType;
private LifeCycle lifeCycle;
private final String ref;
private final String realmRef;
private final Map<String, Object> notes = new HashMap<>();
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.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) {
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) {
@ -59,6 +71,10 @@ public class InstanceContext<T, A extends Annotation> {
return ref;
}
public String getRealmRef() {
return realmRef;
}
public A getAnnotation() {
return annotation;
}

View file

@ -2,6 +2,7 @@ package org.keycloak.test.framework.injection;
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 java.lang.annotation.Annotation;
@ -14,7 +15,6 @@ import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@SuppressWarnings({"rawtypes", "unchecked"})
public class Registry {
@ -39,7 +39,33 @@ public class Registry {
}
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) {
dependency.registerDependency(dependent);
@ -51,8 +77,17 @@ public class Registry {
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) {
dependency = new InstanceContext<Object, Annotation>(this, requestedDependency.getSupplier(), requestedDependency.getAnnotation(), requestedDependency.getValueType());
dependency.setValue(requestedDependency.getSupplier().getValue(dependency));
@ -69,11 +104,20 @@ public class Registry {
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();
if (supplied.isPresent()) {
Supplier<T, ?> supplier = (Supplier<T, ?>) supplied.get();
dependency = new InstanceContext(this, supplier, null, typeClass);
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.registerDependency(dependent);
dependency.setValue(supplier.getValue(dependency));
@ -87,8 +131,7 @@ public class Registry {
return (T) dependency.getValue();
}
throw new RuntimeException("Dependency not found: " + typeClass);
return null;
}
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);
}
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) {
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);
}
}

View file

@ -9,6 +9,7 @@ public class RequestedInstance<T, A extends Annotation> {
private final Class<? extends T> valueType;
private final LifeCycle lifeCycle;
private final String ref;
private final String realmRef;
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
this.supplier = supplier;
@ -16,6 +17,7 @@ public class RequestedInstance<T, A extends Annotation> {
this.valueType = valueType;
this.lifeCycle = supplier.getLifeCycle(annotation);
this.ref = supplier.getRef(annotation);
this.realmRef = supplier.getRealmRef(annotation);
}
public Supplier<T, A> getSupplier() {
@ -37,4 +39,8 @@ public class RequestedInstance<T, A extends Annotation> {
public String getRef() {
return ref;
}
public String getRealmRef() {
return realmRef;
}
}

View file

@ -41,6 +41,20 @@ public interface Supplier<T, S extends Annotation> {
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() {
return LifeCycle.CLASS;
}