1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.cloud.contract.maven.verifier;
18
19 import java.io.File;
20
21 import org.codehaus.plexus.util.xml.Xpp3Dom;
22 import org.junit.Ignore;
23 import org.junit.Test;
24
25 import org.springframework.util.StringUtils;
26
27 import static java.nio.charset.Charset.defaultCharset;
28 import static org.apache.commons.io.FileUtils.readFileToString;
29 import static org.apache.maven.plugin.testing.MojoParameters.newParameter;
30 import static org.assertj.core.api.BDDAssertions.then;
31
32
33 @Ignore("Testing harness uses an old version of maven")
34 public class PluginUnitTest extends AbstractMojoTest {
35
36 @Test
37 public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception {
38 File basedir = getBasedir("basic");
39 executeMojo(basedir, "convert");
40 assertFilesPresent(basedir,
41 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json"
42 .replace("/", File.separator));
43 assertFilesNotPresent(basedir,
44 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Messaging.json"
45 .replace("/", File.separator));
46 }
47
48 private Xpp3Dom defaultPackageForTests() {
49 return newParameter("basePackageForTests", "org.springframework.cloud.contract.verifier.tests");
50 }
51
52 @Test
53 public void shouldGenerateWireMockFromStubsDirectory() throws Exception {
54 File basedir = getBasedir("withStubs");
55 executeMojo(basedir, "convert", newParameter("contractsDirectory", "src/test/resources/stubs"));
56 assertFilesPresent(basedir,
57 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json"
58 .replace("/", File.separator));
59 }
60
61 @Test
62 public void shouldCopyContracts() throws Exception {
63 File basedir = getBasedir("basic");
64 executeMojo(basedir, "convert");
65 assertFilesPresent(basedir,
66 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/contracts/Sample.groovy"
67 .replace("/", File.separator));
68 assertFilesPresent(basedir,
69 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/contracts/Messaging.groovy"
70 .replace("/", File.separator));
71 }
72
73 @Test
74 public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception {
75 File basedir = getBasedir("basic");
76 executeMojo(basedir, "convert", newParameter("stubsDirectory", "target/foo"));
77 assertFilesPresent(basedir,
78 "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json");
79 }
80
81 @Test
82 public void shouldGenerateContractSpecificationInDefaultLocation() throws Exception {
83 File basedir = getBasedir("basic");
84 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
85 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierSpec.groovy";
86 assertFilesPresent(basedir, path);
87 File test = new File(basedir, path);
88 then(readFileToString(test, defaultCharset())).contains("spock.lang.Ignore");
89 }
90
91 @Test
92 public void shouldGenerateContractTestsInDefaultLocation() throws Exception {
93 File basedir = getBasedir("basic");
94 executeMojo(basedir, "generateTests", defaultPackageForTests());
95 assertFilesPresent(basedir,
96 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
97 }
98
99 @Test
100 public void shouldGenerateContractTestsWithCustomImports() throws Exception {
101 File basedir = getBasedir("basic");
102 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("imports", ""));
103 assertFilesPresent(basedir,
104 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
105 }
106
107 @Test
108 public void shouldGenerateContractTestsWithoutArraySize() throws Exception {
109 File basedir = getBasedir("basic");
110 executeMojo(basedir, "generateTests", defaultPackageForTests());
111 assertFilesPresent(basedir,
112 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
113 File test = new File(basedir,
114 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
115 then(readFileToString(test, defaultCharset())).doesNotContain("hasSize(4)");
116 }
117
118 @Test
119 public void shouldGenerateContractTestsWithArraySize() throws Exception {
120 File basedir = getBasedir("basic");
121 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("assertJsonSize", "true"));
122 assertFilesPresent(basedir,
123 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
124 File test = new File(basedir,
125 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
126 then(readFileToString(test, defaultCharset())).contains("hasSize(4)");
127 }
128
129 @Test
130 public void shouldGenerateStubs() throws Exception {
131 File basedir = getBasedir("generatedStubs");
132 executeMojo(basedir, "generateStubs");
133 assertFilesPresent(basedir, "target/sample-project-stubs.jar");
134 }
135
136 @Test
137 public void shouldGenerateStubsWithMappingsOnly() throws Exception {
138 File basedir = getBasedir("generatedStubs");
139 executeMojo(basedir, "generateStubs");
140 assertFilesPresent(basedir, "target/sample-project-stubs.jar");
141
142 }
143
144 @Test
145 public void shouldGenerateStubsWithCustomClassifier() throws Exception {
146 File basedir = getBasedir("generatedStubs");
147 executeMojo(basedir, "generateStubs", newParameter("classifier", "foo"));
148 assertFilesPresent(basedir, "target/sample-project-foo.jar");
149 }
150
151 @Test
152 public void shouldGenerateStubsByDownloadingContractsFromARepo() throws Exception {
153 File basedir = getBasedir("basic-remote-contracts");
154 executeMojo(basedir, "convert", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class
155 .getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
156 assertFilesPresent(basedir,
157 "target/stubs/META-INF/com.example/server/0.1.BUILD-SNAPSHOT/mappings/com/example/server/client1/contracts/shouldMarkClientAsFraud.json");
158 }
159
160 @Test
161 public void shouldGenerateStubsByDownloadingContractsFromARepoWhenCustomPathIsProvided() throws Exception {
162 File basedir = getBasedir("complex-remote-contracts");
163 executeMojo(basedir, "convert", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class
164 .getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
165 assertFilesPresent(basedir,
166 "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/mappings/com/example/server/client1/contracts/shouldMarkClientAsFraud.json");
167 assertFilesNotPresent(basedir,
168 "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/mappings/com/foo/bar/baz/shouldBeIgnoredByPlugin.json");
169 assertFilesNotPresent(basedir,
170 "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/contracts/com/foo/bar/baz/shouldBeIgnoredByPlugin.groovy");
171 }
172
173 @Test
174 public void shouldGenerateOutputWhenCalledConvertFromRootProject() throws Exception {
175 File basedir = getBasedir("different-module-configuration");
176 executeMojo(basedir, "convert");
177 assertFilesPresent(basedir,
178 "target/stubs/META-INF/com.blogspot.toomuchcoding.frauddetection/frauddetection-parent/0.1.0/mappings/shouldMarkClientAsFraud.json");
179 }
180
181 @Test
182 public void shouldGenerateOutputWhenCalledGenerateTestsFromRootProject() throws Exception {
183 File basedir = getBasedir("different-module-configuration");
184 executeMojo(basedir, "generateTests", defaultPackageForTests());
185 assertFilesPresent(basedir,
186 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
187 }
188
189 @Test
190 public void shouldGenerateTestsByDownloadingContractsFromARepo() throws Exception {
191 File basedir = getBasedir("basic-remote-contracts");
192 executeMojo(basedir, "generateTests", defaultPackageForTests(),
193 newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader()
194 .getResource("m2repo/repository").getFile().replace("/", File.separator)));
195 assertFilesPresent(basedir,
196 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/example/server/client1/ContractsTest.java");
197 }
198
199 @Test
200 public void shouldGenerateTestsByDownloadingContractsFromARepoWhenCustomPathIsProvided() throws Exception {
201 File basedir = getBasedir("complex-remote-contracts");
202 executeMojo(basedir, "generateTests", defaultPackageForTests(),
203 newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader()
204 .getResource("m2repo/repository").getFile().replace("/", File.separator)));
205 assertFilesPresent(basedir,
206 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/example/server/client1/ContractsTest.java");
207 assertFilesNotPresent(basedir,
208 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/foo/bar/BazTest.java");
209 assertFilesNotPresent(basedir, "target/stubs/contracts/com/foo/bar/baz/shouldBeIgnoredByPlugin.groovy");
210 }
211
212 @Test
213 public void shouldGenerateContractTestsWithBaseClassResolvedFromConvention() throws Exception {
214 File basedir = getBasedir("basic-generated-baseclass");
215
216 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
217
218 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/hello/V1Test.java";
219 assertFilesPresent(basedir, path);
220 File test = new File(basedir, path);
221 then(readFileToString(test, defaultCharset())).contains("extends HelloV1Base")
222 .contains("import hello.HelloV1Base");
223 }
224
225 @Test
226 public void shouldGenerateContractTestsWithBaseClassResolvedFromConventionForSpock() throws Exception {
227 File basedir = getBasedir("basic-generated-baseclass");
228
229 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
230
231 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/hello/V1Spec.groovy";
232 assertFilesPresent(basedir, path);
233 File test = new File(basedir, path);
234 then(readFileToString(test, defaultCharset())).contains("extends HelloV1Base")
235 .contains("import hello.HelloV1Base");
236 }
237
238 @Test
239 public void shouldGenerateContractTestsWithBaseClassResolvedFromMapping() throws Exception {
240 File basedir = getBasedir("basic-baseclass-from-mappings");
241
242 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
243
244 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Test.java";
245 assertFilesPresent(basedir, path);
246 File test = new File(basedir, path);
247 then(readFileToString(test, defaultCharset())).contains("extends TestBase")
248 .contains("import com.example.TestBase");
249 }
250
251 @Test
252 public void shouldGenerateContractTestsWithBaseClassResolvedFromMappingNameForSpock() throws Exception {
253 File basedir = getBasedir("basic-baseclass-from-mappings");
254
255 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
256
257 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Spec.groovy";
258 assertFilesPresent(basedir, path);
259 File test = new File(basedir, path);
260 then(readFileToString(test, defaultCharset())).contains("extends TestBase")
261 .contains("import com.example.TestBase");
262 }
263
264 @Test
265 public void shouldGenerateContractTestsWithAFileContainingAListOfContracts() throws Exception {
266 File basedir = getBasedir("multiple-contracts");
267
268 executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
269
270 String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Test.java";
271 assertFilesPresent(basedir, path);
272 File test = new File(basedir, path);
273 then(readFileToString(test, defaultCharset()))
274 .contains("public void validate_should_post_a_user() throws Exception {")
275 .contains("public void validate_withList_1() throws Exception {");
276 }
277
278 @Test
279 public void shouldGenerateStubsWithAFileContainingAListOfContracts() throws Exception {
280 File basedir = getBasedir("multiple-contracts");
281
282 executeMojo(basedir, "convert", newParameter("stubsDirectory", "target/foo"));
283
284 String firstFile = "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/com/hello/v1/should post a user.json";
285 File test = new File(basedir, firstFile);
286 assertFilesPresent(basedir,
287 "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/com/hello/v1/WithList_1.json");
288 then(readFileToString(test, defaultCharset())).contains("/users/1");
289 String secondFile = "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/com/hello/v1/WithList_1.json";
290 File test2 = new File(basedir, secondFile);
291 assertFilesPresent(basedir,
292 "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/com/hello/v1/should post a user.json");
293 then(readFileToString(test2, defaultCharset())).contains("/users/2");
294 }
295
296 @Test
297 public void shouldGenerateStubsForCommonRepoWithTargetFolder() throws Exception {
298 File basedir = getBasedir("common-repo");
299
300 executeMojo(basedir, "convert");
301
302 assertFilesNotPresent(basedir, "target/generated-test-sources/contracts/");
303
304 assertFilesPresent(basedir,
305 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/common-repo/0.1/mappings/consumer1");
306 assertFilesPresent(basedir,
307 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/common-repo/0.1/contracts/consumer1/Messaging.groovy");
308 assertFilesPresent(basedir,
309 "target/stubs/META-INF/org.springframework.cloud.verifier.sample/common-repo/0.1/contracts/pom.xml");
310 }
311
312 @Test
313 public void shouldGenerateContractTestsForPactAndMaintainIndents() throws Exception {
314 File basedir = getBasedir("pact");
315
316 executeMojo(basedir, "generateTests", defaultPackageForTests());
317
318 assertFilesPresent(basedir,
319 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
320 File test = new File(basedir,
321 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
322 String testContents = readFileToString(test, defaultCharset());
323 int countOccurrencesOf = StringUtils.countOccurrencesOf(testContents, "\t\tMockMvcRequestSpecification");
324 then(countOccurrencesOf).isEqualTo(4);
325 }
326
327 @Test
328 public void shouldRunPushStubsToScm() throws Exception {
329 File basedir = getBasedir("git-basic-remote-contracts");
330
331 executeMojo(basedir, "pushStubsToScm");
332
333 then(this.capture.toString()).contains("Skipping pushing stubs to scm since your");
334 }
335
336 @Test
337 public void shouldGenerateContractTestsForIncludedFilesPattern() throws Exception {
338 File basedir = getBasedir("complex-common-repo-with-messaging");
339
340 executeMojo(basedir, "generateTests", defaultPackageForTests(),
341 newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader()
342 .getResource("m2repo/repository").getFile().replace("/", File.separator)));
343 assertFilesPresent(basedir,
344 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/common_repo_with_inclusion/kafka_topics/coupon_sent/src/main/resources/contracts/rule_engine_daemon/MessagingTest.java");
345 assertFilesPresent(basedir,
346 "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/common_repo_with_inclusion/reward_rules/src/main/resources/contracts/reward_rules/rest/admin/V1Test.java");
347 }
348
349 }