Skip to content

Commit 50c0a27

Browse files
committed
Adding tests for KeycloakClient
Signed-off-by: Laurent Broudoux <[email protected]>
1 parent bd7debf commit 50c0a27

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>
1313
<maven.compiler.target>17</maven.compiler.target>
14+
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version>
1415
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1516
<jackson.version>2.17.1</jackson.version>
1617
<jakarta-annotation.version>3.0.0</jakarta-annotation.version>
1718
<jackson-databind-nullable.version>0.2.6</jackson-databind-nullable.version>
1819
<apache-httpcore.version>4.4.16</apache-httpcore.version>
1920
<apache-httpmime.version>4.5.14</apache-httpmime.version>
2021
<microcks-testcontainers.version>0.2.7</microcks-testcontainers.version>
22+
<keycloak-testcontainers.version>3.0.0</keycloak-testcontainers.version>
2123
<junit.version>5.10.2</junit.version>
2224
<testcontainers.version>1.20.1</testcontainers.version>
2325
</properties>
@@ -68,16 +70,38 @@
6870
<version>${junit.version}</version>
6971
<scope>test</scope>
7072
</dependency>
73+
<dependency>
74+
<groupId>com.github.dasniko</groupId>
75+
<artifactId>testcontainers-keycloak</artifactId>
76+
<version>${keycloak-testcontainers.version}</version>
77+
<scope>test</scope>
78+
</dependency>
7179
<dependency>
7280
<groupId>org.testcontainers</groupId>
7381
<artifactId>junit-jupiter</artifactId>
7482
<version>${testcontainers.version}</version>
7583
<scope>test</scope>
7684
</dependency>
85+
<dependency>
86+
<groupId>ch.qos.logback</groupId>
87+
<artifactId>logback-classic</artifactId>
88+
<version>1.5.6</version>
89+
<scope>test</scope>
90+
</dependency>
7791
</dependencies>
7892

7993
<build>
8094
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-surefire-plugin</artifactId>
98+
<version>${maven-surefire-plugin.version}</version>
99+
<configuration>
100+
<includes>
101+
<include>**/*Test.java</include>
102+
</includes>
103+
</configuration>
104+
</plugin>
81105
<plugin>
82106
<groupId>org.openapitools</groupId>
83107
<artifactId>openapi-generator-maven-plugin</artifactId>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.microcks.client;
17+
18+
import dasniko.testcontainers.keycloak.KeycloakContainer;
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.junit.jupiter.Container;
21+
import org.testcontainers.junit.jupiter.Testcontainers;
22+
23+
import java.io.IOException;
24+
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
27+
/**
28+
* This is a unit test for KeycloakClient class.
29+
* @author laurent
30+
*/
31+
@Testcontainers
32+
public class KeycloakClientTest {
33+
34+
@Container
35+
public static KeycloakContainer keycloak = new KeycloakContainer("quay.io/keycloak/keycloak:24.0.4")
36+
.withRealmImportFile("io/github/microcks/client/myrealm-realm.json");
37+
38+
@Test
39+
void testConnectAndGetToken() throws IOException, ApiException {
40+
// Retrieve token endpoint.
41+
String tokenEndpoint = keycloak.getAuthServerUrl() + "/realms/myrealm/protocol/openid-connect/token";
42+
43+
// Authenticate and get OAuth token.
44+
String oauthToken = KeycloakClient.connectAndGetOAuthToken("myrealm-serviceaccount",
45+
"ab54d329-e435-41ae-a900-ec6b3fe15c54",
46+
tokenEndpoint);
47+
48+
assertNotNull(oauthToken);
49+
}
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5+
</pattern>
6+
</encoder>
7+
</appender>
8+
<root level="ERROR">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
12+
<logger name="io.github.microcks" level="INFO"/>
13+
14+
<logger name="tc" level="INFO"/>
15+
<logger name="org.testcontainers" level="INFO"/>
16+
<logger name="com.github.dockerjava" level="WARN"/>
17+
</configuration>

0 commit comments

Comments
 (0)