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
@@ -0,0 +1,37 @@
package org.keycloak.test.base;

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.ClientRepresentation;
import org.keycloak.test.framework.KeycloakIntegrationTest;
import org.keycloak.test.framework.TestClient;
import org.keycloak.test.framework.TestRealm;
import org.keycloak.test.framework.injection.LifeCycle;

import java.util.List;

@KeycloakIntegrationTest
public class GlobalManagedResourcesTest {

@TestRealm(lifecycle = LifeCycle.GLOBAL)
RealmResource realmResource;

@TestClient
ClientResource clientResource;

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

@Test
public void testCreatedClient() {
Assertions.assertEquals("GlobalManagedResourcesTest", clientResource.toRepresentation().getClientId());

List<ClientRepresentation> clients = realmResource.clients().findByClientId("GlobalManagedResourcesTest");
Assertions.assertEquals(1, clients.size());
}

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

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.ClientRepresentation;
import org.keycloak.test.framework.KeycloakIntegrationTest;
import org.keycloak.test.framework.TestClient;
import org.keycloak.test.framework.TestRealm;
import org.keycloak.test.framework.injection.LifeCycle;

import java.util.List;

@KeycloakIntegrationTest
public class ManagedResources2Test {

@TestRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource;

@TestClient
ClientResource clientResource;

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import org.keycloak.test.framework.KeycloakIntegrationTest;
import org.keycloak.test.framework.TestClient;
import org.keycloak.test.framework.TestRealm;
import org.keycloak.test.framework.injection.LifeCycle;

import java.util.List;

@KeycloakIntegrationTest
public class ManagedResourcesTest {

@TestRealm
@TestRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource;

@TestClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package org.keycloak.test.framework;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.keycloak.test.framework.injection.Registry;

public class KeycloakIntegrationTestExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback {
public class KeycloakIntegrationTestExtension implements BeforeEachCallback, AfterEachCallback, AfterAllCallback {

@Override
public void beforeAll(ExtensionContext context) {
public void beforeEach(ExtensionContext context) {
if (isExtensionEnabled(context)) {
getRegistry(context).beforeAll(context.getRequiredTestClass());
getRegistry(context).beforeEach(context.getRequiredTestInstance());
}
}

@Override
public void beforeEach(ExtensionContext context) {
public void afterEach(ExtensionContext context) throws Exception {
if (isExtensionEnabled(context)) {
getRegistry(context).beforeEach(context.getRequiredTestInstance());
getRegistry(context).afterEach();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.keycloak.test.framework;

import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ClientConfig;
import org.keycloak.test.framework.realm.DefaultClientConfig;

Expand All @@ -14,4 +15,6 @@

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

LifeCycle lifecycle() default LifeCycle.CLASS;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.keycloak.test.framework;

import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.DefaultRealmConfig;
import org.keycloak.test.framework.realm.RealmConfig;

Expand All @@ -14,4 +15,6 @@

Class<? extends RealmConfig> config() default DefaultRealmConfig.class;

LifeCycle lifecycle() default LifeCycle.CLASS;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@ public InstanceWrapper<Keycloak, TestAdminClient> getValue(Registry registry, Te
KeycloakTestServer testServer = registry.getDependency(KeycloakTestServer.class, wrapper);

Keycloak keycloak = Keycloak.getInstance(testServer.getBaseUrl(), "master", "admin", "admin", "admin-cli");
wrapper.setValue(keycloak);
wrapper.setValue(keycloak, LifeCycle.GLOBAL);

return wrapper;
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.GLOBAL;
}

@Override
public boolean compatible(InstanceWrapper<Keycloak, TestAdminClient> a, InstanceWrapper<Keycloak, TestAdminClient> b) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ public class InstanceWrapper<T, A extends Annotation> {
private final A annotation;
private final Set<InstanceWrapper<T, A>> dependencies = new HashSet<>();
private T value;
private LifeCycle lifeCycle;
private final Map<String, Object> notes = new HashMap<>();

public InstanceWrapper(Supplier<T, A> supplier, A annotation) {
this.supplier = supplier;
this.annotation = annotation;
}

public InstanceWrapper(Supplier<T, A> supplier, A annotation, T value) {
public InstanceWrapper(Supplier<T, A> supplier, A annotation, T value, LifeCycle lifeCycle) {
this.supplier = supplier;
this.annotation = annotation;
this.value = value;
this.lifeCycle = lifeCycle;
}

public void setValue(T value) {
public void setValue(T value, LifeCycle lifeCycle) {
this.value = value;
this.lifeCycle = lifeCycle;
}

public Supplier<T, A> getSupplier() {
Expand All @@ -37,6 +40,10 @@ public T getValue() {
return value;
}

public LifeCycle getLifeCycle() {
return lifeCycle;
}

public A getAnnotation() {
return annotation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum LifeCycle {

GLOBAL,
CLASS
CLASS,
METHOD

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Registry {

public Registry() {
loadSuppliers();
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
}

public ExtensionContext getCurrentContext() {
Expand Down Expand Up @@ -82,7 +83,8 @@ public <T> T getDependency(Class<T> typeClass, InstanceWrapper dependent) {
throw new RuntimeException("Dependency not found: " + typeClass);
}

public void beforeAll(Class testClass) {
public void beforeEach(Object testInstance) {
Class testClass = testInstance.getClass();
InstanceWrapper requestedServerInstance = createInstanceWrapper(testClass.getAnnotations());
requestedInstances.add(requestedServerInstance);

Expand Down Expand Up @@ -137,9 +139,6 @@ public void beforeAll(Class testClass) {
itr.remove();
}

}

public void beforeEach(Object testInstance) {
for (Field f : testInstance.getClass().getDeclaredFields()) {
InstanceWrapper<?, ?> instance = getDeployedInstance(f.getAnnotations());
try {
Expand All @@ -152,7 +151,20 @@ public void beforeEach(Object testInstance) {
}

public void afterAll() {
List<InstanceWrapper<?, ?>> destroy = deployedInstances.stream().filter(i -> i.getSupplier().getLifeCycle().equals(LifeCycle.CLASS)).toList();
LOGGER.trace("Closing instances with class lifecycle");
List<InstanceWrapper<?, ?>> destroy = deployedInstances.stream().filter(i -> i.getLifeCycle().equals(LifeCycle.CLASS)).toList();
destroy.forEach(this::destroy);
}

public void afterEach() {
LOGGER.trace("Closing instances with method lifecycle");
List<InstanceWrapper<?, ?>> destroy = deployedInstances.stream().filter(i -> i.getLifeCycle().equals(LifeCycle.METHOD)).toList();
destroy.forEach(this::destroy);
}

public void onShutdown() {
LOGGER.trace("Closing instances with global lifecycle");
List<InstanceWrapper<?, ?>> destroy = deployedInstances.stream().filter(i -> i.getLifeCycle().equals(LifeCycle.GLOBAL)).toList();
destroy.forEach(this::destroy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public interface Supplier<T, S extends Annotation> {

InstanceWrapper<T, S> getValue(Registry registry, S annotation);

LifeCycle getLifeCycle();

boolean compatible(InstanceWrapper<T, S> a, InstanceWrapper<T, S> b);

default void close(T instance) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public Class<ClientResource> getValueType() {
@Override
public InstanceWrapper<ClientResource, TestClient> getValue(Registry registry, TestClient annotation) {
InstanceWrapper<ClientResource, TestClient> wrapper = new InstanceWrapper<>(this, annotation);
LifeCycle lifecycle = annotation.lifecycle();

RealmResource realm = registry.getDependency(RealmResource.class, wrapper);

ClientConfig config = SupplierHelpers.getInstance(annotation.config());
ClientRepresentation clientRepresentation = config.getRepresentation();

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

Response response = realm.clients().create(clientRepresentation);
Expand All @@ -48,16 +50,11 @@ public InstanceWrapper<ClientResource, TestClient> getValue(Registry registry, T
wrapper.addNote(CLIENT_UUID_KEY, clientId);

ClientResource clientResource = realm.clients().get(clientId);
wrapper.setValue(clientResource);
wrapper.setValue(clientResource, lifecycle);

return wrapper;
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.CLASS;
}

@Override
public boolean compatible(InstanceWrapper<ClientResource, TestClient> a, InstanceWrapper<ClientResource, TestClient> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ public Class<RealmResource> getValueType() {
@Override
public InstanceWrapper<RealmResource, TestRealm> getValue(Registry registry, TestRealm annotation) {
InstanceWrapper<RealmResource, TestRealm> wrapper = new InstanceWrapper<>(this, annotation);
LifeCycle lifecycle = annotation.lifecycle();

Keycloak adminClient = registry.getDependency(Keycloak.class, wrapper);

RealmConfig config = SupplierHelpers.getInstance(annotation.config());
RealmRepresentation realmRepresentation = config.getRepresentation();

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

String realmName = realmRepresentation.getRealm();
Expand All @@ -43,16 +45,11 @@ public InstanceWrapper<RealmResource, TestRealm> getValue(Registry registry, Tes
adminClient.realms().create(realmRepresentation);

RealmResource realmResource = adminClient.realm(realmRepresentation.getRealm());
wrapper.setValue(realmResource);
wrapper.setValue(realmResource, lifecycle);

return wrapper;
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.CLASS;
}

@Override
public boolean compatible(InstanceWrapper<RealmResource, TestRealm> a, InstanceWrapper<RealmResource, TestRealm> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> getValue(Reg

keycloakTestServer.start(serverConfig);

return new InstanceWrapper<>(this, annotation, keycloakTestServer);
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.GLOBAL;
return new InstanceWrapper<>(this, annotation, keycloakTestServer, LifeCycle.GLOBAL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ public Class<WebDriver> getValueType() {
@Override
public InstanceWrapper<WebDriver, TestWebDriver> getValue(Registry registry, TestWebDriver annotation) {
final var driver = new ChromeDriver();
return new InstanceWrapper<>(this, annotation, driver);
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.GLOBAL;
return new InstanceWrapper<>(this, annotation, driver, LifeCycle.GLOBAL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ public Class<WebDriver> getValueType() {
@Override
public InstanceWrapper<WebDriver, TestWebDriver> getValue(Registry registry, TestWebDriver annotation) {
final var driver = new FirefoxDriver();
return new InstanceWrapper<>(this, annotation, driver);
}

@Override
public LifeCycle getLifeCycle() {
return LifeCycle.GLOBAL;
return new InstanceWrapper<>(this, annotation, driver, LifeCycle.GLOBAL);
}

@Override
Expand Down