Skip to content

Commit

Permalink
Removed packages, adapted dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Feb 28, 2025
1 parent 61838c6 commit 0b8fe2a
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 2,299 deletions.
4 changes: 2 additions & 2 deletions modules/authentication/portal-authentication-oauth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<artifactId>portal-authentication-api</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver3-junit5</artifactId>
<groupId>de.cuioss.test</groupId>
<artifactId>cui-test-mockwebserver-junit5</artifactId>
</dependency>
<dependency>
<groupId>de.cuioss.test</groupId>
Expand Down
4 changes: 2 additions & 2 deletions modules/authentication/portal-authentication-token/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<artifactId>portal-core-unit-testing</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver3-junit5</artifactId>
<groupId>de.cuioss.test</groupId>
<artifactId>cui-test-mockwebserver-junit5</artifactId>
</dependency>
<!-- Implementation of jakarta.json-api-->
<dependency>
Expand Down
200 changes: 101 additions & 99 deletions modules/micro-profile/portal-mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,101 +1,103 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.cuioss.portal.mirco-profile</groupId>
<artifactId>micro-profile</artifactId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<artifactId>portal-mp-rest-client</artifactId>
<properties>
<maven.jar.plugin.automatic.module.name>
de.cuioss.portal.restclient
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.cuioss.portal.mirco-profile</groupId>
<artifactId>micro-profile</artifactId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<artifactId>portal-mp-rest-client</artifactId>
<properties>
<maven.jar.plugin.automatic.module.name>
de.cuioss.portal.restclient
</maven.jar.plugin.automatic.module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.cuioss.portal.core</groupId>
<artifactId>portal-configuration</artifactId>
</dependency>
<dependency>
<!-- RestEasy depends on jcl. This is a way to satisfy this. -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<!-- TEST -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver3-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-tls</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy.microprofile</groupId>
<artifactId>microprofile-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.interceptor</groupId>
<artifactId>jboss-interceptors-api_1.2_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.3_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.cuioss.portal.core</groupId>
<artifactId>portal-configuration</artifactId>
</dependency>
<dependency>
<!-- RestEasy depends on jcl. This is a way to satisfy this. -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
<!-- TEST -->
<dependency>
<groupId>de.cuioss.test</groupId>
<artifactId>cui-test-mockwebserver-junit5</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-tls</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy.microprofile</groupId>
<artifactId>microprofile-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.interceptor</groupId>
<artifactId>jboss-interceptors-api_1.2_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.3_spec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
166 changes: 1 addition & 165 deletions modules/test/portal-core-unit-testing/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -178,168 +178,4 @@ class SomeTestClass {}
@Produces
@Mock
private SomeMockedService mockedOne;
----

=== Using MockWebServer

Integration with link:https://github.com/square/okhttp/tree/master/mockwebserver[MockWebServer] for HTTP testing.

Required dependency:

[source,xml]
----
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver3-junit5</artifactId>
</dependency>
----

Basic usage:

[source,java]
----
@EnableMockWebServer
class MockWebServerExtensionTest implements MockWebServerHolder {
@Setter
private MockWebServer mockWebServer;
@Test
void shouldHandleMockWebServer() {
assertNotNull(mockWebServer);
}
}
----

==== Dispatching Requests

Implement custom request handling by extending `MockWebServerHolder.getDispatcher()`:

[source,java]
----
@EnableMockWebServer
class ValueSetClientImplFhirTest implements MockWebServerHolder {
static final FileLoader CONFORMANCE =
CuiFileUtil.getLoaderForPath(FileTypePrefix.CLASSPATH + "/fhir/conformance.xml");
@Override
public Dispatcher getDispatcher() {
return new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
switch (request.getPath()) {
case "/metadata":
return new MockResponse().setResponseCode(HttpServletResponse.SC_OK)
.addHeader("Content-Type", "application/fhir+xml")
.setBody(CuiFileUtil.toStringUnchecked(CONFORMANCE));
case "/ValueSet/C_GE_DRR_PRACTICESETTING_CODE":
return new MockResponse().setResponseCode(HttpServletResponse.SC_OK)
.addHeader("Content-Type", "application/fhir+xml")
.setBody(CuiFileUtil.toStringUnchecked(CONTENT));
default:
return new MockResponse().setResponseCode(HttpServletResponse.SC_NOT_FOUND);
}
}
};
}
}
----

==== ModuleDispatcherElement

`ModuleDispatcherElement` enables reusable request handling in `EnableMockWebServer` contexts. It returns an `Optional<MockResponse>` for matching requests.

Example JWKS endpoint dispatcher:

[source,java]
----
/**
* Handles JWKS file resolution from the mock OAuth server, serving
* "src/test/resources/token/test-public-key.jwks"
*/
public class JwksResolveDispatcher implements ModuleDispatcherElement {
/** "/oidc/jwks.json" */
public static final String LOCAL_PATH = "/oidc/jwks.json";
@Getter
@Setter
private int callCounter = 0;
@Override
public Optional<MockResponse> handleGet(@NonNull RecordedRequest request) {
callCounter++;
return Optional.of(new MockResponse().addHeader("Content-Type", "application/json")
.setBody(FileLoaderUtility
.toStringUnchecked(FileLoaderUtility.getLoaderForPath(PUBLIC_KEY_JWKS)))
.setResponseCode(SC_OK));
}
@Override
public String getBaseUrl() {
return LOCAL_PATH;
}
/**
* Verifies request count
*
* @param expected Expected number of requests
*/
public void assertCallsAnswered(int expected) {
assertEquals(expected, callCounter);
}
}
----

Implementation example:

[source,java]
----
@EnableAutoWeld
@EnablePortalConfiguration
@EnableMockWebServer
class TokenParserProducerTest implements ShouldBeNotNull<TokenParserProducer>, MockWebServerHolder {
@Setter
private MockWebServer mockWebServer;
protected int mockserverPort;
private JwksResolveDispatcher jwksResolveDispatcher = new JwksResolveDispatcher();
@Getter
private final CombinedDispatcher dispatcher = new CombinedDispatcher().addDispatcher(jwksResolveDispatcher);
@BeforeEach
void setupMockServer() {
mockserverPort = mockWebServer.getPort();
configuration.put(VERIFY_SIGNATURE_JWKS_URL,
"http://localhost:" + mockserverPort + jwksResolveDispatcher.getBaseUrl());
configuration.update(VERIFY_SIGNATURE_ISSUER, TestTokenProducer.ISSUER);
configuration.update(VERIFY_SIGNATURE_REFRESH_INTERVAL, "60");
jwksResolveDispatcher.setCallCounter(0);
}
@Test
void shouldCacheMultipleCalls() {
jwksResolveDispatcher.assertCallsAnswered(0);
String token = validSignedJWTWithClaims(PATIENT_ACCESS_TOKEN);
JWTParser parser = parserProvider.get();
for (int i = 0; i < 100; i++) {
JsonWebToken jsonWebToken = assertDoesNotThrow(() -> ParsedToken.jsonWebTokenFrom(token, parser, LOGGER));
assertValidJsonWebToken(jsonWebToken, token);
}
// Note: Initial implementation results in 2 calls instead of 1
assertTrue(jwksResolveDispatcher.getCallCounter() < 3);
for (int i = 0; i < 100; i++) {
JsonWebToken jsonWebToken = assertDoesNotThrow(() -> ParsedToken.jsonWebTokenFrom(token, parser, LOGGER));
assertValidJsonWebToken(jsonWebToken, token);
}
assertTrue(jwksResolveDispatcher.getCallCounter() < 3);
}
}
----
----
Loading

0 comments on commit 0b8fe2a

Please sign in to comment.