Skip to content

Commit bc74604

Browse files
authored
Merge pull request #983 from swagger-api/wiremock-test-feature
added wiremock option for test and updated templates.
2 parents d5b90b0 + d71bcde commit bc74604

File tree

13 files changed

+221
-3
lines changed

13 files changed

+221
-3
lines changed

src/main/java/io/swagger/codegen/v3/generators/java/JavaClientCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
5353
public static final String RETROFIT_1 = "retrofit";
5454
public static final String RETROFIT_2 = "retrofit2";
5555

56+
public static final String WIREMOCK_OPTION = "wiremock";
57+
5658
protected String gradleWrapperPackage = "gradle.wrapper";
5759
protected boolean useRxJava = false;
5860
protected boolean useRxJava2 = false;
@@ -87,6 +89,8 @@ public JavaClientCodegen() {
8789
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Send gzip-encoded requests"));
8890
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
8991

92+
cliOptions.add(CliOption.newBoolean(WIREMOCK_OPTION, "Use wiremock to generate endpoint calls to mock on generated tests."));
93+
9094
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.10.1. Enable gzip request encoding using '-DuseGzipFeature=true'.");
9195
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.10.1");
9296
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.26. JSON processing: Jackson 2.10.1");
@@ -169,6 +173,11 @@ public void processOpts() {
169173
this.setUseRuntimeException(convertPropertyToBooleanAndWriteBack(USE_RUNTIME_EXCEPTION));
170174
}
171175

176+
if (additionalProperties.containsKey(WIREMOCK_OPTION)) {
177+
final boolean useWireMock = additionalProperties.get(WIREMOCK_OPTION) != null && Boolean.parseBoolean(additionalProperties.get(WIREMOCK_OPTION).toString());
178+
additionalProperties.put(WIREMOCK_OPTION, useWireMock);
179+
}
180+
172181
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
173182
final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", File.separator);
174183
final String apiFolder = (sourceFolder + File.separator + apiPackage).replace(".", File.separator);

src/main/resources/handlebars/Java/api_test.mustache

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,63 @@
22

33
package {{package}};
44

5-
import {{invokerPackage}}.ApiException;
65
{{#imports}}import {{import}};
76
{{/imports}}
87
import org.junit.Test;
98
import org.junit.Ignore;
109

10+
{{#wiremock}}
11+
import com.github.tomakehurst.wiremock.WireMockServer;
12+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
13+
import java.net.HttpURLConnection;
14+
import org.junit.AfterClass;
15+
import org.junit.BeforeClass;
16+
{{/wiremock}}
17+
1118
{{^fullJavaUtil}}
1219
import java.util.ArrayList;
1320
import java.util.HashMap;
1421
import java.util.List;
1522
import java.util.Map;
1623
{{/fullJavaUtil}}
1724

25+
{{#wiremock}}
26+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
27+
{{/wiremock}}
28+
1829
/**
1930
* API tests for {{classname}}
2031
*/
2132
@Ignore
2233
public class {{classname}}Test {
2334
2435
private final {{classname}} api = new {{classname}}();
36+
{{#wiremock}}
37+
private static WireMockServer wireMockServer;
38+
39+
public {{classname}}Test() {
40+
api.getApiClient().setBasePath("http://localhost:" + wireMockServer.port());
41+
}
42+
43+
@BeforeClass
44+
public static void setUp() {
45+
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
46+
wireMockServer.start();
47+
configureFor(wireMockServer.port());
48+
{{#operations}}
49+
{{#operation}}
50+
stubFor({{toLowerCase httpMethod}}(urlPathMatching("{{{path}}}"))
51+
.willReturn(aResponse()
52+
.withStatus(HttpURLConnection.HTTP_OK)));
53+
{{/operation}}
54+
{{/operations}}
55+
}
56+
57+
@AfterClass
58+
public static void tearDown() {
59+
wireMockServer.stop();
60+
}
61+
{{/wiremock}}
2562

2663
{{#operations}}
2764
{{#operation}}
@@ -32,11 +69,11 @@ public class {{classname}}Test {
3269
*
3370
* {{notes}}
3471
*
35-
* @throws ApiException
72+
* @throws Exception
3673
* if the Api call fails
3774
*/
3875
@Test
39-
public void {{operationId}}Test() throws ApiException {
76+
public void {{operationId}}Test() throws Exception {
4077
{{#parameters}}
4178
{{{dataType}}} {{paramName}} = null;
4279
{{/parameters}}

src/main/resources/handlebars/Java/libraries/feign/api_test.mustache

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,58 @@ import {{invokerPackage}}.ApiClient;
66
import org.junit.Before;
77
import org.junit.Test;
88

9+
{{#wiremock}}
10+
import com.github.tomakehurst.wiremock.WireMockServer;
11+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
12+
import java.net.HttpURLConnection;
13+
import org.junit.AfterClass;
14+
{{/wiremock}}
15+
916
{{^fullJavaUtil}}
1017
import java.util.ArrayList;
1118
import java.util.HashMap;
1219
import java.util.List;
1320
import java.util.Map;
1421
{{/fullJavaUtil}}
1522

23+
{{#wiremock}}
24+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
25+
{{/wiremock}}
26+
1627
/**
1728
* API tests for {{classname}}
1829
*/
1930
public class {{classname}}Test {
2031
2132
private {{classname}} api;
2233

34+
{{#wiremock}}
35+
private static WireMockServer wireMockServer;
36+
37+
@Before
38+
public void setup() {
39+
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
40+
wireMockServer.start();
41+
configureFor(wireMockServer.port());
42+
api = new ApiClient().setBasePath("http://localhost:" + wireMockServer.port()).buildClient({{classname}}.class);
43+
{{#operations}}{{#operation}}
44+
stubFor({{toLowerCase httpMethod}}(urlPathMatching("{{{path}}}"))
45+
.willReturn(aResponse()
46+
.withStatus(HttpURLConnection.HTTP_OK)));
47+
{{/operation}}{{/operations}}
48+
}
49+
50+
@AfterClass
51+
public static void tearDown() {
52+
wireMockServer.stop();
53+
}
54+
{{/wiremock}}
55+
{{^wiremock}}
2356
@Before
2457
public void setup() {
2558
api = new ApiClient().buildClient({{classname}}.class);
2659
}
60+
{{/wiremock}}
2761

2862
{{#operations}}{{#operation}}{{#contents}}{{#@first}}
2963
/**

src/main/resources/handlebars/Java/libraries/feign/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@
293293
<version>1.7.1</version>
294294
<scope>test</scope>
295295
</dependency>
296+
{{#wiremock}}
297+
<dependency>
298+
<groupId>com.github.tomakehurst</groupId>
299+
<artifactId>wiremock</artifactId>
300+
<version>2.27.2</version>
301+
<scope>test</scope>
302+
</dependency>
303+
{{/wiremock}}
296304
</dependencies>
297305
<properties>
298306
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>

src/main/resources/handlebars/Java/libraries/jersey2/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@
303303
<version>${junit-version}</version>
304304
<scope>test</scope>
305305
</dependency>
306+
{{#wiremock}}
307+
<dependency>
308+
<groupId>com.github.tomakehurst</groupId>
309+
<artifactId>wiremock</artifactId>
310+
<version>2.27.2</version>
311+
<scope>test</scope>
312+
</dependency>
313+
{{/wiremock}}
306314
</dependencies>
307315
<properties>
308316
{{#useOas2}}

src/main/resources/handlebars/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@
290290
<version>${junit-version}</version>
291291
<scope>test</scope>
292292
</dependency>
293+
{{#wiremock}}
294+
<dependency>
295+
<groupId>com.github.tomakehurst</groupId>
296+
<artifactId>wiremock</artifactId>
297+
<version>2.27.2</version>
298+
<scope>test</scope>
299+
</dependency>
300+
{{/wiremock}}
293301
</dependencies>
294302
<properties>
295303
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>

src/main/resources/handlebars/Java/libraries/resteasy/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@
231231
<version>${junit-version}</version>
232232
<scope>test</scope>
233233
</dependency>
234+
{{#wiremock}}
235+
<dependency>
236+
<groupId>com.github.tomakehurst</groupId>
237+
<artifactId>wiremock</artifactId>
238+
<version>2.27.2</version>
239+
<scope>test</scope>
240+
</dependency>
241+
{{/wiremock}}
234242
</dependencies>
235243
<properties>
236244
{{#useOas2}}

src/main/resources/handlebars/Java/libraries/resttemplate/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@
287287
<version>${junit-version}</version>
288288
<scope>test</scope>
289289
</dependency>
290+
{{#wiremock}}
291+
<dependency>
292+
<groupId>com.github.tomakehurst</groupId>
293+
<artifactId>wiremock</artifactId>
294+
<version>2.27.2</version>
295+
<scope>test</scope>
296+
</dependency>
297+
{{/wiremock}}
290298
</dependencies>
291299
<properties>
292300
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/resources/handlebars/Java/libraries/retrofit/api_test.mustache

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,61 @@ import {{invokerPackage}}.ApiClient;
66
import org.junit.Before;
77
import org.junit.Test;
88

9+
{{#wiremock}}
10+
import com.github.tomakehurst.wiremock.WireMockServer;
11+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
12+
import java.net.HttpURLConnection;
13+
import org.junit.AfterClass;
14+
{{/wiremock}}
15+
916
{{^fullJavaUtil}}
1017
import java.util.ArrayList;
1118
import java.util.HashMap;
1219
import java.util.List;
1320
import java.util.Map;
1421
{{/fullJavaUtil}}
1522

23+
{{#wiremock}}
24+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
25+
{{/wiremock}}
26+
1627
/**
1728
* API tests for {{classname}}
1829
*/
1930
public class {{classname}}Test {
2031
2132
private {{classname}} api;
2233

34+
{{#wiremock}}
35+
private static WireMockServer wireMockServer;
36+
37+
@Before
38+
public void setup() {
39+
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
40+
wireMockServer.start();
41+
configureFor(wireMockServer.port());
42+
ApiClient apiClient = new ApiClient();
43+
apiClient.getAdapterBuilder().setEndpoint("http://localhost:" + wireMockServer.port());
44+
api = apiClient.createService({{classname}}.class);
45+
46+
{{#operations}}{{#operation}}
47+
stubFor({{toLowerCase httpMethod}}(urlPathMatching("{{{path}}}"))
48+
.willReturn(aResponse()
49+
.withStatus(HttpURLConnection.HTTP_OK)));
50+
{{/operation}}{{/operations}}
51+
}
52+
53+
@AfterClass
54+
public static void tearDown() {
55+
wireMockServer.stop();
56+
}
57+
{{/wiremock}}
58+
{{^wiremock}}
2359
@Before
2460
public void setup() {
2561
api = new ApiClient().createService({{classname}}.class);
2662
}
63+
{{/wiremock}}
2764

2865
{{#operations}}{{#operation}}{{#contents}}{{#@first}}
2966
/**

src/main/resources/handlebars/Java/libraries/retrofit/pom.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@
245245
<version>${junit-version}</version>
246246
<scope>test</scope>
247247
</dependency>
248+
{{#wiremock}}
249+
<dependency>
250+
<groupId>com.github.tomakehurst</groupId>
251+
<artifactId>wiremock</artifactId>
252+
<version>2.27.2</version>
253+
<scope>test</scope>
254+
</dependency>
255+
{{/wiremock}}
248256
</dependencies>
249257
<properties>
250258
{{#useOas2}}

0 commit comments

Comments
 (0)