Skip to content

Commit df81218

Browse files
authored
replace mockserver with wiremock (#4305)
1 parent 2422e42 commit df81218

File tree

7 files changed

+169
-131
lines changed

7 files changed

+169
-131
lines changed

integration-tests/application-server-integration-tests/pom.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,11 @@
129129
<version>1.6</version>
130130
<scope>test</scope>
131131
</dependency>
132-
<dependency>
133-
<groupId>org.mock-server</groupId>
134-
<artifactId>mockserver-client-java</artifactId>
135-
<version>5.14.0</version>
136-
<scope>test</scope>
137-
</dependency>
138132
<dependency>
139133
<groupId>com.networknt</groupId>
140134
<artifactId>json-schema-validator</artifactId>
141135
<version>${version.json-schema-validator}</version>
142136
<scope>test</scope>
143-
<exclusions>
144-
<!--
145-
mockserver-client does not work with the latest jackson version
146-
see https://github.com/jamesdbloom/mockserver/issues/440
147-
-->
148-
<exclusion>
149-
<groupId>com.fasterxml.jackson.core</groupId>
150-
<artifactId>jackson-databind</artifactId>
151-
</exclusion>
152-
</exclusions>
153137
</dependency>
154138
<dependency>
155139
<groupId>org.apache.commons</groupId>

integration-tests/application-server-integration-tests/src/test/java/co/elastic/apm/servlet/AbstractServletContainerIntegrationTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
import okhttp3.logging.HttpLoggingInterceptor;
3737
import org.junit.After;
3838
import org.junit.Test;
39-
import org.mockserver.model.ClearType;
40-
import org.mockserver.model.HttpRequest;
41-
import org.mockserver.model.HttpResponse;
4239
import org.testcontainers.containers.Network;
4340
import org.testcontainers.containers.wait.strategy.Wait;
4441
import org.testcontainers.utility.MountableFile;
@@ -59,9 +56,7 @@
5956
import java.util.function.Supplier;
6057
import java.util.stream.Collectors;
6158

62-
import static co.elastic.apm.agent.report.IntakeV2ReportingEventHandler.INTAKE_V2_URL;
6359
import static org.assertj.core.api.Assertions.assertThat;
64-
import static org.mockserver.model.HttpRequest.request;
6560

6661
/**
6762
* When you want to execute the test in the IDE, execute {@code mvn clean package} before.
@@ -84,18 +79,17 @@ public abstract class AbstractServletContainerIntegrationTest {
8479
@Nullable
8580
private static final String AGENT_VERSION_TO_DOWNLOAD_FROM_MAVEN = null;
8681

87-
private static final MockServerContainer mockServerContainer;
82+
private static final MockApmServerContainer mockServerContainer;
8883

8984
private static final OkHttpClient httpClient;
9085

9186
static {
92-
mockServerContainer = new MockServerContainer()
87+
mockServerContainer = new MockApmServerContainer()
9388
.withNetworkAliases("apm-server")
94-
.waitingFor(Wait.forHttp(MockServerContainer.HEALTH_ENDPOINT).forStatusCode(200))
9589
.withNetwork(Network.SHARED);
9690

9791
if (JavaExecutable.isDebugging()) {
98-
mockServerContainer.withLogConsumer(TestContainersUtils.createSlf4jLogConsumer(MockServerContainer.class));
92+
mockServerContainer.withLogConsumer(TestContainersUtils.createSlf4jLogConsumer(MockApmServerContainer.class));
9993
}
10094

10195
final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(logger::info);
@@ -106,8 +100,6 @@ public abstract class AbstractServletContainerIntegrationTest {
106100
.build();
107101

108102
mockServerContainer.start();
109-
mockServerContainer.getClient().when(request(INTAKE_V2_URL)).respond(HttpResponse.response().withStatusCode(200));
110-
mockServerContainer.getClient().when(request("/")).respond(HttpResponse.response().withStatusCode(200));
111103
}
112104

113105
private final MockReporter mockReporter = new MockReporter();
@@ -150,7 +142,7 @@ protected AbstractServletContainerIntegrationTest(AgentTestContainer.AppServer c
150142

151143
container
152144
.withNetwork(Network.SHARED)
153-
.withEnv("ELASTIC_APM_SERVER_URL", "http://apm-server:1080")
145+
.withEnv("ELASTIC_APM_SERVER_URL", "http://apm-server:8080")
154146
.withEnv("ELASTIC_APM_IGNORE_URLS", ignoreUrlConfig)
155147
.withEnv("ELASTIC_APM_REPORT_SYNC", "true")
156148
.withEnv("ELASTIC_APM_LOG_LEVEL", "DEBUG")
@@ -247,7 +239,7 @@ public void testAllScenarios() throws Exception {
247239
}
248240

249241
public void clearMockServerLog() {
250-
mockServerContainer.getClient().clear(HttpRequest.request(), ClearType.LOG);
242+
mockServerContainer.clearRecorded();
251243
}
252244

253245
public JsonNode assertTransactionReported(String pathToTest, int expectedResponseCode) {
@@ -423,8 +415,7 @@ public List<JsonNode> getEvents(String eventType) {
423415
try {
424416
final List<JsonNode> events = new ArrayList<>();
425417
final ObjectMapper objectMapper = new ObjectMapper();
426-
for (HttpRequest httpRequest : mockServerContainer.getClient().retrieveRecordedRequests(request(INTAKE_V2_URL))) {
427-
final String bodyAsString = httpRequest.getBodyAsString();
418+
for (String bodyAsString : mockServerContainer.getRecordedRequestBodies()) {
428419
for (String ndJsonLine : bodyAsString.split("\n")) {
429420
final JsonNode ndJson = objectMapper.readTree(ndJsonLine);
430421
if (ndJson.get(eventType) != null) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.apm.servlet;
20+
21+
import com.github.dockerjava.api.command.InspectContainerResponse;
22+
import com.github.tomakehurst.wiremock.client.WireMock;
23+
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
24+
import com.github.tomakehurst.wiremock.matching.UrlPattern;
25+
import org.testcontainers.containers.GenericContainer;
26+
import org.testcontainers.containers.wait.strategy.Wait;
27+
28+
import java.io.ByteArrayOutputStream;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.List;
31+
import java.util.stream.Collectors;
32+
import java.util.zip.DataFormatException;
33+
import java.util.zip.Inflater;
34+
35+
import static com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder.responseDefinition;
36+
import static com.github.tomakehurst.wiremock.client.WireMock.any;
37+
38+
public class MockApmServerContainer extends GenericContainer<MockApmServerContainer> {
39+
40+
private WireMock wireMock;
41+
42+
public MockApmServerContainer() {
43+
super("wiremock/wiremock:3.13.2");
44+
addExposedPorts(8080);
45+
waitStrategy = Wait.forHealthcheck();
46+
}
47+
48+
@Override
49+
protected void containerIsStarted(InspectContainerResponse containerInfo) {
50+
super.containerIsStarted(containerInfo);
51+
wireMock = WireMock.create()
52+
.host(getHost())
53+
.port(getMappedPort(8080))
54+
.build();
55+
56+
wireMock.register(any(UrlPattern.ANY).willReturn(responseDefinition().withStatus(200)));
57+
}
58+
59+
public List<String> getRecordedRequestBodies() {
60+
return wireMock.find(RequestPatternBuilder.newRequestPattern())
61+
.stream()
62+
// wiremock provides the raw request body without any decompression so we have to do it explicitly ourselves
63+
.map(action -> decompressZlib(action.getBody()))
64+
.collect(Collectors.toList());
65+
}
66+
67+
private static String decompressZlib(byte[] input) {
68+
Inflater inflater = new Inflater();
69+
inflater.setInput(input);
70+
try {
71+
ByteArrayOutputStream output = new ByteArrayOutputStream();
72+
byte[] buffer = new byte[1024];
73+
while (!inflater.finished()) {
74+
int count = inflater.inflate(buffer);
75+
output.write(buffer, 0, count);
76+
}
77+
inflater.end();
78+
return output.toString(StandardCharsets.UTF_8);
79+
} catch (DataFormatException e) {
80+
throw new IllegalStateException(e);
81+
}
82+
}
83+
84+
public void clearRecorded() {
85+
wireMock.resetRequests();
86+
}
87+
88+
}

integration-tests/application-server-integration-tests/src/test/java/co/elastic/apm/servlet/MockServerContainer.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

integration-tests/quarkus/pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<scope>import</scope>
3838
</dependency>
3939
<dependency>
40-
<!-- mock-server requires a fixed jackson version -->
4140
<groupId>com.fasterxml.jackson</groupId>
4241
<artifactId>jackson-bom</artifactId>
4342
<version>2.14.1</version>
@@ -70,18 +69,6 @@
7069
<version>1.2</version>
7170
<scope>test</scope>
7271
</dependency>
73-
<dependency>
74-
<groupId>org.testcontainers</groupId>
75-
<artifactId>mockserver</artifactId>
76-
<version>${version.testcontainers}</version>
77-
<scope>test</scope>
78-
</dependency>
79-
<dependency>
80-
<groupId>org.mock-server</groupId>
81-
<artifactId>mockserver-client-java</artifactId>
82-
<version>5.14.0</version>
83-
<scope>test</scope>
84-
</dependency>
8572
<dependency>
8673
<groupId>com.jayway.jsonpath</groupId>
8774
<artifactId>json-path</artifactId>

0 commit comments

Comments
 (0)