Search in sources :

Example 1 with SimpleMessage

use of com.ibm.streamsx.topology.tuple.SimpleMessage in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testMultiTopicProducer.

@Test
public void testMultiTopicProducer() throws Exception {
    checkAssumes();
    // streamsx.messaging issue#118 prevents successful execution
    // For standalone it seems to consistently get 0 topic1 msgs.
    assumeTrue(getTesterType() != StreamsContext.Type.STANDALONE_TESTER);
    Topology top = new Topology("testMultiTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String[] topics = getKafkaTopics();
    String topic1Val = topics[0];
    String topic2Val = topics[1];
    Supplier<String> topic1 = new Value<String>(topic1Val);
    Supplier<String> topic2 = new Value<String>(topic2Val);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that publishes to multiple topics (implies implicit topic)
    List<Message> topic1Msgs = new ArrayList<>();
    topic1Msgs.add(new SimpleMessage(mgen.create(topic1Val, "Hello"), null, topic1Val));
    topic1Msgs.add(new SimpleMessage(mgen.create(topic1Val, "Are you there?"), null, topic1Val));
    List<Message> topic2Msgs = new ArrayList<>();
    topic2Msgs.add(new SimpleMessage(mgen.create(topic2Val, "Hello"), null, topic2Val));
    topic2Msgs.add(new SimpleMessage(mgen.create(topic2Val, "Are you there?"), null, topic2Val));
    List<Message> msgs = new ArrayList<>(topic1Msgs);
    msgs.addAll(topic2Msgs);
    TStream<Message> msgsToPublish = top.constants(msgs);
    msgsToPublish = msgsToPublish.modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish);
    TStream<Message> rcvdTopic1Msgs = consumer.subscribe(topic1);
    TStream<Message> rcvdTopic2Msgs = consumer.subscribe(topic2);
    // for validation...
    TStream<Message> rcvdMsgs = rcvdTopic1Msgs.union(rcvdTopic2Msgs);
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    List<String> expectedAsString = mapList(msgs, msgToJSONStringFunc());
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidateUnordered(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 2 with SimpleMessage

use of com.ibm.streamsx.topology.tuple.SimpleMessage in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testSimpleMessage.

@Test
public void testSimpleMessage() throws Exception {
    // test ctors
    for (Vals vals : testMsgVals) {
        SimpleMessage t = !vals.hasTopic() ? new SimpleMessage(vals.msg, vals.key) : new SimpleMessage(vals.msg, vals.key, vals.topic);
        assertEquals("ctor " + vals.toString(), vals.key, t.getKey());
        assertEquals("ctor " + vals.toString(), vals.msg, t.getMessage());
        assertEquals("ctor " + vals.toString(), vals.topic, t.getTopic());
        if (!vals.hasKey() && !vals.hasTopic()) {
            t = new SimpleMessage(vals.msg);
            assertEquals("ctor " + vals.toString(), vals.key, t.getKey());
            assertEquals("ctor " + vals.toString(), vals.msg, t.getMessage());
            assertEquals("ctor " + vals.toString(), vals.topic, t.getTopic());
        }
        Exception exc = null;
        try {
            // throw IAE
            new SimpleMessage(null, "key", "topic");
        } catch (Exception e) {
            exc = e;
        }
        assertNotNull(exc);
        assertTrue(exc.getClass().toString(), exc instanceof IllegalArgumentException);
    }
    // test equals()
    for (Vals vals : testMsgVals) {
        Message t = !vals.hasTopic() ? new SimpleMessage(vals.msg, vals.key) : new SimpleMessage(vals.msg, vals.key, vals.topic);
        Message t2 = !vals.hasTopic() ? new SimpleMessage(vals.msg, vals.key) : new SimpleMessage(vals.msg, vals.key, vals.topic);
        assertTrue("same equals() - " + t.toString(), t.equals(t));
        assertTrue("equals() - " + t.toString(), t.equals(t2));
    }
    // test equals() negative
    for (Vals vals : testMsgVals) {
        Message t = !vals.hasTopic() ? new SimpleMessage(vals.msg, vals.key) : new SimpleMessage(vals.msg, vals.key, vals.topic);
        Message t2 = !vals.hasTopic() ? new SimpleMessage("someOtherValue", "someOtherKey") : new SimpleMessage("someOtherValue", "someOtherKey", "someOtherTopic");
        assertFalse("neg equals() - " + t.toString(), t.equals(t2));
        t2 = new SimpleMessage(vals.msg, vals.key, "someOtherTopic");
        assertFalse("neg equals() - " + t.toString(), t.equals(t2));
        t2 = new SimpleMessage("someOtherValue", vals.key);
        assertFalse("neg equals() - " + t.toString(), t.equals(t2));
        t2 = new SimpleMessage(vals.msg, "someOtherKey");
        assertFalse("neg equals() - " + t.toString(), t.equals(t2));
        assertFalse("neg equals() - " + t.toString(), t.equals(null));
        assertFalse("neg equals() - " + t.toString(), t.equals("notAMessageObject"));
    }
    // test hashCode()
    {
        SimpleMessage t = new SimpleMessage("x", "y", "z");
        assertTrue(t.hashCode() != 0);
        t = new SimpleMessage("x", null, null);
        assertTrue(t.hashCode() != 0);
    }
}
Also used : SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Test(org.junit.Test)

Example 3 with SimpleMessage

use of com.ibm.streamsx.topology.tuple.SimpleMessage in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method createMsgs.

private List<Message> createMsgs(MsgGenerator mgen, String topic) {
    List<Message> msgs = new ArrayList<>();
    msgs.add(new SimpleMessage(mgen.create(topic, "Hello"), null, topic));
    msgs.add(new SimpleMessage(mgen.create(topic, "Are you there?"), null, topic));
    return msgs;
}
Also used : SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) ArrayList(java.util.ArrayList)

Example 4 with SimpleMessage

use of com.ibm.streamsx.topology.tuple.SimpleMessage in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testMsgImplProducer.

@Test
public void testMsgImplProducer() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testMsgImplProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String subClientId = newSubClientId(top.getName());
    String pubClientId = newPubClientId(top.getName());
    String topicVal = getMqttTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    List<Message> msgs = createMsgs(mgen, null);
    List<String> expectedAsString = mapList(modifyList(msgs, setTopic(topicVal)), msgToJSONStringFunc());
    // Test producer that takes TStream<SimpleMessage> and an explicit topic.
    MqttStreams producer = new MqttStreams(top, createConfig(pubClientId));
    MqttStreams consumer = new MqttStreams(top, createConfig(subClientId));
    TStream<Message> msgsToPublish = top.constants(msgs).modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    TSink sink = producer.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    if (testBuildOnly(top))
        return;
    completeAndValidate(subClientId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
    assertTrue(sink != null);
}
Also used : TSink(com.ibm.streamsx.topology.TSink) MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 5 with SimpleMessage

use of com.ibm.streamsx.topology.tuple.SimpleMessage in project streamsx.topology by IBMStreams.

the class KafkaStreamsTest method testMsgImplProducer.

@Test
public void testMsgImplProducer() throws Exception {
    checkAssumes();
    Topology top = new Topology("testMsgImplProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String groupId = newGroupId(top.getName());
    String topicVal = getKafkaTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    KafkaProducer producer = new KafkaProducer(top, createProducerConfig());
    KafkaConsumer consumer = new KafkaConsumer(top, createConsumerConfig(groupId));
    // Test producer that takes TStream<SimpleMessage> and an explicit topic.
    List<Message> msgs = new ArrayList<>();
    msgs.add(new SimpleMessage(mgen.create(topicVal, "Hello")));
    msgs.add(new SimpleMessage(mgen.create(topicVal, "key1", "Are you there?"), "key1"));
    TStream<Message> msgsToPublish = top.constants(msgs);
    msgsToPublish = msgsToPublish.modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    producer.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    msgs = modifyList(msgs, setTopic(topicVal));
    List<String> expectedAsString = mapList(msgs, msgToJSONStringFunc());
    setupDebug();
    if (testBuildOnly(top))
        return;
    completeAndValidate(groupId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
}
Also used : KafkaProducer(com.ibm.streamsx.topology.messaging.kafka.KafkaProducer) InitialDelay(com.ibm.streamsx.topology.test.InitialDelay) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) ArrayList(java.util.ArrayList) KafkaConsumer(com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Aggregations

Message (com.ibm.streamsx.topology.tuple.Message)5 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)5 Test (org.junit.Test)4 Topology (com.ibm.streamsx.topology.Topology)3 Value (com.ibm.streamsx.topology.logic.Value)3 TestTopology (com.ibm.streamsx.topology.test.TestTopology)3 ArrayList (java.util.ArrayList)3 KafkaConsumer (com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer)2 KafkaProducer (com.ibm.streamsx.topology.messaging.kafka.KafkaProducer)2 InitialDelay (com.ibm.streamsx.topology.test.InitialDelay)2 TSink (com.ibm.streamsx.topology.TSink)1 MqttStreams (com.ibm.streamsx.topology.messaging.mqtt.MqttStreams)1