use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class WindowTest method testContinuousAggregateLastSeconds.
/**
* Test a continuous aggregation.
*/
@Test
public void testContinuousAggregateLastSeconds() throws Exception {
final Topology t = newTopology();
TStream<String> source = t.periodicSource(new PeriodicStrings(), 100, TimeUnit.MILLISECONDS);
TStream<JSONObject> aggregate = source.last(3, TimeUnit.SECONDS).aggregate(new AggregateStrings());
TStream<String> strings = JSONStreams.serialize(aggregate);
Tester tester = t.getTester();
Condition<List<String>> contents = tester.stringContents(strings);
// 10 tuples per second, each is aggregated, so 15 seconds is around 150 tuples.
Condition<Long> ending = tester.atLeastTupleCount(strings, 150);
complete(tester, ending, 30, TimeUnit.SECONDS);
assertTrue(ending.valid());
long startTs = 0;
for (String output : contents.getResult()) {
JSONObject agg = JSONObject.parse(output);
JSONArray items = (JSONArray) agg.get("items");
long ts = (Long) agg.get("ts");
// Should see around 30 tuples per window, once we
// pass the first three seconds.
assertTrue("Number of tuples in window:" + items.size(), items.size() <= 45);
if (agg.containsKey("delta")) {
long delta = (Long) agg.get("delta");
assertTrue(delta >= 0);
if (startTs == 0) {
startTs = ts;
} else {
long diff = ts - startTs;
if (diff > 3000)
assertTrue("Number of tuples in window:" + items.size(), items.size() >= 25);
}
}
}
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testDeserializeArray.
/**
* Test that if the serialized value is
* an array, it ends up wrapped in an object.
*/
@Test
public void testDeserializeArray() throws Exception {
final String data = "[ 100, 500, false, 200, 400 ]";
final Topology t = new Topology();
TStream<String> array = t.strings(data);
TStream<JSONObject> json = JSONStreams.deserialize(array);
TStream<String> jsonString = JSONStreams.serialize(json);
JSONArray ja = (JSONArray) JSON.parse(data);
JSONObject jo = new JSONObject();
jo.put("payload", ja);
checkJsonOutput(jo, jsonString);
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testFlatten.
@Test
public void testFlatten() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
JSONObject e1 = new JSONObject();
e1.put("val", "hello");
array.add(e1);
JSONObject e2 = new JSONObject();
e2.put("val", "goodbye");
array.add(e2);
JSONObject e3 = new JSONObject();
e3.put("val", "farewell");
array.add(e3);
value.put("greetings", array);
List<JSONObject> inputs = new ArrayList<>();
inputs.add(value);
// no list present
inputs.add(new JSONObject());
JSONObject emptyList = new JSONObject();
emptyList.put("greetings", new JSONArray());
inputs.add(emptyList);
TStream<JSONObject> s = t.constants(inputs);
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
completeAndValidate(output, 10, e1.toString(), e2.toString(), e3.toString());
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testJsonSPL.
@Test
public void testJsonSPL() throws Exception {
final Topology t = new Topology();
TStream<String> example = t.strings(JSON_EXAMPLE);
TStream<JSONObject> json = JSONStreams.deserialize(example);
SPLStream spl = JSONStreams.toSPL(json);
assertEquals(JSONSchemas.JSON, spl.getSchema());
TStream<JSONObject> jsonFromSPL = spl.toJSON();
assertEquals(JSONObject.class, jsonFromSPL.getTupleClass());
assertEquals(JSONObject.class, jsonFromSPL.getTupleType());
TStream<String> jsonString = JSONStreams.serialize(jsonFromSPL);
checkJsonOutput(JSON.parse(JSON_EXAMPLE), jsonString);
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testSimpleJson.
/**
* Convert an example JSON as a String back to a String through JSON
* deserialization and serialization.
*/
@Test
public void testSimpleJson() throws Exception {
final Topology t = new Topology("SimpleJson");
TStream<String> example = t.strings(JSON_EXAMPLE);
TStream<JSONObject> json = JSONStreams.deserialize(example);
assertEquals(JSONObject.class, json.getTupleClass());
assertEquals(JSONObject.class, json.getTupleType());
TStream<String> jsonString = JSONStreams.serialize(json);
assertEquals(String.class, jsonString.getTupleClass());
assertEquals(String.class, jsonString.getTupleType());
checkJsonOutput(JSON.parse(JSON_EXAMPLE), jsonString);
}
Aggregations