Search in sources :

Example 1 with BodyMatcher

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

the class XmlBodyVerificationBuilder method addXmlResponseBodyCheck.

void addXmlResponseBodyCheck(BlockBuilder blockBuilder, Object responseBody, BodyMatchers bodyMatchers, String responseString, boolean shouldCommentOutBDDBlocks) {
    addXmlProcessingLines(blockBuilder, responseString);
    Object processedBody = XmlToXPathsConverter.removeMatchingXPaths(responseBody, bodyMatchers);
    List<BodyMatcher> matchers = new XmlToXPathsConverter().mapToMatchers(processedBody);
    if (bodyMatchers != null && bodyMatchers.hasMatchers()) {
        matchers.addAll(bodyMatchers.matchers());
    }
    addBodyMatchingBlock(matchers, blockBuilder, responseBody, shouldCommentOutBDDBlocks);
}
Also used : XmlToXPathsConverter(org.springframework.cloud.contract.verifier.util.xml.XmlToXPathsConverter) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher)

Example 2 with BodyMatcher

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

the class StubRunnerJmsMessageSelector method matchesForJsonPayload.

private boolean matchesForJsonPayload(Contract groovyDsl, Object inputMessage, BodyMatchers matchers, Object dslBody) {
    Object matchingInputMessage = JsonToJsonPathsConverter.removeMatchingJsonPaths(dslBody, matchers);
    JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(matchingInputMessage);
    DocumentContext parsedJson;
    try {
        parsedJson = JsonPath.parse(this.objectMapper.writeValueAsString(inputMessage));
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot serialize to JSON", e);
    }
    List<String> unmatchedJsonPath = new ArrayList<>();
    boolean matches = true;
    for (MethodBufferingJsonVerifiable path : jsonPaths) {
        matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, path.jsonPath());
    }
    if (matchers != null && matchers.hasMatchers()) {
        for (BodyMatcher matcher : matchers.matchers()) {
            String jsonPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(matcher, dslBody);
            matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, jsonPath);
        }
    }
    if (!unmatchedJsonPath.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Contract [" + groovyDsl + "] didn't match the body due to " + unmatchedJsonPath);
        }
    }
    return matches;
}
Also used : MethodBufferingJsonVerifiable(org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable) ArrayList(java.util.ArrayList) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher)

Example 3 with BodyMatcher

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

the class StubRunnerCamelPredicate method matchesForJsonPayload.

private boolean matchesForJsonPayload(Contract groovyDsl, Object inputMessage, BodyMatchers matchers, Object dslBody) {
    Object matchingInputMessage = JsonToJsonPathsConverter.removeMatchingJsonPaths(dslBody, matchers);
    JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(matchingInputMessage);
    DocumentContext parsedJson;
    try {
        parsedJson = JsonPath.parse(this.objectMapper.writeValueAsString(inputMessage));
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot serialize to JSON", e);
    }
    List<String> unmatchedJsonPath = new ArrayList<>();
    boolean matches = true;
    for (MethodBufferingJsonVerifiable path : jsonPaths) {
        matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, path.jsonPath());
    }
    if (matchers != null && matchers.hasMatchers()) {
        for (BodyMatcher matcher : matchers.matchers()) {
            String jsonPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(matcher, dslBody);
            matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, jsonPath);
        }
    }
    if (!unmatchedJsonPath.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Contract [" + groovyDsl + "] didn't match the body due to " + unmatchedJsonPath);
        }
    }
    return matches;
}
Also used : MethodBufferingJsonVerifiable(org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable) ArrayList(java.util.ArrayList) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher)

Example 4 with BodyMatcher

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

the class StubRunnerIntegrationMessageSelector method matchesForJsonPayload.

private boolean matchesForJsonPayload(Contract groovyDsl, Object inputMessage, BodyMatchers matchers, Object dslBody) {
    Object matchingInputMessage = JsonToJsonPathsConverter.removeMatchingJsonPaths(dslBody, matchers);
    JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(matchingInputMessage);
    DocumentContext parsedJson;
    try {
        parsedJson = JsonPath.parse(this.objectMapper.writeValueAsString(inputMessage));
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot serialize to JSON", e);
    }
    List<String> unmatchedJsonPath = new ArrayList<>();
    boolean matches = true;
    for (MethodBufferingJsonVerifiable path : jsonPaths) {
        matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, path.jsonPath());
    }
    if (matchers != null && matchers.hasMatchers()) {
        for (BodyMatcher matcher : matchers.matchers()) {
            String jsonPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(matcher, dslBody);
            matches &= matchesJsonPath(unmatchedJsonPath, parsedJson, jsonPath);
        }
    }
    if (!unmatchedJsonPath.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Contract [" + groovyDsl + "] didn't match the body due to " + unmatchedJsonPath);
        }
    }
    return matches;
}
Also used : MethodBufferingJsonVerifiable(org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable) ArrayList(java.util.ArrayList) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher)

Example 5 with BodyMatcher

use of org.springframework.cloud.contract.spec.internal.BodyMatcher in project eventuate-tram-core by eventuate-tram.

the class EventuateTramRoutesConfigurer method satisfies.

private boolean satisfies(Message message, Contract groovyDsl) {
    if (!headersMatch(message, groovyDsl)) {
        logger.info("Headers don't match {} {} ", groovyDsl.getLabel(), message);
        return false;
    }
    BodyMatchers matchers = groovyDsl.getInput().getBodyMatchers();
    Object dslBody = MapConverter.getStubSideValues(groovyDsl.getInput().getMessageBody());
    Object matchingInputMessage = JsonToJsonPathsConverter.removeMatchingJsonPaths(dslBody, matchers);
    JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(matchingInputMessage);
    DocumentContext parsedJson = JsonPath.parse(message.getPayload());
    boolean matches = true;
    for (MethodBufferingJsonVerifiable path : jsonPaths) {
        matches &= matchesJsonPath(parsedJson, path.jsonPath());
    }
    logger.info("jsonPaths match {} {} {} ", groovyDsl.getLabel(), matches, message);
    if (matchers != null && matchers.hasMatchers()) {
        for (BodyMatcher matcher : matchers.jsonPathMatchers()) {
            String jsonPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(matcher, dslBody);
            matches &= matchesJsonPath(parsedJson, jsonPath);
        }
    }
    logger.info("matchers {} {} {} ", groovyDsl.getLabel(), matches, message);
    return matches;
}
Also used : BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) DocumentContext(com.jayway.jsonpath.DocumentContext) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher)

Aggregations

BodyMatcher (org.springframework.cloud.contract.spec.internal.BodyMatcher)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 DocumentContext (com.jayway.jsonpath.DocumentContext)6 JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)6 MethodBufferingJsonVerifiable (org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable)6 ArrayList (java.util.ArrayList)5 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 Parameters (com.github.tomakehurst.wiremock.extension.Parameters)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1 ContentPattern (com.github.tomakehurst.wiremock.matching.ContentPattern)1 RequestPattern (com.github.tomakehurst.wiremock.matching.RequestPattern)1 RequestPatternBuilder (com.github.tomakehurst.wiremock.matching.RequestPatternBuilder)1 StringValuePattern (com.github.tomakehurst.wiremock.matching.StringValuePattern)1 UrlPattern (com.github.tomakehurst.wiremock.matching.UrlPattern)1 GString (groovy.lang.GString)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1