Search in sources :

Example 11 with RequestResponsePact

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

the class PactDslRootValueTest method rootValueTest.

@Test
public void rootValueTest() {
    PactDslRootValue rootValueBody = new PactDslRootValue();
    rootValueBody.setValue("Brent");
    rootValueBody.setMatcher(new RegexMatcher(".{5}"));
    PactDslWithProvider dsl = new PactDslWithProvider(new ConsumerPactBuilder("consumer"), "provider");
    Map<String, String> headers = new HashMap<String, String>() {

        {
            put("Content-Type", "text/plain");
        }
    };
    RequestResponsePact frag = dsl.given("I am testing root values").uponReceiving("A request for text/plain").path("/some/blah/path").headers(headers).willRespondWith().headers(headers).status(200).body(rootValueBody).toPact().asRequestResponsePact().component1();
    Assert.assertEquals(1, frag.getInteractions().size());
    assertThat(frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getBody().valueAsString(), is("Brent"));
    Map<String, MatchingRuleGroup> matchingGroups = frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules();
    List<MatchingRule> rules = matchingGroups.get("$").getRules();
    Assert.assertEquals(1, rules.size());
    Assert.assertEquals(".{5}", rules.get(0).toMap(PactSpecVersion.V3).get("regex"));
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) PactDslRootValue(au.com.dius.pact.consumer.dsl.PactDslRootValue) HashMap(java.util.HashMap) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Test(org.junit.Test)

Example 12 with RequestResponsePact

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

the class PactDslJsonArrayTemplateTest method createPact.

@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
    DslPart personTemplate = new PactDslJsonBody().id().stringType("name").date("dob");
    DslPart body = new PactDslJsonArray().template(personTemplate, 3);
    RequestResponsePact pact = builder.uponReceiving("java test interaction with a DSL array body with templates").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$[0].id", "$[0].name", "$[0].dob", "$[1].id", "$[1].name", "$[1].dob", "$[2].id", "$[2].name", "$[2].dob");
    return pact;
}
Also used : PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.core.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.core.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").equalTo("V", "test").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", "$.data.array1", "$.data.array1[*].id", "$.data.array1[*].name", "$.data.array1[*].dob", "$.data.array2", "$.data.array2[*].address", "$.data.array2[*].name", "$.data.array3[0]", "$.data.array3[0][*].itemCount", "$.data.array3[0][*].V");
    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/V", "/data/array3/0/0/itemCount", "/data/array3", "/id");
    return pact;
}
Also used : RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart)

Example 14 with RequestResponsePact

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

the class EventsRepositoryDictionaryNestedArrayConsumerTest method createPact.

@Pact(provider = "EventsProvider", consumer = "EventsConsumerDictionaryNestedArray")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonBody().object("events").eachKeyMappedToAnArrayLike(// broken, see pact-jvm issue 401
    "ant").stringType("title", "ant");
    RequestResponsePact pact = builder.given("initialStateForEventsTest").uponReceiving("a request to get events keyed by title").path("/dictionaryNestedArray").headers("Accept", ContentType.APPLICATION_JSON.toString()).method("GET").willRespondWith().status(200).body(body).toPact();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$.events", "$.events.*[*].title");
    HashMap<String, MatchingRuleGroup> matchingRules = new HashMap<>();
    matchingRules.put("$.events", new MatchingRuleGroup(Collections.singletonList(ValuesMatcher.INSTANCE)));
    matchingRules.put("$.events.*[*].title", new MatchingRuleGroup(Collections.singletonList(TypeMatcher.INSTANCE)));
    assertThat(pact.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules(), is(equalTo(matchingRules)));
    return pact;
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) HashMap(java.util.HashMap) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 15 with RequestResponsePact

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

the class ConsumerPactTest 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, (mockServer, context) -> {
        runTest(mockServer, context);
        return null;
    });
    Metrics.INSTANCE.sendMetrics(new MetricEvent.ConsumerTestRun(pact.getInteractions().size(), "junit"));
    if (!(result instanceof PactVerificationResult.Ok)) {
        if (result instanceof PactVerificationResult.Error) {
            PactVerificationResult.Error error = (PactVerificationResult.Error) result;
            if (!(error.getMockServerState() instanceof PactVerificationResult.Ok)) {
                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.consumer.model.MockProviderConfig) MetricEvent(au.com.dius.pact.core.support.MetricEvent) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) PactMismatchesException(au.com.dius.pact.consumer.PactMismatchesException) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Aggregations

RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)15 DslPart (au.com.dius.pact.consumer.dsl.DslPart)8 Pact (au.com.dius.pact.core.model.annotations.Pact)7 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)5 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)4 MockProviderConfig (au.com.dius.pact.consumer.model.MockProviderConfig)3 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)2 MatchingRules (au.com.dius.pact.core.model.matchingrules.MatchingRules)2 PactMismatchesException (au.com.dius.pact.consumer.PactMismatchesException)1 PactDslRootValue (au.com.dius.pact.consumer.dsl.PactDslRootValue)1 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)1 ProviderClient (au.com.dius.pact.consumer.junit.exampleclients.ProviderClient)1 PactXmlBuilder (au.com.dius.pact.consumer.xml.PactXmlBuilder)1 MatchingRule (au.com.dius.pact.core.model.matchingrules.MatchingRule)1 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)1