use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.
the class WireMockRequestStubStrategy method doAppendBody.
private void doAppendBody(RequestPatternBuilder requestPattern) {
if (request.getBody() == null) {
return;
}
boolean bodyHasMatchingStrategy = request.getBody().getClientValue() instanceof MatchingStrategy;
MatchingStrategy matchingStrategy = getMatchingStrategyFromBody(request.getBody());
if (contentType == ContentType.JSON) {
Object clientSideBody = MapConverter.transformToClientValues(request.getBody());
Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
if (bodyHasMatchingStrategy) {
requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
} else if (clientSideBody instanceof Pattern || clientSideBody instanceof RegexProperty) {
requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody(), contentType)));
} else {
Object body = JsonToJsonPathsConverter.removeMatchingJsonPaths(originalBody, request.getBodyMatchers());
JsonPaths values = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(body);
if ((values.isEmpty() && request.getBodyMatchers() != null && !request.getBodyMatchers().hasMatchers()) || onlySizeAssertionsArePresent(values)) {
try {
requestPattern.withRequestBody(WireMock.equalToJson(new ObjectMapper().writeValueAsString(getMatchingStrategy(request.getBody().getClientValue()).getClientValue()), false, false));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("The MatchingStrategy could not be serialized", e);
}
} else {
values.stream().filter(v -> !v.assertsSize()).forEach(it -> requestPattern.withRequestBody(WireMock.matchingJsonPath(it.jsonPath().replace("\\\\", "\\"))));
}
}
Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> {
String newPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(bodyMatcher, originalBody);
requestPattern.withRequestBody(WireMock.matchingJsonPath(newPath.replace("\\\\", "\\")));
}));
} else if (contentType == ContentType.XML) {
Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
if (bodyHasMatchingStrategy) {
requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
} else {
Object body = XmlToXPathsConverter.removeMatchingXPaths(originalBody, request.getBodyMatchers());
List<BodyMatcher> byEqualityMatchersFromXml = XmlToXPathsConverter.mapToMatchers(body);
byEqualityMatchersFromXml.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody));
}
Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody)));
} else if (containsPattern(request.getBody())) {
requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody())));
} else {
requestBodyGuessedFromMatchingStrategy(requestPattern);
}
}
use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.
the class WireMockRequestStubStrategy method getMatchingStrategyIncludingContentType.
private MatchingStrategy getMatchingStrategyIncludingContentType(MatchingStrategy matchingStrategy) {
MatchingStrategy.Type type = matchingStrategy.getType();
Object value = matchingStrategy.getClientValue();
ContentType contentType = ContentUtils.recognizeContentTypeFromMatchingStrategy(type);
if (contentType == ContentType.UNKNOWN && type == MatchingStrategy.Type.EQUAL_TO) {
contentType = ContentUtils.recognizeContentTypeFromContent(value);
type = getEqualsTypeFromContentType(contentType);
}
MatchingStrategy newMatchingStrategy;
if (value instanceof Map) {
newMatchingStrategy = new MatchingStrategy(parseBody((Map<?, ?>) value, contentType), type);
} else if (value instanceof List) {
newMatchingStrategy = new MatchingStrategy(parseBody((List<?>) value, contentType), type);
} else if (value instanceof GString) {
newMatchingStrategy = new MatchingStrategy(parseBody((GString) value, contentType), type);
} else {
newMatchingStrategy = new MatchingStrategy(parseBody(value, contentType), type);
}
return newMatchingStrategy;
}
use of org.springframework.cloud.contract.spec.internal.MatchingStrategy in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method mapRequestMatchersQueryParameters.
private void mapRequestMatchersQueryParameters(YamlContract.Request yamlContractRequest, Url requestUrl) {
yamlContractRequest.matchers.queryParameters.addAll(requestUrl.getQueryParameters().getParameters().stream().map(parameter -> {
Object stubSide = parameter.getClientValue();
if (stubSide instanceof RegexProperty || stubSide instanceof Pattern) {
YamlContract.QueryParameterMatcher queryParameterMatcher = new YamlContract.QueryParameterMatcher();
queryParameterMatcher.key = parameter.getName();
queryParameterMatcher.type = YamlContract.MatchingType.matching;
queryParameterMatcher.value = new RegexProperty(stubSide).pattern();
return queryParameterMatcher;
} else if (stubSide instanceof MatchingStrategy) {
YamlContract.QueryParameterMatcher queryParameterMatcher = new YamlContract.QueryParameterMatcher();
queryParameterMatcher.key = parameter.getName();
queryParameterMatcher.type = YamlContract.MatchingType.from(((MatchingStrategy) stubSide).getType().getName());
queryParameterMatcher.value = MapConverter.getStubSideValuesForNonBody(stubSide);
return queryParameterMatcher;
} else {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList()));
}
Aggregations