Search in sources :

Example 26 with DslPart

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

the class PactDslJsonBodyTest method nestedObjectMatcherTest.

@Test
public void nestedObjectMatcherTest() {
    DslPart body = new PactDslJsonBody().object(FIRST).stringType(LEVEL_1, L_1_EXAMPLE).stringType("@level1").object(SECOND).stringType(LEVEL_2, L_2_EXAMPLE).object(THIRD).stringType("level3", "l3example").object("fourth").stringType("level4", "l4example").closeObject().closeObject().closeObject().closeObject();
    Set<String> expectedMatchers = new HashSet<>(Arrays.asList(".first.second.@third.fourth.level4", ".first.second.@third.level3", ".first.second.level2", ".first.level1", ".first.@level1"));
    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(LEVEL_2).asString(), is(equalTo(L_2_EXAMPLE)));
    assertThat(body.getBody().asObject().get(FIRST).get(SECOND).get(THIRD).get("level3").asString(), is(equalTo("l3example")));
    assertThat(body.getBody().asObject().get(FIRST).get(SECOND).get(THIRD).get("fourth").get("level4").asString(), is(equalTo("l4example")));
}
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 27 with DslPart

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

the class LambdaDslTest method testArrayMinLike.

@Test
public void testArrayMinLike() {
    /*
            [
                {
                    "foo": "string"
                },
                {
                    "foo": "string"
                }
            ]
         */
    String pactDslJson = PactDslJsonArray.arrayMinLike(2).stringType("foo").close().getBody().toString();
    DslPart actualPactDsl = LambdaDsl.newJsonArrayMinLike(2, o -> o.object(oo -> oo.stringType("foo"))).build();
    String actualJson = actualPactDsl.getBody().toString();
    assertThat(actualJson, is(pactDslJson));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) LambdaDsl(au.com.dius.pact.consumer.dsl.LambdaDsl) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) IOException(java.io.IOException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Test(org.junit.jupiter.api.Test)

Example 28 with DslPart

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

the class LambdaDslTest method testNumberValue.

@Test
public void testNumberValue() {
    DslPart dslPart = LambdaDsl.newJsonBody(o -> o.numberValue("number", 1)).build();
    assertThat(dslPart.getBody().toString(), is("{\"number\":1}"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) LambdaDsl(au.com.dius.pact.consumer.dsl.LambdaDsl) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) IOException(java.io.IOException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Test(org.junit.jupiter.api.Test)

Example 29 with DslPart

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

the class LambdaDslTest method testObjectWithComplexStructure.

@Test
public void testObjectWithComplexStructure() throws IOException {
    /*
            {
                "propObj1": {
                    "foo": "Foo",
                    "someProperty": 1
                },
                "someArray": [
                    {
                        "arrayObj1Prop1": "ao1p1"
                    },
                    {
                        "arrayObj1Prop2Obj": {
                            "arrayObj1Prop2ObjProp1": "ao1p2op1"
                        },
                        "arrayObj2Prop1": "ao2p1"
                    }
                ]
            }
         */
    // Old DSL
    final String pactDslJson = new PactDslJsonBody().object("propObj1").stringValue("foo", "Foo").numberValue("someProperty", 1L).closeObject().array("someArray").object().stringValue("arrayObj1Prop1", "ao1p1").closeObject().object().stringValue("arrayObj2Prop1", "ao2p1").object("arrayObj1Prop2Obj").stringValue("arrayObj1Prop2ObjProp1", "ao1p2op1").closeObject().closeObject().closeArray().getBody().toString();
    // Lambda DSL
    final DslPart actualPactDsl = LambdaDsl.newJsonBody((body) -> {
        body.object("propObj1", (o) -> {
            o.stringValue("foo", "Foo");
            o.numberValue("someProperty", 1L);
        }).array("someArray", (a) -> {
            a.object((oo) -> oo.stringValue("arrayObj1Prop1", "ao1p1"));
            a.object((oo) -> {
                oo.stringValue("arrayObj2Prop1", "ao2p1");
                oo.object("arrayObj1Prop2Obj", (ooo) -> ooo.stringValue("arrayObj1Prop2ObjProp1", "ao1p2op1"));
            });
        });
    }).build();
    String actualJson = actualPactDsl.getBody().toString();
    assertThat(actualJson, is(pactDslJson));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) LambdaDsl(au.com.dius.pact.consumer.dsl.LambdaDsl) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) IOException(java.io.IOException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Test(org.junit.jupiter.api.Test)

Example 30 with DslPart

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

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