Search in sources :

Example 1 with JsonPaths

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

the class ContractsToYaml method setOutputBodyMatchers.

protected void setOutputBodyMatchers(DslProperty<?> body, List<YamlContract.BodyTestMatcher> bodyMatchers) {
    Object testSideValues = MapConverter.getTestSideValues(body);
    JsonPaths paths = new JsonToJsonPathsConverter().transformToJsonPathWithTestsSideValues(body);
    paths.stream().filter(m -> m.valueBeforeChecking() instanceof Pattern).forEach((m) -> {
        Object element = JsonToJsonPathsConverter.readElement(testSideValues, m.keyBeforeChecking());
        YamlContract.BodyTestMatcher bodyTestMatcher = new YamlContract.BodyTestMatcher();
        bodyTestMatcher.path = m.keyBeforeChecking();
        bodyTestMatcher.type = YamlContract.TestMatcherType.by_regex;
        bodyTestMatcher.value = ((Pattern) m.valueBeforeChecking()).pattern();
        bodyTestMatcher.regexType = regexType(element);
        bodyMatchers.add(bodyTestMatcher);
    });
    Optional.ofNullable(body).filter(b -> b.getServerValue() instanceof Pattern).ifPresent((b) -> {
        YamlContract.BodyTestMatcher bodyTestMatcher = new YamlContract.BodyTestMatcher();
        bodyTestMatcher.type = YamlContract.TestMatcherType.by_regex;
        bodyTestMatcher.value = ((Pattern) b.getServerValue()).pattern();
        bodyMatchers.add(bodyTestMatcher);
    });
}
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) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths)

Example 2 with JsonPaths

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

the class RequestResponseSCContractCreator method mapResponseBodyRules.

private void mapResponseBodyRules(org.springframework.cloud.contract.spec.internal.Response contractResponse, Response pactResponse, Category bodyRules) {
    contractResponse.bodyMatchers((bodyMatchers) -> {
        bodyRules.getMatchingRules().forEach((key, matchindRuleGroup) -> {
            if (matchindRuleGroup.getRuleLogic() != RuleLogic.AND) {
                throw new UnsupportedOperationException("Currently only the AND combination rule logic is supported");
            }
            if (FULL_BODY.equals(key)) {
                JsonPaths jsonPaths = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(new String(pactResponse.getBody().getValue()));
                jsonPaths.forEach((jsonPath) -> bodyMatchers.jsonPath(jsonPath.keyBeforeChecking(), bodyMatchers.byType()));
            } else {
                matchindRuleGroup.getRules().forEach((matchingRule) -> {
                    if (matchingRule instanceof NullMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byNull());
                    } else if (matchingRule instanceof RegexMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byRegex(((RegexMatcher) matchingRule).getRegex()));
                    } else if (matchingRule instanceof DateMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byDate());
                    } else if (matchingRule instanceof TimeMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byTime());
                    } else if (matchingRule instanceof TimestampMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byTimestamp());
                    } else if (matchingRule instanceof MinTypeMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byType((valueHolder) -> valueHolder.minOccurrence((((MinTypeMatcher) matchingRule).getMin()))));
                    } else if (matchingRule instanceof MinMaxTypeMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byType((valueHolder) -> {
                            valueHolder.minOccurrence((((MinMaxTypeMatcher) matchingRule).getMin()));
                            valueHolder.maxOccurrence((((MinMaxTypeMatcher) matchingRule).getMax()));
                        }));
                    } else if (matchingRule instanceof MaxTypeMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byType((valueHolder) -> valueHolder.maxOccurrence((((MaxTypeMatcher) matchingRule).getMax()))));
                    } else if (matchingRule instanceof TypeMatcher) {
                        bodyMatchers.jsonPath(key, bodyMatchers.byType());
                    } else if (matchingRule instanceof NumberTypeMatcher) {
                        switch(((NumberTypeMatcher) matchingRule).getNumberType()) {
                            case NUMBER:
                                bodyMatchers.jsonPath(key, bodyMatchers.byRegex(RegexPatterns.number()));
                                break;
                            case INTEGER:
                                bodyMatchers.jsonPath(key, bodyMatchers.byRegex(RegexPatterns.anInteger()));
                                break;
                            case DECIMAL:
                                bodyMatchers.jsonPath(key, bodyMatchers.byRegex(RegexPatterns.aDouble()));
                                break;
                            default:
                                throw new UnsupportedOperationException("Unsupported number type!");
                        }
                    }
                });
            }
        });
    });
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) Interaction(au.com.dius.pact.core.model.Interaction) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponseInteraction(au.com.dius.pact.core.model.RequestResponseInteraction) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Request(au.com.dius.pact.core.model.Request) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) Stream(java.util.stream.Stream) ProviderState(au.com.dius.pact.core.model.ProviderState) OptionalBody(au.com.dius.pact.core.model.OptionalBody) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(au.com.dius.pact.core.model.Response) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher)

Example 3 with JsonPaths

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

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

use of org.springframework.cloud.contract.verifier.util.JsonPaths 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)

Aggregations

JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 DocumentContext (com.jayway.jsonpath.DocumentContext)6 BodyMatcher (org.springframework.cloud.contract.spec.internal.BodyMatcher)6 MethodBufferingJsonVerifiable (org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 JsonToJsonPathsConverter (org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter)5 Collection (java.util.Collection)4 List (java.util.List)4 Pattern (java.util.regex.Pattern)4 Collectors (java.util.stream.Collectors)4 Contract (org.springframework.cloud.contract.spec.Contract)4 Optional (java.util.Optional)3 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)3 HashMap (java.util.HashMap)2 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)2 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)2 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)2 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)2