Search in sources :

Example 16 with DslPart

use of au.com.dius.pact.consumer.dsl.DslPart 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").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", "$.body.data.array1", "$.body.data.array1[*].id", "$.body.data.array1[*].name", "$.body.data.array1[*].dob", "$.body.data.array2", "$.body.data.array2[*].address", "$.body.data.array2[*].name", "$.body.data.array3[0]", "$.body.data.array3[0][*].itemCount");
    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/itemCount", "/data/array3", "/id");
    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 17 with DslPart

use of au.com.dius.pact.consumer.dsl.DslPart 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 18 with DslPart

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

Example 19 with DslPart

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

the class PactDslJsonBodyTest method guardAgainstFieldNamesThatDontConformToGatlingFields.

@Test
public void guardAgainstFieldNamesThatDontConformToGatlingFields() {
    DslPart body = new PactDslJsonBody().id("1").stringType("@field").hexValue("200", "abc").integerType(K_DEPRECIATION_BIPS);
    Set<String> expectedMatchers = new HashSet<String>(Arrays.asList("$.body['200']", "$.body['1']", "$.body['@field']", "$.body['10k-depreciation-bips']"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).keySet(), is(equalTo((Set) new HashSet(Arrays.asList("200", K_DEPRECIATION_BIPS, "1", "@field")))));
}
Also used : PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with DslPart

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

the class PactDslJsonBodyTest method nestedArrayAndObjectMatcherTest.

@Test
public void nestedArrayAndObjectMatcherTest() {
    DslPart body = new PactDslJsonBody().object(FIRST).stringType(LEVEL_1, L_1_EXAMPLE).array(SECOND).stringType("al2example").object().stringType(LEVEL_2, L_2_EXAMPLE).array("third").stringType("al3example").closeArray().closeObject().closeArray().closeObject();
    Set<String> expectedMatchers = new HashSet<String>(Arrays.asList(".first.level1", ".first.second[1].level2", ".first.second[0]", ".first.second[1].third[0]"));
    assertThat(body.getMatchers().getMatchingRules().keySet(), is(equalTo(expectedMatchers)));
    assertThat(body.getBody().asObject().get(FIRST).get(LEVEL_1).asString(), is(equalTo(L_1_EXAMPLE)));
    assertThat(body.getBody().asObject().get(FIRST).get(SECOND).get(0).asString(), is(equalTo("al2example")));
    assertThat(body.getBody().asObject().get(FIRST).get(SECOND).get(1).get(LEVEL_2).asString(), is(equalTo(L_2_EXAMPLE)));
    assertThat(body.getBody().asObject().get(FIRST).get(SECOND).get(1).get("third").get(0).asString(), is(equalTo("al3example")));
}
Also used : PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DslPart (au.com.dius.pact.consumer.dsl.DslPart)32 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)26 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)15 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)8 LambdaDsl (au.com.dius.pact.consumer.dsl.LambdaDsl)7 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)7 IOException (java.io.IOException)7 CoreMatchers.is (org.hamcrest.CoreMatchers.is)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Test (org.junit.jupiter.api.Test)7 Pact (au.com.dius.pact.core.model.annotations.Pact)4 MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)3 Map (java.util.Map)3 MatchingRules (au.com.dius.pact.core.model.matchingrules.MatchingRules)2 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)2 JsonValue (au.com.dius.pact.core.support.json.JsonValue)2 JsonSlurper (groovy.json.JsonSlurper)2 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)1