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));
}
}));
}
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!");
}
}
});
}
});
});
}
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"));
}
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);
}
}));
}
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);
}
}));
}
Aggregations