Adds the env prop for every run server (#158)
without this change you can't reference the port of a server as an env var fixes #147
This commit is contained in:
committed by
GitHub
parent
7a347da55a
commit
cf2dbc24a6
@@ -159,6 +159,15 @@ Below you can find an example of achieving the same result by setting values on
|
||||
include::src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/cloud/StubRunnerSpringCloudAutoConfigurationSpec.groovy[tags=autoconfigure]
|
||||
----
|
||||
|
||||
Stub Runner Spring registers environment variables in the following manner
|
||||
for every registered WireMock server. Example for Stub Runner ids
|
||||
`com.example:foo`, `com.example:bar`.
|
||||
|
||||
- `stubrunner.runningstubs.foo.port`
|
||||
- `stubrunner.runningstubs.bar.port`
|
||||
|
||||
Which you can reference in your code.
|
||||
|
||||
|
||||
=== Stub Runner Spring Cloud
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.cloud.contract.stubrunner.spring;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -24,6 +26,8 @@ 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;
|
||||
import org.springframework.cloud.contract.stubrunner.RunningStubs;
|
||||
import org.springframework.cloud.contract.stubrunner.StubConfiguration;
|
||||
import org.springframework.cloud.contract.stubrunner.StubDownloader;
|
||||
import org.springframework.cloud.contract.stubrunner.StubRunnerOptions;
|
||||
import org.springframework.cloud.contract.stubrunner.StubRunnerOptionsBuilder;
|
||||
@@ -31,6 +35,9 @@ import org.springframework.cloud.contract.verifier.messaging.MessageVerifier;
|
||||
import org.springframework.cloud.contract.verifier.messaging.noop.NoOpStubMessages;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@@ -42,12 +49,16 @@ import org.springframework.core.io.Resource;
|
||||
@ConditionalOnMissingBean(type = "org.springframework.cloud.contract.wiremock.WiremockServerConfiguration")
|
||||
public class StubRunnerConfiguration {
|
||||
|
||||
private static final String STUBRUNNER_PREFIX = "stubrunner.runningstubs";
|
||||
|
||||
@Autowired(required = false)
|
||||
private MessageVerifier<?> contractVerifierMessaging;
|
||||
@Autowired(required = false)
|
||||
private StubDownloader stubDownloader;
|
||||
@Autowired
|
||||
private StubRunnerProperties props;
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
/**
|
||||
* Bean that initializes stub runners, runs them and on shutdown closes them. Upon its
|
||||
@@ -70,7 +81,8 @@ public class StubRunnerConfiguration {
|
||||
this.contractVerifierMessaging != null ? this.contractVerifierMessaging
|
||||
: new NoOpStubMessages()).buildBatchStubRunner();
|
||||
// TODO: Consider running it in a separate thread
|
||||
batchStubRunner.runStubs();
|
||||
RunningStubs runningStubs = batchStubRunner.runStubs();
|
||||
registerPort(runningStubs);
|
||||
return batchStubRunner;
|
||||
}
|
||||
|
||||
@@ -78,4 +90,17 @@ public class StubRunnerConfiguration {
|
||||
return stubRepositoryRoot != null ? stubRepositoryRoot.getURI().toString() : "";
|
||||
}
|
||||
|
||||
private void registerPort(RunningStubs runStubs) {
|
||||
MutablePropertySources propertySources = this.environment.getPropertySources();
|
||||
if (!propertySources.contains(STUBRUNNER_PREFIX)) {
|
||||
propertySources.addFirst(
|
||||
new MapPropertySource(STUBRUNNER_PREFIX, new HashMap<String, Object>()));
|
||||
}
|
||||
Map<String, Object> source = ((MapPropertySource) propertySources
|
||||
.get(STUBRUNNER_PREFIX)).getSource();
|
||||
for (Map.Entry<StubConfiguration, Integer> entry : runStubs.validNamesAndPorts().entrySet()) {
|
||||
source.put(STUBRUNNER_PREFIX + "." + entry.getKey().getArtifactId() + ".port", entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.boot.test.context.SpringBootContextLoader
|
||||
import org.springframework.cloud.contract.stubrunner.StubFinder
|
||||
import org.springframework.cloud.contract.stubrunner.StubNotFoundException
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.core.env.Environment
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import org.springframework.test.context.ActiveProfiles
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
@@ -45,6 +46,7 @@ import spock.lang.Specification
|
||||
class StubRunnerConfigurationSpec extends Specification {
|
||||
|
||||
@Autowired StubFinder stubFinder
|
||||
@Autowired Environment environment
|
||||
|
||||
@BeforeClass
|
||||
@AfterClass
|
||||
@@ -81,6 +83,15 @@ class StubRunnerConfigurationSpec extends Specification {
|
||||
thrown(StubNotFoundException)
|
||||
}
|
||||
|
||||
def 'should register started servers as environment variables'() {
|
||||
expect:
|
||||
environment.getProperty("stubrunner.runningstubs.loanIssuance.port") != null
|
||||
stubFinder.findAllRunningStubs().getPort("loanIssuance") == (environment.getProperty("stubrunner.runningstubs.loanIssuance.port") as Integer)
|
||||
and:
|
||||
environment.getProperty("stubrunner.runningstubs.fraudDetectionServer.port") != null
|
||||
stubFinder.findAllRunningStubs().getPort("fraudDetectionServer") == (environment.getProperty("stubrunner.runningstubs.fraudDetectionServer.port") as Integer)
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
static class Config {}
|
||||
|
||||
Reference in New Issue
Block a user