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  import java.util.HashMap;
21  
22  import org.apache.maven.model.Dependency;
23  import org.apache.maven.plugin.testing.SilentLog;
24  import org.apache.maven.project.MavenProject;
25  import org.assertj.core.api.BDDAssertions;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.io.TempDir;
28  
29  import org.springframework.cloud.contract.stubrunner.ContractDownloader;
30  import org.springframework.cloud.contract.stubrunner.InclusionPropertiesAccessor;
31  import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
32  import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
33  
34  class MavenContractsDownloaderTests {
35  
36  	@TempDir
37  	File defaultFolder;
38  
39  	@TempDir
40  	File fileForDependencyOne;
41  
42  	@TempDir
43  	File fileForDependencyTwo;
44  
45  	@Test
46  	void shouldNotReadFolderFromCacheWhenExecutedTwiceFor2DifferentArtifactsInTheSameProject() {
47  		MavenProject mavenProject = new MavenProject();
48  
49  		Dependency one = dependency(1);
50  		MavenContractsDownloader mavenContractsDownloader = contractsDownloader(mavenProject, one,
51  				this.fileForDependencyOne);
52  		File dependencyOneFile = mavenContractsDownloader
53  				.downloadAndUnpackContractsIfRequired(new ContractVerifierConfigProperties(), this.defaultFolder);
54  		BDDAssertions.then(dependencyOneFile).as("Location for dependency 1 should be computed since it's not cached")
55  				.isEqualTo(this.fileForDependencyOne);
56  
57  		mavenContractsDownloader = contractsDownloader(mavenProject, one, this.fileForDependencyOne);
58  		File fileForDependencyOneAgain = mavenContractsDownloader
59  				.downloadAndUnpackContractsIfRequired(new ContractVerifierConfigProperties(), this.defaultFolder);
60  		BDDAssertions.then(dependencyOneFile).as("Location for dependency 1 should be taken from cache")
61  				.isEqualTo(fileForDependencyOneAgain);
62  
63  		Dependency two = dependency(2);
64  		mavenContractsDownloader = contractsDownloader(mavenProject, two, this.fileForDependencyTwo);
65  		File dependencyTwoFile = mavenContractsDownloader
66  				.downloadAndUnpackContractsIfRequired(new ContractVerifierConfigProperties(), this.defaultFolder);
67  
68  		BDDAssertions.then(dependencyTwoFile).as("Location for dependency 2 should be computed again")
69  				.isNotEqualTo(dependencyOneFile).isEqualTo(this.fileForDependencyTwo);
70  	}
71  
72  	private MavenContractsDownloader contractsDownloader(MavenProject mavenProject, Dependency one, File file) {
73  		return new MavenContractsDownloader(mavenProject, one, "", "", StubRunnerProperties.StubsMode.LOCAL,
74  				new SilentLog(), "", "", "", null, false, new HashMap<>(), false) {
75  			@Override
76  			ContractDownloader contractDownloader() {
77  				return new ContractDownloader(null, null, null, null, null, null) {
78  
79  					int counterForDependencyOne;
80  
81  					@Override
82  					public File unpackAndDownloadContracts() {
83  						if (file == fileForDependencyOne) {
84  							this.counterForDependencyOne = this.counterForDependencyOne + 1;
85  						}
86  						if (file == fileForDependencyOne && this.counterForDependencyOne == 2) {
87  							throw new AssertionError("Second call for dependency 1 should come from cache");
88  						}
89  						return file;
90  					}
91  
92  					@Override
93  					public InclusionProperties createNewInclusionProperties(File contractsDirectory) {
94  						return new InclusionPropertiesAccessor("a", "b");
95  					}
96  				};
97  			}
98  		};
99  	}
100 
101 	private Dependency dependency(int number) {
102 		Dependency dependency = new Dependency();
103 		dependency.setGroupId("a" + number);
104 		dependency.setArtifactId("b" + number);
105 		dependency.setVersion("c" + number);
106 		dependency.setClassifier("d" + number);
107 		return dependency;
108 	}
109 
110 }