Search in sources :

Example 86 with Topology

use of com.ibm.streamsx.topology.Topology in project streamsx.topology by IBMStreams.

the class JSONStreamsTest method testFlattenNoObjects.

@Test
public void testFlattenNoObjects() throws Exception {
    final Topology t = new Topology();
    final JSONObject value = new JSONObject();
    final JSONArray array = new JSONArray();
    array.add("hello");
    value.put("greetings", array);
    TStream<JSONObject> s = t.constants(Collections.singletonList(value));
    TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
    TStream<String> output = JSONStreams.serialize(jsonm);
    JSONObject payload = new JSONObject();
    payload.put("payload", "hello");
    completeAndValidate(output, 10, payload.toString());
}
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 87 with Topology

use of com.ibm.streamsx.topology.Topology in project streamsx.topology by IBMStreams.

the class JSONStreamsTest method testJSONAble.

@Test
public void testJSONAble() throws IOException, Exception {
    Topology topology = new Topology();
    final TestJSONAble value = new TestJSONAble(42, QUESTION);
    TStream<TestJSONAble> s = topology.constants(Collections.singletonList(value)).asType(TestJSONAble.class);
    TStream<JSONObject> js = JSONStreams.toJSON(s);
    JSONObject ev = new JSONObject();
    ev.put("b", QUESTION);
    ev.put("a", 42l);
    checkJsonOutput(ev, JSONStreams.serialize(js));
}
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)

Example 88 with Topology

use of com.ibm.streamsx.topology.Topology in project streamsx.topology by IBMStreams.

the class JSONStreamsTest method testModifyingJson.

@Test
public void testModifyingJson() throws Exception {
    final Topology t = new Topology();
    final JSONObject value = new JSONObject();
    value.put("question", QUESTION);
    TStream<JSONObject> s = t.constants(Collections.singletonList(value));
    TStream<String> jsonString = JSONStreams.serialize(s);
    TStream<JSONObject> json = JSONStreams.deserialize(jsonString);
    TStream<JSONObject> jsonm = modifyJSON(json);
    assertFalse(value.containsKey("answer"));
    JSONObject ev = new JSONObject();
    ev.put("question", QUESTION);
    ev.put("answer", 42l);
    checkJsonOutput(ev, JSONStreams.serialize(jsonm));
}
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)

Example 89 with Topology

use of com.ibm.streamsx.topology.Topology in project streamsx.topology by IBMStreams.

the class TuplePassingTest method testObjectMillion5Standalone.

@Test
public void testObjectMillion5Standalone() throws Exception {
    assumeTrue(SC_OK && PERF_OK);
    assumeTrue(isMainRun());
    Topology t = new Topology("t1m5ObjectStandalone");
    System.err.println("Object(TestValue)-Standalone");
    addTimer(objectWorkload(objectSource(t, 1000000), 5));
    StreamsContextFactory.getStreamsContext(Type.STANDALONE).submit(t).get();
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 90 with Topology

use of com.ibm.streamsx.topology.Topology in project streamsx.topology by IBMStreams.

the class SubscribeSPLDictTest method testSubscribeSPLDictMap.

@Test
public void testSubscribeSPLDictMap() throws Exception {
    assumeTrue(!isStreamingAnalyticsRun());
    Topology topology = new Topology("testSubscribeMap");
    // Publish a stream with all the SPL types supported by Python including sets
    SPLStream tuples = PythonFunctionalOperatorsTest.testTupleStream(topology, true);
    tuples = addStartupDelay(tuples);
    tuples.publish("pytest/spl/map");
    SPLStream viaSPL = SPL.invokeOperator("spl.relational::Functor", tuples, tuples.getSchema(), null);
    // Python that subscribes to the SPL tuple stream and then republishes as Json.
    includePythonApp(topology, "spl_map_json.py", "spl_map_json::spl_map_json");
    TStream<JSONObject> viaPythonJson = topology.subscribe("pytest/spl/map/result", JSONObject.class);
    SPLStream viaPythonJsonSpl = JSONStreams.toSPL(viaPythonJson.isolate());
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(viaPythonJsonSpl, TUPLE_COUNT);
    Condition<Long> expectedCountSpl = tester.tupleCount(viaSPL, TUPLE_COUNT);
    Condition<List<Tuple>> viaSPLResult = tester.tupleContents(viaSPL);
    Condition<List<Tuple>> viaPythonResult = tester.tupleContents(viaPythonJsonSpl);
    complete(tester, allConditions(expectedCount, expectedCountSpl), 60, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(expectedCountSpl.valid());
    List<Tuple> splResults = viaSPLResult.getResult();
    List<Tuple> pyJsonResults = viaPythonResult.getResult();
    assertEquals(TUPLE_COUNT, splResults.size());
    assertEquals(TUPLE_COUNT, pyJsonResults.size());
    for (int i = 0; i < TUPLE_COUNT; i++) {
        Tuple spl = splResults.get(i);
        JSONObject json = (JSONObject) JSON.parse(pyJsonResults.get(i).getString("jsonString"));
        System.out.println(spl);
        System.out.println(pyJsonResults.get(i).getString("jsonString"));
        assertEquals(spl.getBoolean("b"), ((Boolean) json.get("b")).booleanValue());
        assertEquals(spl.getInt("i8"), ((Number) json.get("i8")).intValue());
        assertEquals(spl.getInt("i16"), ((Number) json.get("i16")).intValue());
        assertEquals(spl.getInt("i32"), ((Number) json.get("i32")).intValue());
        assertEquals(spl.getLong("i64"), ((Number) json.get("i64")).longValue());
        assertEquals(spl.getString("r"), json.get("r").toString());
        assertEquals(spl.getDouble("f32"), ((Number) json.get("f32")).doubleValue(), 0.1);
        assertEquals(spl.getDouble("f64"), ((Number) json.get("f64")).doubleValue(), 0.1);
        {
            List<?> ex = spl.getList("li32");
            JSONArray pya = (JSONArray) json.get("li32");
            assertEquals(ex.size(), pya.size());
            for (int j = 0; j < ex.size(); j++) {
                assertEquals(ex.get(j), ((Number) pya.get(j)).intValue());
            }
        }
        {
            Set<?> ex = spl.getSet("si32");
            JSONArray pya = (JSONArray) json.get("si32");
            assertEquals(ex.size(), pya.size());
            setAssertHelper(ex, pya);
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Set(java.util.Set) TreeSet(java.util.TreeSet) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Tuple(com.ibm.streams.operator.Tuple) PythonFunctionalOperatorsTest(com.ibm.streamsx.topology.test.splpy.PythonFunctionalOperatorsTest) Test(org.junit.Test)

Aggregations

Topology (com.ibm.streamsx.topology.Topology)372 TestTopology (com.ibm.streamsx.topology.test.TestTopology)315 Test (org.junit.Test)301 List (java.util.List)116 Tester (com.ibm.streamsx.topology.tester.Tester)115 ArrayList (java.util.ArrayList)70 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)68 HashMap (java.util.HashMap)49 TStream (com.ibm.streamsx.topology.TStream)42 Condition (com.ibm.streamsx.topology.tester.Condition)28 File (java.io.File)27 TimeUnit (java.util.concurrent.TimeUnit)27 StreamSchema (com.ibm.streams.operator.StreamSchema)26 Random (java.util.Random)26 JSONObject (com.ibm.json.java.JSONObject)23 HashSet (java.util.HashSet)23 RString (com.ibm.streams.operator.types.RString)21 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)21 Message (com.ibm.streamsx.topology.tuple.Message)20 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)20