Skip to content

Commit

Permalink
Merge pull request #1254 from DependencyTrack/issue-1237-introduce-qu…
Browse files Browse the repository at this point in the history
…arkus-wiremock

Introduce quarkus wiremock extension
  • Loading branch information
nscuro authored May 16, 2024
2 parents 8e8006f + 0c2d989 commit 76fde8d
Show file tree
Hide file tree
Showing 26 changed files with 323 additions and 724 deletions.
11 changes: 11 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
*/
package org.dependencytrack.common;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.Body;
import com.github.tomakehurst.wiremock.http.ContentTypeHeader;
import io.micrometer.core.instrument.MeterRegistry;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.ResourceArg;
import io.quarkiverse.wiremock.devservice.ConnectWireMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
Expand All @@ -35,7 +34,6 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.dependencytrack.util.WireMockTestResource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -59,11 +57,8 @@
})
public class HttpClientTests {
@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigurationTest.TestProfile.class)
@ConnectWireMock
static class HttpClientConfigurationTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
Expand All @@ -80,18 +75,18 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

WireMock wireMock;

@AfterEach
void afterEach() {
wireMockServer.resetAll();
wireMock.resetToDefaultMappings();
}

@Test
void clientCreatedTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
wireMockServer.stubFor(get(urlPathEqualTo("/hello"))
wireMock.register(get(urlPathEqualTo("/hello"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withResponseBody(Body.ofBinaryOrText("hello test".getBytes(),
new ContentTypeHeader("application/json"))).withStatus(HttpStatus.SC_OK)));
Expand All @@ -117,11 +112,8 @@ void clientCreatedTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithProxyTest.TestProfile.class)
@ConnectWireMock
public static class HttpClientConfigWithProxyTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
Expand All @@ -142,18 +134,18 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

WireMock wireMock;

@AfterEach
void afterEach() {
wireMockServer.resetAll();
wireMock.resetToDefaultMappings();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
wireMockServer.stubFor(get(urlPathEqualTo("/hello"))
wireMock.register(get(urlPathEqualTo("/hello"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, String.valueOf("application/json"))
.withResponseBody(Body.ofBinaryOrText("hello test".getBytes(),
new ContentTypeHeader("application/json"))).withStatus(HttpStatus.SC_OK)));
Expand All @@ -171,11 +163,8 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyTest.TestProfile.class)
@ConnectWireMock
public static class HttpClientConfigWithNoProxyTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
Expand All @@ -198,18 +187,17 @@ public Map<String, String> getConfigOverrides() {
@Inject
MeterRegistry meterRegistry;

@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;
WireMock wireMock;

@AfterEach
void afterEach() {
wireMockServer.resetAll();
wireMock.resetToDefaultMappings();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
wireMockServer.stubFor(get(urlPathEqualTo("/hello"))
wireMock.register(get(urlPathEqualTo("/hello"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, String.valueOf("application/json"))
.withResponseBody(Body.ofBinaryOrText("hello test".getBytes(),
new ContentTypeHeader("application/json"))).withStatus(HttpStatus.SC_OK)));
Expand All @@ -227,11 +215,8 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyStarTest.TestProfile.class)
@ConnectWireMock
public static class HttpClientConfigWithNoProxyStarTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
Expand All @@ -254,18 +239,17 @@ public Map<String, String> getConfigOverrides() {
@Inject
MeterRegistry meterRegistry;

@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;
WireMock wireMock;

@AfterEach
void afterEach() {
wireMockServer.resetAll();
wireMock.resetToDefaultMappings();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
wireMockServer.stubFor(get(urlPathEqualTo("/hello"))
wireMock.register(get(urlPathEqualTo("/hello"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, String.valueOf("application/json"))
.withResponseBody(Body.ofBinaryOrText("hello test".getBytes(),
new ContentTypeHeader("application/json"))).withStatus(HttpStatus.SC_OK)));
Expand All @@ -283,11 +267,8 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyDomainTest.TestProfile.class)
@ConnectWireMock
public static class HttpClientConfigWithNoProxyDomainTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
Expand All @@ -309,18 +290,17 @@ public Map<String, String> getConfigOverrides() {
@Inject
MeterRegistry meterRegistry;

@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;
WireMock wireMock;

@AfterEach
void afterEach() {
wireMockServer.resetAll();
wireMock.resetToDefaultMappings();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
wireMockServer.stubFor(get(urlPathEqualTo("/hello"))
wireMock.register(get(urlPathEqualTo("/hello"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, String.valueOf("application/json"))
.withResponseBody(Body.ofBinaryOrText("hello test".getBytes(),
new ContentTypeHeader("application/json"))).withStatus(HttpStatus.SC_OK)));
Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions mirror-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,23 @@
<artifactId>quarkus-test-kafka-companion</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jacoco</artifactId>
Expand Down
Loading

0 comments on commit 76fde8d

Please sign in to comment.