Search in sources :

Example 1 with DslPart

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

the class Defect266Test method getUsersFragment.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment(PactDslWithProvider builder) {
    Map<String, Map<String, Object>> matchers = (Map<String, Map<String, Object>>) new JsonSlurper().parseText("{" + "\"$.body[0][*].userName\": {\"match\": \"type\"}," + "\"$.body[0][*].id\": {\"match\": \"regex\", \"regex\": \"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\"}," + "\"$.body[0]\": {\"match\": \"type\", \"max\": 5}," + "\"$.body[0][*].email\": {\"match\": \"type\"}" + "}");
    DslPart body = new PactDslJsonArray().maxArrayLike(5).uuid("id").stringType("userName").stringType("email").closeObject();
    RequestResponsePact pact = builder.given("a user with an id named 'user' exists").uponReceiving("get all users for max").path("/idm/user").method("GET").willRespondWith().status(200).body(body).toPact();
    assertThat(pact.getInteractions().get(0).getResponse().getMatchingRules(), is(equalTo(matchers)));
    return pact;
}
Also used : JsonSlurper(groovy.json.JsonSlurper) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Map(java.util.Map) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact)

Example 2 with DslPart

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

the class Defect266Test method getUsersFragment2.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment2(PactDslWithProvider builder) {
    Map<String, Map<String, Object>> matchers = (Map<String, Map<String, Object>>) new JsonSlurper().parseText("{" + "\"$.body[0][*].userName\": {\"match\": \"type\"}," + "\"$.body[0][*].id\": {\"match\": \"regex\", \"regex\": \"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\"}," + "\"$.body[0]\": {\"match\": \"type\", \"min\": 5}," + "\"$.body[0][*].email\": {\"match\": \"type\"}" + "}");
    DslPart body = new PactDslJsonArray().minArrayLike(5).uuid("id").stringType("userName").stringType("email").closeObject();
    RequestResponsePact pact = builder.given("a user with an id named 'user' exists").uponReceiving("get all users for min").path("/idm/user").method("GET").willRespondWith().status(200).body(body).toPact();
    assertThat(pact.getInteractions().get(0).getResponse().getMatchingRules(), is(equalTo(matchers)));
    return pact;
}
Also used : JsonSlurper(groovy.json.JsonSlurper) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Map(java.util.Map) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact)

Example 3 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", "$.body[0].name", "$.body[0].dob", "$.body[1].id", "$.body[1].name", "$.body[1].dob", "$.body[2].id", "$.body[2].name", "$.body[2].dob");
    return pact;
}
Also used : PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart)

Example 4 with DslPart

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

the class PactDslJsonBodyTest method guardAgainstObjectNamesThatDontConformToGatlingFields.

@Test
public void guardAgainstObjectNamesThatDontConformToGatlingFields() {
    DslPart body = new PactDslJsonBody().id().object("2").id().stringValue("test", "A Test String").closeObject().array(NUMBERS).id().number(100).numberValue(101).hexValue().object().id().stringValue("name", "Rogger the Dogger").timestamp().date("dob", "MM/dd/yyyy").object(K_DEPRECIATION_BIPS).id().closeObject().closeObject().closeArray();
    Set<String> expectedMatchers = new HashSet<String>(Arrays.asList("$.body.id", "$.body['2'].id", "$.body.numbers[3]", "$.body.numbers[0]", "$.body.numbers[4].timestamp", "$.body.numbers[4].dob", "$.body.numbers[4].id", "$.body.numbers[4]['10k-depreciation-bips'].id"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).keySet(), is(equalTo((Set) new HashSet(Arrays.asList("2", NUMBERS, "id")))));
}
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 5 with DslPart

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

the class LambdaDslTest method testObjectWithObjects.

@Test
public void testObjectWithObjects() throws IOException {
    /*
            {
                "propObj1": {
                    "foo": "Foo"
                },
                "propObj2": {
                    "bar": "Bar"
                },
                "someProperty": "Prop"
            }
         */
    // Old DSL
    final String pactDslJson = new PactDslJsonBody().stringValue("someProperty", "Prop").object("propObj1").stringValue("foo", "Foo").closeObject().object("propObj2").stringValue("bar", "Bar").closeObject().getBody().toString();
    // Lambda DSL
    final DslPart actualPactDsl = LambdaDsl.newJsonBody((body) -> {
        body.stringValue("someProperty", "Prop").object("propObj1", (o) -> {
            o.stringValue("foo", "Foo");
        }).object("propObj2", (o) -> {
            o.stringValue("bar", "Bar");
        });
    }).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)

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