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