From bd054e3220d69cb2bbcbea9795cdf6c64a98ab4a Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 25 Mar 2020 14:48:26 +0100 Subject: [PATCH] Improved bean restarting and resetting --- .../src/main/resources/application.yml | 1 + .../wiremock/WireMockConfiguration.java | 63 +++++++++++--- .../WireMockTestExecutionListener.java | 3 +- .../sameConfigsDifferentTests/Client.java | 38 +++++++++ .../sameConfigsDifferentTests/FirstTests.java | 83 +++++++++++++++++++ .../FraudCheckStatus.java | 23 +++++ .../FraudServiceRequest.java | 51 ++++++++++++ .../FraudServiceResponse.java | 44 ++++++++++ .../LoanApplication.java | 61 ++++++++++++++ .../SecondTests.java | 82 ++++++++++++++++++ .../issues/staticInit/ClientProperties.java | 34 ++++++++ .../staticInit/RestTemplateConfiguration.java | 33 ++++++++ .../issues/staticInit/TimeoutApplication.java | 29 +++++++ .../wiremock/issues/staticInit/WebClient.java | 41 +++++++++ .../issues/staticInit/WebClientTest.java | 82 ++++++++++++++++++ .../shouldMarkClientAsFraud.json | 26 ++++++ .../shouldMarkClientAsNotFraud.json | 26 ++++++ 17 files changed, 706 insertions(+), 14 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/Client.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FirstTests.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudCheckStatus.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceRequest.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceResponse.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/LoanApplication.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/SecondTests.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/ClientProperties.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/RestTemplateConfiguration.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/TimeoutApplication.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClient.java create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClientTest.java create mode 100644 spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsFraud.json create mode 100644 spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsNotFraud.json diff --git a/samples/standalone/restdocs/http-client/src/main/resources/application.yml b/samples/standalone/restdocs/http-client/src/main/resources/application.yml index 056c802c77..456f83e828 100644 --- a/samples/standalone/restdocs/http-client/src/main/resources/application.yml +++ b/samples/standalone/restdocs/http-client/src/main/resources/application.yml @@ -1,5 +1,6 @@ server.port: 8097 logging: level: + org.springframework.cloud.contract.wiremock: debug org.springframework.web.client: debug com.github.tomakehurst.wiremock: trace \ No newline at end of file diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index 4270356ffc..47d7f46202 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -114,28 +114,58 @@ public class WireMockConfiguration implements SmartLifecycle { updateCurrentServer(); } + void initIfNotRunning() throws IOException { + if (!this.running) { + init(); + } + } + private void reRegisterBeans() { if (!this.beanFactory.containsBean(WIREMOCK_SERVER_BEAN_NAME)) { + if (log.isDebugEnabled()) { + log.debug("Registering WireMock [" + this.server + "] instance"); + } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } else { + if (log.isDebugEnabled()) { + log.debug("Destroying WireMock [" + + this.beanFactory.getBean(WIREMOCK_SERVER_BEAN_NAME) + + "] instance"); + } this.beanFactory.destroySingleton(WIREMOCK_SERVER_BEAN_NAME); + if (log.isDebugEnabled()) { + log.debug("Registering WireMock [" + this.server + "] instance"); + } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } } private void reRegisterServer() { - if (log.isDebugEnabled()) { - log.debug("Creating a new server at " + "http port [" + if (log.isTraceEnabled()) { + log.trace("Creating a new server at http port [" + this.wireMock.getServer().getPort() + "] and " + "https port [" + this.wireMock.getServer().getHttpsPort() + "]"); } if (this.isRunning()) { - this.server.stop(); + if (log.isDebugEnabled()) { + log.debug("Stopping server [" + this.server + "] at port [" + + port(this.server) + "]"); + } + stop(); + } + else if (this.server == null) { + this.server = new WireMockServer(this.options); + if (log.isDebugEnabled()) { + log.debug("Created new server [" + this.server + "] at port [" + + port(this.server) + "]"); + } + } + start(); + if (log.isDebugEnabled()) { + log.debug("Started server [" + this.server + "] at port [" + port(this.server) + + "]"); } - this.server = new WireMockServer(this.options); - this.server.start(); - updateCurrentServer(); logRegisteredMappings(); } @@ -148,8 +178,8 @@ public class WireMockConfiguration implements SmartLifecycle { void reRegisterServerWithResetMappings() { reRegisterServer(); + resetMappings(); if (this.server.isRunning()) { - resetMappings(); updateCurrentServer(); } } @@ -170,7 +200,7 @@ public class WireMockConfiguration implements SmartLifecycle { private void registerStubs() { if (log.isDebugEnabled()) { log.debug("Will register [" + this.wireMock.getServer().getStubs().length - + "] stubs"); + + "] stub locations"); } for (String stubs : this.wireMock.getServer().getStubs()) { if (StringUtils.hasText(stubs)) { @@ -225,8 +255,10 @@ public class WireMockConfiguration implements SmartLifecycle { @Override public void start() { if (isRunning()) { + int port = port(this.server); if (log.isDebugEnabled()) { - log.debug("Server is already running"); + log.debug("Server [" + this.server + "] is already running at port [" + + port + "]"); } return; } @@ -246,20 +278,25 @@ public class WireMockConfiguration implements SmartLifecycle { @Override public void stop() { if (this.running) { + WireMockServer server = this.server; + int port = port(server); this.server.stop(); - this.server = null; this.running = false; - this.options = null; if (log.isDebugEnabled()) { - log.debug("Stopped WireMock instance"); + log.debug( + "Stopped WireMock [" + server + "] instance port [" + port + "]"); } - this.beanFactory.destroySingleton(WIREMOCK_SERVER_BEAN_NAME); } else if (log.isDebugEnabled()) { log.debug("Server already stopped"); } } + private int port(WireMockServer server) { + return server.isRunning() ? (server.getOptions().httpsSettings().enabled() + ? server.httpsPort() : server.port()) : -1; + } + @Override public boolean isRunning() { return this.running; diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java index b0785de2f7..0cf3fe3de1 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockTestExecutionListener.java @@ -46,7 +46,8 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi if (log.isDebugEnabled()) { log.debug("Re-registering default mappings"); } - wireMockConfig(testContext).init(); + wireMockConfig(testContext).initIfNotRunning(); + wireMockConfig(testContext).start(); } } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/Client.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/Client.java new file mode 100644 index 0000000000..76e80a8bcc --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/Client.java @@ -0,0 +1,38 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +public class Client { + + private String pesel; + + public Client() { + } + + public Client(String pesel) { + this.pesel = pesel; + } + + public String getPesel() { + return pesel; + } + + public void setPesel(String pesel) { + this.pesel = pesel; + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FirstTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FirstTests.java new file mode 100644 index 0000000000..a6097a02ac --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FirstTests.java @@ -0,0 +1,83 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +import java.nio.charset.Charset; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.stubbing.StubMapping; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.StreamUtils; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "service.port=${wiremock.server.port}", + classes = FirstTests.Config.class) +@AutoConfigureWireMock(port = 0) +public class FirstTests { + + @Value("classpath:example-mappings/shouldMarkClientAsFraud.json") + private Resource markClientAsFraud; + + @Autowired + private WireMockServer server; + + @Test + public void shouldBeRejectedDueToAbnormalLoanAmount() throws Exception { + server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString( + markClientAsFraud.getInputStream(), Charset.forName("UTF-8")))); + // given: + LoanApplication loanApplication = new LoanApplication(new Client("1234567890"), + 99999); + + // when: + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/vnd.fraud.v1+json"); + + ResponseEntity response = new RestTemplate().exchange( + "http://localhost:" + server.port() + "/fraudcheck", HttpMethod.PUT, + new HttpEntity<>(new FraudServiceRequest(loanApplication), httpHeaders), + FraudServiceResponse.class); + // then: + assertThat(response.getBody().getFraudCheckStatus()) + .isEqualTo(FraudCheckStatus.FRAUD); + assertThat(response.getBody().getRejectionReason()).isEqualTo("Amount too high"); + } + + @EnableAutoConfiguration + @Configuration + static class Config { + + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudCheckStatus.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudCheckStatus.java new file mode 100644 index 0000000000..b9bf1c053b --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudCheckStatus.java @@ -0,0 +1,23 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +public enum FraudCheckStatus { + + OK, FRAUD + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceRequest.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceRequest.java new file mode 100644 index 0000000000..54f1070c33 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceRequest.java @@ -0,0 +1,51 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +import java.math.BigDecimal; + +public class FraudServiceRequest { + + private String clientId; + + private BigDecimal loanAmount; + + public FraudServiceRequest() { + } + + public FraudServiceRequest(LoanApplication loanApplication) { + this.clientId = loanApplication.getClient().getPesel(); + this.loanAmount = loanApplication.getAmount(); + } + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public BigDecimal getLoanAmount() { + return loanAmount; + } + + public void setLoanAmount(BigDecimal loanAmount) { + this.loanAmount = loanAmount; + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceResponse.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceResponse.java new file mode 100644 index 0000000000..5837f20d64 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/FraudServiceResponse.java @@ -0,0 +1,44 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +public class FraudServiceResponse { + + private FraudCheckStatus fraudCheckStatus; + + private String rejectionReason; + + public FraudServiceResponse() { + } + + public FraudCheckStatus getFraudCheckStatus() { + return fraudCheckStatus; + } + + public void setFraudCheckStatus(FraudCheckStatus fraudCheckStatus) { + this.fraudCheckStatus = fraudCheckStatus; + } + + public String getRejectionReason() { + return rejectionReason; + } + + public void setRejectionReason(String rejectionReason) { + this.rejectionReason = rejectionReason; + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/LoanApplication.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/LoanApplication.java new file mode 100644 index 0000000000..1efdbe5271 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/LoanApplication.java @@ -0,0 +1,61 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +import java.math.BigDecimal; + +public class LoanApplication { + + private Client client; + + private BigDecimal amount; + + private String loanApplicationId; + + public LoanApplication() { + } + + public LoanApplication(Client client, double amount) { + this.client = client; + this.amount = BigDecimal.valueOf(amount); + } + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public String getLoanApplicationId() { + return loanApplicationId; + } + + public void setLoanApplicationId(String loanApplicationId) { + this.loanApplicationId = loanApplicationId; + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/SecondTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/SecondTests.java new file mode 100644 index 0000000000..fdf973e2cb --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/sameConfigsDifferentTests/SecondTests.java @@ -0,0 +1,82 @@ +/* + * 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.wiremock.issues.sameConfigsDifferentTests; + +import java.nio.charset.Charset; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.stubbing.StubMapping; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.StreamUtils; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "service.port=${wiremock.server.port}", + classes = SecondTests.Config.class) +@AutoConfigureWireMock(port = 0) +public class SecondTests { + + @Value("classpath:example-mappings/shouldMarkClientAsNotFraud.json") + private Resource markClientAsNotFraud; + + @Autowired + private WireMockServer server; + + @Test + public void shouldBeRejectedDueToAbnormalLoanAmount() throws Exception { + server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString( + markClientAsNotFraud.getInputStream(), Charset.forName("UTF-8")))); + // given: + LoanApplication loanApplication = new LoanApplication(new Client("1234567890"), + 123.123); + + // when: + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/vnd.fraud.v1+json"); + + ResponseEntity response = new RestTemplate().exchange( + "http://localhost:" + server.port() + "/fraudcheck", HttpMethod.PUT, + new HttpEntity<>(new FraudServiceRequest(loanApplication), httpHeaders), + FraudServiceResponse.class); + // then: + assertThat(response.getBody().getFraudCheckStatus()) + .isEqualTo(FraudCheckStatus.OK); + } + + @EnableAutoConfiguration + @Configuration + static class Config { + + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/ClientProperties.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/ClientProperties.java new file mode 100644 index 0000000000..546de70b05 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/ClientProperties.java @@ -0,0 +1,34 @@ +/* + * 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.wiremock.issues.staticInit; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "my-client") +public class ClientProperties { + + private String url; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/RestTemplateConfiguration.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/RestTemplateConfiguration.java new file mode 100644 index 0000000000..ae6dc4b969 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/RestTemplateConfiguration.java @@ -0,0 +1,33 @@ +/* + * 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.wiremock.issues.staticInit; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +@EnableConfigurationProperties(ClientProperties.class) +public class RestTemplateConfiguration { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/TimeoutApplication.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/TimeoutApplication.java new file mode 100644 index 0000000000..5ffcc68e35 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/TimeoutApplication.java @@ -0,0 +1,29 @@ +/* + * 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.wiremock.issues.staticInit; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TimeoutApplication { + + public static void main(String[] args) { + SpringApplication.run(TimeoutApplication.class, args); + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClient.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClient.java new file mode 100644 index 0000000000..b3035a8cdf --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClient.java @@ -0,0 +1,41 @@ +/* + * 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.wiremock.issues.staticInit; + +import java.net.URI; + +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class WebClient { + + private final RestTemplate restTemplate; + + private final ClientProperties clientProperties; + + public WebClient(RestTemplate restTemplate, ClientProperties clientProperties) { + this.restTemplate = restTemplate; + this.clientProperties = clientProperties; + } + + public String get() { + URI url = URI.create(clientProperties.getUrl()); + return restTemplate.getForObject(url, String.class); + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClientTest.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClientTest.java new file mode 100644 index 0000000000..e73a313f77 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/issues/staticInit/WebClientTest.java @@ -0,0 +1,82 @@ +/* + * 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.wiremock.issues.staticInit; + +import com.github.tomakehurst.wiremock.WireMockServer; +import org.assertj.core.api.BDDAssertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; +import org.springframework.context.annotation.Bean; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; + +@SpringBootTest( + properties = "my-client.url=http://localhost:${wiremock.server.port}/resources") +@AutoConfigureWireMock(port = 0, httpsPort = 0, files = "src/test/resources/__files") +class WebClientTest { + + private static Logger log = LoggerFactory.getLogger(WebClientTest.class); + + @Autowired + WebClient underTest; + + @Autowired + Stubber stubber; + + @Test + void shouldRespondForGetJustFine() { + String actual = underTest.get(); + + BDDAssertions.then(actual).isEqualTo("Everything seems fine!"); + } + + @TestConfiguration + static class WebClientTestConfiguration { + + @Bean + Stubber stubberWithoutDeps() { + return new Stubber(); + } + + } + + static class Stubber { + + Stubber() { + log.info("Setting up GET"); + stubFor(get(urlEqualTo("/resources")) + .willReturn(aResponse().withBody("Everything seems fine!"))); + } + + Stubber(WireMockServer wireMockServer) { + log.info("Setting up GET"); + wireMockServer.stubFor(get(urlEqualTo("/resources")) + .willReturn(aResponse().withBody("Everything seems fine!"))); + } + + } + +} diff --git a/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsFraud.json b/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsFraud.json new file mode 100644 index 0000000000..2453086652 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsFraud.json @@ -0,0 +1,26 @@ +{ + "id" : "6655794c-a651-4b71-a2b9-b7dc7e1c273f", + "request" : { + "url" : "/fraudcheck", + "method" : "PUT", + "headers" : { + "Content-Type" : { + "matches" : "application/vnd\\.fraud\\.v1\\+json.*" + } + }, + "bodyPatterns" : [ { + "matchesJsonPath" : "$[?(@.['clientId'] =~ /[0-9]{10}/)]" + }, { + "matchesJsonPath" : "$[?(@.['loanAmount'] == 99999)]" + } ] + }, + "response" : { + "status" : 200, + "body" : "{\"fraudCheckStatus\":\"FRAUD\",\"rejectionReason\":\"Amount too high\"}", + "headers" : { + "Content-Type" : "application/vnd.fraud.v1+json" + }, + "transformers" : [ "response-template" ] + }, + "uuid" : "6655794c-a651-4b71-a2b9-b7dc7e1c273f" +} diff --git a/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsNotFraud.json b/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsNotFraud.json new file mode 100644 index 0000000000..90e351fa0a --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/resources/example-mappings/shouldMarkClientAsNotFraud.json @@ -0,0 +1,26 @@ +{ + "id" : "de2f8246-ae1a-4bdf-8d4c-a44af6df7a83", + "request" : { + "url" : "/fraudcheck", + "method" : "PUT", + "headers" : { + "Content-Type" : { + "matches" : "application/vnd\\.fraud\\.v1\\+json.*" + } + }, + "bodyPatterns" : [ { + "matchesJsonPath" : "$[?(@.['clientId'] =~ /[0-9]{10}/)]" + }, { + "matchesJsonPath" : "$[?(@.['loanAmount'] == 123.123)]" + } ] + }, + "response" : { + "status" : 200, + "body" : "{\"fraudCheckStatus\":\"OK\",\"rejectionReason\":null}", + "headers" : { + "Content-Type" : "application/vnd.fraud.v1+json" + }, + "transformers" : [ "response-template" ] + }, + "uuid" : "de2f8246-ae1a-4bdf-8d4c-a44af6df7a83" +}