Skip to content

Commit

Permalink
Aded Registry test for realmRef
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Vacek <simonvacky@email.cz>
  • Loading branch information
vaceksimon authored and pedroigor committed Aug 9, 2024
1 parent 5a6ac58 commit e8d2946
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,49 @@
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;
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
public class MultipleInstancesTest {

private final String REALM_A_REF = "realm";
private final String USER_A_REF = "user";

@InjectAdminClient
Keycloak adminClient;

@InjectRealm
ManagedRealm realmDef1;

@InjectRealm
ManagedRealm realmDef2;
ManagedRealm realmDef;

@InjectRealm(ref = "A", config = CustomRealmConfig.class)
@InjectRealm(ref = REALM_A_REF, config = CustomRealmConfig.class)
ManagedRealm realmA;

@InjectClient(ref = "client1")
ManagedClient client1;

@InjectUser(ref = "user1", realmRef = "default")
ManagedUser user1;
@InjectUser
ManagedUser userDef;

@InjectUser(ref = "user2", realmRef = "A")
ManagedUser user2;

@InjectUser(ref = "user3", realmRef = "A")
ManagedUser user3;

@InjectUser(ref = "user4", realmRef = "B")
ManagedUser user4;
@InjectUser(ref = USER_A_REF, realmRef = REALM_A_REF)
ManagedUser userA;

@Test
public void testMultipleInstances() {
Assertions.assertEquals("default", realmDef1.getName());
Assertions.assertEquals("default", realmDef2.getName());
Assertions.assertSame(realmDef1, realmDef2);

Assertions.assertEquals("A", realmA.getName());
Assertions.assertEquals("default", realmDef.getName());
Assertions.assertEquals(REALM_A_REF, realmA.getName());
}

@Test
public void testRealmRef() {
Assertions.assertFalse(realmDef1.admin().clients().findByClientId("client1").isEmpty());

Assertions.assertEquals(1, realmDef1.admin().users().count());
Assertions.assertEquals(2, realmA.admin().users().count());
var realmDefUsers = adminClient.realm("default").users().search("default");
var realmAUsers = adminClient.realm(REALM_A_REF).users().search(USER_A_REF);
Assertions.assertEquals(1, realmDefUsers.size());
Assertions.assertEquals(1, realmAUsers.size());

Assertions.assertNotNull(adminClient.realm("B"));
Assertions.assertNotNull(adminClient.realm("B").users().get("user4"));
Assertions.assertEquals("default", realmDefUsers.get(0).getUsername());
Assertions.assertEquals(USER_A_REF, realmAUsers.get(0).getUsername());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public <T> T getDependency(Class<T> typeClass, InstanceContext dependent) {

private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
InstanceContext dependency;
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
if(!dependent.getRealmRef().equals("")) {
dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
} else {
dependency = getDeployedInstance(typeClass);
Expand All @@ -84,7 +84,7 @@ private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependen
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
InstanceContext dependency;
RequestedInstance requestedDependency;
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
if(!dependent.getRealmRef().equals("")) {
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
} else {
requestedDependency = getRequestedInstance(typeClass);
Expand Down Expand Up @@ -113,7 +113,7 @@ private <T> T getUnConfiguredDependency(Class<T> typeClass, InstanceContext depe
Optional<Supplier<?, ?>> supplied = suppliers.stream().filter(s -> s.getValueType().equals(typeClass)).findFirst();
if (supplied.isPresent()) {
Supplier<T, ?> supplier = (Supplier<T, ?>) supplied.get();
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
if(!dependent.getRealmRef().equals("")) {
dependency = new InstanceContext(this, supplier, typeClass, dependent.getRealmRef(), DefaultRealmConfig.class);
} else {
dependency = new InstanceContext(this, supplier, null, typeClass);
Expand Down Expand Up @@ -207,6 +207,9 @@ private void deployRequestedInstances() {
private void injectFields(Object testInstance) {
for (Field f : testInstance.getClass().getDeclaredFields()) {
InstanceContext<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations());
if(instance == null) { // a test class might have fields not meant for injection
continue;
}
try {
f.setAccessible(true);
f.set(testInstance, instance.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ public void testMultiplRef() {
Assertions.assertNotSame(refTest.a, refTest.b);

assertRunning(refTest.def, refTest.a, refTest.b);
Assertions.assertSame(refTest.a, refTest.a2);

registry.afterEach();

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

Assertions.assertSame(def1, refTest.def);
Assertions.assertSame(a1, refTest.a);
Expand All @@ -237,6 +238,16 @@ public void testMultiplRef() {
assertClosed(refTest.def, refTest.a, refTest.b);
}

@Test
public void testRealmRef() {
RealmRefTest test = new RealmRefTest();
registry.beforeEach(test);

assertRunning(test.childABC, test.childABC.getParent(), test.child123, test.parent123);
Assertions.assertNotSame(test.childABC.getParent(), test.parent123);
Assertions.assertSame(test.child123.getParent(), test.parent123);
}

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 @@ -280,7 +291,21 @@ public static final class MultipleRefTest {
@MockParentAnnotation(ref = "a")
MockParentValue a;

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

@MockParentAnnotation(ref = "b")
MockParentValue b;
}

public static final class RealmRefTest {
@MockParentAnnotation(ref = "123")
MockParentValue parent123;

@MockChildAnnotation(ref = "123", realmRef = "123")
MockChildValue child123;

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

String ref() default "";
String realmRef() default "";
}

0 comments on commit e8d2946

Please sign in to comment.