Search in sources :

Example 1 with MethodBufferingJsonVerifiable

use of org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable 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 2 with MethodBufferingJsonVerifiable

use of org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable 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 3 with MethodBufferingJsonVerifiable

use of org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable 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 4 with MethodBufferingJsonVerifiable

use of org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable in project spring-cloud-contract by spring-cloud.

the class StubRunnerKafkaMessageSelector 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 MethodBufferingJsonVerifiable

use of org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable in project spring-cloud-contract by spring-cloud.

the class StubRunnerStreamMessageSelector 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 DocumentContext (com.jayway.jsonpath.DocumentContext)5 ArrayList (java.util.ArrayList)5 BodyMatcher (org.springframework.cloud.contract.spec.internal.BodyMatcher)5 JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)5 MethodBufferingJsonVerifiable (org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable)5