Search in sources :

Example 11 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("$.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 12 with DslPart

use of au.com.dius.pact.consumer.dsl.DslPart 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 13 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("$.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 14 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)

Aggregations

DslPart (au.com.dius.pact.consumer.dsl.DslPart)14 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)11 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 JSONObject (org.json.JSONObject)4 Map (java.util.Map)3 JsonSlurper (groovy.json.JsonSlurper)2 JSONArray (org.json.JSONArray)1