/* * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.siddhi.extension.output.transport.test; import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.wso2.siddhi.core.ExecutionPlanRuntime; import org.wso2.siddhi.core.SiddhiManager; import org.wso2.siddhi.core.stream.input.InputHandler; import org.wso2.siddhi.core.util.transport.InMemoryBroker; import org.wso2.siddhi.core.stream.output.sink.PassThroughSinkmapper; import org.wso2.siddhi.query.api.ExecutionPlan; import org.wso2.siddhi.query.api.annotation.Annotation; import org.wso2.siddhi.query.api.definition.Attribute; import org.wso2.siddhi.query.api.definition.StreamDefinition; import org.wso2.siddhi.query.api.execution.query.Query; import org.wso2.siddhi.query.api.execution.query.input.stream.InputStream; import org.wso2.siddhi.query.api.execution.query.selection.Selector; import org.wso2.siddhi.query.api.expression.Variable; import java.util.concurrent.atomic.AtomicInteger; public class TestSinkTestCase { static final Logger log = Logger.getLogger(TestSinkTestCase.class); private AtomicInteger wso2Count = new AtomicInteger(0); private AtomicInteger ibmCount = new AtomicInteger(0); @Before public void init() { wso2Count.set(0); ibmCount.set(0); } // from FooStream // select symbol // publish inMemory options (topic ‘foo’) // map text; @Test public void testPublisherWithSelector() throws InterruptedException { InMemoryBroker.subscribe(new InMemoryBroker.Subscriber() { @Override public void onMessage(Object msg) { wso2Count.incrementAndGet(); } @Override public String getTopic() { return "WSO2"; } }); InMemoryBroker.subscribe(new InMemoryBroker.Subscriber() { @Override public void onMessage(Object msg) { ibmCount.incrementAndGet(); } @Override public String getTopic() { return "IBM"; } }); StreamDefinition inputDefinition = StreamDefinition.id("FooStream") .attribute("symbol", Attribute.Type.STRING) .attribute("price", Attribute.Type.INT) .attribute("volume", Attribute.Type.FLOAT); StreamDefinition outputDefinition = StreamDefinition.id("BarStream") .attribute("symbol", Attribute.Type.STRING) .attribute("price", Attribute.Type.INT) .attribute("volume", Attribute.Type.FLOAT) .annotation(Annotation.annotation("sink") .element("type", "test") .element("topic", "{{symbol}}") .annotation(Annotation.annotation("map") .element("type", "text"))); Query query = Query.query(); query.from( InputStream.stream("FooStream") ); query.select( Selector.selector().select(new Variable("symbol")).select(new Variable("price")).select(new Variable ("volume")) ); query.insertInto("BarStream"); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setExtension("sinkMapper:text", PassThroughSinkmapper.class); ExecutionPlan executionPlan = new ExecutionPlan("ep1"); executionPlan.defineStream(inputDefinition); executionPlan.defineStream(outputDefinition); executionPlan.addQuery(query); ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(executionPlan); try { InputHandler stockStream = executionPlanRuntime.getInputHandler("FooStream"); executionPlanRuntime.start(); stockStream.send(new Object[]{"WSO2", 55.6f, 100L}); stockStream.send(new Object[]{"IBM", 75.6f, 100L}); stockStream.send(new Object[]{"WSO2", 57.6f, 100L}); Thread.sleep(100); Assert.assertEquals("Number of WSO2 events", 2, wso2Count.get()); Assert.assertEquals("Number of IBM events", 1, ibmCount.get()); } finally { executionPlanRuntime.shutdown(); } } @Test public void testPublisherWithSelectorQL() throws InterruptedException { InMemoryBroker.subscribe(new InMemoryBroker.Subscriber() { @Override public void onMessage(Object msg) { wso2Count.incrementAndGet(); } @Override public String getTopic() { return "WSO2"; } }); InMemoryBroker.subscribe(new InMemoryBroker.Subscriber() { @Override public void onMessage(Object msg) { ibmCount.incrementAndGet(); } @Override public String getTopic() { return "IBM"; } }); SiddhiManager siddhiManager = new SiddhiManager(); siddhiManager.setExtension("sinkMapper:text", PassThroughSinkmapper.class); String streams = "" + "@Plan:name('TestExecutionPlan')" + "define stream FooStream (symbol string, price float, volume long); " + "@sink(type='test', topic='{{symbol}}', @map(type='text')) " + "define stream BarStream (symbol string, price float, volume long); "; String query = "" + "from FooStream " + "select * " + "insert into BarStream; "; ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query); try { InputHandler stockStream = executionPlanRuntime.getInputHandler("FooStream"); executionPlanRuntime.start(); stockStream.send(new Object[]{"WSO2", 55.6f, 100L}); stockStream.send(new Object[]{"IBM", 75.6f, 100L}); stockStream.send(new Object[]{"WSO2", 57.6f, 100L}); Thread.sleep(100); Assert.assertEquals("Number of WSO2 events", 2, wso2Count.get()); Assert.assertEquals("Number of IBM events", 1, ibmCount.get()); } finally { executionPlanRuntime.shutdown(); } } }