Search in sources :

Example 1 with PactVerificationResult

use of au.com.dius.pact.consumer.PactVerificationResult 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 -> {
        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);
        }
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
Also used : ProviderClient(au.com.dius.pact.consumer.exampleclients.ProviderClient) 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) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 2 with PactVerificationResult

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

the class PactMultiProviderTest method provider1Fails.

@Test
@PactVerification(value = { "test_provider", "test_provider2" })
public void provider1Fails() throws IOException, InterruptedException {
    mockTestProvider.validateResultWith((result, t) -> {
        assertThat(t, is(instanceOf(AssertionError.class)));
        assertThat(t.getMessage(), startsWith("The following mismatched requests occurred:\nUnexpected Request:\n\tmethod: GET\n\tpath: /abc"));
        assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
        PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
        assertThat(error.getMismatches(), hasSize(1));
        PactVerificationResult result1 = error.getMismatches().get(0);
        assertThat(result1, is(instanceOf(PactVerificationResult.UnexpectedRequest.class)));
        PactVerificationResult.UnexpectedRequest unexpectedRequest = (PactVerificationResult.UnexpectedRequest) result1;
        assertThat(unexpectedRequest.getRequest().getPath(), is("/abc"));
    });
    doTest("/abc", NAME_LARRY_JSON);
}
Also used : PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test)

Example 3 with PactVerificationResult

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

the class PactMultiProviderTest method bothprovidersFail.

@Test
@PactVerification(value = { "test_provider", "test_provider2" })
public void bothprovidersFail() throws IOException, InterruptedException {
    mockTestProvider.validateResultWith((result, t) -> {
        assertThat(t, is(instanceOf(AssertionError.class)));
        assertThat(t.getMessage(), startsWith("The following mismatched requests occurred:\nUnexpected Request:\n\tmethod: GET\n\tpath: /abc"));
        assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
        PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
        assertThat(error.getMismatches(), hasSize(1));
        PactVerificationResult result1 = error.getMismatches().get(0);
        assertThat(result1, is(instanceOf(PactVerificationResult.UnexpectedRequest.class)));
        PactVerificationResult.UnexpectedRequest unexpectedRequest = (PactVerificationResult.UnexpectedRequest) result1;
        assertThat(unexpectedRequest.getRequest().getPath(), is("/abc"));
    });
    mockTestProvider2.validateResultWith((result, t) -> {
        assertThat(t, is(instanceOf(AssertionError.class)));
        assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" + "PartialMismatch(mismatches=[BodyMismatch(larry,farry,Some(Expected 'larry' but received 'farry'),$.body.name,None)])"));
        assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
        PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
        assertThat(error.getMismatches(), hasSize(1));
        PactVerificationResult result1 = error.getMismatches().get(0);
        assertThat(result1, is(instanceOf(PactVerificationResult.PartialMismatch.class)));
        PactVerificationResult.PartialMismatch error1 = (PactVerificationResult.PartialMismatch) result1;
        assertThat(error1.getMismatches(), hasSize(1));
        RequestPartMismatch mismatch = error1.getMismatches().get(0);
        assertThat(mismatch, is(instanceOf(BodyMismatch.class)));
    });
    doTest("/abc", "{\"name\": \"farry\"}");
}
Also used : RequestPartMismatch(au.com.dius.pact.model.RequestPartMismatch) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test)

Example 4 with PactVerificationResult

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

the class PactMultiProviderTest method provider2Fails.

@Test
@PactVerification(value = { "test_provider", "test_provider2" })
public void provider2Fails() throws IOException, InterruptedException {
    mockTestProvider2.validateResultWith((result, t) -> {
        assertThat(t, is(instanceOf(AssertionError.class)));
        assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" + "PartialMismatch(mismatches=[BodyMismatch(larry,farry,Some(Expected 'larry' but received 'farry'),$.body.name,None)])"));
        assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
        PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
        assertThat(error.getMismatches(), hasSize(1));
        PactVerificationResult result1 = error.getMismatches().get(0);
        assertThat(result1, is(instanceOf(PactVerificationResult.PartialMismatch.class)));
        PactVerificationResult.PartialMismatch error1 = (PactVerificationResult.PartialMismatch) result1;
        assertThat(error1.getMismatches(), hasSize(1));
        RequestPartMismatch mismatch = error1.getMismatches().get(0);
        assertThat(mismatch, is(instanceOf(BodyMismatch.class)));
    });
    doTest("/", "{\"name\": \"farry\"}");
}
Also used : RequestPartMismatch(au.com.dius.pact.model.RequestPartMismatch) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test)

Aggregations

PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)4 Test (org.junit.Test)4 PactVerification (au.com.dius.pact.consumer.PactVerification)3 RequestPartMismatch (au.com.dius.pact.model.RequestPartMismatch)2 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)1 ProviderClient (au.com.dius.pact.consumer.exampleclients.ProviderClient)1 MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)1 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1