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