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);
});
}
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;
}
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);
});
}
Aggregations