/*
***************************************************************************************
* Copyright (C) 2006 EsperTech, Inc. All rights reserved. *
* http://www.espertech.com/esper *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
***************************************************************************************
*/
package com.espertech.esper.regression.epl;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.soda.EPStatementFormatter;
import com.espertech.esper.client.soda.EPStatementObjectModel;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import com.espertech.esper.supportregression.bean.bookexample.*;
import com.espertech.esper.supportregression.bean.word.SentenceEvent;
import com.espertech.esper.supportregression.client.SupportConfigFactory;
import junit.framework.TestCase;
import static com.espertech.esper.supportregression.bean.bookexample.OrderBeanFactory.*;
public class TestContainedEventSimple extends TestCase
{
private String NEWLINE = System.getProperty("line.separator");
private EPServiceProvider epService;
private SupportUpdateListener listener;
public void setUp()
{
epService = EPServiceProviderManager.getDefaultProvider(SupportConfigFactory.getConfiguration());
epService.initialize();
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.startTest(epService, this.getClass(), getName());}
listener = new SupportUpdateListener();
}
protected void tearDown() throws Exception {
if (InstrumentationHelper.ENABLED) { InstrumentationHelper.endTest();}
listener = null;
}
// Assures that the events inserted into the named window are preemptive to events generated by contained-event syntax.
// This example generates 3 contained-events: One for each book.
// It then inserts them into a named window to determine the highest price among all.
// The named window updates first becoming useful to subsequent events (versus last and not useful).
public void testNamedWindowPremptive()
{
String[] fields = "bookId".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
epService.getEPAdministrator().getConfiguration().addEventType("BookDesc", BookDesc.class);
String stmtText = "insert into BookStream select * from OrderEvent[books]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
EPStatement stmtNW = epService.getEPAdministrator().createEPL("create window MyWindow#lastevent as BookDesc");
epService.getEPAdministrator().createEPL("insert into MyWindow select * from BookStream bs where not exists (select * from MyWindow mw where mw.price > bs.price)");
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10020"}, {"10021"}, {"10022"}});
listener.reset();
// higest price (27 is the last value)
EventBean theEvent = stmtNW.iterator().next();
assertEquals(35.0, theEvent.get("price"));
}
public void testUnidirectionalJoin()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select * from " +
"OrderEvent as orderEvent unidirectional, " +
"OrderEvent[select * from books] as book, " +
"OrderEvent[select * from orderdetail.items] as item " +
"where book.bookId=item.productId " +
"order by book.bookId, item.amount";
String stmtTextFormatted = "select *" + NEWLINE +
"from OrderEvent as orderEvent unidirectional," + NEWLINE +
"OrderEvent[select * from books] as book," + NEWLINE +
"OrderEvent[select * from orderdetail.items] as item" + NEWLINE +
"where book.bookId=item.productId" + NEWLINE +
"order by book.bookId, item.amount";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
runAssertion();
stmt.destroy();
EPStatementObjectModel model = epService.getEPAdministrator().compileEPL(stmtText);
assertEquals(stmtText, model.toEPL());
assertEquals(stmtTextFormatted, model.toEPL(new EPStatementFormatter(true)));
stmt = epService.getEPAdministrator().create(model);
stmt.addListener(listener);
runAssertion();
}
private void runAssertion()
{
String[] fields = "orderEvent.orderdetail.orderId,book.bookId,book.title,item.amount".split(",");
epService.getEPRuntime().sendEvent(makeEventOne());
assertEquals(3, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200901", "10020", "Enders Game", 10}, {"PO200901", "10020", "Enders Game", 30}, {"PO200901", "10021", "Foundation 1", 25}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventTwo());
assertEquals(1, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200902", "10022", "Stranger in a Strange Land", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventThree());
assertEquals(1, listener.getLastNewData().length);
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"PO200903", "10021", "Foundation 1", 50}});
}
public void testUnidirectionalJoinCount()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from " +
"OrderEvent orderEvent unidirectional, " +
"OrderEvent[books] as book, " +
"OrderEvent[orderdetail.items] item " +
"where book.bookId = item.productId order by book.bookId asc, item.amount asc";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{3L});
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{1L});
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{1L});
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
}
public void testJoinCount()
{
String[] fields = "count(*)".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from " +
"OrderEvent[books]#unique(bookId) book, " +
"OrderEvent[orderdetail.items]#keepall item " +
"where book.bookId = item.productId";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{3L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{3L}});
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{4L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{4L}});
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{5L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{5L}});
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), "count(*)".split(","), new Object[]{8L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{8L}});
}
public void testJoin()
{
String[] fields = "book.bookId,item.itemId,amount".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select book.bookId,item.itemId,amount from " +
"OrderEvent[books]#firstunique(bookId) book, " +
"OrderEvent[orderdetail.items]#keepall item " +
"where book.bookId = item.productId " +
"order by book.bookId, item.itemId";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventTwo());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10022", "B001", 5}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}, {"10022", "B001", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventThree());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), fields, new Object[][]{{"10021", "C001", 50}});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{"10020", "A001", 10}, {"10020", "A003", 30}, {"10021", "A002", 25}, {"10021", "C001", 50}, {"10022", "B001", 5}});
listener.reset();
epService.getEPRuntime().sendEvent(makeEventFour());
assertFalse(listener.isInvoked());
}
public void testAloneCount()
{
String[] fields = "count(*)".split(",");
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "select count(*) from OrderEvent[books]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{3L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{3L}});
epService.getEPRuntime().sendEvent(makeEventFour());
EPAssertionUtil.assertProps(listener.assertOneGetNewAndReset(), fields, new Object[]{5L});
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), fields, new Object[][]{{5L}});
}
public void testPropertyAccess()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
EPStatement stmtOne = epService.getEPAdministrator().createEPL("@IterableUnbound select bookId from OrderEvent[books]");
stmtOne.addListener(listener);
EPStatement stmtTwo = epService.getEPAdministrator().createEPL("@IterableUnbound select books[0].author as val from OrderEvent(books[0].bookId = '10020')");
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}, {"10021"}, {"10022"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmtOne.iterator(), "bookId".split(","), new Object[][]{{"10020"}, {"10021"}, {"10022"}});
EPAssertionUtil.assertPropsPerRow(stmtTwo.iterator(), "val".split(","), new Object[][]{{"Orson Scott Card"}});
epService.getEPRuntime().sendEvent(makeEventFour());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10031"}, {"10032"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmtOne.iterator(), "bookId".split(","), new Object[][]{{"10031"}, {"10032"}});
EPAssertionUtil.assertPropsPerRow(stmtTwo.iterator(), "val".split(","), new Object[][]{{"Orson Scott Card"}});
// add where clause
stmtOne.destroy();
stmtTwo.destroy();
stmtOne = epService.getEPAdministrator().createEPL("select bookId from OrderEvent[books where author='Orson Scott Card']");
stmtOne.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}});
listener.reset();
}
public void testIRStreamArrayItem()
{
epService.getEPAdministrator().getConfiguration().addEventType("OrderEvent", OrderBean.class);
String stmtText = "@IterableUnbound select irstream bookId from OrderEvent[books[0]]";
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(makeEventOne());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10020"}});
assertNull(listener.getLastOldData());
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), "bookId".split(","), new Object[][]{{"10020"}});
epService.getEPRuntime().sendEvent(makeEventFour());
assertNull(listener.getLastOldData());
EPAssertionUtil.assertPropsPerRow(listener.getLastNewData(), "bookId".split(","), new Object[][]{{"10031"}});
listener.reset();
EPAssertionUtil.assertPropsPerRow(stmt.iterator(), "bookId".split(","), new Object[][]{{"10031"}});
}
public void testSplitWords() {
epService.getEPAdministrator().getConfiguration().addEventType(SentenceEvent.class);
String stmtText = "insert into WordStream select * from SentenceEvent[words]";
String[] fields = "word".split(",");
EPStatement stmt = epService.getEPAdministrator().createEPL(stmtText);
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(new SentenceEvent("I am testing this"));
EPAssertionUtil.assertPropsPerRow(listener.getAndResetLastNewData(), fields, new Object[][]{{"I"}, {"am"}, {"testing"}, {"this"}});
}
public void testArrayProperty() {
epService.getEPAdministrator().getConfiguration().addEventType(MyBeanWithArray.class);
epService.getEPAdministrator().createEPL("create objectarray schema ContainedId(id string)");
EPStatement stmt = epService.getEPAdministrator().createEPL("select * from MyBeanWithArray[select topId, * from containedIds @type(ContainedId)]");
stmt.addListener(listener);
epService.getEPRuntime().sendEvent(new MyBeanWithArray("A", "one,two,three".split(",")));
EPAssertionUtil.assertPropsPerRow(listener.getAndResetLastNewData(), "topId,id".split(","),
new Object[][] {{"A", "one"}, {"A", "two"}, {"A", "three"}});
}
public static class MyBeanWithArray {
private final String topId;
private final String[] containedIds;
public MyBeanWithArray(String topId, String[] containedIds) {
this.topId = topId;
this.containedIds = containedIds;
}
public String getTopId() {
return topId;
}
public String[] getContainedIds() {
return containedIds;
}
}
}