Additional stub resetting, updated wiremock listener conditions

fixes gh-1027
This commit is contained in:
Marcin Grzejszczak
2019-09-26 14:14:34 +02:00
parent 84e259fe13
commit 0a67fcbab3
9 changed files with 64 additions and 37 deletions

View File

@@ -2193,6 +2193,7 @@ $ touch .springformat
==== Intellij IDEA
In order to setup Intellij you should import our coding conventions, inspection profiles and set up the checkstyle plugin.
The following files can be found in the https://github.com/spring-cloud/spring-cloud-build/tree/master/spring-cloud-build-tools[Spring Cloud Build] project.
.spring-cloud-build-tools/
----
@@ -2233,7 +2234,7 @@ image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring
Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on the `+` icon in the `Configuration file` section. There, you'll have to define where the checkstyle rules should be picked from. In the image above, we've picked the rules from the cloned Spring Cloud Build repository. However, you can point to the Spring Cloud Build's GitHub repository (e.g. for the `checkstyle.xml` : `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml`). We need to provide the following variables:
- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.

View File

@@ -34,10 +34,6 @@ dependencies {
testCompile 'org.springframework.cloud:spring-cloud-contract-wiremock'
}
contracts {
failOnNoContracts = false
}
test {
systemProperty 'spring.profiles.active', 'gradle'
testLogging {

View File

@@ -1,3 +1,4 @@
org.gradle.daemon=false
verifierVersion=2.1.4.BUILD-SNAPSHOT
BOM_VERSION=Greenwich.BUILD-SNAPSHOT
BOM_VERSION=Greenwich.BUILD-SNAPSHOT
bootVersion=2.1.7.RELEASE

View File

@@ -46,7 +46,8 @@ public final class StubRunnerWireMockTestExecutionListener
}
return;
}
if (WireMockHttpServerStub.SERVERS.values().stream().noneMatch(p -> p.random)) {
if (!WireMockHttpServerStub.SERVERS.isEmpty() && WireMockHttpServerStub.SERVERS
.values().stream().noneMatch(p -> p.random)) {
if (log.isWarnEnabled()) {
log.warn("You've used fixed ports for WireMock setup - "
+ "will mark context as dirty. Please use random ports, as much "

View File

@@ -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(

View File

@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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());
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.wiremock;
public class AutoConfigureWireMockRandomPortInheretedApplicationTests extends AutoConfigureWireMockRandomPortApplicationTests {
public class AutoConfigureWireMockRandomPortInheretedApplicationTests
extends AutoConfigureWireMockRandomPortApplicationTests {
}