Skip to content

Commit 734ccc2

Browse files
Merge pull request #66 from wiremock/api-patches
Provide API for adding mapping stubs and files without IDs + Allow loading mappings from any classpath location + Deprecate `WireMockContainer#withMapping()`
2 parents 4b35ac4 + 0136f1f commit 734ccc2

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

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

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,51 +123,150 @@ public WireMockContainer withCliArg(String arg) {
123123
return this;
124124
}
125125

126+
/**
127+
* Add mapping JSON file from its value
128+
* @param json JSON sting
129+
* @return This instance
130+
*/
131+
public WireMockContainer withMappingFromJSON(String json) {
132+
return withMappingFromJSON(Integer.toString(json.hashCode()), json);
133+
}
134+
126135
/**
127136
* Adds a JSON mapping stub to WireMock configuration
128137
* @param name Name of the mapping stub
129138
* @param json Configuration JSON
130139
* @return this instance
131140
*/
132-
public WireMockContainer withMapping(String name, String json) {
141+
public WireMockContainer withMappingFromJSON(String name, String json) {
133142
mappingStubs.put(name, new Stub(name, json));
134143
// TODO: Prevent duplication
135144
return this;
136145
}
137146

147+
/**
148+
* @deprecated use {@link #withMappingFromJSON(String, String)}
149+
*/
150+
@Deprecated
151+
public WireMockContainer withMapping(String name, String json) {
152+
return withMappingFromJSON(name, json);
153+
}
154+
138155
/**
139156
* Loads mapping stub from the class resource
140157
* @param name Name of the mapping stub
141158
* @param resource Resource class. Name of the class will be appended to the resource path
159+
* @param resourceJson Reference to the mapping definition file, starting from the {@code resource} root
160+
* (normally package)
161+
* @return this instance
162+
*/
163+
public WireMockContainer withMappingFromResource(String name, Class<?> resource, String resourceJson) {
164+
final URL url = Resources.getResource(resource, resourceJson);
165+
return withMappingFromResource(name, url);
166+
}
167+
168+
/**
169+
* Loads mapping stub from the class resource
170+
* @param resource Resource class. Name of the class will be appended to the resource path
142171
* @param resourceJson Mapping definition file
143172
* @return this instance
144173
*/
174+
public WireMockContainer withMappingFromResource(Class<?> resource, String resourceJson) {
175+
final String id = resource.getName() + "_" + resourceJson;
176+
return withMappingFromResource(id, resource.getSimpleName() + "/" + resourceJson);
177+
}
178+
179+
/**
180+
* @deprecated use {@link #withMappingFromResource(String, Class, String)}.
181+
* Note that the new method scopes to the package, not to class
182+
*/
183+
@Deprecated
145184
public WireMockContainer withMapping(String name, Class<?> resource, String resourceJson) {
185+
return withMappingFromResource(name, resource, resource.getSimpleName() + "/" + resourceJson);
186+
}
187+
188+
/**
189+
* Loads mapping stub from the resource file
190+
* @param name Name of the mapping stub
191+
* @param resourceName Resource name and path
192+
* @return this instance
193+
*/
194+
public WireMockContainer withMappingFromResource(String name, String resourceName) {
195+
final URL url = Resources.getResource(resourceName);
196+
return withMappingFromResource(name, url);
197+
}
198+
199+
200+
201+
/**
202+
* Loads mapping stub from the resource file
203+
* @param resourceName Resource name and path
204+
* @return this instance
205+
*/
206+
public WireMockContainer withMappingFromResource(String resourceName) {
207+
String id = resourceName.replace('/', '_');
208+
return withMappingFromResource(id, resourceName);
209+
}
210+
211+
/**
212+
* Loads mapping stub from the resource file
213+
* @param name Name of the mapping stub
214+
* @param url Resource file URL
215+
* @return this instance
216+
*/
217+
public WireMockContainer withMappingFromResource(String name, URL url) {
146218
try {
147-
URL url = Resources.getResource(resource, resource.getSimpleName() + "/" + resourceJson);
148219
String text = Resources.toString(url, StandardCharsets.UTF_8);
149220
return withMapping(name, text);
150221
} catch (IOException ex) {
151222
throw new IllegalArgumentException(ex);
152223
}
153224
}
154225

226+
/**
227+
* Adds file
228+
* @param name ID to be used
229+
* @param file File to add
230+
* @return This instance
231+
*/
155232
public WireMockContainer withFile(String name, File file) {
156233
mappingFiles.put(name, MountableFile.forHostPath(file.getPath()));
157234
// TODO: Prevent duplication
158235
return this;
159236
}
160237

238+
/**
239+
* Adds file
240+
* @param file File to add
241+
* @return This instance
242+
*/
243+
public WireMockContainer withFile(File file) {
244+
mappingFiles.put(file.getName(), MountableFile.forHostPath(file.getPath()));
245+
// TODO: Prevent duplication
246+
return this;
247+
}
248+
161249
public WireMockContainer withFileFromResource(String name, String classpathResource) {
162250
mappingFiles.put(name, MountableFile.forClasspathResource(classpathResource));
163251
// TODO: Prevent duplication
164252
return this;
165253
}
166254

255+
public WireMockContainer withFileFromResource(String classpathResource) {
256+
String id = classpathResource.replace('/', '_');
257+
// TODO: Prevent duplication
258+
return withFileFromResource(id, classpathResource);
259+
}
260+
167261
public WireMockContainer withFileFromResource(String name, Class<?> resource, String filename) {
168262
return withFileFromResource(name, resource.getName().replace('.', '/') + "/" + filename);
169263
}
170264

265+
public WireMockContainer withFileFromResource(Class<?> resource, String filename) {
266+
String id = resource.getSimpleName() + "_" + filename;
267+
return withFileFromResource(id, resource, filename);
268+
}
269+
171270
/**
172271
* Add extension that will be loaded from the specified JAR file.
173272
* @param id Unique ID of the extension, for logging purposes

0 commit comments

Comments
 (0)