Search in sources :

Example 6 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class EventTestCase method testQueryParser.

@Test
public void testQueryParser() {
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition outStreamDefinition = StreamDefinition.id("outputStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT);
    Query query = new Query();
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.NOT_EQUAL, Expression.value(50))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("outputStream");
    Map<String, AbstractDefinition> tableDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> windowDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> aggregationDefinitionMap = new HashMap<>();
    Map<String, Table> tableMap = new HashMap<String, Table>();
    Map<String, Window> eventWindowMap = new HashMap<String, Window>();
    Map<String, AggregationRuntime> aggregationMap = new HashMap<String, AggregationRuntime>();
    Map<String, List<Source>> eventSourceMap = new HashMap<String, List<Source>>();
    Map<String, List<Sink>> eventSinkMap = new HashMap<String, List<Sink>>();
    Map<String, AbstractDefinition> streamDefinitionMap = new HashMap<String, AbstractDefinition>();
    LockSynchronizer lockSynchronizer = new LockSynchronizer();
    streamDefinitionMap.put("cseEventStream", streamDefinition);
    streamDefinitionMap.put("outputStream", outStreamDefinition);
    SiddhiContext siddhicontext = new SiddhiContext();
    SiddhiAppContext context = new SiddhiAppContext();
    context.setSiddhiContext(siddhicontext);
    context.setElementIdGenerator(new ElementIdGenerator(context.getName()));
    context.setSnapshotService(new SnapshotService(context));
    QueryRuntime runtime = QueryParser.parse(query, context, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, aggregationMap, eventWindowMap, lockSynchronizer, "1");
    AssertJUnit.assertNotNull(runtime);
    AssertJUnit.assertTrue(runtime.getStreamRuntime() instanceof SingleStreamRuntime);
    AssertJUnit.assertNotNull(runtime.getSelector());
    AssertJUnit.assertTrue(runtime.getMetaComplexEvent() instanceof MetaStreamEvent);
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) HashMap(java.util.HashMap) ElementIdGenerator(org.wso2.siddhi.core.util.ElementIdGenerator) Source(org.wso2.siddhi.core.stream.input.source.Source) SiddhiContext(org.wso2.siddhi.core.config.SiddhiContext) Sink(org.wso2.siddhi.core.stream.output.sink.Sink) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) LockSynchronizer(org.wso2.siddhi.core.util.lock.LockSynchronizer) List(java.util.List) AggregationRuntime(org.wso2.siddhi.core.aggregation.AggregationRuntime) Window(org.wso2.siddhi.core.window.Window) SnapshotService(org.wso2.siddhi.core.util.snapshot.SnapshotService) Table(org.wso2.siddhi.core.table.Table) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) SiddhiAppContext(org.wso2.siddhi.core.config.SiddhiAppContext) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) Test(org.testng.annotations.Test)

Example 7 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class LogSinkTest method testWithAllOptions.

@Test
public void testWithAllOptions() throws Exception {
    SiddhiManager siddhiManager = new SiddhiManager();
    String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
    String outputStream = "@sink(type='log', prefix='My Log',  priority='info')\n" + "define stream OutputStream(weight int, totalWeight long);";
    String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
    siddhiAppRuntime.start();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
    Logger logger = Logger.getLogger(LogSink.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[] { 2 });
        AssertJUnit.assertTrue(appender.getMessages().contains("My Log"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[2, 2]"));
        inputHandler.send(new Object[] { 3 });
        AssertJUnit.assertTrue(appender.getMessages().contains("My Log"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[3, 5]"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing with all options", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) UnitTestAppender(org.wso2.siddhi.core.UnitTestAppender) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Logger(org.apache.log4j.Logger) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 8 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class LogSinkTest method testWithDefaultPrefix.

@Test
public void testWithDefaultPrefix() throws Exception {
    SiddhiManager siddhiManager = new SiddhiManager();
    String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
    String outputStream = "@sink(type='log',  priority='info')\n" + "define stream OutputStream(weight int, totalWeight long);";
    String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
    siddhiAppRuntime.start();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
    Logger logger = Logger.getLogger(LogSink.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[] { 2 });
        AssertJUnit.assertTrue(appender.getMessages().contains("HelloWorldApp : OutputStream"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[2, 2]"));
        inputHandler.send(new Object[] { 3 });
        AssertJUnit.assertTrue(appender.getMessages().contains("HelloWorldApp : OutputStream"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[3, 5]"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing with default prefix", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) UnitTestAppender(org.wso2.siddhi.core.UnitTestAppender) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Logger(org.apache.log4j.Logger) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 9 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class LogSinkTest method testWithDefaultPriority.

@Test
public void testWithDefaultPriority() throws Exception {
    SiddhiManager siddhiManager = new SiddhiManager();
    String inputStream = "@App:name(\"HelloWorldApp\")\n" + "define stream CargoStream (weight int);";
    String outputStream = "@sink(type='log', prefix='My Log')\n" + "define stream OutputStream(weight int, totalWeight long);";
    String query = ("@info(name='HelloWorldQuery') " + "from CargoStream " + "select weight, sum(weight) as totalWeight " + "insert into OutputStream;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream + outputStream + query);
    siddhiAppRuntime.start();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("CargoStream");
    Logger logger = Logger.getLogger(LogSink.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[] { 2 });
        AssertJUnit.assertTrue(appender.getMessages().contains("My Log"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[2, 2]"));
        inputHandler.send(new Object[] { 3 });
        AssertJUnit.assertTrue(appender.getMessages().contains("My Log"));
        AssertJUnit.assertTrue(appender.getMessages().contains("data=[3, 5]"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing with default priority", e);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) UnitTestAppender(org.wso2.siddhi.core.UnitTestAppender) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Logger(org.apache.log4j.Logger) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 10 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryTestCase4.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void inMemoryTestCase4() throws InterruptedException {
    log.info("Test inMemory 4");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='Foo', @map(type='passThrough', @attributes('symbol','price'))) " + "define stream FooStream (symbol string, price float, volume long); " + "@sink(type='inMemory', topic='{{symbol}}', @map(type='passThrough')) " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)27 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)25 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)25 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)17 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 Sink (org.wso2.siddhi.core.stream.output.sink.Sink)5 HashMap (java.util.HashMap)4 Logger (org.apache.log4j.Logger)4 UnitTestAppender (org.wso2.siddhi.core.UnitTestAppender)4 Extension (org.wso2.siddhi.query.api.extension.Extension)4 Event (org.wso2.siddhi.core.event.Event)3 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)3 Source (org.wso2.siddhi.core.stream.input.source.Source)3 InMemoryConfigManager (org.wso2.siddhi.core.util.config.InMemoryConfigManager)3 OptionHolder (org.wso2.siddhi.core.util.transport.OptionHolder)3 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)2 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)2 ConnectionUnavailableException (org.wso2.siddhi.core.exception.ConnectionUnavailableException)2 SourceHandlerManager (org.wso2.siddhi.core.stream.input.source.SourceHandlerManager)2 SinkHandlerManager (org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager)2