Search in sources :

Example 1 with PathBodyMatcher

use of org.springframework.cloud.contract.spec.internal.PathBodyMatcher in project spring-cloud-contract by spring-cloud.

the class WireMockRequestStubStrategy method addWireMockStubMatchingSection.

private static void addWireMockStubMatchingSection(BodyMatcher matcher, RequestPatternBuilder requestPattern, Object body) {
    Set<MatchingType> matchingTypesUnsupportedForRequest = new HashSet<>(Arrays.asList(MatchingType.NULL, MatchingType.COMMAND, MatchingType.TYPE));
    if (!(matcher instanceof PathBodyMatcher)) {
        throw new IllegalArgumentException("Only jsonPath and XPath matchers can be processed.");
    }
    String retrievedValue = Optional.ofNullable(matcher.value()).map(Object::toString).orElseGet(() -> {
        if (matchingTypesUnsupportedForRequest.contains(matcher.matchingType())) {
            throw new IllegalArgumentException("Null, Command and Type matchers are not supported in requests.");
        }
        if (EQUALITY == matcher.matchingType()) {
            return retrieveValue(matcher, body);
        } else {
            return "";
        }
    });
    PathBodyMatcher pathMatcher = (PathBodyMatcher) matcher;
    requestPattern.withRequestBody(WireMock.matchingXPath(pathMatcher.path(), XPathBodyMatcherToWireMockValuePatternConverter.mapToPattern(pathMatcher.matchingType(), String.valueOf(retrievedValue))));
}
Also used : MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) PathBodyMatcher(org.springframework.cloud.contract.spec.internal.PathBodyMatcher) GString(groovy.lang.GString) HashSet(java.util.HashSet)

Aggregations

GString (groovy.lang.GString)1 HashSet (java.util.HashSet)1 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)1 PathBodyMatcher (org.springframework.cloud.contract.spec.internal.PathBodyMatcher)1