Search in sources :

Example 21 with PactDslJsonBody

use of au.com.dius.pact.consumer.dsl.PactDslJsonBody 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("$.body.first.level1", "$.body.first.second[1].level2", "$.body.first.second[0]", "$.body.first.second[1].third[0]"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getString(LEVEL_1), is(equalTo(L_1_EXAMPLE)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONArray(SECOND).getString(0), is(equalTo("al2example")));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONArray(SECOND).getJSONObject(1).getString(LEVEL_2), is(equalTo(L_2_EXAMPLE)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONArray(SECOND).getJSONObject(1).getJSONArray("third").getString(0), is(equalTo("al3example")));
}
Also used : JSONObject(org.json.JSONObject) 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 22 with PactDslJsonBody

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

the class PactDslJsonBodyTest method allowSettingFieldsToNull.

@Test
public void allowSettingFieldsToNull() {
    DslPart body = new PactDslJsonBody().id().object("2").id().stringValue("test", null).nullValue("nullValue").closeObject().array(NUMBERS).id().nullValue().stringValue(null).closeArray();
    JSONObject jsonObject = (JSONObject) body.getBody();
    assertThat(jsonObject.keySet(), is(equalTo((Set) new HashSet(Arrays.asList("2", NUMBERS, "id")))));
    assertThat(jsonObject.getJSONObject("2").get("test"), is(JSONObject.NULL));
    JSONArray numbers = jsonObject.getJSONArray(NUMBERS);
    assertThat(numbers.length(), is(3));
    assertThat(numbers.get(0), is(notNullValue()));
    assertThat(numbers.get(1), is(JSONObject.NULL));
    assertThat(numbers.get(2), is(JSONObject.NULL));
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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 23 with PactDslJsonBody

use of au.com.dius.pact.consumer.dsl.PactDslJsonBody 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("$.body.first.second['@third'].fourth.level4", "$.body.first.second['@third'].level3", "$.body.first.second.level2", "$.body.first.level1", "$.body.first['@level1']"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getString(LEVEL_1), is(equalTo(L_1_EXAMPLE)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONObject(SECOND).getString(LEVEL_2), is(equalTo(L_2_EXAMPLE)));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONObject(SECOND).getJSONObject(THIRD).getString("level3"), is(equalTo("l3example")));
    assertThat(((JSONObject) body.getBody()).getJSONObject(FIRST).getJSONObject(SECOND).getJSONObject(THIRD).getJSONObject("fourth").getString("level4"), is(equalTo("l4example")));
}
Also used : JSONObject(org.json.JSONObject) 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 24 with PactDslJsonBody

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

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

the class Defect371Test method createPact.

@Pact(provider = "provider1", consumer = "Defect371")
public MessagePact createPact(MessagePactBuilder builder) {
    PactDslJsonBody body = new PactDslJsonBody();
    body.stringValue("testParam1", "value1");
    body.stringValue("testParam2", "value2");
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");
    return builder.given("SomeProviderState").expectsToReceive("a test message").withMetadata(metadata).withContent(body).toPact();
}
Also used : HashMap(java.util.HashMap) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) Pact(au.com.dius.pact.consumer.Pact) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact)

Aggregations

PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)27 HashMap (java.util.HashMap)14 Pact (au.com.dius.pact.consumer.Pact)12 MessagePact (au.com.dius.pact.model.v3.messaging.MessagePact)12 DslPart (au.com.dius.pact.consumer.dsl.DslPart)11 Test (org.junit.Test)11 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)9 HashSet (java.util.HashSet)7 JSONObject (org.json.JSONObject)6 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)4 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)1 Date (java.util.Date)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1