Skip to content

Commit

Permalink
Merge pull request #1119 from DependencyTrack/feature/removeMockServer
Browse files Browse the repository at this point in the history
removed usage of mockserver
  • Loading branch information
nscuro authored Mar 5, 2024
2 parents 8c10168 + 4203e8b commit 509e78c
Show file tree
Hide file tree
Showing 15 changed files with 619 additions and 888 deletions.
10 changes: 5 additions & 5 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>quarkus-junit5</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-junit5-mockito</artifactId>
Expand All @@ -73,11 +78,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
186 changes: 76 additions & 110 deletions commons/src/test/java/org/dependencytrack/common/HttpClientTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.dependencytrack.common;

import com.github.tomakehurst.wiremock.WireMockServer;
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.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
Expand All @@ -12,20 +17,19 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.AfterAll;
import org.dependencytrack.util.WireMockTestResource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;

import java.io.IOException;
import java.util.Map;

import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

@Suite
@SelectClasses(value = {
Expand All @@ -37,8 +41,12 @@
})
public class HttpClientTests {
@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigurationTest.TestProfile.class)
public static class HttpClientConfigurationTest {
static class HttpClientConfigurationTest {
public static class TestProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
Expand All @@ -54,34 +62,21 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
private static ClientAndServer mockServer;
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

@BeforeAll
public static void beforeClass() {
mockServer = ClientAndServer.startClientAndServer(1080);
@AfterEach
void afterEach() {
wireMockServer.resetAll();
}

@AfterAll
public static void afterClass() {
mockServer.stop();
}


@Test
void clientCreatedTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
new MockServerClient("localhost", mockServer.getPort())
.when(
request()
.withMethod("GET")
.withPath("/hello")
)
.respond(
response()
.withStatusCode(HttpStatus.SC_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("hello test")
);
wireMockServer.stubFor(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)));
HttpUriRequest request = new HttpGet("http://localhost:1080/hello");
try (CloseableHttpResponse response = client.execute(request)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Expand All @@ -104,6 +99,10 @@ void clientCreatedTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithProxyTest.TestProfile.class)
public static class HttpClientConfigWithProxyTest {
public static class TestProfile implements QuarkusTestProfile {
Expand All @@ -125,33 +124,21 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
private static ClientAndServer mockServer;

@BeforeAll
public static void beforeClass() {
mockServer = ClientAndServer.startClientAndServer(1080);
}
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

@AfterAll
public static void afterClass() {
mockServer.stop();
@AfterEach
void afterEach() {
wireMockServer.resetAll();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
new MockServerClient("localhost", mockServer.getPort())
.when(
request()
.withMethod("GET")
.withPath("/hello")
)
.respond(
response()
.withStatusCode(HttpStatus.SC_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("hello test")
);
wireMockServer.stubFor(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)));
HttpUriRequest request = new HttpGet("http://localhost:1080/hello");
try (CloseableHttpResponse response = client.execute(request)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Expand All @@ -166,6 +153,10 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyTest.TestProfile.class)
public static class HttpClientConfigWithNoProxyTest {
public static class TestProfile implements QuarkusTestProfile {
Expand All @@ -188,33 +179,22 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
private static ClientAndServer mockServer;

@BeforeAll
public static void beforeClass() {
mockServer = ClientAndServer.startClientAndServer(1080);
}
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

@AfterAll
public static void afterClass() {
mockServer.stop();
@AfterEach
void afterEach() {
wireMockServer.resetAll();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
new MockServerClient("localhost", mockServer.getPort())
.when(
request()
.withMethod("GET")
.withPath("/hello")
)
.respond(
response()
.withStatusCode(HttpStatus.SC_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("hello test")
);
wireMockServer.stubFor(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)));
HttpUriRequest request = new HttpGet("http://localhost:1080/hello");
try (CloseableHttpResponse response = client.execute(request)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Expand All @@ -229,6 +209,10 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyStarTest.TestProfile.class)
public static class HttpClientConfigWithNoProxyStarTest {
public static class TestProfile implements QuarkusTestProfile {
Expand All @@ -251,33 +235,22 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
private static ClientAndServer mockServer;

@BeforeAll
public static void beforeClass() {
mockServer = ClientAndServer.startClientAndServer(1080);
}
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

@AfterAll
public static void afterClass() {
mockServer.stop();
@AfterEach
void afterEach() {
wireMockServer.resetAll();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
new MockServerClient("localhost", mockServer.getPort())
.when(
request()
.withMethod("GET")
.withPath("/hello")
)
.respond(
response()
.withStatusCode(HttpStatus.SC_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("hello test")
);
wireMockServer.stubFor(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)));
HttpUriRequest request = new HttpGet("http://localhost:1080/hello");
try (CloseableHttpResponse response = client.execute(request)) {
Assertions.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Expand All @@ -292,6 +265,10 @@ void clientCreatedWithProxyInfoTest() throws IOException {
}

@QuarkusTest
@QuarkusTestResource(
value = WireMockTestResource.class,
initArgs = @ResourceArg(name = "serverUrlProperty", value = "http://localhost")
)
@TestProfile(HttpClientConfigWithNoProxyDomainTest.TestProfile.class)
public static class HttpClientConfigWithNoProxyDomainTest {
public static class TestProfile implements QuarkusTestProfile {
Expand All @@ -313,33 +290,22 @@ public Map<String, String> getConfigOverrides() {
HttpClientConfiguration configuration;
@Inject
MeterRegistry meterRegistry;
private static ClientAndServer mockServer;

@BeforeAll
public static void beforeClass() {
mockServer = ClientAndServer.startClientAndServer(1080);
}
@WireMockTestResource.InjectWireMock
WireMockServer wireMockServer;

@AfterAll
public static void afterClass() {
mockServer.stop();
@AfterEach
void afterEach() {
wireMockServer.resetAll();
}

@Test
void clientCreatedWithProxyInfoTest() throws IOException {
try (CloseableHttpClient client = configuration.newManagedHttpClient(meterRegistry)) {
new MockServerClient("localhost", mockServer.getPort())
.when(
request()
.withMethod("GET")
.withPath("/hello")
)
.respond(
response()
.withStatusCode(200)
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("hello test")
);
wireMockServer.stubFor(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)));
HttpUriRequest request = new HttpGet("http://localhost:1080/hello");
try (CloseableHttpResponse response = client.execute(request)) {
Assertions.assertEquals(200, response.getStatusLine().getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.dependencytrack.util;

import com.github.tomakehurst.wiremock.WireMockServer;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

/**
* A Quarkus test resource that provisions a WireMock server on a random open port.
* <p>
* Note that the same {@link WireMockServer} instance will be used for all tests in the
* annotated test class. Stubs will need to manually be reset after each test using {@link WireMockServer#resetAll()}.
*
* @see <a href="https://quarkus.io/guides/getting-started-testing#altering-the-test-class">Quarkus Documentation</a>
*/
public class WireMockTestResource implements QuarkusTestResourceLifecycleManager {

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InjectWireMock {
}

private WireMockServer wireMockServer;
private String serverUrlProperty;

@Override
public void init(final Map<String, String> initArgs) {
serverUrlProperty = initArgs.get("serverUrlProperty");
}

@Override
public Map<String, String> start() {
wireMockServer = new WireMockServer(options().dynamicPort());
wireMockServer.start();

if (serverUrlProperty == null) {
return null;
}

return Map.of(serverUrlProperty, wireMockServer.baseUrl());
}

@Override
public synchronized void stop() {
if (wireMockServer != null) {
wireMockServer.stop();
wireMockServer = null;
}
}

@Override
public void inject(final TestInjector testInjector) {
testInjector.injectIntoFields(wireMockServer,
new TestInjector.AnnotatedAndMatchesType(InjectWireMock.class, WireMockServer.class));
}

}
Loading

0 comments on commit 509e78c

Please sign in to comment.