Search in sources :

Example 46 with JSONObject

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

the class PublishSubscribeTest method testFilterOnJson.

@Test(expected = IllegalArgumentException.class)
public void testFilterOnJson() throws Exception {
    final Topology t = new Topology();
    TStream<JSONObject> json = t.constants(Collections.<JSONObject>emptyList()).asType(JSONObject.class);
    ;
    json.publish("sometopic", true);
}
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 47 with JSONObject

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

the class SPLOperatorsTest method pvToStr.

private String pvToStr(JSONObject jo) {
    // A Client of the API shouldn't find itself in
    // a place to need this.  It's just an artifact of
    // the way these tests are composed plus lack of a 
    // public form of valueToString(SPL.createValue(...)).
    String type = (String) jo.get("type");
    if (!"__spl_value".equals(type))
        throw new IllegalArgumentException("jo " + jo);
    JSONObject value = (JSONObject) jo.get("value");
    String metaType = (String) value.get("metaType");
    Object v = value.get("value");
    if (metaType.startsWith("UINT"))
        return SPLGenerator.unsignedString(v);
    else
        return v.toString();
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONObject(com.ibm.json.java.JSONObject)

Example 48 with JSONObject

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

the class BeaconTest method testBeaconTuples.

@Test
public void testBeaconTuples() throws Exception {
    Topology topology = new Topology("testFixedCount");
    final int count = new Random().nextInt(1000) + 37;
    TStream<BeaconTuple> beacon = BeaconStreams.beacon(topology, count);
    TStream<JSONObject> json = JSONStreams.toJSON(beacon);
    TStream<String> strings = JSONStreams.serialize(json);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(strings, count);
    @SuppressWarnings("serial") Condition<String> contents = tester.stringTupleTester(strings, new Predicate<String>() {

        private transient BeaconTuple lastTuple;

        @Override
        public boolean test(String tuple) {
            JSONObject json;
            try {
                json = (JSONObject) JSON.parse(tuple);
            } catch (NullPointerException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            BeaconTuple bt = new BeaconTuple((Long) json.get("sequence"), (Long) json.get("time"));
            boolean ok;
            if (lastTuple == null) {
                ok = bt.getSequence() == 0;
            } else {
                ok = lastTuple.compareTo(bt) < 0 && lastTuple.getTime() <= bt.getTime() && lastTuple.getSequence() + 1 == bt.getSequence();
            }
            ok = ok && bt.getTime() != 0;
            ok = ok && bt.getKey() == bt.getSequence();
            lastTuple = bt;
            return ok;
        }
    });
    complete(tester, expectedCount, 20, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(contents.toString(), contents.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) IOException(java.io.IOException) Random(java.util.Random) JSONObject(com.ibm.json.java.JSONObject) BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) Test(org.junit.Test)

Example 49 with JSONObject

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

the class PlacementInfo method disallowColocateIsolatedOpWithParent.

/** throw if s1.isolate().filter().colocate(s1) */
private void disallowColocateIsolatedOpWithParent(Placeable<?> first, Placeable<?>... toFuse) {
    JSONObject graph = first.builder().complete();
    JSONObject colocateOp = first.operator().complete();
    Set<JsonObject> parents = GraphUtilities.getUpstream(JSON4JUtilities.gson(colocateOp), JSON4JUtilities.gson(graph));
    if (!parents.isEmpty()) {
        JsonObject isolate = parents.iterator().next();
        String kind = jstring(isolate, "kind");
        if (!ISOLATE.kind().equals(kind))
            return;
        parents = GraphUtilities.getUpstream(isolate, JSON4JUtilities.gson(graph));
        if (parents.isEmpty())
            return;
        JsonObject isolateParentOp = parents.iterator().next();
        for (Placeable<?> placeable : toFuse) {
            JSONObject tgtOp = placeable.operator().complete();
            if (tgtOp.get("name").equals(jstring(isolateParentOp, "name")))
                throw new IllegalStateException("Illegal to colocate an isolated stream with its parent.");
        }
    }
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JsonObject(com.google.gson.JsonObject)

Example 50 with JSONObject

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

the class PlacementInfo method updatePlacementJSON.

/**
     * Update an element's placement configuration.
     */
private void updatePlacementJSON(Placeable<?> element) {
    JSONObject placement = JOperatorConfig.createJSONItem(element.operator().json(), PLACEMENT);
    placement.put(PLACEMENT_EXPLICIT_COLOCATE_ID, fusingIds.get(element));
    Set<String> elementResourceTags = resourceTags.get(element);
    if (elementResourceTags != null && !elementResourceTags.isEmpty()) {
        JSONArray listOfTags = new JSONArray();
        listOfTags.addAll(elementResourceTags);
        placement.put(PLACEMENT_RESOURCE_TAGS, listOfTags);
    }
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray)

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