Search in sources :

Example 21 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class SPLValue method toJSON.

public JSONObject toJSON() {
    // meet the requirements of BOperatorInvocation.setParameter()
    /*
         * The Value object is
         * <pre><code>
         * object {
         *   type : "__spl_value"
         *   value : object {
         *     value : any. non-null. type appropriate for metaType
         *     metaType : com.ibm.streams.operator.Type.MetaType.name() string
         *   }
         * }
         * </code></pre>
         */
    JSONObject jo = new JSONObject();
    JSONObject jv = new JSONObject();
    jo.put("type", "__spl_value");
    jo.put("value", jv);
    jv.put("metaType", metaType.name());
    jv.put("value", value);
    return jo;
}
Also used : JSONObject(com.ibm.json.java.JSONObject)

Example 22 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class PlaceableTest method getColocate.

private static String getColocate(BOperator bop) {
    JSONObject placement = JOperatorConfig.getJSONItem(bop.json(), PLACEMENT);
    if (placement == null)
        return null;
    Object ido = placement.get(PLACEMENT_EXPLICIT_COLOCATE_ID);
    if (ido == null)
        return null;
    return ido.toString();
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONObject(com.ibm.json.java.JSONObject)

Example 23 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class WindowTest method testPeriodicAggregateLastSeconds.

/**
     * Test a periodic aggregation.
     */
@Test
public void testPeriodicAggregateLastSeconds() 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(), 1, TimeUnit.SECONDS);
    TStream<String> strings = JSONStreams.serialize(aggregate);
    Tester tester = t.getTester();
    Condition<List<String>> contents = tester.stringContents(strings);
    // 10 tuples per second, aggregate every second, so 15 seconds is around 15 tuples.
    Condition<Long> ending = tester.atLeastTupleCount(strings, 15);
    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);
            assertTrue("timeBetweenAggs: " + delta, delta > 800 && delta < 1200);
            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 24 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class WindowTest method testPeriodicAggregateLastMicroseconds.

/**
     * Test a periodic aggregation with microsecond aggregation.
     * Basically a test that the application runs, hard to test
     * for specific results for such a short window.
     */
@Test
@Ignore("Java SPL not supporting small window sizes")
public void testPeriodicAggregateLastMicroseconds() throws Exception {
    // Embedded doesn't support window sizes < 1ms (see issue #211)
    assumeTrue(!isEmbedded());
    final Topology t = newTopology();
    TStream<String> source = t.periodicSource(new PeriodicStrings(), 10, TimeUnit.MILLISECONDS);
    TStream<JSONObject> aggregate = source.last(3, TimeUnit.MICROSECONDS).aggregate(new AggregateStrings(), 10, TimeUnit.MICROSECONDS);
    TStream<String> strings = JSONStreams.serialize(aggregate);
    Tester tester = t.getTester();
    // Aggregate 10 microseconds, so 2 seconds is around 200,000 tuples.
    Condition<Long> ending = tester.atLeastTupleCount(strings, 200_000);
    complete(tester, ending, 30, TimeUnit.SECONDS);
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) JSONObject(com.ibm.json.java.JSONObject) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 25 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class PublishSubscribeJsonPythonTest method testPublishJsonMap.

/**
	 * Json Subscribe feeding a map
	 */
@Test
public void testPublishJsonMap() throws Exception {
    Random r = new Random();
    final Topology t = new Topology();
    JSONObject j1 = new JSONObject();
    j1.put("a", r.nextLong());
    j1.put("b", "Hello:" + r.nextInt(200));
    JSONObject j2 = new JSONObject();
    j2.put("a", r.nextLong());
    j2.put("b", "Goodbye:" + r.nextInt(200));
    JSONObject j3 = new JSONObject();
    j3.put("a", r.nextLong());
    j3.put("b", "So long:" + r.nextInt(200));
    String s1 = "R" + j1.get("a") + "X" + j1.get("b") + "X" + (((Long) j1.get("a")) + 235L);
    String s2 = "R" + j2.get("a") + "X" + j2.get("b") + "X" + (((Long) j2.get("a")) + 235L);
    String s3 = "R" + j3.get("a") + "X" + j3.get("b") + "X" + (((Long) j3.get("a")) + 235L);
    includePythonApp(t, "json_map_json.py", "json_map_json::json_map_json");
    TStream<JSONObject> source = t.constants(Arrays.asList(j1, j2, j3));
    source = addStartupDelay(source).asType(JSONObject.class);
    source.publish("pytest/json/map");
    TStream<JSONObject> subscribe = t.subscribe("pytest/json/map/result", JSONObject.class);
    TStream<String> asString = subscribe.transform(j -> "R" + j.get("a") + "X" + j.get("b") + "X" + j.get("c"));
    completeAndValidate(asString, 30, s1, s2, s3);
}
Also used : Random(java.util.Random) JSONObject(com.ibm.json.java.JSONObject) Topology(com.ibm.streamsx.topology.Topology) 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