Search in sources :

Example 1 with JsonToJsonPathsConverter

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

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

the class JsonBodyVerificationBuilder method addJsonResponseBodyCheck.

Object addJsonResponseBodyCheck(BlockBuilder bb, Object convertedResponseBody, BodyMatchers bodyMatchers, String responseString, boolean shouldCommentOutBDDBlocks) {
    appendJsonPath(bb, responseString);
    DocumentContext parsedRequestBody = null;
    boolean dontParseStrings = convertedResponseBody instanceof Map;
    Function<String, Object> parsingFunction = dontParseStrings ? MapConverter.IDENTITY : MapConverter.JSON_PARSING_FUNCTION;
    if (hasRequestBody()) {
        Object testSideRequestBody = MapConverter.getTestSideValues(contract.getRequest().getBody(), parsingFunction);
        parsedRequestBody = JsonPath.parse(testSideRequestBody);
        if (convertedResponseBody instanceof String && !textContainsJsonPathTemplate(convertedResponseBody.toString())) {
            convertedResponseBody = templateProcessor.transform(contract.getRequest(), convertedResponseBody.toString());
        }
    }
    Object copiedBody = cloneBody(convertedResponseBody);
    convertedResponseBody = JsonToJsonPathsConverter.removeMatchingJsonPaths(convertedResponseBody, bodyMatchers);
    // If it was a map or list where all elements covered by matchers - json paths
    // should not include empty check
    boolean includeEmptyCheck = !mapOrListBodyReducedToEmpty(copiedBody, convertedResponseBody);
    // remove quotes from fromRequest objects before picking json paths
    TestSideRequestTemplateModel templateModel = hasRequestBody() ? TestSideRequestTemplateModel.from(contract.getRequest()) : null;
    convertedResponseBody = MapConverter.transformValues(convertedResponseBody, returnReferencedEntries(templateModel), parsingFunction);
    JsonPaths jsonPaths = new JsonToJsonPathsConverter(assertJsonSize).transformToJsonPathWithTestsSideValues(convertedResponseBody, parsingFunction, includeEmptyCheck);
    DocumentContext finalParsedRequestBody = parsedRequestBody;
    jsonPaths.forEach(it -> {
        String method = it.method();
        method = processIfTemplateIsPresent(method, finalParsedRequestBody);
        String postProcessedMethod = templateProcessor.containsJsonPathTemplateEntry(method) ? method : postProcessJsonPathCall.apply(method);
        bb.addLine("assertThatJson(parsedJson)" + postProcessedMethod);
        addColonIfRequired(lineSuffix, bb);
    });
    doBodyMatchingIfPresent(bodyMatchers, bb, copiedBody, shouldCommentOutBDDBlocks);
    return convertedResponseBody;
}
Also used : JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) DocumentContext(com.jayway.jsonpath.DocumentContext) Map(java.util.Map)

Example 3 with JsonToJsonPathsConverter

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

the class ContractsToYaml method setInputBodyMatchers.

protected void setInputBodyMatchers(DslProperty<?> body, List<YamlContract.BodyStubMatcher> bodyMatchers) {
    Object testSideValues = MapConverter.getTestSideValues(body);
    JsonPaths paths = new JsonToJsonPathsConverter().transformToJsonPathWithStubsSideValues(body);
    paths.stream().filter((path) -> path.valueBeforeChecking() instanceof Pattern).forEach((path) -> {
        Object element = JsonToJsonPathsConverter.readElement(testSideValues, path.keyBeforeChecking());
        YamlContract.BodyStubMatcher bodyStubMatcher = new YamlContract.BodyStubMatcher();
        bodyStubMatcher.path = path.keyBeforeChecking();
        bodyStubMatcher.type = YamlContract.StubMatcherType.by_regex;
        bodyStubMatcher.value = ((Pattern) path.valueBeforeChecking()).pattern();
        bodyStubMatcher.regexType = regexType(element);
        bodyMatchers.add(bodyStubMatcher);
    });
}
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)

Aggregations

Map (java.util.Map)3 JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)3 JsonToJsonPathsConverter (org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)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 Cookies (org.springframework.cloud.contract.spec.internal.Cookies)2 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)2 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)2 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)2 Headers (org.springframework.cloud.contract.spec.internal.Headers)2 Input (org.springframework.cloud.contract.spec.internal.Input)2 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)2 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)2