use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.
the class PactDefectTest method test.
private void test(final String requestBody, final String expectedResponseBody, final String contentType) {
RequestResponsePact pact = ConsumerPactBuilder.consumer("ping_consumer").hasPactWith("ping_provider").uponReceiving("Ping with " + contentType).path(path).method(method).body(requestBody, contentType).willRespondWith().status(200).body(expectedResponseBody).toPact();
PactVerificationResult result = runConsumerTest(pact, new MockProviderConfig("localhost", 0, PactSpecVersion.V2), new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
try {
URL url = new URL(mockServer.getUrl() + path);
String response = post(url, contentType, requestBody);
assertEquals(expectedResponseBody, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
Assert.assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.
the class PactTest method testPact.
@Test
public void testPact() {
RequestResponsePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving("a request to say Hello").path("/hello").method("POST").body("{\"name\": \"harry\"}").willRespondWith().status(200).body("{\"hello\": \"harry\"}").toPact();
MockProviderConfig config = MockProviderConfig.createDefault();
PactVerificationResult result = runConsumerTest(pact, config, new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
Map expectedResponse = new HashMap();
expectedResponse.put("hello", "harry");
assertEquals(expectedResponse, new ConsumerClient(mockServer.getUrl()).post("/hello", "{\"name\": \"harry\"}", ContentType.APPLICATION_JSON));
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.
the class PactDslJsonArrayTest method createPact.
@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
DslPart body = new PactDslJsonArray().object().id().stringValue("name", "Rogger the Dogger").timestamp().date("dob", "MM/dd/yyyy").closeObject().object().id().stringValue("name", "Cat in the Hat").timestamp().date("dob", "MM/dd/yyyy").closeObject();
RequestResponsePact pact = builder.uponReceiving("java test interaction with a DSL array body").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "$.body[0].id", "$.body[0].timestamp", "$.body[0].dob", "$.body[1].id", "$.body[1].timestamp", "$.body[1].dob");
return pact;
}
use of au.com.dius.pact.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").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", "$.body.data.array1", "$.body.data.array1[*].id", "$.body.data.array1[*].name", "$.body.data.array1[*].dob", "$.body.data.array2", "$.body.data.array2[*].address", "$.body.data.array2[*].name", "$.body.data.array3[0]", "$.body.data.array3[0][*].itemCount");
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/itemCount", "/data/array3", "/id");
return pact;
}
use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.
the class BaseProviderRule method evaluatePactVerifications.
private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base) throws Throwable {
Optional<PactVerification> possiblePactVerification = findPactVerification(pactVerifications);
if (!possiblePactVerification.isPresent()) {
base.evaluate();
return;
}
PactVerification pactVerification = possiblePactVerification.get();
Optional<Method> possiblePactMethod = findPactMethod(pactVerification);
if (!possiblePactMethod.isPresent()) {
throw new UnsupportedOperationException("Could not find method with @Pact for the provider " + provider);
}
Method method = possiblePactMethod.get();
Pact pactAnnotation = method.getAnnotation(Pact.class);
PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(pactAnnotation.consumer()).hasPactWith(provider);
RequestResponsePact pact;
try {
pact = (RequestResponsePact) method.invoke(target, dslBuilder);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke pact method", e);
}
PactVerificationResult result = runPactTest(base, pact);
validateResult(result, pactVerification);
}
Aggregations