11package org .wiremock .integrations .testcontainers ;
22
3+ import java .io .File ;
34import java .io .IOException ;
45import java .net .URI ;
56import java .net .URISyntaxException ;
1112import org .testcontainers .containers .GenericContainer ;
1213import org .testcontainers .images .builder .Transferable ;
1314import org .testcontainers .shaded .com .google .common .io .Resources ;
15+ import org .testcontainers .utility .MountableFile ;
1416
1517/**
1618 * Provisions WireMock standalone server as a container.
@@ -19,13 +21,15 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
1921 private static final String DEFAULT_IMAGE_NAME = "wiremock/wiremock" ;
2022 private static final String DEFAULT_TAG = "latest" ;
2123
22- private static final String SNIPPETS_DIR = "/home/wiremock/mappings/" ;
24+ private static final String MAPPINGS_DIR = "/home/wiremock/mappings/" ;
25+ private static final String FILES_DIR = "/home/wiremock/__files/" ;
2326
2427 private static final int PORT = 8080 ;
2528
2629 private final StringBuilder wireMockArgs ;
2730
28- private final Map <String , Stub > stubs = new HashMap <>();
31+ private final Map <String , Stub > mappingStubs = new HashMap <>();
32+ private final Map <String , MountableFile > mappingFiles = new HashMap <>();
2933
3034 public WireMockContainer () {
3135 this (DEFAULT_TAG );
@@ -35,27 +39,67 @@ public WireMockContainer(String version) {
3539 this (DEFAULT_IMAGE_NAME , version );
3640 }
3741
38- public WireMockContainer (String image ,String version ) {
42+ public WireMockContainer (String image , String version ) {
3943 super (image + ":" + version );
4044 wireMockArgs = new StringBuilder ();
4145 }
4246
43- public WireMockContainer withStub (String name , String json ) {
44- stubs .put (name , new Stub (name , json ));
47+ /**
48+ * Adds CLI argument to the WireMock call.
49+ * @param arg Argument
50+ * @return this instance
51+ */
52+ public WireMockContainer withCliArg (String arg ) {
53+ //TODO: Switch to framework with proper CLI escaping
54+ wireMockArgs .append (' ' ).append (arg );
55+ return this ;
56+ }
57+
58+ /**
59+ * Adds a JSON mapping stub to WireMock configuration
60+ * @param name Name of the mapping stub
61+ * @param json Configuration JSON
62+ * @return this instance
63+ */
64+ public WireMockContainer withMapping (String name , String json ) {
65+ mappingStubs .put (name , new Stub (name , json ));
4566 // TODO: Prevent duplication
4667 return this ;
4768 }
4869
49- public WireMockContainer withStubResource (String name , Class <?> resource , String resourceFile ) {
70+ /**
71+ * Loads mapping stub from the class resource
72+ * @param name Name of the mapping stub
73+ * @param resource Resource class. Name of the class will be appended to the resource path
74+ * @param resourceJson Mapping definition file
75+ * @return this instance
76+ */
77+ public WireMockContainer withMapping (String name , Class <?> resource , String resourceJson ) {
5078 try {
51- URL url = Resources .getResource (resource , resource .getSimpleName () + "/" + resourceFile );
79+ URL url = Resources .getResource (resource , resource .getSimpleName () + "/" + resourceJson );
5280 String text = Resources .toString (url , StandardCharsets .UTF_8 );
53- return withStub (name , text );
81+ return withMapping (name , text );
5482 } catch (IOException ex ) {
5583 throw new IllegalArgumentException (ex );
5684 }
5785 }
5886
87+ public WireMockContainer withFile (String name , File file ) {
88+ mappingFiles .put (name , MountableFile .forHostPath (file .getPath ()));
89+ // TODO: Prevent duplication
90+ return this ;
91+ }
92+
93+ public WireMockContainer withFileFromResource (String name , String classpathResource ) {
94+ mappingFiles .put (name , MountableFile .forClasspathResource (classpathResource ));
95+ // TODO: Prevent duplication
96+ return this ;
97+ }
98+
99+ public WireMockContainer withFileFromResource (String name , Class <?> resource , String filename ) {
100+ return withFileFromResource (name , resource .getName ().replace ('.' , '/' ) + "/" + filename );
101+ }
102+
59103 public String getEndpoint () {
60104 return String .format ("http://%s:%d" , getHost (), getMappedPort (PORT ));
61105 }
@@ -73,8 +117,12 @@ protected void configure() {
73117 super .configure ();
74118 withExposedPorts (PORT );
75119 withCommand (wireMockArgs .toString ());
76- for (Stub stub : stubs .values ()) {
77- withCopyToContainer (Transferable .of (stub .json ), SNIPPETS_DIR + stub .name + ".json" );
120+ for (Stub stub : mappingStubs .values ()) {
121+ withCopyToContainer (Transferable .of (stub .json ), MAPPINGS_DIR + stub .name + ".json" );
122+ }
123+
124+ for (Map .Entry <String , MountableFile > mount : mappingFiles .entrySet ()) {
125+ withCopyToContainer (mount .getValue (), FILES_DIR + mount .getKey ());
78126 }
79127 }
80128
0 commit comments