Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public class GlobalManagedResourcesTest {

@Test
public void testCreatedRealm() {
Assertions.assertEquals(DefaultRealmConfig.class.getSimpleName(), realmResource.toRepresentation().getRealm());
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
}

@Test
public void testCreatedClient() {
Assertions.assertEquals(DefaultClientConfig.class.getSimpleName(), clientResource.toRepresentation().getClientId());
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());
}

@Test
public void testCreatedUser() {
Assertions.assertEquals(DefaultUserConfig.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername());
Assertions.assertEquals("default", userResource.toRepresentation().getUsername());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class ManagedResources2Test {

@Test
public void testCreatedRealm() {
Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), realmResource.toRepresentation().getRealm());
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
}

@Test
public void testCreatedClient() {
Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), clientResource.toRepresentation().getClientId());
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());

List<ClientRepresentation> clients = realmResource.clients().findByClientId(ManagedResources2Test.class.getSimpleName());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public class ManagedResourcesTest {

@Test
public void testCreatedRealm() {
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), realmResource.toRepresentation().getRealm());
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
}

@Test
public void testCreatedClient() {
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), clientResource.toRepresentation().getClientId());
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());

List<ClientRepresentation> clients = realmResource.clients().findByClientId(ManagedResourcesTest.class.getSimpleName());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
}

@Test
public void testCreatedUser() {
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername());
Assertions.assertEquals("default", userResource.toRepresentation().getUsername());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.keycloak.test.examples;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.TestClient;
import org.keycloak.test.framework.annotations.TestRealm;
import org.keycloak.test.framework.realm.RealmConfig;

@KeycloakIntegrationTest
public class MultipleInstancesTest {

@TestRealm
RealmResource realm1;

@TestRealm
RealmResource realm2;

@TestRealm(ref = "another", config = CustomRealmConfig.class)
RealmResource realm3;

@TestClient(ref = "client1")
ClientResource client;

@TestClient
ClientResource client2;

@Test
public void testMultipleInstances() {
Assertions.assertEquals("default", realm1.toRepresentation().getRealm());
Assertions.assertEquals("default", realm2.toRepresentation().getRealm());
Assertions.assertEquals(realm1, realm2);

Assertions.assertEquals("another", realm3.toRepresentation().getRealm());

Assertions.assertEquals("client1", client.toRepresentation().getClientId());
Assertions.assertEquals("default", client2.toRepresentation().getClientId());
Comment on lines +34 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to make assertions about the internals of the framework, perhaps this should be part of the framework module?

}


public static class CustomRealmConfig implements RealmConfig {
@Override
public RealmRepresentation getRepresentation() {
return new RealmRepresentation();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

Class<? extends ClientConfig> config() default DefaultClientConfig.class;

String ref() default "default";

LifeCycle lifecycle() default LifeCycle.CLASS;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

LifeCycle lifecycle() default LifeCycle.CLASS;

String ref() default "default";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

LifeCycle lifecycle() default LifeCycle.CLASS;

String ref() default "default";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class InstanceContext<T, A extends Annotation> {
private T value;
private Class<? extends T> requestedValueType;
private LifeCycle lifeCycle;
private final String ref;
private final Map<String, Object> notes = new HashMap<>();

public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation, Class<? extends T> requestedValueType) {
Expand All @@ -23,6 +24,7 @@ public InstanceContext(Registry registry, Supplier<T, A> supplier, A annotation,
this.annotation = annotation;
this.requestedValueType = requestedValueType;
this.lifeCycle = supplier.getLifeCycle(annotation);
this.ref = supplier.getRef(annotation);
}

public <D> D getDependency(Class<D> typeClazz) {
Expand Down Expand Up @@ -53,6 +55,10 @@ public LifeCycle getLifeCycle() {
return lifeCycle;
}

public String getRef() {
return ref;
}

public A getAnnotation() {
return annotation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings({"rawtypes", "unchecked"})
public class Registry {
Expand Down Expand Up @@ -146,13 +147,15 @@ private void deployRequestedInstances() {
while (!requestedInstances.isEmpty()) {
RequestedInstance requestedInstance = requestedInstances.remove(0);

InstanceContext instance = new InstanceContext(this, requestedInstance.getSupplier(), requestedInstance.getAnnotation(), requestedInstance.getValueType());
instance.setValue(requestedInstance.getSupplier().getValue(instance));
deployedInstances.add(instance);
if (getDeployedInstance(requestedInstance) == null) {
InstanceContext instance = new InstanceContext(this, requestedInstance.getSupplier(), requestedInstance.getAnnotation(), requestedInstance.getValueType());
instance.setValue(requestedInstance.getSupplier().getValue(instance));
deployedInstances.add(instance);

if (LOGGER.isTraceEnabled()) {
LOGGER.tracev("Created instance: {0}",
requestedInstance.getSupplier().getClass().getSimpleName());
if (LOGGER.isTraceEnabled()) {
LOGGER.tracev("Created instance: {0}",
requestedInstance.getSupplier().getClass().getSimpleName());
}
}
}
}
Expand Down Expand Up @@ -205,8 +208,10 @@ public void close() {
private InstanceContext<?, ?> getDeployedInstance(Class<?> valueType, Annotation[] annotations) {
for (Annotation a : annotations) {
for (InstanceContext<?, ?> i : deployedInstances) {
Supplier<?, ?> supplier = i.getSupplier();
if (supplier.getAnnotationClass().equals(a.annotationType()) && valueType.isAssignableFrom(i.getValue().getClass())) {
Supplier supplier = i.getSupplier();
if (supplier.getAnnotationClass().equals(a.annotationType())
&& valueType.isAssignableFrom(i.getValue().getClass())
&& supplier.getRef(a).equals(i.getRef()) ) {
return i;
}
}
Expand All @@ -229,8 +234,13 @@ private void destroy(InstanceContext instanceContext) {
}

private InstanceContext getDeployedInstance(RequestedInstance requestedInstance) {
String requestedRef = requestedInstance.getRef();
Class requestedValueType = requestedInstance.getValueType();
for (InstanceContext<?, ?> i : deployedInstances) {
if(!i.getRef().equals(requestedRef)) {
continue;
}

if (requestedValueType != null) {
if (requestedValueType.isAssignableFrom(i.getValue().getClass())) {
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public class RequestedInstance<T, A extends Annotation> {
private final A annotation;
private final Class<? extends T> valueType;
private final LifeCycle lifeCycle;
private final String ref;

public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
this.supplier = supplier;
this.annotation = annotation;
this.valueType = valueType;
this.lifeCycle = supplier.getLifeCycle(annotation);
this.ref = supplier.getRef(annotation);
}

public Supplier<T, A> getSupplier() {
Expand All @@ -31,4 +33,8 @@ public Class<? extends T> getValueType() {
public LifeCycle getLifeCycle() {
return lifeCycle;
}

public String getRef() {
return ref;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ default LifeCycle getLifeCycle(S annotation) {
return getDefaultLifecycle();
}

default String getRef(S annotation) {
if (annotation != null) {
Optional<Method> ref = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("ref")).findFirst();
if (ref.isPresent()) {
try {
return (String) ref.get().invoke(annotation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return "";
}

default LifeCycle getDefaultLifecycle() {
return LifeCycle.CLASS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ClientResource getValue(InstanceContext<ClientResource, TestClient> insta
ClientRepresentation clientRepresentation = config.getRepresentation();

if (clientRepresentation.getClientId() == null) {
String clientId = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName();
String clientId = instanceContext.getRef();
clientRepresentation.setClientId(clientId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RealmResource getValue(InstanceContext<RealmResource, TestRealm> instance
RealmRepresentation realmRepresentation = config.getRepresentation();

if (realmRepresentation.getRealm() == null) {
String realmName = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName();
String realmName = instanceContext.getRef();
realmRepresentation.setRealm(realmName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public UserResource getValue(InstanceContext<UserResource, TestUser> instanceCon
UserRepresentation userRepresentation = config.getRepresentation();

if (userRepresentation.getUsername() == null) {
String username = instanceContext.getLifeCycle().equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : instanceContext.getRegistry().getCurrentContext().getRequiredTestClass().getSimpleName();
String username = instanceContext.getRef();
userRepresentation.setUsername(username);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ public void testDependencyRequestedAfter() {
assertRunning(test.child, test.child.getParent());
}

@Test
public void testMultiplRef() {
MultipleRefTest refTest = new MultipleRefTest();
registry.beforeEach(refTest);

MockParentValue def1 = refTest.def;
MockParentValue a1 = refTest.a;

Assertions.assertNotSame(refTest.def, refTest.a);
Assertions.assertNotSame(refTest.def, refTest.b);
Assertions.assertNotSame(refTest.a, refTest.b);

assertRunning(refTest.def, refTest.a, refTest.b);

registry.afterEach();

registry.beforeEach(refTest);
assertRunning(refTest.def, refTest.a, refTest.b);

Assertions.assertSame(def1, refTest.def);
Assertions.assertSame(a1, refTest.a);

registry.afterEach();
assertRunning(refTest.def, refTest.a, refTest.b);

registry.afterAll();
assertClosed(refTest.def, refTest.a, refTest.b);
}

public static void assertRunning(Object... values) {
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values));
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length));
Expand Down Expand Up @@ -243,4 +272,15 @@ public static final class ChildAndParentTest {
@MockParentAnnotation
MockParentValue parent;
}

public static final class MultipleRefTest {
@MockParentAnnotation()
MockParentValue def;

@MockParentAnnotation(ref = "a")
MockParentValue a;

@MockParentAnnotation(ref = "b")
MockParentValue b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MockParentAnnotation {

String ref() default "";

}