Search in sources :

Example 1 with MatchingType

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;
}
Also used : MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) Category(au.com.dius.pact.core.model.matchingrules.Category) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher)

Example 2 with MatchingType

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

MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)2 Category (au.com.dius.pact.core.model.matchingrules.Category)1 DateMatcher (au.com.dius.pact.core.model.matchingrules.DateMatcher)1 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)1 MinMaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher)1 MinTypeMatcher (au.com.dius.pact.core.model.matchingrules.MinTypeMatcher)1 NumberTypeMatcher (au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher)1 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)1 TimeMatcher (au.com.dius.pact.core.model.matchingrules.TimeMatcher)1 TimestampMatcher (au.com.dius.pact.core.model.matchingrules.TimestampMatcher)1 GString (groovy.lang.GString)1 HashSet (java.util.HashSet)1 PathBodyMatcher (org.springframework.cloud.contract.spec.internal.PathBodyMatcher)1