Migrate stubrunner config to @ConfigurationProperties

This commit is contained in:
Dave Syer
2016-07-18 09:36:48 +01:00
parent a4371b1d9f
commit ac873f9c82
9 changed files with 178 additions and 44 deletions

View File

@@ -61,17 +61,17 @@ include::{stubrunner_core_path}/spring-cloud-contract-stub-runner-spring-cloud/R
==== Common properties for JUnit and Spring
Some of the properties that are repetitive can be set using system properties or property sources (for Spring). Here are their names with their default values:
Some of the properties that are repetitive can be set using system properties or configuration properties (for Spring). Here are their names with their default values:
[frame="topbot",options="header"]
|======================
| Property name | Default value | Description
|stubrunner.port.range.min|10000| Minimal value of a port for a started WireMock with stubs
|stubrunner.port.range.max|15000| Minimal value of a port for a started WireMock with stubs
|stubrunner.stubs.repository.root|| Comma separated list of Maven repo urls. If blank then will call the local maven repo
|stubrunner.minPort|10000| Minimal value of a port for a started WireMock with stubs
|stubrunner.maxPort|15000| Minimal value of a port for a started WireMock with stubs
|stubrunner.stubs.repositoryRoot|| Maven repo url. If blank then will call the local maven repo
|stubrunner.stubs.classifier|stubs| Default classifier for the stub artifacts
|stubrunner.work-offline|false| If true then will not contact any remote repositories to download stubs
|stubrunner.stubs.ids|| Comma separated list of Ivy notation of stubs to download
|stubrunner.workOffline|false| If true then will not contact any remote repositories to download stubs
|stubrunner.stubs.ids|| Array of Ivy notation stubs to download
|======================
===== Stub runner stubs ids

View File

@@ -1,2 +1,2 @@
stubrunner.stubs.repository.root: classpath:m2repo/repository/
stubrunner.stubs.repositoryRoot: classpath:m2repo/repository/
stubrunner.stubs.ids: org.springframework.cloud.contract.verifier.stubs:camelService

View File

@@ -1,2 +1,2 @@
stubrunner.stubs.repository.root: classpath:m2repo/repository/
stubrunner.stubs.repositoryRoot: classpath:m2repo/repository/
stubrunner.stubs.ids: org.springframework.cloud.contract.verifier.stubs:integrationService:0.0.1-SNAPSHOT

View File

@@ -1,4 +1,4 @@
stubrunner.stubs.repository.root: classpath:m2repo/repository/
stubrunner.stubs.repositoryRoot: classpath:m2repo/repository/
stubrunner.stubs.ids: org.springframework.cloud.contract.verifier.stubs:streamService:0.0.1-SNAPSHOT:stubs
spring:

View File

@@ -40,6 +40,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-spec</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.contract.stubrunner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.cloud.contract.stubrunner.util.StubsParser;
import org.springframework.util.StringUtils;
public class StubRunnerOptionsBuilder {
@@ -44,7 +45,7 @@ public class StubRunnerOptionsBuilder {
withOptions(options);
}
public StubRunnerOptionsBuilder withStubs(String stubs) {
public StubRunnerOptionsBuilder withStubs(String... stubs) {
addStub(stubsToList(stubs));
return this;
}
@@ -112,8 +113,12 @@ public class StubRunnerOptionsBuilder {
return StubsParser.fromString(stubs, stubsClassifier);
}
private static List<String> stubsToList(String stubIdsToPortMapping) {
return Arrays.asList(stubIdsToPortMapping.split(","));
private static List<String> stubsToList(String[] stubIdsToPortMapping) {
List<String> list = new ArrayList<>();
for (String stub : stubIdsToPortMapping) {
list.addAll(StringUtils.commaDelimitedListToSet(stub));
}
return list;
}
private void addStub(List<String> notations) {

View File

@@ -19,8 +19,8 @@ package org.springframework.cloud.contract.stubrunner.spring;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.contract.stubrunner.AetherStubDownloader;
import org.springframework.cloud.contract.stubrunner.BatchStubRunner;
import org.springframework.cloud.contract.stubrunner.BatchStubRunnerFactory;
@@ -35,45 +35,49 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
/**
* Configuration that initializes a {@link BatchStubRunner} that runs {@link StubRunner} instance for each stub
* Configuration that initializes a {@link BatchStubRunner} that runs {@link StubRunner}
* instance for each stub
*/
@Configuration
@ConditionalOnMissingBean(type="org.springframework.cloud.contract.wiremock.WiremockServerConfiguration")
@EnableConfigurationProperties(StubRunnerProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.cloud.contract.wiremock.WiremockServerConfiguration")
public class StubRunnerConfiguration {
@Autowired(required = false) ContractVerifierMessaging<?,?> contractVerifierMessaging;
@Autowired(required = false) StubDownloader stubDownloader;
@Autowired(required = false)
private ContractVerifierMessaging<?, ?> contractVerifierMessaging;
@Autowired(required = false)
private StubDownloader stubDownloader;
@Autowired
private StubRunnerProperties props;
/**
* Bean that initializes stub runners, runs them and on shutdown closes them. Upon its instantiation
* JAR with stubs is downloaded and unpacked to a temporary folder and WireMock server are started
* for each of those stubs
* Bean that initializes stub runners, runs them and on shutdown closes them. Upon its
* instantiation JAR with stubs is downloaded and unpacked to a temporary folder and
* WireMock server are started for each of those stubs
*
* @param minPortValue min port value of the WireMock instance for stubs
* @param maxPortValue max port value of the WireMock instance for stubs
* @param stubRepositoryRoot root URL from where the JAR with stub mappings will be downloaded
* @param stubsSuffix classifier for the jar containing stubs
* @param workOffline forces offline work
* @param stubs comma separated list of stubs presented in Ivy notation
* @param minPortValue min port value of the WireMock instance for stubs
* @param maxPortValue max port value of the WireMock instance for stubs
* @param stubRepositoryRoot root URL from where the JAR with stub mappings will be
* downloaded
* @param stubsSuffix classifier for the jar containing stubs
* @param workOffline forces offline work
* @param stubs comma separated list of stubs presented in Ivy notation
*/
@Bean
public BatchStubRunner batchStubRunner(
@Value("${stubrunner.port.range.min:10000}") Integer minPortValue,
@Value("${stubrunner.port.range.max:15000}") Integer maxPortValue,
@Value("${stubrunner.stubs.repository.root:}") Resource stubRepositoryRoot,
@Value("${stubrunner.stubs.classifier:stubs}") String stubsSuffix,
@Value("${stubrunner.work-offline:false}") boolean workOffline,
@Value("${stubrunner.stubs.ids:}") String stubs) throws IOException {
public BatchStubRunner batchStubRunner() throws IOException {
StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder()
.withMinMaxPort(minPortValue, maxPortValue)
.withStubRepositoryRoot(uriStringOrEmpty(stubRepositoryRoot))
.withWorkOffline(stubRepositoryRoot == null || workOffline)
.withStubsClassifier(stubsSuffix)
.withStubs(stubs)
.build();
.withMinMaxPort(props.getMinPort(), props.getMaxPort())
.withStubRepositoryRoot(
uriStringOrEmpty(props.getStubs().getRepositoryRoot()))
.withWorkOffline(props.getStubs().getRepositoryRoot() == null
|| props.isWorkOffline())
.withStubsClassifier(props.getStubs().getClassifier())
.withStubs(props.getStubs().getIds()).build();
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions,
stubDownloader != null ? stubDownloader : new AetherStubDownloader(stubRunnerOptions),
contractVerifierMessaging != null ? contractVerifierMessaging : new NoOpContractVerifierMessaging()).buildBatchStubRunner();
stubDownloader != null ? stubDownloader
: new AetherStubDownloader(stubRunnerOptions),
contractVerifierMessaging != null ? contractVerifierMessaging
: new NoOpContractVerifierMessaging()).buildBatchStubRunner();
// TODO: Consider running it in a separate thread
batchStubRunner.runStubs();
return batchStubRunner;

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.stubrunner.spring;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
/**
* @author Dave Syer
*
*/
@ConfigurationProperties("stubrunner")
public class StubRunnerProperties {
/**
*
*/
private int minPort = 10000;
/**
*
*/
private int maxPort = 15000;
/**
*
*/
private boolean workOffline;
private Stubs stubs = new Stubs();
public int getMinPort() {
return minPort;
}
public void setMinPort(int minPort) {
this.minPort = minPort;
}
public int getMaxPort() {
return maxPort;
}
public void setMaxPort(int maxPort) {
this.maxPort = maxPort;
}
public boolean isWorkOffline() {
return workOffline;
}
public void setWorkOffline(boolean workOffline) {
this.workOffline = workOffline;
}
public Stubs getStubs() {
return stubs;
}
public void setStubs(Stubs stubs) {
this.stubs = stubs;
}
public static class Stubs {
/**
* The repository root to use (defaults to local Maven repo).
*/
private Resource repositoryRoot;
/**
* The ids of the stubs to run in "ivy" notation (groupId:artifactId[:classifier]:version[:port]).
*/
private String[] ids = new String[0];
/**
* The classifier to use by default in ivy co-ordinates for a stub.
*/
private String classifier = "stubs";
public Resource getRepositoryRoot() {
return repositoryRoot;
}
public void setRepositoryRoot(Resource repositoryRoot) {
this.repositoryRoot = repositoryRoot;
}
public String[] getIds() {
return ids;
}
public void setIds(String[] ids) {
this.ids = ids;
}
public String getClassifier() {
return classifier;
}
public void setClassifier(String classifier) {
this.classifier = classifier;
}
}
}

View File

@@ -1,5 +1,8 @@
stubrunner.stubs.repository.root: classpath:m2repo/repository/
stubrunner.stubs.ids: org.springframework.cloud.contract.verifier.stubs:loanIssuance,org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer,org.springframework.cloud.contract.verifier.stubs:bootService
stubrunner.stubs.repositoryRoot: classpath:m2repo/repository/
stubrunner.stubs.ids:
- org.springframework.cloud.contract.verifier.stubs:loanIssuance
- org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer
- org.springframework.cloud.contract.verifier.stubs:bootService
stubrunner.stubs.idsToServiceIds:
ivyNotation: someValueInsideYourCode