Search in sources :

Example 36 with JSONObject

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);
            }
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Test(org.junit.Test)

Example 37 with JSONObject

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);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 38 with JSONObject

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());
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) ArrayList(java.util.ArrayList) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 39 with JSONObject

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);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test)

Example 40 with JSONObject

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);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Aggregations

JSONObject (com.ibm.json.java.JSONObject)56 JSONArray (com.ibm.json.java.JSONArray)19 Topology (com.ibm.streamsx.topology.Topology)18 Test (org.junit.Test)18 TestTopology (com.ibm.streamsx.topology.test.TestTopology)14 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)6 Tester (com.ibm.streamsx.topology.tester.Tester)5 JsonObject (com.google.gson.JsonObject)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 OrderedJSONObject (com.ibm.json.java.OrderedJSONObject)2 StreamConnection (com.ibm.streams.flow.declare.StreamConnection)2 StreamSchema (com.ibm.streams.operator.StreamSchema)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Attribute (com.ibm.streams.operator.Attribute)1