|
| 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 | +} |
0 commit comments