Skip to content

Commit 100dc2c

Browse files
Merge pull request #26 from bitxon/feature/multiple-extentions
Simplify extensions class names concatenation + Add a test for extensions combination
2 parents 1cd7b0f + 09e5ca8 commit 100dc2c

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
<version>0.4.1</version>
147147
<classifier>jar-with-dependencies</classifier>
148148
</artifactItem>
149+
<dependency>
150+
<groupId>org.wiremock</groupId>
151+
<artifactId>wiremock-webhooks-extension</artifactId>
152+
<version>2.35.0</version>
153+
</dependency>
149154
</artifactItems>
150155
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
151156
</configuration>

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,7 @@ protected void configure() {
219219
}
220220
if (!extensionClassNames.isEmpty()) {
221221
wireMockArgs.append(" --extensions ");
222-
int counter = 0;
223-
for (String className : extensionClassNames) {
224-
wireMockArgs.append(className);
225-
wireMockArgs.append( (++counter != extensionClassNames.size()) ? ',' : ' ');
226-
}
222+
wireMockArgs.append(String.join(",", extensionClassNames));
227223
}
228224

229225
// Add CLI arguments

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
*/
3838
public class WireMockContainerExtensionTest {
3939

40-
public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
40+
public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
4141

4242
@Rule
4343
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
4444
.withStartupTimeout(Duration.ofSeconds(60))
4545
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
46-
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
46+
.withExtension("JSON Body Transformer",
47+
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4748
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
4849

4950
@Before
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
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 org.wiremock.integrations.testcontainers;
17+
18+
import org.junit.Before;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
import org.testcontainers.containers.output.Slf4jLogConsumer;
24+
25+
import java.net.http.HttpClient;
26+
import java.net.http.HttpRequest;
27+
import java.net.http.HttpResponse;
28+
import java.nio.file.Paths;
29+
import java.time.Duration;
30+
import java.util.Collections;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests the WireMock extension loading.
36+
* It uses multiple external Jars supplied by the Maven Dependency Plugin.
37+
*/
38+
public class WireMockContainerExtensionsCombinationTest {
39+
40+
public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
41+
42+
@Rule
43+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
44+
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
45+
.withExtension("Webhook",
46+
Collections.singleton("org.wiremock.webhooks.Webhooks"),
47+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
48+
.withExtension("JSON Body Transformer",
49+
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
50+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
51+
52+
@Before
53+
public void before() {
54+
wiremockServer.followOutput(new Slf4jLogConsumer(LOGGER));
55+
}
56+
57+
@Test
58+
public void testJSONBodyTransformer() throws Exception {
59+
final HttpClient client = HttpClient.newBuilder().build();
60+
final HttpRequest request = HttpRequest.newBuilder()
61+
.uri(wiremockServer.getRequestURI("json-body-transformer"))
62+
.timeout(Duration.ofSeconds(10))
63+
.header("Content-Type", "application/json")
64+
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
65+
66+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
67+
68+
assertThat(response.body())
69+
.as("Wrong response body")
70+
.contains("Hello, John Doe!");
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"request": {
3+
"method": "POST",
4+
"url": "/json-body-transformer"
5+
},
6+
"response": {
7+
"status": 201,
8+
"headers": {
9+
"content-type": "application/json"
10+
},
11+
"jsonBody": {
12+
"message": "Hello, $(name)!"
13+
},
14+
"transformers" : ["json-body-transformer"]
15+
}
16+
}

0 commit comments

Comments
 (0)