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")));
}
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));
}
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")));
}
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")))));
}
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();
}
Aggregations