Search in sources :

Example 11 with PactDslJsonBody

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

the class MatchingTest method testMatchingByTypeOnBody.

@Test
public void testMatchingByTypeOnBody() {
    PactDslJsonBody body = new PactDslJsonBody().stringType("name").booleanType("happy").hexValue("hexCode").id().ipAddress("localAddress").numberValue("age", 100).numberType("ageAverage", 150.0).integerType("age2", 200).timestamp();
    PactDslJsonBody responseBody = new PactDslJsonBody();
    HashMap expectedResponse = new HashMap();
    runTest(buildPactFragment(body, responseBody, "a test interaction that requires type matching"), new JSONObject().put("name", "Giggle and Hoot").put("happy", true).put("hexCode", "abcdef0123456789").put("id", 1234567890).put("localAddress", "192.168.0.1").put("age", 100).put("ageAverage", 150.0).put("age2", 200).put("timestamp", DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date())).toString(), expectedResponse, HELLO);
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) Date(java.util.Date) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 12 with PactDslJsonBody

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

the class MatchingTest method testRegexpMatchingOnBody.

@Test
public void testRegexpMatchingOnBody() {
    PactDslJsonBody body = new PactDslJsonBody().stringMatcher("name", "\\w+", HARRY).stringMatcher("position", "staff|contactor");
    PactDslJsonBody responseBody = new PactDslJsonBody().stringMatcher("name", "\\w+", HARRY);
    HashMap<String, String> expectedResponse = new HashMap<String, String>();
    expectedResponse.put("name", HARRY);
    runTest(buildPactFragment(body, responseBody, "a test interaction that requires regex matching"), "{\"name\": \"Arnold\", \"position\": \"staff\"}", expectedResponse, HELLO);
}
Also used : HashMap(java.util.HashMap) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 13 with PactDslJsonBody

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

the class MimeTypeTest method testMatchingJson.

@Test
public void testMatchingJson() {
    String body = new PactDslJsonBody().object("person").stringValue("name", "fred").numberValue("age", 100).closeObject().toString();
    String responseBody = "{\"status\": \"OK\"}";
    runTest(buildPact(body, responseBody, "a test interaction with json", ContentType.APPLICATION_JSON), body, responseBody, ContentType.APPLICATION_JSON);
}
Also used : PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 14 with PactDslJsonBody

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

the class PactDslJsonBodyTest method eachLikeMatcherTest.

@Test
public void eachLikeMatcherTest() {
    DslPart body = new PactDslJsonBody().eachLike("ids").id().closeObject().closeArray();
    Set<String> expectedMatchers = new HashSet<String>(Arrays.asList("$.body.ids", "$.body.ids[*].id"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).keySet(), is(equalTo((Set) new HashSet(Arrays.asList("ids")))));
}
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 15 with PactDslJsonBody

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

the class PactDslJsonBodyTest method nestedArrayMatcherTest.

@Test
public void nestedArrayMatcherTest() {
    DslPart body = new PactDslJsonBody().array(FIRST).stringType(L_1_EXAMPLE).array().stringType(L_2_EXAMPLE).closeArray().closeArray();
    Set<String> expectedMatchers = new HashSet<String>(Arrays.asList("$.body.first[0]", "$.body.first[1][0]"));
    assertThat(body.getMatchers().keySet(), is(equalTo(expectedMatchers)));
    assertThat(((JSONObject) body.getBody()).getJSONArray(FIRST).getString(0), is(equalTo(L_1_EXAMPLE)));
    assertThat(((JSONObject) body.getBody()).getJSONArray(FIRST).getJSONArray(1).getString(0), is(equalTo(L_2_EXAMPLE)));
}
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)

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