Fixing the way StubRunnerBuilder options are built

without this change we're completely ignoring the option of using system properties to set the stub runner builder options
with this change we set it up with defaults from system properties and then we override it with the user's code

fixes #462
This commit is contained in:
Marcin Grzejszczak
2017-11-23 11:53:15 +01:00
parent 0358629a1e
commit 4c073c8a52
4 changed files with 58 additions and 20 deletions

View File

@@ -123,6 +123,26 @@ public class StubRunnerOptions {
}
}
public static StubRunnerOptions fromSystemProps() {
StubRunnerOptionsBuilder builder = new StubRunnerOptionsBuilder()
.withMinPort(Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000")))
.withMaxPort(Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000")))
.withStubRepositoryRoot(System.getProperty("stubrunner.repository.root", ""))
.withWorkOffline(Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false")))
.withStubsClassifier(System.getProperty("stubrunner.classifier", "stubs"))
.withStubs(System.getProperty("stubrunner.ids", ""))
.withUsername(System.getProperty("stubrunner.username"))
.withPassword(System.getProperty("stubrunner.password"))
.withStubPerConsumer(Boolean.parseBoolean(System.getProperty("stubrunner.stubs-per-consumer", "false")))
.withConsumerName(System.getProperty("stubrunner.consumer-name"))
.withMappingsOutputFolder(System.getProperty("stubrunner.mappings-output-folder"));
String proxyHost = System.getProperty("stubrunner.proxy.host");
if (proxyHost != null) {
builder.withProxy(proxyHost, Integer.parseInt(System.getProperty("stubrunner.proxy.port")));
}
return builder.build();
}
public Integer getMinPortValue() {
return this.minPortValue;
}

View File

@@ -45,7 +45,7 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio
private static final String DELIMITER = ":";
private static final String LATEST_VERSION = "+";
StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder(defaultStubRunnerOptions());
StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder(StubRunnerOptions.fromSystemProps());
BatchStubRunner stubFinder;
MessageVerifier verifier = new ExceptionThrowingMessageVerifier();
StubRunnerRule delegate = this;
@@ -70,25 +70,6 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio
};
}
private StubRunnerOptions defaultStubRunnerOptions() {
StubRunnerOptionsBuilder builder = new StubRunnerOptionsBuilder()
.withMinPort(Integer.valueOf(System.getProperty("stubrunner.port.range.min", "10000")))
.withMaxPort(Integer.valueOf(System.getProperty("stubrunner.port.range.max", "15000")))
.withStubRepositoryRoot(System.getProperty("stubrunner.repository.root", ""))
.withWorkOffline(Boolean.parseBoolean(System.getProperty("stubrunner.work-offline", "false")))
.withStubsClassifier(System.getProperty("stubrunner.classifier", "stubs"))
.withStubs(System.getProperty("stubrunner.ids", ""))
.withUsername(System.getProperty("stubrunner.username"))
.withPassword(System.getProperty("stubrunner.password"))
.withStubPerConsumer(Boolean.parseBoolean(System.getProperty("stubrunner.stubsPerConsumer", "false")))
.withConsumerName(System.getProperty("stubrunner.consumer-name"));
String proxyHost = System.getProperty("stubrunner.proxy.host");
if (proxyHost != null) {
builder.withProxy(proxyHost, Integer.parseInt(System.getProperty("stubrunner.proxy.port")));
}
return builder.build();
}
@Override public StubRunnerRule messageVerifier(MessageVerifier messageVerifier) {
verifier(messageVerifier);
return this.delegate;

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.contract.stubrunner
import spock.lang.Issue
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties
class StubRunnerOptionsBuilderSpec extends Specification {
@@ -160,4 +161,39 @@ class StubRunnerOptionsBuilderSpec extends Specification {
options.consumerName == "consumer"
options.mappingsOutputFolder == "folder"
}
@Issue("#462")
@RestoreSystemProperties
def shouldSetAllPropsFromSystemProps() {
given:
System.setProperty("stubrunner.port.range.min", "1")
System.setProperty("stubrunner.port.range.max", "2")
System.setProperty("stubrunner.repository.root", "root")
System.setProperty("stubrunner.work-offline", "true")
System.setProperty("stubrunner.classifier", "classifier")
System.setProperty("stubrunner.ids", "a:b:c,foo:bar:baz:classifier")
System.setProperty("stubrunner.username", "foo")
System.setProperty("stubrunner.password", "bar")
System.setProperty("stubrunner.stubs-per-consumer", "true")
System.setProperty("stubrunner.consumer-name", "consumer")
System.setProperty("stubrunner.proxy.host", "host")
System.setProperty("stubrunner.proxy.port", "4")
System.setProperty("stubrunner.mappings-output-folder", "folder")
when:
StubRunnerOptions options = StubRunnerOptions.fromSystemProps()
then:
options.minPortValue == 1
options.maxPortValue == 2
options.stubRepositoryRoot == "root"
options.workOffline == true
options.stubsClassifier == "classifier"
options.dependencies == [new StubConfiguration("a:b:c"), new StubConfiguration("foo:bar:baz:classifier")]
options.username == "foo"
options.password == "bar"
options.proxyOptions.proxyHost == "host"
options.proxyOptions.proxyPort == 4
options.stubsPerConsumer == true
options.consumerName == "consumer"
options.mappingsOutputFolder == "folder"
}
}

View File

@@ -114,6 +114,7 @@ class MavenContractsDownloader {
StubRunnerOptions buildOptions() {
return new StubRunnerOptionsBuilder()
.withOptions(StubRunnerOptions.fromSystemProps())
.withStubRepositoryRoot(this.contractsRepositoryUrl)
.withWorkOffline(this.contractsWorkOffline)
.build();