Search in sources :

Example 1 with DataResultSnapshotSerializer

use of org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer in project apex-malhar by apache.

the class DataResultSnapshotSerializerTest method simpleCountdownTest.

@Test
public void simpleCountdownTest() throws Exception {
    List<GPOMutable> dataValues = createValues();
    DataQuerySnapshot gQuery = new DataQuerySnapshot("1", new Fields(Sets.newHashSet("a", "b")));
    DataResultSnapshot result = new DataResultSnapshot(gQuery, dataValues, 2);
    DataResultSnapshotSerializer serializer = new DataResultSnapshotSerializer();
    String resultJSON = serializer.serialize(result, new ResultFormatter());
    JSONObject rjo = new JSONObject(resultJSON);
    long countdown = rjo.getLong(Result.FIELD_COUNTDOWN);
    Assert.assertEquals(2, countdown);
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) DataResultSnapshotSerializer(org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 2 with DataResultSnapshotSerializer

use of org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer in project apex-malhar by apache.

the class DataResultSnapshotSerializerTest method simpleResultSerializerTest.

@Test
public void simpleResultSerializerTest() throws Exception {
    List<GPOMutable> dataValues = createValues();
    DataQuerySnapshot gQuery = new DataQuerySnapshot("1", new Fields(Sets.newHashSet("a", "b")));
    DataResultSnapshot result = new DataResultSnapshot(gQuery, dataValues);
    DataResultSnapshotSerializer serializer = new DataResultSnapshotSerializer();
    final String expectedJSONResult = "{\"id\":\"1\",\"type\":\"dataResult\",\"data\":[{\"b\":\"hello\",\"a\":\"1\"}," + "{\"b\":\"world\",\"a\":\"2\"}]}";
    String resultJSON = serializer.serialize(result, new ResultFormatter());
    JSONObject jo = new JSONObject(expectedJSONResult);
    JSONArray data = jo.getJSONArray("data");
    JSONObject jo1 = data.getJSONObject(0);
    JSONObject jo2 = data.getJSONObject(1);
    JSONObject rjo = new JSONObject(resultJSON);
    JSONArray rdata = rjo.getJSONArray("data");
    JSONObject rjo1 = rdata.getJSONObject(0);
    JSONObject rjo2 = rdata.getJSONObject(1);
    Assert.assertFalse(rjo.has(Result.FIELD_COUNTDOWN));
    Assert.assertEquals("The json doesn't match.", jo1.get("a"), rjo1.get("a"));
    Assert.assertEquals("The json doesn't match.", jo1.get("b"), rjo1.get("b"));
    Assert.assertEquals("The json doesn't match.", jo2.get("a"), rjo2.get("a"));
    Assert.assertEquals("The json doesn't match.", jo2.get("b"), rjo2.get("b"));
    Assert.assertEquals("The type doesn't match.", DataResultSnapshot.TYPE, jo.get(DataResultSnapshot.FIELD_TYPE));
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) DataResultSnapshotSerializer(org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) Test(org.junit.Test)

Aggregations

GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)2 DataResultSnapshotSerializer (org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Test (org.junit.Test)2 JSONArray (org.codehaus.jettison.json.JSONArray)1