Skip to content

Commit 65a7d9f

Browse files
committed
Add negative tests for custom root dir
1 parent bca8541 commit 65a7d9f

File tree

14 files changed

+200
-167
lines changed

14 files changed

+200
-167
lines changed

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

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

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

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright (C) 2025 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.jupiter.api.Test;
19+
import org.testcontainers.junit.jupiter.Container;
20+
import org.testcontainers.junit.jupiter.Testcontainers;
21+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
22+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
23+
24+
import java.io.File;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
@Testcontainers(parallel = true)
29+
class WireMockContainerRootDirTest {
30+
@Container
31+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
32+
.withMapping("hello", WireMockContainerRootDirTest.class, "hello.json")
33+
.withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json")
34+
.withRootDir(new File("src/test/resources/root-dir"));
35+
36+
@Test
37+
void testThatLoadMappingFromRootDirectory() throws Exception {
38+
// given
39+
String url = wiremockServer.getUrl("hello/root/dir");
40+
41+
// when
42+
HttpResponse response = new TestHttpClient().get(url);
43+
44+
// then
45+
assertThat(response.getBody())
46+
.as("response body")
47+
.contains("Hello root dir");
48+
}
49+
50+
@Test
51+
void testThatLoadMappingFromNestedRootDirectory() throws Exception {
52+
// given
53+
String url = wiremockServer.getUrl("hello/nested/root/dir");
54+
55+
// when
56+
HttpResponse response = new TestHttpClient().get(url);
57+
58+
// then
59+
assertThat(response.getBody())
60+
.as("response body")
61+
.contains("Hello nested root dir");
62+
}
63+
64+
@Test
65+
void testThatServesFileFromRootDirectory() throws Exception {
66+
// given
67+
String url = wiremockServer.getUrl("root-dir-file.json");
68+
69+
// when
70+
HttpResponse response = new TestHttpClient().get(url);
71+
72+
// then
73+
assertThat(response.getBody())
74+
.as("response body")
75+
.isEqualTo("{ \"message\": \"file from root dir\" }");
76+
}
77+
78+
@Test
79+
void testThatServesNestedFileFromRootDirectory() throws Exception {
80+
// given
81+
String url = wiremockServer.getUrl("nested/root-dir-nested-file.json");
82+
83+
// when
84+
HttpResponse response = new TestHttpClient().get(url);
85+
86+
// then
87+
assertThat(response.getBody())
88+
.as("response body")
89+
.isEqualTo("{ \"message\": \"nested file from root dir\" }");
90+
}
91+
92+
@Test
93+
void testThatDirectMappingsAndFilesAreLoadedWithCustomRootDirEnabled() throws Exception {
94+
// given
95+
String url = wiremockServer.getUrl("/hello");
96+
97+
// when
98+
HttpResponse response = new TestHttpClient().get(url);
99+
100+
// then
101+
assertThat(response.getBody())
102+
.as("Wrong response body")
103+
.contains("file contents from direct mapping");
104+
}
105+
106+
@Test
107+
void testThatDirectMappingsAndFilesAreLoadedEvenWhenRootDirIsSpecified() throws Exception {
108+
// given
109+
String url = wiremockServer.getUrl("/hello");
110+
111+
// when
112+
HttpResponse response = new TestHttpClient().get(url);
113+
114+
// then
115+
assertThat(response.getBody())
116+
.as("Wrong response body")
117+
.contains("file contents from direct mapping");
118+
}
119+
120+
@Test
121+
void testThatMappingsAndFileAreLoadedFromDefaultRootDir() throws Exception {
122+
123+
try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)) {
124+
wmc.start();
125+
126+
// given
127+
String url = wmc.getUrl("/hello/default/root/dir");
128+
129+
// when
130+
HttpResponse response = new TestHttpClient().get(url);
131+
132+
// then
133+
assertThat(response.getBody())
134+
.as("Wrong response body")
135+
.contains("contents from default root dir file");
136+
}
137+
}
138+
139+
@Test
140+
void testThatInvalidRootDirIsIgnored() throws Exception {
141+
142+
try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
143+
.withMapping("hello", WireMockContainerRootDirTest.class, "hello.json")
144+
.withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json")
145+
.withRootDir(new File("invalid/root/dir"))) {
146+
wmc.start();
147+
148+
// given
149+
String url = wmc.getUrl("/hello");
150+
151+
// when
152+
HttpResponse response = new TestHttpClient().get(url);
153+
154+
// then
155+
assertThat(response.getBody())
156+
.as("Wrong response body")
157+
.contains("file contents from direct mapping");
158+
}
159+
}
160+
161+
@Test
162+
void testThatNullRootDirIsIgnored() throws Exception {
163+
164+
try (WireMockContainer wmc = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
165+
.withMapping("hello", WireMockContainerRootDirTest.class, "hello.json")
166+
.withFileFromResource("file.json", WireMockContainerRootDirTest.class, "file.json")
167+
.withRootDir(null)) {
168+
wmc.start();
169+
170+
// given
171+
String url = wmc.getUrl("/hello");
172+
173+
// when
174+
HttpResponse response = new TestHttpClient().get(url);
175+
176+
// then
177+
assertThat(response.getBody())
178+
.as("Wrong response body")
179+
.contains("file contents from direct mapping");
180+
}
181+
}
182+
183+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "message": "file from default root dir" }
1+
{ "message": "contents from default root dir file" }

src/test/resources/__files/nested/default-root-dir-nested-file.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/resources/integration-tests/__files/custom-root-dir-file.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/resources/integration-tests/mappings/hello-file-custom-root-dir.json

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello/default/root/dir"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "default-root-dir-file.json"
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "message": "file contents from direct mapping" }
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"request": {
33
"method": "GET",
4-
"url": "/hello/custom/root/dir"
4+
"url": "/hello"
55
},
66
"response": {
77
"status": 200,
8-
"body": "Hello custom root dir"
8+
"bodyFileName": "file.json"
99
}
10-
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "message": "nested file from root dir" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "message": "file from root dir" }

0 commit comments

Comments
 (0)