Search in sources :

Example 6 with RequestResponsePact

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);
}
Also used : MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException)

Example 7 with RequestResponsePact

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);
}
Also used : HashMap(java.util.HashMap) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 8 with RequestResponsePact

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;
}
Also used : PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) DslPart(au.com.dius.pact.consumer.dsl.DslPart)

Example 9 with RequestResponsePact

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;
}
Also used : RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart)

Example 10 with RequestResponsePact

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);
}
Also used : RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) Method(java.lang.reflect.Method) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider)

Aggregations

RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)13 DslPart (au.com.dius.pact.consumer.dsl.DslPart)7 Map (java.util.Map)5 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)4 MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)4 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)2 JsonSlurper (groovy.json.JsonSlurper)2 HashMap (java.util.HashMap)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)1 ProviderClient (au.com.dius.pact.consumer.exampleclients.ProviderClient)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1