Search in sources :

Example 1 with MockProviderConfig

use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.

the class DirectDSLConsumerPactTest 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, (mockServer, context) -> {
        Map expectedResponse = new HashMap();
        expectedResponse.put("hello", "harry");
        try {
            assertEquals(new ProviderClient(mockServer.getUrl()).hello("{\"name\": \"harry\"}"), expectedResponse);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Also used : ProviderClient(au.com.dius.pact.consumer.junit.exampleclients.ProviderClient) HashMap(java.util.HashMap) MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 2 with MockProviderConfig

use of au.com.dius.pact.consumer.model.MockProviderConfig 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, contentType).toPact().asRequestResponsePact().component1();
    PactVerificationResult result = runConsumerTest(pact, new MockProviderConfig("localhost", 0, PactSpecVersion.V3), (mockServer, context) -> {
        try {
            URL url = new URL(mockServer.getUrl() + path);
            String response = post(url, contentType, requestBody);
            assertEquals(expectedResponseBody, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Also used : MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) URL(java.net.URL)

Example 3 with MockProviderConfig

use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.

the class MatchingTest method runTest.

private void runTest(PactDslResponse pactFragment, final String body, final Map expectedResponse, final String path) {
    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, (mockServer, context) -> {
        try {
            Assert.assertEquals(expectedResponse, new ConsumerClient(mockServer.getUrl()).post(path, body, ContentType.APPLICATION_JSON));
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        }
        return true;
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Also used : MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) IOException(java.io.IOException)

Example 4 with MockProviderConfig

use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.

the class MimeTypeTest method runTest.

private void runTest(BasePact pact, final String body, final String expectedResponse, final ContentType mimeType) {
    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        try {
            assertEquals(new ConsumerClient(mockServer.getUrl()).postBody("/hello", body, mimeType), expectedResponse);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return true;
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Also used : MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) IOException(java.io.IOException)

Example 5 with MockProviderConfig

use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.

the class PactQueryParameterTest method verifyRequestMatches.

private void verifyRequestMatches(BasePact pact, String fullPath) {
    MockProviderConfig config = MockProviderConfig.createDefault();
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        String uri = mockServer.getUrl() + fullPath;
        Request.get(uri).execute().handleResponse(httpResponse -> {
            String content = EntityUtils.toString(httpResponse.getEntity());
            if (httpResponse.getCode() == 500) {
                Map map = new ObjectMapper().readValue(content, Map.class);
                Assert.fail((String) map.get("error"));
            }
            return null;
        });
        return true;
    });
    if (result instanceof PactVerificationResult.Error) {
        Throwable error = ((PactVerificationResult.Error) result).getError();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        } else {
            throw new RuntimeException(error);
        }
    }
    assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Also used : MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

MockProviderConfig (au.com.dius.pact.consumer.model.MockProviderConfig)6 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)3 IOException (java.io.IOException)3 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)2 Map (java.util.Map)2 Test (org.junit.Test)2 PactMismatchesException (au.com.dius.pact.consumer.PactMismatchesException)1 ProviderClient (au.com.dius.pact.consumer.junit.exampleclients.ProviderClient)1 MetricEvent (au.com.dius.pact.core.support.MetricEvent)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1