use of au.com.dius.pact.core.model.RequestResponsePact 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.RequestResponsePact in project pact-jvm by DiUS.
the class PactDslJsonArrayTemplateTest method createPact.
@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
DslPart personTemplate = new PactDslJsonBody().id().stringType("name").date("dob");
DslPart body = new PactDslJsonArray().template(personTemplate, 3);
RequestResponsePact pact = builder.uponReceiving("java test interaction with a DSL array body with templates").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$[0].id", "$[0].name", "$[0].dob", "$[1].id", "$[1].name", "$[1].dob", "$[2].id", "$[2].name", "$[2].dob");
return pact;
}
use of au.com.dius.pact.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class PactDslJsonBodyArrayLikeTest method createPact.
@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
DslPart body = new PactDslJsonBody().id().object("data").eachLike("array1").id().stringType("name").date("dob").closeObject().closeArray().minArrayLike("array2", 1).ipAddress("address").stringType("name").closeObject().closeArray().array("array3").maxArrayLike(5).integerType("itemCount").equalTo("V", "test").closeObject().closeArray().closeArray().closeObject();
RequestResponsePact pact = builder.uponReceiving("java test interaction with an array like matcher").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$.id", "$.data.array1", "$.data.array1[*].id", "$.data.array1[*].name", "$.data.array1[*].dob", "$.data.array2", "$.data.array2[*].address", "$.data.array2[*].name", "$.data.array3[0]", "$.data.array3[0][*].itemCount", "$.data.array3[0][*].V");
MatcherTestUtils.assertResponseKeysEqualTo(pact, "/data", "/data/array1", "/data/array1/0/dob", "/data/array1/0/id", "/data/array1/0/name", "/data/array2", "/data/array2/0/address", "/data/array2/0/name", "/data/array3/0/0/V", "/data/array3/0/0/itemCount", "/data/array3", "/id");
return pact;
}
use of au.com.dius.pact.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class EventsRepositoryDictionaryNestedArrayConsumerTest method createPact.
@Pact(provider = "EventsProvider", consumer = "EventsConsumerDictionaryNestedArray")
public RequestResponsePact createPact(PactDslWithProvider builder) {
DslPart body = new PactDslJsonBody().object("events").eachKeyMappedToAnArrayLike(// broken, see pact-jvm issue 401
"ant").stringType("title", "ant");
RequestResponsePact pact = builder.given("initialStateForEventsTest").uponReceiving("a request to get events keyed by title").path("/dictionaryNestedArray").headers("Accept", ContentType.APPLICATION_JSON.toString()).method("GET").willRespondWith().status(200).body(body).toPact();
MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$.events", "$.events.*[*].title");
HashMap<String, MatchingRuleGroup> matchingRules = new HashMap<>();
matchingRules.put("$.events", new MatchingRuleGroup(Collections.singletonList(ValuesMatcher.INSTANCE)));
matchingRules.put("$.events.*[*].title", new MatchingRuleGroup(Collections.singletonList(TypeMatcher.INSTANCE)));
assertThat(pact.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules(), is(equalTo(matchingRules)));
return pact;
}
use of au.com.dius.pact.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class ConsumerPactTest method testPact.
@Test
public void testPact() throws Throwable {
RequestResponsePact pact = createPact(ConsumerPactBuilder.consumer(consumerName()).hasPactWith(providerName()));
final MockProviderConfig config = MockProviderConfig.createDefault(getSpecificationVersion());
PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
runTest(mockServer, context);
return null;
});
Metrics.INSTANCE.sendMetrics(new MetricEvent.ConsumerTestRun(pact.getInteractions().size(), "junit"));
if (!(result instanceof PactVerificationResult.Ok)) {
if (result instanceof PactVerificationResult.Error) {
PactVerificationResult.Error error = (PactVerificationResult.Error) result;
if (!(error.getMockServerState() instanceof PactVerificationResult.Ok)) {
throw new AssertionError("Pact Test function failed with an exception, possibly due to " + error.getMockServerState(), ((PactVerificationResult.Error) result).getError());
} else {
throw new AssertionError("Pact Test function failed with an exception: " + error.getError().getMessage(), error.getError());
}
} else {
throw new PactMismatchesException(result);
}
}
}
Aggregations