diff --git a/pom.xml b/pom.xml index 612225ed9d..f721fc47df 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ 4.3.1 6.7.0.202309050840-r 1 + 2.1.1 2.38.0 5.2.1 [2.0.0,) @@ -238,6 +239,11 @@ javax.inject ${javax-inject.version} + + jakarta.annotation + jakarta.annotation-api + ${jakarta-annotation.version} + diff --git a/spring-cloud-contract-dependencies/pom.xml b/spring-cloud-contract-dependencies/pom.xml index e6fc4bde0f..bfc696972d 100644 --- a/spring-cloud-contract-dependencies/pom.xml +++ b/spring-cloud-contract-dependencies/pom.xml @@ -15,7 +15,7 @@ spring-cloud-contract-dependencies Spring Cloud Contract Dependencies - 2.35.1 + 3.3.0 0.8.0 @@ -96,8 +96,8 @@ ${project.version} - com.github.tomakehurst - wiremock-jre8-standalone + org.wiremock + wiremock-standalone ${wiremock.version} diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java index 8ca2223eb1..5686192283 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/provider/wiremock/WireMockHttpServerStub.java @@ -19,12 +19,11 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -42,14 +41,12 @@ import org.apache.commons.logging.LogFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import wiremock.com.github.jknack.handlebars.Helper; import org.springframework.cloud.contract.stubrunner.HttpServerStub; import org.springframework.cloud.contract.stubrunner.HttpServerStubConfiguration; import org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer; -import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsEscapeHelper; -import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsJsonPathHelper; -import org.springframework.cloud.contract.verifier.dsl.wiremock.DefaultResponseTransformer; +import org.springframework.cloud.contract.verifier.dsl.wiremock.HandlebarsEscapeHelperExtension; +import org.springframework.cloud.contract.verifier.dsl.wiremock.HandlebarsJsonHelperExtension; import org.springframework.cloud.contract.verifier.dsl.wiremock.SpringCloudContractRequestMatcher; import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions; import org.springframework.cloud.contract.wiremock.WireMockSpring; @@ -95,19 +92,12 @@ private Extension[] responseTransformers() { } } else { - extensions.addAll(Arrays.asList(new DefaultResponseTransformer(false, helpers()), + extensions.addAll(Arrays.asList(new HandlebarsEscapeHelperExtension(), new HandlebarsJsonHelperExtension(), new SpringCloudContractRequestMatcher())); } return extensions.toArray(new Extension[extensions.size()]); } - private Map> helpers() { - Map> helpers = new HashMap<>(); - helpers.put(HandlebarsJsonPathHelper.NAME, new HandlebarsJsonPathHelper()); - helpers.put(HandlebarsEscapeHelper.NAME, new HandlebarsEscapeHelper()); - return helpers; - } - @Override public int port() { return isRunning() ? (this.https ? this.wireMockServer.httpsPort() : this.wireMockServer.port()) : INVALID_PORT; @@ -212,7 +202,7 @@ private boolean validMapping(File file) { StubMapping getMapping(File file) { try (InputStream stream = Files.newInputStream(file.toPath())) { - return StubMapping.buildFrom(StreamUtils.copyToString(stream, Charset.forName("UTF-8"))); + return StubMapping.buildFrom(StreamUtils.copyToString(stream, StandardCharsets.UTF_8)); } catch (IOException | JsonException e) { throw new IllegalStateException("Cannot read file", e); diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/TestWireMockExtensions.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/TestWireMockExtensions.groovy index aa7b2ef9ab..74a76849b3 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/TestWireMockExtensions.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/provider/wiremock/TestWireMockExtensions.groovy @@ -20,14 +20,12 @@ import com.github.tomakehurst.wiremock.common.FileSource import com.github.tomakehurst.wiremock.extension.Extension import com.github.tomakehurst.wiremock.extension.Parameters import com.github.tomakehurst.wiremock.extension.ResponseTransformer -import com.github.tomakehurst.wiremock.http.ChunkedDribbleDelay import com.github.tomakehurst.wiremock.http.HttpHeader import com.github.tomakehurst.wiremock.http.HttpHeaders import com.github.tomakehurst.wiremock.http.Request import com.github.tomakehurst.wiremock.http.Response import groovy.transform.CompileStatic -import org.springframework.cloud.contract.verifier.dsl.wiremock.DefaultResponseTransformer import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions /** @@ -37,7 +35,7 @@ import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensio class TestWireMockExtensions implements WireMockExtensions { @Override List extensions() { - return [ new DefaultResponseTransformer(), new CustomExtension() ] as List + return [new CustomExtension()] as List } } @@ -60,16 +58,16 @@ class CustomExtension extends ResponseTransformer { Response transform(Request request, Response response, FileSource files, Parameters parameters) { HttpHeaders headers = response.headers + new HttpHeader("X-My-Header", "surprise!") return Response.response() - .status(response.status) - .statusMessage(response.statusMessage) - .body(response.body) - .headers(headers) - .configured(response.wasConfigured()) - .fault(response.fault) - .incrementInitialDelay(response.initialDelay) - .chunkedDribbleDelay(response.chunkedDribbleDelay) - .fromProxy(response.fromProxy) - .build() + .status(response.status) + .statusMessage(response.statusMessage) + .body(response.body) + .headers(headers) + .configured(response.wasConfigured()) + .fault(response.fault) + .incrementInitialDelay(response.initialDelay) + .chunkedDribbleDelay(response.chunkedDribbleDelay) + .fromProxy(response.fromProxy) + .build() } /** diff --git a/spring-cloud-contract-tools/spring-cloud-contract-converters/pom.xml b/spring-cloud-contract-tools/spring-cloud-contract-converters/pom.xml index 826da1fbb5..dc0c8ad08e 100644 --- a/spring-cloud-contract-tools/spring-cloud-contract-converters/pom.xml +++ b/spring-cloud-contract-tools/spring-cloud-contract-converters/pom.xml @@ -43,8 +43,8 @@ groovy-nio - com.github.tomakehurst - wiremock-jre8-standalone + org.wiremock + wiremock-standalone org.springframework.boot diff --git a/spring-cloud-contract-verifier/pom.xml b/spring-cloud-contract-verifier/pom.xml index 2721a8462a..5b04399908 100644 --- a/spring-cloud-contract-verifier/pom.xml +++ b/spring-cloud-contract-verifier/pom.xml @@ -115,8 +115,12 @@ javax.inject - com.github.tomakehurst - wiremock-jre8-standalone + jakarta.annotation + jakarta.annotation-api + + + org.wiremock + wiremock-standalone com.toomuchcoding.jsonassert diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/DefaultResponseTransformer.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/DefaultResponseTransformer.java deleted file mode 100644 index aa2b81a6d6..0000000000 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/DefaultResponseTransformer.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2013-2020 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.verifier.dsl.wiremock; - -import java.util.HashMap; -import java.util.Map; - -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import wiremock.com.github.jknack.handlebars.Helper; - -import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsEscapeHelper; -import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsJsonPathHelper; - -/** - * . Default implementation of {@link ResponseTemplateTransformer} that contains default - * set of handlebars helpers - * - * @author Marcin Grzejszczak - * @since 1.2.0 - */ -public class DefaultResponseTransformer extends ResponseTemplateTransformer { - - public DefaultResponseTransformer() { - super(false, defaultHelpers()); - } - - public DefaultResponseTransformer(boolean global) { - super(global); - } - - public DefaultResponseTransformer(boolean global, String helperName, Helper helper) { - super(global, helperName, helper); - } - - public DefaultResponseTransformer(boolean global, Map> helpers) { - super(global, helpers); - } - - private static Map> defaultHelpers() { - Map> helpers = new HashMap<>(); - helpers.put(HandlebarsJsonPathHelper.NAME, new HandlebarsJsonPathHelper()); - helpers.put(HandlebarsEscapeHelper.NAME, new HandlebarsEscapeHelper()); - return helpers; - } - -} diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsEscapeHelperExtension.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsEscapeHelperExtension.java new file mode 100644 index 0000000000..acd72e5719 --- /dev/null +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsEscapeHelperExtension.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013-2020 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.verifier.dsl.wiremock; + +import java.util.Map; + +import com.github.tomakehurst.wiremock.extension.TemplateHelperProviderExtension; +import wiremock.com.github.jknack.handlebars.Helper; + +import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsEscapeHelper; + +/** + * Template extension that register {@link HandlebarsEscapeHelper} in Wiremock. + * @author Kamil Dobrzynski + */ +public class HandlebarsEscapeHelperExtension implements TemplateHelperProviderExtension { + + @Override + public Map> provideTemplateHelpers() { + return Map.of(HandlebarsEscapeHelper.NAME, new HandlebarsEscapeHelper()); + } + + @Override + public String getName() { + return HandlebarsEscapeHelper.NAME; + } + +} diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsJsonHelperExtension.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsJsonHelperExtension.java new file mode 100644 index 0000000000..30846231b9 --- /dev/null +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/HandlebarsJsonHelperExtension.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013-2020 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.verifier.dsl.wiremock; + +import java.util.Map; + +import com.github.tomakehurst.wiremock.extension.TemplateHelperProviderExtension; +import wiremock.com.github.jknack.handlebars.Helper; + +import org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsJsonPathHelper; + +/** + * Template extension that register {@link HandlebarsJsonPathHelper} in Wiremock. + * @author Kamil Dobrzynski + */ +public class HandlebarsJsonHelperExtension implements TemplateHelperProviderExtension { + + @Override + public Map> provideTemplateHelpers() { + return Map.of(HandlebarsJsonPathHelper.NAME, new HandlebarsJsonPathHelper()); + } + + @Override + public String getName() { + return HandlebarsJsonPathHelper.NAME; + } + +} diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockResponseStubStrategy.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockResponseStubStrategy.java index 2d803f769b..21a0a23af2 100755 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockResponseStubStrategy.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockResponseStubStrategy.java @@ -20,9 +20,11 @@ import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Stream; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.extension.Extension; +import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; import com.github.tomakehurst.wiremock.http.HttpHeader; import com.github.tomakehurst.wiremock.http.HttpHeaders; import com.github.tomakehurst.wiremock.http.ResponseDefinition; @@ -83,10 +85,12 @@ private String[] responseTransformerNames() { List wireMockExtensions = SpringFactoriesLoader.loadFactories(WireMockExtensions.class, null); if (!wireMockExtensions.isEmpty()) { - return wireMockExtensions.stream().map(WireMockExtensions::extensions).flatMap(Collection::stream) - .map(Extension::getName).toArray(String[]::new); + return Stream + .concat(Stream.of(ResponseTemplateTransformer.NAME), wireMockExtensions.stream() + .map(WireMockExtensions::extensions).flatMap(Collection::stream).map(Extension::getName)) + .toArray(String[]::new); } - return new String[] { new DefaultResponseTransformer().getName(), SpringCloudContractRequestMatcher.NAME }; + return new String[] { ResponseTemplateTransformer.NAME, SpringCloudContractRequestMatcher.NAME }; } private void appendHeaders(ResponseDefinitionBuilder builder) { diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierReceiver.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierReceiver.java index b6c8af02cd..7c278baa9b 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierReceiver.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierReceiver.java @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; +import jakarta.annotation.Nullable; import org.springframework.cloud.contract.verifier.converter.YamlContract; diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierSender.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierSender.java index b93dccf4c6..6641e013fd 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierSender.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/MessageVerifierSender.java @@ -18,7 +18,7 @@ import java.util.Map; -import javax.annotation.Nullable; +import jakarta.annotation.Nullable; import org.springframework.cloud.contract.verifier.converter.YamlContract; diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/internal/ContractVerifierMessaging.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/internal/ContractVerifierMessaging.java index 886b1a3eda..4aa25f98e7 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/internal/ContractVerifierMessaging.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/internal/ContractVerifierMessaging.java @@ -18,8 +18,7 @@ import java.util.Map; -import javax.annotation.Nullable; - +import jakarta.annotation.Nullable; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/TestWireMockExtensions.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/TestWireMockExtensions.groovy index df5d0e6e46..af322f14ce 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/TestWireMockExtensions.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/TestWireMockExtensions.groovy @@ -25,7 +25,6 @@ class TestWireMockExtensions implements WireMockExtensions { @Override List extensions() { return [ - new DefaultResponseTransformer(), new CustomExtension() ] } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy index e700b83c01..d410cab861 100755 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/WireMockGroovyDslSpec.groovy @@ -34,9 +34,9 @@ import org.springframework.cloud.contract.verifier.converter.YamlContractConvert import org.springframework.cloud.contract.verifier.file.ContractMetadata import org.springframework.cloud.contract.verifier.util.AssertionUtil import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter +import org.springframework.cloud.test.TestSocketUtils import org.springframework.http.RequestEntity import org.springframework.http.ResponseEntity -import org.springframework.cloud.test.TestSocketUtils class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifier { @@ -2250,7 +2250,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie "paramIndex":"bar2", "authorization":"secret", "path":"/api/v1/xxxx", - "rawUrl":"/api/v1/xxxx?foo=bar&foo=bar2", + "rawUrl":"/api/v1/xxxx?foo=bar&foo=bar2", "rawPath":"/api/v1/xxxx", "rawResponseBaz2":"Bla bla bar bla bla", "param":"bar", @@ -3199,14 +3199,10 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie } WireMockConfiguration config() { - return new WireMockConfiguration().extensions(responseTemplateTransformer()) + return new WireMockConfiguration().extensions(new HandlebarsJsonHelperExtension(), new HandlebarsEscapeHelperExtension()).globalTemplating(false).templatingEnabled(true) } - private ResponseTemplateTransformer responseTemplateTransformer() { - return new ResponseTemplateTransformer(false, - [(HandlebarsJsonPathHelper.NAME): new HandlebarsJsonPathHelper(), - (HandlebarsEscapeHelper.NAME) : new HandlebarsEscapeHelper()]) - } + ResponseEntity call(int port) { return new TestRestTemplate().exchange( diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/SyntaxChecker.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/SyntaxChecker.groovy index ceb01b0187..64557c24b4 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/SyntaxChecker.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/util/SyntaxChecker.groovy @@ -176,7 +176,7 @@ private void test(String test) { String className = className(test) String fqnClassName = "com.example.${className}" test = test.replaceAll("class FooTest", "class " + className) - .replaceAll("import javax.ws.rs.core.Response", "import javax.ws.rs.core.Response; import javax.ws.rs.client.WebTarget;") + .replaceAll("import javax.ws.rs.core.Response;", "import javax.ws.rs.core.Response; import javax.ws.rs.client.WebTarget;") return compileJava(fqnClassName, test) } diff --git a/spring-cloud-contract-wiremock/pom.xml b/spring-cloud-contract-wiremock/pom.xml index 33d24f4674..60aac6abf8 100644 --- a/spring-cloud-contract-wiremock/pom.xml +++ b/spring-cloud-contract-wiremock/pom.xml @@ -79,8 +79,8 @@ - com.github.tomakehurst - wiremock-jre8-standalone + org.wiremock + wiremock-standalone io.rest-assured 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 e2549c30aa..548c3537a3 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 @@ -26,7 +26,6 @@ import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.common.Notifier; import com.github.tomakehurst.wiremock.core.Options; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; import com.github.tomakehurst.wiremock.stubbing.StubMapping; import jakarta.annotation.PostConstruct; import org.apache.commons.logging.Log; @@ -104,7 +103,7 @@ public void init() throws IOException { registerFiles(factory); factory.notifier(new Slf4jNotifier(true)); if (this.wireMock.getPlaceholders().isEnabled()) { - factory.extensions(new ResponseTemplateTransformer(false)); + factory.templatingEnabled(true).globalTemplating(false); } this.options = factory; if (this.customizers != null) { diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/BasicMappingBuilder.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/BasicMappingBuilder.java index 0f9944b9a9..0feec200ae 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/BasicMappingBuilder.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/BasicMappingBuilder.java @@ -16,8 +16,11 @@ package org.springframework.cloud.contract.wiremock.restdocs; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import com.github.tomakehurst.wiremock.client.BasicCredentials; @@ -26,10 +29,13 @@ import com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder; import com.github.tomakehurst.wiremock.common.Metadata; import com.github.tomakehurst.wiremock.extension.Parameters; +import com.github.tomakehurst.wiremock.extension.ServeEventListener.RequestPhase; +import com.github.tomakehurst.wiremock.extension.ServeEventListenerDefinition; import com.github.tomakehurst.wiremock.http.Request; import com.github.tomakehurst.wiremock.http.RequestMethod; import com.github.tomakehurst.wiremock.http.ResponseDefinition; import com.github.tomakehurst.wiremock.matching.ContentPattern; +import com.github.tomakehurst.wiremock.matching.MultiValuePattern; import com.github.tomakehurst.wiremock.matching.MultipartValuePatternBuilder; import com.github.tomakehurst.wiremock.matching.RequestPattern; import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder; @@ -42,7 +48,7 @@ class BasicMappingBuilder implements ScenarioMappingBuilder { - private RequestPatternBuilder requestPatternBuilder; + private final RequestPatternBuilder requestPatternBuilder; private ResponseDefinitionBuilder responseDefBuilder; @@ -60,7 +66,9 @@ class BasicMappingBuilder implements ScenarioMappingBuilder { private boolean isPersistent = false; - private Map postServeActions = new LinkedHashMap<>(); + private final Map postServeActions = new LinkedHashMap<>(); + + private final List serveEventListeners = new ArrayList<>(); private Metadata metadata = new Metadata(); @@ -76,6 +84,10 @@ class BasicMappingBuilder implements ScenarioMappingBuilder { this.requestPatternBuilder = new RequestPatternBuilder(customRequestMatcherName, parameters); } + private static

Parameters resolveParameters(P parameters) { + return parameters instanceof Parameters ? (Parameters) parameters : Parameters.of(parameters); + } + @Override public BasicMappingBuilder willReturn(ResponseDefinitionBuilder responseDefBuilder) { this.responseDefBuilder = responseDefBuilder; @@ -112,6 +124,18 @@ public BasicMappingBuilder withHeader(String key, StringValuePattern headerPatte return this; } + @Override + public ScenarioMappingBuilder withHeader(String key, MultiValuePattern headerPattern) { + this.requestPatternBuilder.withHeader(key, headerPattern); + return this; + } + + @Override + public MappingBuilder withPathParam(String name, StringValuePattern pattern) { + this.requestPatternBuilder.withPathParam(name, pattern); + return this; + } + @Override public BasicMappingBuilder withCookie(String name, StringValuePattern cookieValuePattern) { this.requestPatternBuilder.withCookie(name, cookieValuePattern); @@ -124,6 +148,24 @@ public BasicMappingBuilder withQueryParam(String key, StringValuePattern queryPa return this; } + @Override + public ScenarioMappingBuilder withQueryParam(String key, MultiValuePattern queryParamPattern) { + this.requestPatternBuilder.withQueryParam(key, queryParamPattern); + return this; + } + + @Override + public ScenarioMappingBuilder withFormParam(String key, StringValuePattern formParamPattern) { + this.requestPatternBuilder.withFormParam(key, formParamPattern); + return this; + } + + @Override + public ScenarioMappingBuilder withFormParam(String key, MultiValuePattern formParamPattern) { + this.requestPatternBuilder.withFormParam(key, formParamPattern); + return this; + } + @Override public ScenarioMappingBuilder withQueryParams(Map queryParams) { for (Map.Entry entry : queryParams.entrySet()) { @@ -199,6 +241,20 @@ public

BasicMappingBuilder withPostServeAction(String extensionName, P param return this; } + @Override + public

MappingBuilder withServeEventListener(Set requestPhases, String extensionName, + P parameters) { + this.serveEventListeners + .add(new ServeEventListenerDefinition(extensionName, requestPhases, resolveParameters(parameters))); + return this; + } + + @Override + public

MappingBuilder withServeEventListener(String extensionName, P parameters) { + this.serveEventListeners.add(new ServeEventListenerDefinition(extensionName, resolveParameters(parameters))); + return this; + } + @Override public ScenarioMappingBuilder withMetadata(Map map) { this.metadata = new Metadata(map); diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java index 3dd7a49937..2cfc149119 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractExchangeHandler.java @@ -18,7 +18,7 @@ import java.io.IOException; import java.lang.reflect.Field; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -26,6 +26,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -34,12 +35,12 @@ import com.github.tomakehurst.wiremock.http.Body; import com.github.tomakehurst.wiremock.http.ContentTypeHeader; import com.github.tomakehurst.wiremock.http.Cookie; +import com.github.tomakehurst.wiremock.http.FormParameter; import com.github.tomakehurst.wiremock.http.HttpHeader; import com.github.tomakehurst.wiremock.http.QueryParameter; import com.github.tomakehurst.wiremock.http.Request; import com.github.tomakehurst.wiremock.http.RequestMethod; import org.apache.commons.codec.binary.Base64; -import wiremock.com.google.common.base.Optional; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -56,7 +57,6 @@ /** * @author Dave Syer - * */ public class ContractExchangeHandler extends WireMockVerifyHelper, ContractExchangeHandler> implements Consumer> { @@ -115,7 +115,7 @@ class WireMockHttpRequestAdapter implements Request { private static final boolean SERVLET_API_PRESENT = ClassUtils.isPresent("jakarta.servlet.http.Part", null); - private EntityExchangeResult result; + private final EntityExchangeResult result; WireMockHttpRequestAdapter(EntityExchangeResult result) { this.result = result; @@ -227,6 +227,25 @@ else if (name.startsWith(key + "=")) { return new QueryParameter(key, values); } + @Override + public FormParameter formParameter(String key) { + for (String pair : this.getBodyAsString().split("&")) { + String[] split = pair.split("="); + return new FormParameter(split[0], List.of(split[1])); + } + return FormParameter.absent(key); + } + + @Override + public Map formParameters() { + var value = new HashMap(); + for (String pair : this.getBodyAsString().split("&")) { + String[] split = pair.split("="); + value.put(split[0], new FormParameter(split[0], List.of(split[1]))); + } + return value; + } + @Override public byte[] getBody() { return this.result.getRequestBodyContent(); @@ -234,7 +253,7 @@ public byte[] getBody() { @Override public String getBodyAsString() { - return new String(this.result.getRequestBodyContent(), Charset.forName("UTF-8")); + return new String(this.result.getRequestBodyContent(), StandardCharsets.UTF_8); } @Override @@ -323,7 +342,7 @@ public boolean isBrowserProxyRequest() { @Override public Optional getOriginalRequest() { - return Optional.absent(); + return Optional.empty(); } @Override diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java index b8b5b5bfa1..fc56906b0a 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/ContractResultHandler.java @@ -19,12 +19,14 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -32,12 +34,12 @@ import com.github.tomakehurst.wiremock.http.Body; import com.github.tomakehurst.wiremock.http.ContentTypeHeader; import com.github.tomakehurst.wiremock.http.Cookie; +import com.github.tomakehurst.wiremock.http.FormParameter; import com.github.tomakehurst.wiremock.http.HttpHeader; import com.github.tomakehurst.wiremock.http.HttpHeaders; import com.github.tomakehurst.wiremock.http.QueryParameter; import com.github.tomakehurst.wiremock.http.Request; import com.github.tomakehurst.wiremock.http.RequestMethod; -import wiremock.com.google.common.base.Optional; import wiremock.org.apache.commons.io.IOUtils; import org.springframework.http.MediaType; @@ -193,6 +195,18 @@ public QueryParameter queryParameter(String key) { return new QueryParameter(key, Collections.singletonList(result.getRequest().getParameter(key))); } + @Override + public FormParameter formParameter(String key) { + return new FormParameter(key, Arrays.stream(result.getRequest().getParameterValues(key)).toList()); + } + + @Override + public Map formParameters() { + return result.getRequest().getParameterMap().entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, + (value) -> new FormParameter(value.getKey(), Arrays.asList(value.getValue())))); + } + @Override public byte[] getBody() { return result.getRequest().getContentAsByteArray(); @@ -270,7 +284,7 @@ public boolean isBrowserProxyRequest() { @Override public Optional getOriginalRequest() { - return Optional.absent(); + return Optional.empty(); } @Override diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java index b27169e9b6..52c5af1e6c 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/WiremockServerRestDocsApplicationTests.java @@ -20,7 +20,6 @@ import java.nio.file.Files; import java.util.Map; -import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.matching.MultiValuePattern; import com.github.tomakehurst.wiremock.stubbing.StubMapping; import jakarta.servlet.http.HttpServletRequest; @@ -85,10 +84,9 @@ public void queryParamsAreFetchedFromStubs() throws Exception { BDDAssertions.then(file).exists(); StubMapping stubMapping = StubMapping.buildFrom(new String(Files.readAllBytes(file.toPath()))); Map queryParameters = stubMapping.getRequest().getQueryParameters(); - BDDAssertions.then(queryParameters.get("callback").getValuePattern()) - .isEqualTo(WireMock.equalTo("a_function_name")); - BDDAssertions.then(queryParameters.get("foo").getValuePattern()).isEqualTo(WireMock.equalTo("foo")); - BDDAssertions.then(queryParameters.get("bar").getValuePattern()).isEqualTo(WireMock.equalTo("bar")); + BDDAssertions.then(queryParameters.get("callback").getExpected()).isEqualTo("a_function_name"); + BDDAssertions.then(queryParameters.get("foo").getExpected()).isEqualTo("foo"); + BDDAssertions.then(queryParameters.get("bar").getExpected()).isEqualTo("bar"); } @Test