Search in sources :

Example 11 with RequestResponsePact

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

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

the class PactDslJsonBodyTest method createPact.

@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonBody().id().object("2").id().stringValue("test", null).closeObject().array("numbers").id().number(100).numberValue(101).hexValue().object().id().stringValue("name", "Rogger the Dogger").timestamp().date("dob", "MM/dd/yyyy").closeObject().closeArray();
    RequestResponsePact pact = builder.uponReceiving("java test interaction with a DSL body").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "$.body.id", "$.body['2'].id", "$.body.numbers[0]", "$.body.numbers[3]", "$.body.numbers[4].id", "$.body.numbers[4].timestamp", "$.body.numbers[4].dob");
    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 13 with RequestResponsePact

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

the class WildcardKeysTest method createFragment.

@Pact(provider = "WildcardKeysProvider", consumer = "WildcardKeysConsumer")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonBody().eachLike("articles").eachLike("variants").eachKeyMappedToAnArrayLike("001").eachLike("bundles").eachKeyLike("001-A").stringType("description", "Some Description").eachLike("referencedArticles").id("bundleId", 23456L).eachKeyLike("001-A-1", PactDslJsonRootValue.id(12345L)).closeObject().closeArray().closeObject().closeObject().closeArray().closeObject().closeArray().closeObject().closeArray().closeObject().closeArray().object("foo").eachKeyLike("001", PactDslJsonRootValue.numberType(42)).closeObject();
    RequestResponsePact pact = builder.uponReceiving("a request for an article").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
    Map<String, Map<String, Object>> matchingRules = pact.getInteractions().get(0).getResponse().getMatchingRules();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "$.body.articles", "$.body.articles[*].variants", "$.body.articles[*].variants[*].*", "$.body.articles[*].variants[*].*[*].bundles", "$.body.articles[*].variants[*].*[*].bundles[*].*", "$.body.articles[*].variants[*].*[*].bundles[*].*.description", "$.body.articles[*].variants[*].*[*].bundles[*].*.referencedArticles", "$.body.articles[*].variants[*].*[*].bundles[*].*.referencedArticles[*].*", "$.body.articles[*].variants[*].*[*].bundles[*].*.referencedArticles[*].bundleId", "$.body.foo.*");
    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) Map(java.util.Map) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact)

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