use of org.springframework.cloud.contract.spec.internal.MatchingType in project spring-cloud-contract by spring-cloud.
the class MatchingRulesConverter method matchingRulesFor.
private static Category matchingRulesFor(String categoryName, BodyMatchers bodyMatchers) {
Category category = new Category(categoryName);
bodyMatchers.matchers().forEach((b) -> {
String key = getMatcherKey(b.path());
MatchingType matchingType = b.matchingType();
switch(matchingType) {
case NULL:
category.addRule(key, NullMatcher.INSTANCE);
break;
case EQUALITY:
category.addRule(key, EqualsMatcher.INSTANCE);
break;
case TYPE:
if (b.minTypeOccurrence() != null && b.maxTypeOccurrence() != null) {
category.addRule(key, new MinMaxTypeMatcher(b.minTypeOccurrence(), b.maxTypeOccurrence()));
} else if (b.minTypeOccurrence() != null) {
category.addRule(key, new MinTypeMatcher(b.minTypeOccurrence()));
} else if (b.maxTypeOccurrence() != null) {
category.addRule(key, new MaxTypeMatcher(b.maxTypeOccurrence()));
} else {
category.addRule(key, TypeMatcher.INSTANCE);
}
break;
case DATE:
category.addRule(key, new DateMatcher());
break;
case TIME:
category.addRule(key, new TimeMatcher());
break;
case TIMESTAMP:
category.addRule(key, new TimestampMatcher());
break;
case REGEX:
String pattern = b.value().toString();
if (pattern.equals(RegexPatterns.number().pattern())) {
category.addRule(key, new NumberTypeMatcher(NumberTypeMatcher.NumberType.NUMBER));
} else if (pattern.equals(RegexPatterns.anInteger().pattern())) {
category.addRule(key, new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER));
} else if (pattern.equals(RegexPatterns.aDouble().pattern())) {
category.addRule(key, new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL));
} else {
category.addRule(key, new RegexMatcher(pattern));
}
break;
default:
break;
}
});
return category;
}
use of org.springframework.cloud.contract.spec.internal.MatchingType 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))));
}
Aggregations