StubRunner and a random port

added additional resolvers for the properties via environment

fixes gh-147
This commit is contained in:
Marcin Grzejszczak
2019-09-17 17:11:38 +02:00
parent 775af906fd
commit f336751ee1
6 changed files with 131 additions and 13 deletions

View File

@@ -161,11 +161,17 @@ public class StubRunnerOptionsBuilder {
public StubRunnerOptionsBuilder withStubsMode(
StubRunnerProperties.StubsMode stubsMode) {
if (stubsMode == null) {
return this;
}
this.stubsMode = stubsMode;
return this;
}
public StubRunnerOptionsBuilder withStubsMode(String stubsMode) {
if (stubsMode == null) {
return this;
}
this.stubsMode = StubRunnerProperties.StubsMode.valueOf(stubsMode);
return this;
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.stubrunner.spring;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -78,7 +79,7 @@ public class StubRunnerConfiguration {
if (this.props.getProxyHost() != null) {
builder.withProxy(this.props.getProxyHost(), this.props.getProxyPort());
}
StubRunnerOptions stubRunnerOptions = builder.build();
StubRunnerOptions stubRunnerOptions = stubRunnerOptions(builder);
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions,
this.provider.get(stubRunnerOptions),
this.contractVerifierMessaging != null ? this.contractVerifierMessaging
@@ -89,26 +90,54 @@ public class StubRunnerConfiguration {
return batchStubRunner;
}
private StubRunnerOptions stubRunnerOptions(StubRunnerOptionsBuilder builder) {
return builder.build();
}
private StubRunnerOptionsBuilder builder() {
return new StubRunnerOptionsBuilder()
.withMinMaxPort(this.props.getMinPort(), this.props.getMaxPort())
.withMinMaxPort(
Integer.valueOf(resolvePlaceholder(this.props.getMinPort(),
this.props.getMinPort())),
Integer.valueOf(resolvePlaceholder(this.props.getMaxPort(),
this.props.getMaxPort())))
.withStubRepositoryRoot(this.props.getRepositoryRoot())
.withStubsMode(this.props.getStubsMode())
.withStubsClassifier(this.props.getClassifier())
.withStubs(this.props.getIds()).withUsername(this.props.getUsername())
.withPassword(this.props.getPassword())
.withStubPerConsumer(this.props.isStubsPerConsumer())
.withStubsMode(resolvePlaceholder(this.props.getStubsMode()))
.withStubsClassifier(resolvePlaceholder(this.props.getClassifier()))
.withStubs(resolvePlaceholder(this.props.getIds()))
.withUsername(resolvePlaceholder(this.props.getUsername()))
.withPassword(resolvePlaceholder(this.props.getPassword()))
.withStubPerConsumer(Boolean.parseBoolean(
resolvePlaceholder(this.props.isStubsPerConsumer())))
.withConsumerName(consumerName())
.withMappingsOutputFolder(this.props.getMappingsOutputFolder())
.withDeleteStubsAfterTest(this.props.isDeleteStubsAfterTest())
.withGenerateStubs(this.props.isGenerateStubs())
.withMappingsOutputFolder(
resolvePlaceholder(this.props.getMappingsOutputFolder()))
.withDeleteStubsAfterTest(Boolean.parseBoolean(
resolvePlaceholder(this.props.isDeleteStubsAfterTest())))
.withGenerateStubs(Boolean
.parseBoolean(resolvePlaceholder(this.props.isGenerateStubs())))
.withProperties(this.props.getProperties())
.withHttpServerStubConfigurer(this.props.getHttpServerStubConfigurer());
}
private String[] resolvePlaceholder(String[] string) {
return Arrays.stream(string).map(this::resolvePlaceholder).toArray(String[]::new);
}
private String resolvePlaceholder(Object string) {
return resolvePlaceholder(string, null);
}
private String resolvePlaceholder(Object string, Object defaultValue) {
if (string == null) {
return defaultValue != null ? defaultValue.toString() : null;
}
return this.environment.resolvePlaceholders(string.toString());
}
private String consumerName() {
if (StringUtils.hasText(this.props.getConsumerName())) {
return this.props.getConsumerName();
return resolvePlaceholder(this.props.getConsumerName());
}
return this.environment.getProperty("spring.application.name");
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2013-2019 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
*
* https://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 spock.lang.Specification
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootContextLoader
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Configuration
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
/**
* @author Marcin Grzejszczak
*/
@ContextConfiguration(classes = Config, loader = SpringBootContextLoader)
@SpringBootTest(properties = ['some.property1=org.springframework.cloud.contract.verifier.stubs:loanIssuance'])
@AutoConfigureStubRunner
@ActiveProfiles("test-with-placeholders")
class StubRunnerOptionsBuilderSpec extends Specification {
@StubRunnerPort("fraudDetectionServer")
int fraudDetectionServerPort
@StubRunnerPort("loanIssuance")
int loanIssuancePort
@Value('${stub.port}')
int stubPort
def 'should resolve placeholders'() {
expect:
fraudDetectionServerPort > 1000
loanIssuancePort > 1000
and:
stubPort == fraudDetectionServerPort
}
@Configuration
@EnableAutoConfiguration
static class Config {}
}

View File

@@ -0,0 +1,23 @@
provider.stub.artifact.id: fraudDetectionServer
stubrunner:
repositoryRoot: classpath:m2repo/repository/
ids:
- ${some.property1}
- 'org.springframework.cloud.contract.verifier.stubs:${provider.stub.artifact.id}'
stubs-mode: ${some.stubsmode:remote}
min-port: ${some.minport:1000}
max-port: ${some.maxport:20000}
classifier: ${some.classifier:stubs}
username: ${some.username:user}
password: ${some.password:pass}
stubs-per-consumer: ${some.stubsperconsumer:false}
consumer-name: ${some.consumername:name}
mappings-output-folder: ${some.mappingsoutputfolder:target}
delete-stubs-after-test: ${some.deletestubsaftertest:true}
generate-stubs: ${some.generatestubs:false}
spring:
main:
allow-bean-definition-overriding: true
stub.port: ${stubrunner.runningstubs.${provider.stub.artifact.id}.port}

View File

@@ -11,8 +11,8 @@
</parent>
<artifactId>spring-cloud-contract-stub-runner-kafka</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Contract Stub Runner JMS</name>
<description>Spring Cloud Contract Stub Runner JMS</description>
<name>Spring Cloud Contract Stub Runner Kafka</name>
<description>Spring Cloud Contract Stub Runner Kafka</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>

View File

@@ -45,6 +45,7 @@ import org.springframework.kafka.test.context.EmbeddedKafka
import org.springframework.messaging.Message
import org.springframework.messaging.MessageHeaders
import org.springframework.messaging.support.MessageBuilder
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.ContextConfiguration
/**
* @author Marcin Grzejszczak