Additional stub resetting, updated wiremock listener conditions
fixes gh-1027
This commit is contained in:
@@ -19,6 +19,9 @@ package org.springframework.cloud.contract.wiremock;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
@@ -41,6 +44,8 @@ import org.springframework.util.SocketUtils;
|
||||
public class WireMockApplicationListener
|
||||
implements ApplicationListener<ApplicationPreparedEvent> {
|
||||
|
||||
private static final Log log = LogFactory.getLog(WireMockApplicationListener.class);
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationPreparedEvent event) {
|
||||
registerPort(event.getApplicationContext().getEnvironment());
|
||||
@@ -54,26 +59,22 @@ public class WireMockApplicationListener
|
||||
if (httpPortProperty == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (httpPortProperty.equals(0)) {
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
addPropertySource(propertySources);
|
||||
Map<String, Object> source = ((MapPropertySource) propertySources
|
||||
.get("wiremock")).getSource();
|
||||
source.put("wiremock.server.port",
|
||||
SocketUtils.findAvailableTcpPort(10000, 12500));
|
||||
source.put("wiremock.server.port-dynamic", true);
|
||||
if (isHttpDynamic(httpPortProperty)) {
|
||||
registerPropertySourceForDynamicEntries(environment, "wiremock.server.port",
|
||||
10000, 12500, "wiremock.server.port-dynamic");
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Registered property source for dynamic http port");
|
||||
}
|
||||
}
|
||||
int httpsPortProperty = environment.getProperty("wiremock.server.https-port",
|
||||
Integer.class, 0);
|
||||
if (httpsPortProperty == 0) {
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
addPropertySource(propertySources);
|
||||
Map<String, Object> source = ((MapPropertySource) propertySources
|
||||
.get("wiremock")).getSource();
|
||||
source.put("wiremock.server.https-port",
|
||||
SocketUtils.findAvailableTcpPort(12500, 15000));
|
||||
source.put("wiremock.server.https-port-dynamic", true);
|
||||
if (isHttpsDynamic(httpsPortProperty)) {
|
||||
registerPropertySourceForDynamicEntries(environment,
|
||||
"wiremock.server.https-port", 12500, 15000,
|
||||
"wiremock.server.https-port-dynamic");
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Registered property source for dynamic https port");
|
||||
}
|
||||
}
|
||||
else if (httpsPortProperty == -1) {
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
@@ -81,10 +82,33 @@ public class WireMockApplicationListener
|
||||
Map<String, Object> source = ((MapPropertySource) propertySources
|
||||
.get("wiremock")).getSource();
|
||||
source.put("wiremock.server.https-port-dynamic", true);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(
|
||||
"Registered property source for dynamic https with https port property set to -1");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean isHttpsDynamic(int httpsPortProperty) {
|
||||
return httpsPortProperty == 0;
|
||||
}
|
||||
|
||||
private boolean isHttpDynamic(Integer httpPortProperty) {
|
||||
return httpPortProperty.equals(0);
|
||||
}
|
||||
|
||||
private void registerPropertySourceForDynamicEntries(
|
||||
ConfigurableEnvironment environment, String portProperty, int minPort,
|
||||
int maxPort, String dynamicPortProperty) {
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
addPropertySource(propertySources);
|
||||
Map<String, Object> source = ((MapPropertySource) propertySources.get("wiremock"))
|
||||
.getSource();
|
||||
source.put(portProperty, SocketUtils.findAvailableTcpPort(minPort, maxPort));
|
||||
source.put(dynamicPortProperty, true);
|
||||
}
|
||||
|
||||
private void addPropertySource(MutablePropertySources propertySources) {
|
||||
if (!propertySources.contains("wiremock")) {
|
||||
propertySources.addFirst(
|
||||
|
||||
@@ -109,8 +109,7 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
}
|
||||
this.server = new WireMockServer(this.options);
|
||||
}
|
||||
registerStubs();
|
||||
logRegisteredMappings();
|
||||
resetMappings();
|
||||
if (!this.beanFactory.containsBean(WIREMOCK_SERVER_BEAN_NAME)) {
|
||||
this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server);
|
||||
}
|
||||
@@ -125,10 +124,11 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
|
||||
void resetMappings() {
|
||||
this.server.resetAll();
|
||||
registerStubs();
|
||||
logRegisteredMappings();
|
||||
}
|
||||
|
||||
private void registerStubs() throws IOException {
|
||||
private void registerStubs() {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Will register [" + this.wireMock.getServer().getStubs().length
|
||||
+ "] stubs");
|
||||
@@ -144,11 +144,16 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
}
|
||||
pattern = pattern + "**/*.json";
|
||||
}
|
||||
for (Resource resource : resolver.getResources(pattern)) {
|
||||
StubMapping stubMapping = WireMockStubMapping
|
||||
.buildFrom(StreamUtils.copyToString(resource.getInputStream(),
|
||||
Charset.forName("UTF-8")));
|
||||
this.server.addStubMapping(stubMapping);
|
||||
try {
|
||||
for (Resource resource : resolver.getResources(pattern)) {
|
||||
StubMapping stubMapping = WireMockStubMapping.buildFrom(
|
||||
StreamUtils.copyToString(resource.getInputStream(),
|
||||
Charset.forName("UTF-8")));
|
||||
this.server.addStubMapping(stubMapping);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -58,8 +57,8 @@ public abstract class WireMockSpring {
|
||||
.build().getSocketFactory());
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assert.fail("Cannot install custom socket factory: [" + e.getMessage()
|
||||
+ "]");
|
||||
throw new AssertionError("Cannot install custom socket factory: ["
|
||||
+ e.getMessage() + "]");
|
||||
}
|
||||
}
|
||||
initialized = true;
|
||||
|
||||
@@ -53,7 +53,6 @@ public class AutoConfigureWireMockRandomPortApplicationTests {
|
||||
.withHeader("Content-Type", "text/plain").withBody("Hello World!")));
|
||||
assertThat(this.service.go()).isEqualTo("Hello World!");
|
||||
|
||||
|
||||
wireMockServer.verify(1, RequestPatternBuilder.allRequests());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
public class AutoConfigureWireMockRandomPortInheretedApplicationTests extends AutoConfigureWireMockRandomPortApplicationTests {
|
||||
public class AutoConfigureWireMockRandomPortInheretedApplicationTests
|
||||
extends AutoConfigureWireMockRandomPortApplicationTests {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user