Search in sources :

Example 1 with MatchingRule

use of au.com.dius.pact.core.model.matchingrules.MatchingRule in project spring-cloud-contract by spring-cloud.

the class RequestResponseSCContractCreator method mapRequestHeaders.

private void mapRequestHeaders(org.springframework.cloud.contract.spec.internal.Request contractRequest, Request pactRequest) {
    Category headerRules = pactRequest.getMatchingRules().rulesForCategory("header");
    contractRequest.headers((headers) -> pactRequest.getHeaders().forEach((key, values) -> {
        if (key.equalsIgnoreCase("Cookie")) {
            return;
        }
        if (headerRules.getMatchingRules().containsKey(key)) {
            MatchingRuleGroup matchingRuleGroup = headerRules.getMatchingRules().get(key);
            if (matchingRuleGroup.getRules().size() > 1) {
                throw new UnsupportedOperationException("Currently only 1 rule at a time for a header is supported");
            }
            MatchingRule matchingRule = matchingRuleGroup.getRules().get(0);
            if (matchingRule instanceof RegexMatcher) {
                values.forEach((value) -> {
                    headers.header(key, contractRequest.$(contractRequest.c(contractRequest.regex(((RegexMatcher) matchingRule).getRegex())), contractRequest.p(value)));
                });
            } else {
                throw new UnsupportedOperationException("Currently only the header matcher of type regex is supported");
            }
        } else {
            values.forEach((value) -> headers.header(key, value));
        }
    }));
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) Interaction(au.com.dius.pact.core.model.Interaction) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponseInteraction(au.com.dius.pact.core.model.RequestResponseInteraction) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Request(au.com.dius.pact.core.model.Request) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) Stream(java.util.stream.Stream) ProviderState(au.com.dius.pact.core.model.ProviderState) OptionalBody(au.com.dius.pact.core.model.OptionalBody) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(au.com.dius.pact.core.model.Response) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule)

Example 2 with MatchingRule

use of au.com.dius.pact.core.model.matchingrules.MatchingRule 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!");
                        }
                    }
                });
            }
        });
    });
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) Interaction(au.com.dius.pact.core.model.Interaction) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponseInteraction(au.com.dius.pact.core.model.RequestResponseInteraction) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Request(au.com.dius.pact.core.model.Request) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) Stream(java.util.stream.Stream) ProviderState(au.com.dius.pact.core.model.ProviderState) OptionalBody(au.com.dius.pact.core.model.OptionalBody) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(au.com.dius.pact.core.model.Response) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) 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) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher)

Example 3 with MatchingRule

use of au.com.dius.pact.core.model.matchingrules.MatchingRule in project pact-jvm by DiUS.

the class PactDslRootValueTest method rootValueTest.

@Test
public void rootValueTest() {
    PactDslRootValue rootValueBody = new PactDslRootValue();
    rootValueBody.setValue("Brent");
    rootValueBody.setMatcher(new RegexMatcher(".{5}"));
    PactDslWithProvider dsl = new PactDslWithProvider(new ConsumerPactBuilder("consumer"), "provider");
    Map<String, String> headers = new HashMap<String, String>() {

        {
            put("Content-Type", "text/plain");
        }
    };
    RequestResponsePact frag = dsl.given("I am testing root values").uponReceiving("A request for text/plain").path("/some/blah/path").headers(headers).willRespondWith().headers(headers).status(200).body(rootValueBody).toPact().asRequestResponsePact().component1();
    Assert.assertEquals(1, frag.getInteractions().size());
    assertThat(frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getBody().valueAsString(), is("Brent"));
    Map<String, MatchingRuleGroup> matchingGroups = frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules();
    List<MatchingRule> rules = matchingGroups.get("$").getRules();
    Assert.assertEquals(1, rules.size());
    Assert.assertEquals(".{5}", rules.get(0).toMap(PactSpecVersion.V3).get("regex"));
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) PactDslRootValue(au.com.dius.pact.consumer.dsl.PactDslRootValue) HashMap(java.util.HashMap) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Test(org.junit.Test)

Example 4 with MatchingRule

use of au.com.dius.pact.core.model.matchingrules.MatchingRule in project spring-cloud-contract by spring-cloud.

the class RequestResponseSCContractCreator method mapRequestCookies.

private void mapRequestCookies(org.springframework.cloud.contract.spec.internal.Request contractRequest, Request pactRequest) {
    Category headerRules = pactRequest.getMatchingRules().rulesForCategory("header");
    String[] splitCookiesHeader = pactRequest.getHeaders().get("Cookie").get(0).split(";");
    Map<String, String> foundCookies = Stream.of(splitCookiesHeader).map((cookieHeader) -> cookieHeader.split("=")).collect(Collectors.toMap(splittedCookieHeader -> splittedCookieHeader[0], splittedCookieHeader -> splittedCookieHeader[1]));
    contractRequest.cookies((cookies) -> foundCookies.forEach((key, value) -> {
        if (headerRules.getMatchingRules().containsKey("Cookie")) {
            MatchingRuleGroup matchingRuleGroup = headerRules.getMatchingRules().get("Cookie");
            if (matchingRuleGroup.getRules().size() > 1) {
                throw new UnsupportedOperationException("Currently only 1 rule at a time for a header is supported");
            }
            MatchingRule matchingRule = matchingRuleGroup.getRules().get(0);
            if (matchingRule instanceof RegexMatcher) {
                cookies.cookie(key, contractRequest.$(contractRequest.c(contractRequest.regex(((RegexMatcher) matchingRule).getRegex())), contractRequest.p(value)));
            } else {
                throw new UnsupportedOperationException("Currently only the header matcher of type regex is supported");
            }
        } else {
            cookies.cookie(key, value);
        }
    }));
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) Interaction(au.com.dius.pact.core.model.Interaction) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponseInteraction(au.com.dius.pact.core.model.RequestResponseInteraction) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Request(au.com.dius.pact.core.model.Request) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) Stream(java.util.stream.Stream) ProviderState(au.com.dius.pact.core.model.ProviderState) OptionalBody(au.com.dius.pact.core.model.OptionalBody) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(au.com.dius.pact.core.model.Response) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule)

Example 5 with MatchingRule

use of au.com.dius.pact.core.model.matchingrules.MatchingRule in project spring-cloud-contract by spring-cloud.

the class RequestResponseSCContractCreator method mapResponseCookies.

private void mapResponseCookies(org.springframework.cloud.contract.spec.internal.Response contractResponse, Response pactResponse) {
    Category headerRules = pactResponse.getMatchingRules().rulesForCategory("header");
    String[] splitCookiesHeader = pactResponse.getHeaders().get("Cookie").get(0).split(";");
    Map<String, String> foundCookies = Stream.of(splitCookiesHeader).map((cookieHeader) -> cookieHeader.split("=")).collect(Collectors.toMap(splittedCookieHeader -> splittedCookieHeader[0], splittedCookieHeader -> splittedCookieHeader[1]));
    contractResponse.cookies((cookies) -> foundCookies.forEach((key, value) -> {
        if (headerRules.getMatchingRules().containsKey("Cookie")) {
            MatchingRuleGroup matchingRuleGroup = headerRules.getMatchingRules().get("Cookie");
            if (matchingRuleGroup.getRules().size() > 1) {
                throw new UnsupportedOperationException("Currently only 1 rule at a time for a header is supported");
            }
            MatchingRule matchingRule = matchingRuleGroup.getRules().get(0);
            if (matchingRule instanceof RegexMatcher) {
                cookies.cookie(key, contractResponse.$(contractResponse.p(contractResponse.regex(Pattern.compile(((RegexMatcher) matchingRule).getRegex()))), contractResponse.c(value)));
            } else {
                throw new UnsupportedOperationException("Currently only the header matcher of type regex is supported");
            }
        } else {
            cookies.cookie(key, value);
        }
    }));
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) Interaction(au.com.dius.pact.core.model.Interaction) TimestampMatcher(au.com.dius.pact.core.model.matchingrules.TimestampMatcher) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponseInteraction(au.com.dius.pact.core.model.RequestResponseInteraction) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Request(au.com.dius.pact.core.model.Request) MinMaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher) MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) NumberTypeMatcher(au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher) Collection(java.util.Collection) TypeMatcher(au.com.dius.pact.core.model.matchingrules.TypeMatcher) TimeMatcher(au.com.dius.pact.core.model.matchingrules.TimeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Collectors(java.util.stream.Collectors) NullMatcher(au.com.dius.pact.core.model.matchingrules.NullMatcher) DateMatcher(au.com.dius.pact.core.model.matchingrules.DateMatcher) RuleLogic(au.com.dius.pact.core.model.matchingrules.RuleLogic) List(java.util.List) Stream(java.util.stream.Stream) ProviderState(au.com.dius.pact.core.model.ProviderState) OptionalBody(au.com.dius.pact.core.model.OptionalBody) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(au.com.dius.pact.core.model.Response) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) Category(au.com.dius.pact.core.model.matchingrules.Category) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule)

Aggregations

RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)6 MatchingRule (au.com.dius.pact.core.model.matchingrules.MatchingRule)6 MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)6 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)6 Interaction (au.com.dius.pact.core.model.Interaction)5 OptionalBody (au.com.dius.pact.core.model.OptionalBody)5 ProviderState (au.com.dius.pact.core.model.ProviderState)5 Request (au.com.dius.pact.core.model.Request)5 RequestResponseInteraction (au.com.dius.pact.core.model.RequestResponseInteraction)5 Response (au.com.dius.pact.core.model.Response)5 Category (au.com.dius.pact.core.model.matchingrules.Category)5 DateMatcher (au.com.dius.pact.core.model.matchingrules.DateMatcher)5 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)5 MinMaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MinMaxTypeMatcher)5 MinTypeMatcher (au.com.dius.pact.core.model.matchingrules.MinTypeMatcher)5 NullMatcher (au.com.dius.pact.core.model.matchingrules.NullMatcher)5 NumberTypeMatcher (au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher)5 RuleLogic (au.com.dius.pact.core.model.matchingrules.RuleLogic)5 TimeMatcher (au.com.dius.pact.core.model.matchingrules.TimeMatcher)5 TimestampMatcher (au.com.dius.pact.core.model.matchingrules.TimestampMatcher)5