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