Merge branch 'master' into 2.0.x

This commit is contained in:
Marcin Grzejszczak
2017-07-13 19:14:04 +02:00
4 changed files with 185 additions and 22 deletions

View File

@@ -124,7 +124,9 @@ contracts {
}
contractsPath = ''
contractsWorkOffline = false
cacheDownloadedContracts = true
contractRepository {
cacheDownloadedContracts(true)
}
}
tasks.create(type: Jar, name: 'verifierStubsJar', dependsOn: 'generateClientStubs') {
@@ -440,11 +442,16 @@ To change default configuration just add `configuration` section to plugin defin
If you want to download your contract definitions from a Maven repository you can use
- **contractsRepositoryUrl** - URL to a repo with the artifacts with contracts, if not provided should use the current Maven ones
- **contractDependency** - the contract dependency that contains all the packaged contracts
- **contractsPath** - path to concrete contracts in the JAR with packaged contracts. Defaults to `groupid/artifactid` where `gropuid` is slash separated.
- **contractsWorkOffline** - if the dependencies should be downloaded or local Maven only should be reused
- **cacheDownloadedContracts** - if you want to reuse download JARs that contain contract definitions.
- **contractsRepositoryUrl** - *DEPRECATED PROPERTY - please use the `contractRepository` closure* - URL to a repo with the artifacts with contracts, if not provided should use the current Maven ones
- **contractRepository** - closure where you can define properties related to repository with contracts
* **username** - username to be used to connect to the repo
* **password** - username to be used to connect to the repo
* **proxyHost** - proxy host to be used to connect to the repo
* **proxyPort** - proxy port to be used to connect to the repo
* **cacheDownloadedContracts** - if you want to reuse download JARs that contain contract definitions.
We cache only non-snapshot, explicitly provided versions (e.g. `+` or `1.0.0.BUILD-SNAPSHOT` won't get cached).
By default this feature is turned on.

View File

@@ -90,9 +90,22 @@ class ContractVerifierExtension {
* The URL from which a JAR containing the contracts should get downloaded. If not provided
* but artifactid / coordinates notation was provided then the current Maven's build repositories will be
* taken into consideration
*
* @deprecated - use {@link ContractVerifierExtension#contractRepository(groovy.lang.Closure)}
*/
@Deprecated
String contractsRepositoryUrl
/*
* @deprecated - use {@link ContractVerifierExtension#contractRepository(groovy.lang.Closure)}
*/
@Deprecated
void setContractsRepositoryUrl(String contractsRepositoryUrl) {
this.contractRepository.repositoryUrl(contractsRepositoryUrl)
}
ContractRepository contractRepository = new ContractRepository()
/**
* Dependency that contains packaged contracts
*/
@@ -141,12 +154,6 @@ class ContractVerifierExtension {
*/
boolean excludeBuildFolders
/**
* If set to true then will cache the folder where non snapshot contract artifacts
* got downloaded.
*/
boolean cacheDownloadedContracts = true
void contractDependency(@DelegatesTo(Dependency) Closure closure) {
closure.delegate = contractDependency
closure.call()
@@ -157,6 +164,11 @@ class ContractVerifierExtension {
closure.call()
}
void contractRepository(@DelegatesTo(ContractRepository) Closure closure) {
closure.delegate = contractRepository
closure.call()
}
@ToString
static class Dependency {
String groupId
@@ -201,4 +213,61 @@ class ContractVerifierExtension {
this.delegate.putAll(mapping)
}
}
static class ContractRepository {
/**
* Repository URL
*/
String repositoryUrl
/**
* Repository username
*/
String username
/**
* Repository password
*/
String password
/**
* Repository proxy port
*/
Integer proxyPort
/**
* Repository proxy host
*/
String proxyHost
/**
* If set to true then will cache the folder where non snapshot contract artifacts
* got downloaded.
*/
boolean cacheDownloadedContracts = true
void repositoryUrl(String repositoryUrl) {
this.repositoryUrl = repositoryUrl
}
void username(String username) {
this.username = username
}
void password(String password) {
this.password = password
}
void proxyPort(Integer proxyPort) {
this.proxyPort = proxyPort
}
void proxyHost(String proxyHost) {
this.proxyHost = proxyHost
}
void cacheDownloadedContracts(boolean cacheDownloadedContracts) {
this.cacheDownloadedContracts = cacheDownloadedContracts
}
}
}

View File

@@ -9,6 +9,7 @@ import org.springframework.cloud.contract.stubrunner.ContractDownloader
import org.springframework.cloud.contract.stubrunner.StubConfiguration
import org.springframework.cloud.contract.stubrunner.StubDownloader
import org.springframework.cloud.contract.stubrunner.StubDownloaderBuilderProvider
import org.springframework.cloud.contract.stubrunner.StubRunnerOptions
import org.springframework.cloud.contract.stubrunner.StubRunnerOptionsBuilder
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
import org.springframework.util.StringUtils
@@ -43,8 +44,8 @@ class GradleContractsDownloader {
StubConfiguration configuration = stubConfiguration(extension.contractDependency)
this.log.info("Got the following contract dependency to download [{}]", configuration)
this.log.info("The contract dependency is a changing one [{}] and cache download switch is set to [{}]",
configuration.isVersionChanging(), extension.cacheDownloadedContracts)
if (!configuration.isVersionChanging() && extension.cacheDownloadedContracts) {
configuration.isVersionChanging(), extension.contractRepository.cacheDownloadedContracts)
if (!configuration.isVersionChanging() && extension.contractRepository.cacheDownloadedContracts) {
this.log.info("Resolved a non changing version - will try to return the folder from a cache")
File cachedFolder = downloadedContract.get(configuration)
if (cachedFolder) {
@@ -62,7 +63,7 @@ class GradleContractsDownloader {
}
private boolean shouldDownloadContracts(ContractVerifierExtension extension) {
return (StringUtils.hasText(extension.contractsRepositoryUrl) || extension.contractsWorkOffline) &&
return (StringUtils.hasText(extension.contractRepository.repositoryUrl) || extension.contractsWorkOffline) &&
(StringUtils.hasText(extension.contractDependency.artifactId) ||
StringUtils.hasText(extension.contractDependency.stringNotation))
}
@@ -73,12 +74,16 @@ class GradleContractsDownloader {
}
protected StubDownloader stubDownloader(ContractVerifierExtension extension) {
StubDownloaderBuilderProvider provider = new StubDownloaderBuilderProvider()
return provider.getOrDefaultDownloader(
new StubRunnerOptionsBuilder()
.withStubRepositoryRoot(extension.contractsRepositoryUrl)
.withWorkOffline(extension.contractsWorkOffline)
.build())
StubDownloaderBuilderProvider provider = new StubDownloaderBuilderProvider()
StubRunnerOptionsBuilder options = new StubRunnerOptionsBuilder()
.withStubRepositoryRoot(extension.contractRepository.repositoryUrl)
.withWorkOffline(extension.contractsWorkOffline)
.withUsername(extension.contractRepository.username)
.withPassword(extension.contractRepository.password)
if (extension.contractRepository.proxyPort) {
options = options.withProxy(extension.contractRepository.proxyHost, extension.contractRepository.proxyPort)
}
return provider.getOrDefaultDownloader(options.build())
}
@PackageScope StubConfiguration stubConfiguration(ContractVerifierExtension.Dependency contractDependency) {

View File

@@ -76,7 +76,7 @@ class GradleContractsDownloaderSpec extends Specification {
stubConfig.classifier == "stubs"
}
def "should pick dependency from cache for a non snapshot contract dependency"() {
def "should pick dependency from cache for a non snapshot contract dependency with old property"() {
given:
ContractVerifierExtension ext = new ContractVerifierExtension()
ext.with {
@@ -103,6 +103,35 @@ class GradleContractsDownloaderSpec extends Specification {
file == expectedFileFromCache
}
def "should pick dependency from cache for a non snapshot contract dependency with new property"() {
given:
ContractVerifierExtension ext = new ContractVerifierExtension()
ext.with {
contractDependency {
groupId("com.example")
artifactId("foo")
version("1.0.0")
classifier("stubs")
}
contractRepository {
repositoryUrl("foo")
}
}
and:
final AetherStubDownloader downloader = Mock(AetherStubDownloader)
final ContractDownloader contractDownloader = Mock(ContractDownloader)
and:
def gradleDownloader = stubbedContractDownloader(downloader, contractDownloader)
and:
StubConfiguration expectedStubConfig = new StubConfiguration("com.example:foo:1.0.0:stubs")
File expectedFileFromCache = new File("foo/bar")
GradleContractsDownloader.downloadedContract.put(expectedStubConfig, expectedFileFromCache)
when:
File file = gradleDownloader.downloadAndUnpackContractsIfRequired(ext, new ContractVerifierConfigProperties())
then:
file == expectedFileFromCache
}
def "should not pick dependency from cache for a non snapshot contract dependency with cache switch off"() {
given:
ContractVerifierExtension ext = new ContractVerifierExtension()
@@ -113,8 +142,10 @@ class GradleContractsDownloaderSpec extends Specification {
version("1.0.0")
classifier("stubs")
}
contractsRepositoryUrl = "foo"
cacheDownloadedContracts = false
contractRepository {
repositoryUrl("foo")
cacheDownloadedContracts(false)
}
}
and:
final AetherStubDownloader downloader = Mock(AetherStubDownloader)
@@ -144,7 +175,9 @@ class GradleContractsDownloaderSpec extends Specification {
version("1.0.0.BUILD-SNAPSHOT")
classifier("stubs")
}
contractsRepositoryUrl = "foo"
contractRepository {
repositoryUrl("foo")
}
}
and:
final AetherStubDownloader downloader = Mock(AetherStubDownloader)
@@ -193,4 +226,53 @@ class GradleContractsDownloaderSpec extends Specification {
file == new File("/foo/bar/baz")
}
def "should pass contract dependency properties as a parameter to the builder"() {
given:
ContractVerifierExtension ext = new ContractVerifierExtension()
ext.with {
contractDependency {
groupId("com.example")
artifactId("foo")
version("1.0.0.BUILD-SNAPSHOT")
classifier("stubs")
}
contractRepository {
repositoryUrl("foo")
username("foo1")
password("foo2")
proxyHost("foo3")
proxyPort(12)
}
}
and:
final AetherStubDownloader downloader = Mock(AetherStubDownloader)
final ContractDownloader contractDownloader = Mock(ContractDownloader)
File expectedFileNotFromCache = new File("foo/bar/baz")
contractDownloader.unpackedDownloadedContracts(_) >> expectedFileNotFromCache
and:
def gradleDownloader = assertingContractDownloader(downloader, contractDownloader)
when:
gradleDownloader.downloadAndUnpackContractsIfRequired(ext, new ContractVerifierConfigProperties())
then:
noExceptionThrown()
}
private GradleContractsDownloader assertingContractDownloader(downloader, contractDownloader) {
new GradleContractsDownloader(project, logger) {
@Override
protected AetherStubDownloader stubDownloader(ContractVerifierExtension extension) {
assert extension.contractRepository.username == "foo1"
assert extension.contractRepository.password == "foo2"
assert extension.contractRepository.proxyHost == "foo3"
assert extension.contractRepository.proxyPort == 12
return downloader
}
@Override
protected ContractDownloader contractDownloader(ContractVerifierExtension extension, StubConfiguration configuration) {
return contractDownloader
}
}
}
}