Search in sources :

Example 1 with PortContext

use of com.datatorrent.stram.engine.PortContext in project apex-malhar by apache.

the class POJOInnerJoinOperatorTest method testUpdateStream1Values.

@Test
public void testUpdateStream1Values() throws IOException, InterruptedException {
    POJOInnerJoinOperator oper = new POJOInnerJoinOperator();
    oper.setIncludeFieldStr("ID,Name;OID,Amount");
    oper.setLeftKeyExpression("ID");
    oper.setRightKeyExpression("CID");
    oper.setLeftKeyPrimary(true);
    oper.setExpiryTime(10000L);
    oper.setup(context);
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, CustOrder.class);
    oper.outputPort.setup(new PortContext(attributes, context));
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, Customer.class);
    oper.input1.setup(new PortContext(attributes, context));
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, Order.class);
    oper.input2.setup(new PortContext(attributes, context));
    oper.activate(context);
    CollectorTestSink<CustOrder> sink = new CollectorTestSink<>();
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectorTestSink<Object> tmp = (CollectorTestSink) sink;
    oper.outputPort.setSink(tmp);
    oper.beginWindow(0);
    Customer tuple1 = new Customer(1, "Anil");
    oper.input1.process(tuple1);
    oper.endWindow();
    oper.beginWindow(1);
    Customer tuple2 = new Customer(1, "Join");
    oper.input1.process(tuple2);
    Order order = new Order(102, 1, 300);
    oper.input2.process(order);
    Order order2 = new Order(103, 3, 300);
    oper.input2.process(order2);
    oper.endWindow();
    /* Number of tuple, emitted */
    Assert.assertEquals("Number of tuple emitted ", 1, sink.collectedTuples.size());
    CustOrder emitted = sink.collectedTuples.get(0);
    Assert.assertEquals("value of ID :", tuple2.ID, emitted.ID);
    Assert.assertEquals("value of Name :", tuple2.Name, emitted.Name);
    Assert.assertEquals("value of OID: ", order.OID, emitted.OID);
    Assert.assertEquals("value of Amount: ", order.Amount, emitted.Amount);
    oper.teardown();
}
Also used : CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) PortContext(com.datatorrent.stram.engine.PortContext) Test(org.junit.Test)

Example 2 with PortContext

use of com.datatorrent.stram.engine.PortContext in project apex-malhar by apache.

the class DeduperTimeBasedPOJOImplTest method testDedup.

@Test
public void testDedup() {
    com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributes = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
    attributes.put(DAG.APPLICATION_ID, APP_ID);
    attributes.put(DAG.APPLICATION_PATH, applicationPath);
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, TestPojo.class);
    OperatorContext context = mockOperatorContext(OPERATOR_ID, attributes);
    deduper.setup(context);
    deduper.input.setup(new PortContext(attributes, context));
    deduper.activate(context);
    CollectorTestSink<TestPojo> uniqueSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.unique, uniqueSink);
    CollectorTestSink<TestPojo> duplicateSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.duplicate, duplicateSink);
    CollectorTestSink<TestPojo> expiredSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.expired, expiredSink);
    deduper.beginWindow(0);
    long millis = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        TestPojo pojo = new TestPojo(i, new Date(millis + i));
        deduper.input.process(pojo);
    }
    TestPojo expiredPojo = new TestPojo(100, new Date(millis - 1000 * 60));
    deduper.input.process(expiredPojo);
    for (int i = 90; i < 200; i++) {
        TestPojo pojo = new TestPojo(i, new Date(millis + i));
        deduper.input.process(pojo);
    }
    deduper.handleIdleTime();
    deduper.endWindow();
    Assert.assertTrue(uniqueSink.collectedTuples.size() == 200);
    Assert.assertTrue(duplicateSink.collectedTuples.size() == 10);
    Assert.assertTrue(expiredSink.collectedTuples.size() == 1);
    deduper.teardown();
}
Also used : Date(java.util.Date) PortContext(com.datatorrent.stram.engine.PortContext) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 3 with PortContext

use of com.datatorrent.stram.engine.PortContext in project apex-malhar by apache.

the class DeduperTimeBasedPOJOImplTest method testDedupDifferentWindowSameKey.

@Test
public void testDedupDifferentWindowSameKey() {
    com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap attributes = new com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap();
    attributes.put(DAG.APPLICATION_ID, APP_ID);
    attributes.put(DAG.APPLICATION_PATH, applicationPath);
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, TestPojo.class);
    OperatorContext context = mockOperatorContext(OPERATOR_ID, attributes);
    deduper.setup(context);
    deduper.input.setup(new PortContext(attributes, context));
    deduper.activate(context);
    CollectorTestSink<TestPojo> uniqueSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.unique, uniqueSink);
    CollectorTestSink<TestPojo> duplicateSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.duplicate, duplicateSink);
    CollectorTestSink<TestPojo> expiredSink = new CollectorTestSink<TestPojo>();
    TestUtils.setSink(deduper.expired, expiredSink);
    deduper.beginWindow(0);
    long millis = System.currentTimeMillis();
    deduper.input.process(new TestPojo(10, new Date(millis)));
    deduper.input.process(new TestPojo(11, new Date(millis + 10000)));
    deduper.input.process(new TestPojo(12, new Date(millis + 20000)));
    deduper.input.process(new TestPojo(13, new Date(millis + 30000)));
    deduper.input.process(new TestPojo(14, new Date(millis + 40000)));
    deduper.input.process(new TestPojo(15, new Date(millis + 50000)));
    // Duplicate
    deduper.input.process(new TestPojo(10, new Date(millis)));
    deduper.input.process(new TestPojo(16, new Date(millis + 60000)));
    // New tuple with same key but outside expired window.
    deduper.input.process(new TestPojo(10, new Date(millis + 70000)));
    // Earlier tuple with earlier time -- Expired
    deduper.input.process(new TestPojo(10, new Date(millis)));
    // New tuple repeated again - Duplicate
    deduper.input.process(new TestPojo(10, new Date(millis + 70000)));
    deduper.handleIdleTime();
    deduper.endWindow();
    Assert.assertTrue(uniqueSink.collectedTuples.size() == 8);
    Assert.assertTrue(duplicateSink.collectedTuples.size() == 2);
    Assert.assertTrue(expiredSink.collectedTuples.size() == 1);
    deduper.teardown();
}
Also used : Date(java.util.Date) PortContext(com.datatorrent.stram.engine.PortContext) OperatorContextTestHelper.mockOperatorContext(org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext) OperatorContext(com.datatorrent.api.Context.OperatorContext) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 4 with PortContext

use of com.datatorrent.stram.engine.PortContext in project apex-malhar by apache.

the class FilterTest method prepareFilterOperator.

public void prepareFilterOperator(Class<?> inClass, String condition) {
    filter.truePort.setSink(trueSink);
    filter.falsePort.setSink(falseSink);
    filter.error.setSink(errorSink);
    filter.setup(null);
    Attribute.AttributeMap in = new Attribute.AttributeMap.DefaultAttributeMap();
    in.put(Context.PortContext.TUPLE_CLASS, inClass);
    filter.input.setup(new PortContext(in, null));
    filter.setCondition(condition);
    filter.activate(null);
}
Also used : Attribute(com.datatorrent.api.Attribute) PortContext(com.datatorrent.stram.engine.PortContext)

Example 5 with PortContext

use of com.datatorrent.stram.engine.PortContext in project apex-malhar by apache.

the class POJOInnerJoinOperatorTest method testMultipleValues.

@Test
public void testMultipleValues() throws IOException, InterruptedException {
    POJOInnerJoinOperator oper = new POJOInnerJoinOperator();
    oper.setIncludeFieldStr("ID,Name;OID,Amount");
    oper.setLeftKeyExpression("ID");
    oper.setRightKeyExpression("CID");
    oper.setExpiryTime(10000L);
    oper.setup(context);
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, CustOrder.class);
    oper.outputPort.setup(new PortContext(attributes, context));
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, Customer.class);
    oper.input1.setup(new PortContext(attributes, context));
    attributes.put(DAG.InputPortMeta.TUPLE_CLASS, Order.class);
    oper.input2.setup(new PortContext(attributes, context));
    oper.activate(context);
    CollectorTestSink<CustOrder> sink = new CollectorTestSink<>();
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectorTestSink<Object> tmp = (CollectorTestSink) sink;
    oper.outputPort.setSink(tmp);
    oper.beginWindow(0);
    Order order = new Order(102, 1, 300);
    oper.input2.process(order);
    Order order2 = new Order(103, 3, 300);
    oper.input2.process(order2);
    oper.endWindow();
    oper.beginWindow(1);
    Order order3 = new Order(104, 1, 300);
    oper.input2.process(order3);
    Customer tuple = new Customer(1, "Anil");
    oper.input1.process(tuple);
    oper.endWindow();
    /* Number of tuple, emitted */
    Assert.assertEquals("Number of tuple emitted ", 2, sink.collectedTuples.size());
    CustOrder emitted = sink.collectedTuples.get(0);
    Assert.assertEquals("value of ID :", tuple.ID, emitted.ID);
    Assert.assertEquals("value of Name :", tuple.Name, emitted.Name);
    Assert.assertEquals("value of OID: ", order.OID, emitted.OID);
    Assert.assertEquals("value of Amount: ", order.Amount, emitted.Amount);
    emitted = sink.collectedTuples.get(1);
    Assert.assertEquals("value of ID :", tuple.ID, emitted.ID);
    Assert.assertEquals("value of Name :", tuple.Name, emitted.Name);
    Assert.assertEquals("value of OID: ", order3.OID, emitted.OID);
    Assert.assertEquals("value of Amount: ", order3.Amount, emitted.Amount);
    oper.teardown();
}
Also used : CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) PortContext(com.datatorrent.stram.engine.PortContext) Test(org.junit.Test)

Aggregations

PortContext (com.datatorrent.stram.engine.PortContext)9 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)7 Test (org.junit.Test)7 OperatorContext (com.datatorrent.api.Context.OperatorContext)3 Date (java.util.Date)3 OperatorContextTestHelper.mockOperatorContext (org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext)3 Attribute (com.datatorrent.api.Attribute)1 Context (com.datatorrent.api.Context)1 BaseContext (com.datatorrent.stram.api.BaseContext)1 Field (java.lang.reflect.Field)1 Random (java.util.Random)1