Search in sources :

Example 1 with MatchingStrategy

use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.

the class WireMockRequestStubStrategy method doAppendBody.

private void doAppendBody(RequestPatternBuilder requestPattern) {
    if (request.getBody() == null) {
        return;
    }
    boolean bodyHasMatchingStrategy = request.getBody().getClientValue() instanceof MatchingStrategy;
    MatchingStrategy matchingStrategy = getMatchingStrategyFromBody(request.getBody());
    if (contentType == ContentType.JSON) {
        Object clientSideBody = MapConverter.transformToClientValues(request.getBody());
        Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
        if (bodyHasMatchingStrategy) {
            requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
        } else if (clientSideBody instanceof Pattern || clientSideBody instanceof RegexProperty) {
            requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody(), contentType)));
        } else {
            Object body = JsonToJsonPathsConverter.removeMatchingJsonPaths(originalBody, request.getBodyMatchers());
            JsonPaths values = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(body);
            if ((values.isEmpty() && request.getBodyMatchers() != null && !request.getBodyMatchers().hasMatchers()) || onlySizeAssertionsArePresent(values)) {
                try {
                    requestPattern.withRequestBody(WireMock.equalToJson(new ObjectMapper().writeValueAsString(getMatchingStrategy(request.getBody().getClientValue()).getClientValue()), false, false));
                } catch (JsonProcessingException e) {
                    throw new IllegalArgumentException("The MatchingStrategy could not be serialized", e);
                }
            } else {
                values.stream().filter(v -> !v.assertsSize()).forEach(it -> requestPattern.withRequestBody(WireMock.matchingJsonPath(it.jsonPath().replace("\\\\", "\\"))));
            }
        }
        Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> {
            String newPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(bodyMatcher, originalBody);
            requestPattern.withRequestBody(WireMock.matchingJsonPath(newPath.replace("\\\\", "\\")));
        }));
    } else if (contentType == ContentType.XML) {
        Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
        if (bodyHasMatchingStrategy) {
            requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
        } else {
            Object body = XmlToXPathsConverter.removeMatchingXPaths(originalBody, request.getBodyMatchers());
            List<BodyMatcher> byEqualityMatchersFromXml = XmlToXPathsConverter.mapToMatchers(body);
            byEqualityMatchersFromXml.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody));
        }
        Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody)));
    } else if (containsPattern(request.getBody())) {
        requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody())));
    } else {
        requestBodyGuessedFromMatchingStrategy(requestPattern);
    }
}
Also used : Arrays(java.util.Arrays) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) XmlToXPathsConverter.retrieveValue(org.springframework.cloud.contract.verifier.util.xml.XmlToXPathsConverter.retrieveValue) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) SingleContractMetadata(org.springframework.cloud.contract.verifier.file.SingleContractMetadata) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher) RegexpBuilders.buildGStringRegexpForStubSide(org.springframework.cloud.contract.verifier.util.RegexpBuilders.buildGStringRegexpForStubSide) QueryParameters(org.springframework.cloud.contract.spec.internal.QueryParameters) YamlContractConverter(org.springframework.cloud.contract.verifier.converter.YamlContractConverter) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) FORM(org.springframework.cloud.contract.verifier.util.ContentType.FORM) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) ContentUtils(org.springframework.cloud.contract.verifier.util.ContentUtils) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) RegexpBuilders.buildJSONRegexpMatch(org.springframework.cloud.contract.verifier.util.RegexpBuilders.buildJSONRegexpMatch) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) GString(groovy.lang.GString) ClientDslProperty(org.springframework.cloud.contract.spec.internal.ClientDslProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) StringValuePattern(com.github.tomakehurst.wiremock.matching.StringValuePattern) PathBodyMatcher(org.springframework.cloud.contract.spec.internal.PathBodyMatcher) OptionalProperty(org.springframework.cloud.contract.spec.internal.OptionalProperty) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) ContentPattern(com.github.tomakehurst.wiremock.matching.ContentPattern) Pattern(java.util.regex.Pattern) EQUALITY(org.springframework.cloud.contract.spec.internal.MatchingType.EQUALITY) ContentUtils.getEqualsTypeFromContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.getEqualsTypeFromContentType) Request(org.springframework.cloud.contract.spec.internal.Request) Function(java.util.function.Function) WireMock(com.github.tomakehurst.wiremock.client.WireMock) HashSet(java.util.HashSet) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) Body(org.springframework.cloud.contract.spec.internal.Body) Url(org.springframework.cloud.contract.spec.internal.Url) XmlToXPathsConverter(org.springframework.cloud.contract.verifier.util.xml.XmlToXPathsConverter) Parameters(com.github.tomakehurst.wiremock.extension.Parameters) Iterator(java.util.Iterator) JSON(org.springframework.cloud.contract.verifier.util.ContentType.JSON) MethodBufferingJsonVerifiable(org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ContractVerifierMetadata(org.springframework.cloud.contract.verifier.dsl.ContractVerifierMetadata) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Collections(java.util.Collections) BINARY_EQUAL_TO(org.springframework.cloud.contract.spec.internal.MatchingStrategy.Type.BINARY_EQUAL_TO) StringUtils(org.springframework.util.StringUtils) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) StringValuePattern(com.github.tomakehurst.wiremock.matching.StringValuePattern) ContentPattern(com.github.tomakehurst.wiremock.matching.ContentPattern) Pattern(java.util.regex.Pattern) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) GString(groovy.lang.GString) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) ClientDslProperty(org.springframework.cloud.contract.spec.internal.ClientDslProperty) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) List(java.util.List) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with MatchingStrategy

use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.

the class WireMockRequestStubStrategy method getMatchingStrategyIncludingContentType.

private MatchingStrategy getMatchingStrategyIncludingContentType(MatchingStrategy matchingStrategy) {
    MatchingStrategy.Type type = matchingStrategy.getType();
    Object value = matchingStrategy.getClientValue();
    ContentType contentType = ContentUtils.recognizeContentTypeFromMatchingStrategy(type);
    if (contentType == ContentType.UNKNOWN && type == MatchingStrategy.Type.EQUAL_TO) {
        contentType = ContentUtils.recognizeContentTypeFromContent(value);
        type = getEqualsTypeFromContentType(contentType);
    }
    MatchingStrategy newMatchingStrategy;
    if (value instanceof Map) {
        newMatchingStrategy = new MatchingStrategy(parseBody((Map<?, ?>) value, contentType), type);
    } else if (value instanceof List) {
        newMatchingStrategy = new MatchingStrategy(parseBody((List<?>) value, contentType), type);
    } else if (value instanceof GString) {
        newMatchingStrategy = new MatchingStrategy(parseBody((GString) value, contentType), type);
    } else {
        newMatchingStrategy = new MatchingStrategy(parseBody(value, contentType), type);
    }
    return newMatchingStrategy;
}
Also used : ContentType(org.springframework.cloud.contract.verifier.util.ContentType) ContentUtils.getEqualsTypeFromContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.getEqualsTypeFromContentType) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) List(java.util.List) GString(groovy.lang.GString) Map(java.util.Map)

Example 3 with MatchingStrategy

use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.

the class ContractsToYaml method mapRequestMatchersQueryParameters.

private void mapRequestMatchersQueryParameters(YamlContract.Request yamlContractRequest, Url requestUrl) {
    yamlContractRequest.matchers.queryParameters.addAll(requestUrl.getQueryParameters().getParameters().stream().map(parameter -> {
        Object stubSide = parameter.getClientValue();
        if (stubSide instanceof RegexProperty || stubSide instanceof Pattern) {
            YamlContract.QueryParameterMatcher queryParameterMatcher = new YamlContract.QueryParameterMatcher();
            queryParameterMatcher.key = parameter.getName();
            queryParameterMatcher.type = YamlContract.MatchingType.matching;
            queryParameterMatcher.value = new RegexProperty(stubSide).pattern();
            return queryParameterMatcher;
        } else if (stubSide instanceof MatchingStrategy) {
            YamlContract.QueryParameterMatcher queryParameterMatcher = new YamlContract.QueryParameterMatcher();
            queryParameterMatcher.key = parameter.getName();
            queryParameterMatcher.type = YamlContract.MatchingType.from(((MatchingStrategy) stubSide).getType().getName());
            queryParameterMatcher.value = MapConverter.getStubSideValuesForNonBody(stubSide);
            return queryParameterMatcher;
        } else {
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList()));
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) HashMap(java.util.HashMap) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) Request(org.springframework.cloud.contract.spec.internal.Request) XML(org.springframework.cloud.contract.verifier.util.ContentType.XML) OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Cookies(org.springframework.cloud.contract.spec.internal.Cookies) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) Url(org.springframework.cloud.contract.spec.internal.Url) Input(org.springframework.cloud.contract.spec.internal.Input) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) Multipart(org.springframework.cloud.contract.spec.internal.Multipart) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) QueryParameter(org.springframework.cloud.contract.spec.internal.QueryParameter) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Response(org.springframework.cloud.contract.spec.internal.Response) Headers(org.springframework.cloud.contract.spec.internal.Headers) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) Pattern(java.util.regex.Pattern) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) Objects(java.util.Objects)

Aggregations

List (java.util.List)3 Map (java.util.Map)3 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)3 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)3 GString (groovy.lang.GString)2 Collection (java.util.Collection)2 Optional (java.util.Optional)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Contract (org.springframework.cloud.contract.spec.Contract)2 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)2 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)2 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)2 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)2 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)2 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)2 Request (org.springframework.cloud.contract.spec.internal.Request)2 Url (org.springframework.cloud.contract.spec.internal.Url)2 ContentUtils.getEqualsTypeFromContentType (org.springframework.cloud.contract.verifier.util.ContentUtils.getEqualsTypeFromContentType)2 JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)2