Java Code Examples for io.siddhi.core.SiddhiManager#setExtension()

The following examples show how to use io.siddhi.core.SiddhiManager#setExtension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefineTableTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testQuery21() {
    log.info("testTableDefinition21 - Table w/ ref to an undefined store type");

    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.type", "testdb");
    systemConfigs.put("test1.uri", "http://localhost");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    inMemoryConfigManager.extractSystemConfigs("test2");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    siddhiManager.setExtension("store:test", TestStore.class);
    String siddhiApp = "" +
            "@store(ref='test2', uri='http://localhost:8080', table.name ='Foo')" +
            "define table testTable (symbol string, price int, volume float); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();
}
 
Example 2
Source File: DefineTableTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testQuery20() {
    log.info("testTableDefinition20 - Table w/ ref to an undefined store");

    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.uri", "http://localhost");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    inMemoryConfigManager.extractSystemConfigs("test2");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    siddhiManager.setExtension("store:test", TestStore.class);
    String siddhiApp = "" +
            "@store(ref='test2', uri='http://localhost:8080', table.name ='Foo')" +
            "define table testTable (symbol string, price int, volume float); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();
}
 
Example 3
Source File: DefineTableTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testQuery19() {
    log.info("testTableDefinition19 - Table w/ ref w/o type");

    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.uri", "http://localhost");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    inMemoryConfigManager.extractSystemConfigs("test1");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    siddhiManager.setExtension("store:test", TestStore.class);
    String siddhiApp = "" +
            "@store(ref='test1', uri='http://localhost:8080', table.name ='Foo')" +
            "define table testTable (symbol string, price int, volume float); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();
}
 
Example 4
Source File: DefineTableTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery18() {
    log.info("testTableDefinition18 - Table w/ ref and additional properties");

    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.type", "test");
    systemConfigs.put("test1.uri", "http://localhost");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    inMemoryConfigManager.extractSystemConfigs("test1");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    siddhiManager.setExtension("store:test", TestStore.class);
    String siddhiApp = "" +
            "@store(ref='test1', uri='http://localhost:8080', table.name ='Foo')" +
            "define table testTable (symbol string, price int, volume float); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();

    Map<String, String> expectedSystemConfigs = new HashMap<>();
    expectedSystemConfigs.put("type", "test");
    expectedSystemConfigs.put("uri", "http://localhost:8080");
    expectedSystemConfigs.put("table.name", "Foo");
    AssertJUnit.assertEquals("Test store initialization failure", expectedSystemConfigs,
            TestStore.systemConfigs);
}
 
Example 5
Source File: DefineTableTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery16() {
    log.info("testTableDefinition16 - Table w/ ref");

    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.type", "test");
    systemConfigs.put("test1.uri", "http://localhost");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    inMemoryConfigManager.extractSystemConfigs("test1");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    siddhiManager.setExtension("store:test", TestStore.class);
    String siddhiApp = "" +
            "@store(ref='test1')" +
            "define table testTable (symbol string, price int, volume float); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();

    Map<String, String> expectedSystemConfigs = new HashMap<>();
    expectedSystemConfigs.put("type", "test");
    expectedSystemConfigs.put("uri", "http://localhost");
    AssertJUnit.assertEquals("Test store initialization failure", expectedSystemConfigs, TestStore.systemConfigs);
}
 
Example 6
Source File: StreamFunctionTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void nonStandardAttribute() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:get", AttributeStreamFunction.class);
    String siddhiApp = "" +
            "define stream `$InputStream` (`56$2theta` double, rho double); " +
            "@info(name = 'query1') " +
            "from `$InputStream`#custom:get('test(0)') " +
            "select `56$2theta`, rho, `test(0)` as foo " +
            "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                AssertJUnit.assertEquals(22.6, inEvents[0].getData(0));
                AssertJUnit.assertEquals(13.0, inEvents[0].getData(1));
                AssertJUnit.assertEquals("test", inEvents[0].getData(2));
            }
            eventArrived = true;
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("$InputStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{22.6, 13.0});
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 7
Source File: SiddhiOperatorContext.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * @return Created new SiddhiManager instance with registered siddhi extensions
 */
public SiddhiManager createSiddhiManager() {
    SiddhiManager siddhiManager = new SiddhiManager();
    for (Map.Entry<String, Class<?>> entry : getExtensions().entrySet()) {
        siddhiManager.setExtension(entry.getKey(), entry.getValue());
    }
    return siddhiManager;
}
 
Example 8
Source File: ExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void extensionTest4() throws InterruptedException, ClassNotFoundException {
    log.info("extension test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAll", StringConcatAggregatorExecutorString.class);


    String cseEventStream = "define stream cseEventStream (price long, volume long);";
    String query = ("@info(name = 'query1') from cseEventStream select  custom:plus(*) as totalCount " +
            "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(800L, inEvent.getData(0));
                } else if (count == 2) {
                    AssertJUnit.assertEquals(805L, inEvent.getData(0));
                } else if (count == 3) {
                    AssertJUnit.assertEquals(260L, inEvent.getData(0));
                }
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{700L, 100L});
    inputHandler.send(new Object[]{605L, 200L});
    inputHandler.send(new Object[]{60L, 200L});
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 9
Source File: YAMLConfigManagerTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void yamlConfigManagerTest4() throws InterruptedException {
    log.info("yamlConfigManagerTest4");

    String baseDir = Paths.get(".").toString();
    Path path = Paths.get(baseDir, "src", "test", "resources", "systemProperties.yaml");

    String fileContent = FileReader.readYAMLConfigFile(path);

    YAMLConfigManager yamlConfigManager = new YAMLConfigManager(fileContent);

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorExecutorString.class);
    siddhiManager.setConfigManager(yamlConfigManager);


    String cseEventStream = "" +
            "" +
            "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select price , email:getAllNew(symbol,'') as toConcat " +
            "group by volume " +
            "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);


    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC-abc", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 100L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 200L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"ABC", 60.5f, 200L});
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: ExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void extensionTest5() throws InterruptedException, ClassNotFoundException {
    log.info("extension test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    Map<String, String> configMap = new HashMap<>();
    configMap.put("email.getAllNew.append.abc", "true");
    siddhiManager.setConfigManager(new InMemoryConfigManager(configMap, null));
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorExecutorString.class);

    String cseEventStream = "" +
            "" +
            "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select price , email:getAllNew(symbol,'') as toConcat " +
            "group by volume " +
            "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC-abc", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 100L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 200L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"ABC", 60.5f, 200L});
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 11
Source File: ExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void extensionTest3() throws InterruptedException {
    log.info("extension test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorExecutorString.class);

    String cseEventStream = "" +
            "" +
            "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("" +
            "@info(name = 'query1') " +
            "from cseEventStream " +
            "select price , email:getAllNew(symbol,'') as toConcat " +
            "group by volume " +
            "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700f, 100L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"WSO2", 60.5f, 200L});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"ABC", 60.5f, 200L});
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 12
Source File: ExtensionTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void extensionTest2() throws InterruptedException, ClassNotFoundException {
    log.info("extension test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAll", StringConcatAggregatorExecutorString.class);

    String cseEventStream = "define stream cseEventStream (symbol string, price long, volume long);";
    String query = ("@info(name = 'query1') from cseEventStream select symbol , custom:plus(price,volume) as " +
            "totalCount " +
            "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(800L, inEvent.getData(1));
                } else if (count == 2) {
                    AssertJUnit.assertEquals(805L, inEvent.getData(1));
                } else if (count == 3) {
                    AssertJUnit.assertEquals(260L, inEvent.getData(1));
                }
            }
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 700L, 100L});
    inputHandler.send(new Object[]{"WSO2", 605L, 200L});
    inputHandler.send(new Object[]{"ABC", 60L, 200L});
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
 
Example 13
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "faultStreamTest4")
public void faultStreamTest5() throws InterruptedException {
    log.info("faultStreamTest5-Tests fault handling when it's set to stream. " +
            "Events would be available in the corresponding fault stream");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);

    String siddhiApp = "" +
            "@OnError(action='stream')" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream[custom:fault() > volume] " +
            "select symbol, price , symbol as sym1 " +
            "insert into outputStream ;" +
            "";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("!cseEventStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events[0].getData(3) != null);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    Logger logger = Logger.getLogger(StreamJunction.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages() == null);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

    AssertJUnit.assertEquals(1, count.get());
    AssertJUnit.assertTrue(eventArrived);
}
 
Example 14
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "faultStreamTest3")
public void faultStreamTest4() throws InterruptedException {
    log.info("faultStreamTest4-Tests fault handling when it's set to stream. " +
            "Events would be available in the corresponding fault stream");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);

    String siddhiApp = "" +
            "@OnError(action='stream')" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream[custom:fault() > volume] " +
            "select symbol, price, symbol as sym1 " +
            "insert into outputStream ;" +
            "" +
            "from !cseEventStream " +
            "select * " +
            "insert into faultStream";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("faultStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events[0].getData(3) != null);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    Logger logger = Logger.getLogger(StreamJunction.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages() == null);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

    AssertJUnit.assertEquals(1, count.get());
    AssertJUnit.assertTrue(eventArrived);
}
 
Example 15
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "faultStreamTest2")
public void faultStreamTest3() throws InterruptedException {
    log.info("faultStreamTest3-Tests fault handling when it's set to stream. " +
            "No errors would be logged since exceptions are being gracefully handled.");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);

    String siddhiApp = "" +
            "@OnError(action='stream')" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream[custom:fault() > volume] " +
            "select symbol, price, symbol as sym1 " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    Logger logger = Logger.getLogger(StreamJunction.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages() == null);
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

    AssertJUnit.assertEquals(0, count.get());
    AssertJUnit.assertFalse(eventArrived);

}
 
Example 16
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "faultStreamTest1")
public void faultStreamTest2() throws InterruptedException {
    log.info("faultStreamTest2-Tests logging when fault handling is set to log.");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);

    String siddhiApp = "" +
            "@OnError(action='log')" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream[custom:fault() > volume] " +
            "select symbol, price , symbol as sym1 " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    Logger logger = Logger.getLogger(StreamJunction.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages().contains("Error when running faultAdd(). Exception on " +
                "class 'io.siddhi.core.stream.FaultFunctionExtension'"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

    AssertJUnit.assertEquals(0, count.get());
    AssertJUnit.assertFalse(eventArrived);
}
 
Example 17
Source File: FaultStreamTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void faultStreamTest1() throws InterruptedException {
    log.info("faultStreamTest1-Tests logging by default when fault handling is not configured explicitly.");

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:fault", FaultFunctionExtension.class);

    String siddhiApp = "" +
            "define stream cseEventStream (symbol string, price float, volume long);" +
            "" +
            "@info(name = 'query1') " +
            "from cseEventStream[custom:fault() > volume] " +
            "select symbol, price , symbol as sym1 " +
            "insert into outputStream ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
            eventArrived = true;
        }

    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    Logger logger = Logger.getLogger(StreamJunction.class);
    UnitTestAppender appender = new UnitTestAppender();
    logger.addAppender(appender);
    try {
        inputHandler.send(new Object[]{"IBM", 0f, 100L});
        AssertJUnit.assertTrue(appender.getMessages().contains("Error when running faultAdd(). " +
                "Exception on class 'io.siddhi.core.stream.FaultFunctionExtension'"));
    } catch (Exception e) {
        Assert.fail("Unexpected exception occurred when testing.", e);
    } finally {
        logger.removeAppender(appender);
        siddhiAppRuntime.shutdown();
    }

    AssertJUnit.assertEquals(0, count.get());
    AssertJUnit.assertFalse(eventArrived);

}
 
Example 18
Source File: ExtensionSample.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        // Creating Siddhi Manager
        SiddhiManager siddhiManager = new SiddhiManager();

        //Register the extension to Siddhi Manager
        siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);

        //Siddhi Application
        String siddhiApp = "" +
                "define stream StockStream (symbol string, price long, volume long);" +
                "" +
                "@info(name = 'query1') " +
                "from StockStream " +
                "select symbol , custom:plus(price, volume) as totalCount " +
                "insert into Output;";

        //Generating runtime
        SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

        //Adding callback to retrieve output events from query
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {
            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
            }
        });

        //Retrieving InputHandler to push events into Siddhi
        InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");

        //Starting event processing
        siddhiAppRuntime.start();

        //Sending events to Siddhi
        inputHandler.send(new Object[]{"IBM", 700L, 100L});
        inputHandler.send(new Object[]{"WSO2", 600L, 200L});
        inputHandler.send(new Object[]{"GOOG", 60L, 200L});
        Thread.sleep(500);

        //Shutting down the runtime
        siddhiAppRuntime.shutdown();

        //Shutting down Siddhi
        siddhiManager.shutdown();

    }