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