Search in sources :

Example 6 with MockProviderConfig

use of au.com.dius.pact.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).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 MockProviderConfig

use of au.com.dius.pact.model.MockProviderConfig 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 MockProviderConfig

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

the class ConsumerPactTestMk2 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, this::runTest);
    if (!result.equals(PactVerificationResult.Ok.INSTANCE)) {
        if (result instanceof PactVerificationResult.Error) {
            PactVerificationResult.Error error = (PactVerificationResult.Error) result;
            if (error.getMockServerState() != PactVerificationResult.Ok.INSTANCE) {
                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);
        }
    }
}
Also used : MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 9 with MockProviderConfig

use of au.com.dius.pact.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.V2);
    PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, new PactTestRun() {

        @Override
        public void run(@NotNull MockServer mockServer) throws IOException {
            try {
                Assert.assertEquals(expectedResponse, new ConsumerClient(config.url()).post(path, body, ContentType.APPLICATION_JSON));
            } catch (IOException e) {
                LOGGER.error(e.getMessage(), e);
                throw 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) IOException(java.io.IOException)

Aggregations

MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)9 IOException (java.io.IOException)6 Test (org.junit.Test)5 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)4 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)4 PactFragment (au.com.dius.pact.model.PactFragment)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)1 ProviderClient (au.com.dius.pact.consumer.exampleclients.ProviderClient)1 URL (java.net.URL)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 JSONObject (org.json.JSONObject)1 Statement (org.junit.runners.model.Statement)1