-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathWireMockContainerExtensionsWebhookTest.java
More file actions
100 lines (86 loc) · 4.5 KB
/
WireMockContainerExtensionsWebhookTest.java
File metadata and controls
100 lines (86 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wiremock.integrations.testcontainers;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpServer;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testcontainers.Testcontainers.exposeHostPorts;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
/**
* Tests the WireMock Webhook extension and TestContainers Networking
* For this type of tests we should use following steps:
* <p>
* Use {@link GenericContainer#withAccessToHost(boolean)} to force the host access mechanism
* <p>
* Use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)} to expose host machine ports to containers
* <p>
* Use {@link GenericContainer#INTERNAL_HOST_HOSTNAME} to calculate hostname for callback
*
* @see <a href="https://www.testcontainers.org/features/networking/">Testcontainers Networking</a>
*/
@Testcontainers
class WireMockContainerExtensionsWebhookTest {
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsWebhookTest.class);
private static final String WIREMOCK_PATH = "/wiremock/callback-trigger";
private static final String APPLICATION_PATH = "/application/callback-receiver";
TestHttpServer applicationServer = TestHttpServer.newInstance();
@Container
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withCliArg("--global-response-templating")
.withMapping("webhook-callback-template", WireMockContainerExtensionsWebhookTest.class, "webhook-callback-template.json")
.withExtension(
Collections.singleton("org.wiremock.webhooks.Webhooks"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
.withAccessToHost(true); // Force the host access mechanism
@Test
void callbackUsingJsonStub() throws Exception {
// given
exposeHostPorts(applicationServer.getPort()); // Exposing host ports to the container
String wiremockUrl = wiremockServer.getUrl(WIREMOCK_PATH);
String applicationCallbackUrl = String.format("http://%s:%d%s", GenericContainer.INTERNAL_HOST_HOSTNAME, applicationServer.getPort(), APPLICATION_PATH);
// when
HttpResponse response = new TestHttpClient().post(
wiremockUrl,
"{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + applicationCallbackUrl + "\"}"
);
// then
assertThat(response).as("Wiremock Response").isNotNull().satisfies(it -> {
assertThat(it.getStatusCode()).as("Wiremock Response Status").isEqualTo(200);
assertThat(it.getBody()).as("Wiremock Response Body")
.contains("Please wait callback")
.contains("PUT")
.contains(applicationCallbackUrl);
});
await().atMost(Duration.ofMillis(5000)).untilAsserted(() -> {
assertThat(applicationServer.getRecordedRequests()).as("Received Callback")
.hasSize(1)
.first().usingRecursiveComparison()
.isEqualTo(new TestHttpServer.RecordedRequest("PUT", APPLICATION_PATH, "Async processing Finished"));
});
}
}