Skip to content

Commit

Permalink
update to 24 and switch (finally) to testcontainers. (#51)
Browse files Browse the repository at this point in the history
* update to 24 and switch (finally) to testcontainers.

* getting webhook port working properly

* stop npe when session.getContext() is not set. #52
  • Loading branch information
xgp authored Mar 21, 2024
1 parent c346668 commit a604ebd
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 220 deletions.
101 changes: 62 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.java.package>io.phasetwo.keycloak.events</main.java.package>
<junit.version>4.13.2</junit.version>
<keycloak.version>23.0.4</keycloak.version>
<junit.version>5.8.2</junit.version>
<keycloak.version>24.0.0</keycloak.version>
<lombok.version>1.18.30</lombok.version>
<auto-service.version>1.1.1</auto-service.version>
<ossrh.url>https://s01.oss.sonatype.org</ossrh.url>
<maven.test.skip>true</maven.test.skip>
</properties>

<scm>
Expand Down Expand Up @@ -114,6 +113,16 @@
<versionCommit>${buildNumber}</versionCommit>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<systemPropertyVariables>
<keycloak-version>${keycloak.version}</keycloak-version>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -203,11 +212,59 @@

<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.dasniko</groupId>
<artifactId>testcontainers-keycloak</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
<version>3.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
<scope>test</scope>
</dependency>
<dependency> <!-- so we can test webhooks for org methods -->
<groupId>io.phasetwo.keycloak</groupId>
<artifactId>keycloak-orgs</artifactId>
<version>[0.61,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
Expand All @@ -216,38 +273,4 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>test</id>
<properties>
<maven.test.skip>false</maven.test.skip>
</properties>
<dependencies>
<dependency> <!-- if you're getting a missing dependency error here, you need to install keycloak from source locally, as this never hits maven central -->
<groupId>org.keycloak</groupId>
<artifactId>keycloak-testsuite-utils</artifactId>
<version>${keycloak.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- so we can test webhooks for org methods -->
<groupId>io.phasetwo.keycloak</groupId>
<artifactId>keycloak-orgs</artifactId>
<version>[0.45,)</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.phasetwo.keycloak.config;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
Expand All @@ -13,8 +14,9 @@ public interface ConfigurationAware {
String getId();

default List<Map<String, Object>> getConfigurations(KeycloakSession session) {
return RealmAttributesConfigLoader.loadConfigurations(
session, getRealm(session).getName(), getId())
RealmModel realm = getRealm(session);
if (realm == null) return ImmutableList.of();
return RealmAttributesConfigLoader.loadConfigurations(session, realm.getName(), getId())
.stream()
.map(config -> RealmAttributesConfigLoader.safeConvertToMap(config))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.keycloak.http.HttpRequest;
import org.keycloak.http.HttpResponse;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resources.Cors;
import org.keycloak.services.cors.Cors;
import org.keycloak.services.resources.admin.AdminAuth;

@JBossLog
Expand Down
119 changes: 0 additions & 119 deletions src/test/java/io/phasetwo/keycloak/KeycloakSuite.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.phasetwo.keycloak.resources;

import dasniko.testcontainers.keycloak.KeycloakContainer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.jupiter.api.BeforeAll;
import org.keycloak.admin.client.Keycloak;
import org.testcontainers.Testcontainers;

public abstract class AbstractResourceTest {

public static final String KEYCLOAK_IMAGE =
String.format(
"quay.io/phasetwo/keycloak-crdb:%s", System.getProperty("keycloak-version", "23.0.0"));
public static final String REALM = "master";
public static final String ADMIN_CLI = "admin-cli";

static final String[] deps = {
"org.keycloak:keycloak-admin-client",
"io.phasetwo.keycloak:keycloak-orgs",
"com.github.xgp:kitchen-sink",
"org.openjdk.nashorn:nashorn-core"
};

static List<File> getDeps() {
List<File> dependencies = new ArrayList<File>();
for (String dep : deps) {
dependencies.addAll(getDep(dep));
}
return dependencies;
}

static List<File> getDep(String pkg) {
return Maven.resolver()
.loadPomFromFile("./pom.xml")
.resolve(pkg)
.withoutTransitivity()
.asList(File.class);
}

public static Keycloak keycloak;

public static final KeycloakContainer container =
new KeycloakContainer(KEYCLOAK_IMAGE)
.withContextPath("/auth")
.withReuse(true)
.withProviderClassesFrom("target/classes")
.withProviderLibsFrom(getDeps())
.withAccessToHost(true);

protected static final int WEBHOOK_SERVER_PORT = 8083;

static {
container.start();
}

@BeforeAll
public static void beforeAll() {
Testcontainers.exposeHostPorts(WEBHOOK_SERVER_PORT);
keycloak =
getKeycloak(REALM, ADMIN_CLI, container.getAdminUsername(), container.getAdminPassword());
}

public static Keycloak getKeycloak(String realm, String clientId, String user, String pass) {
return Keycloak.getInstance(getAuthUrl(), realm, user, pass, clientId);
}

public static String getAuthUrl() {
return container.getAuthServerUrl();
}
}
Loading

0 comments on commit a604ebd

Please sign in to comment.