Search in sources :

Example 51 with CollectorTestSink

use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.

the class RightOuterJoinOperatorTest method testSqlSelect.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSqlSelect() {
    // create operator
    OuterJoinOperator oper = new OuterJoinOperator();
    oper.setRighttJoin();
    CollectorTestSink sink = new CollectorTestSink();
    oper.outport.setSink(sink);
    // set column join condition
    Condition cond = new JoinColumnEqualCondition("a", "a");
    oper.setJoinCondition(cond);
    // add columns
    oper.selectTable1Column(new ColumnIndex("b", null));
    oper.selectTable2Column(new ColumnIndex("c", null));
    oper.setup(null);
    oper.beginWindow(1);
    HashMap<String, Object> tuple = new HashMap<String, Object>();
    tuple.put("a", 0);
    tuple.put("b", 1);
    tuple.put("c", 2);
    oper.inport1.process(tuple);
    tuple = new HashMap<String, Object>();
    tuple.put("a", 1);
    tuple.put("b", 3);
    tuple.put("c", 4);
    oper.inport1.process(tuple);
    tuple = new HashMap<String, Object>();
    tuple.put("a", 0);
    tuple.put("b", 7);
    tuple.put("c", 8);
    oper.inport2.process(tuple);
    tuple = new HashMap<String, Object>();
    tuple.put("a", 1);
    tuple.put("b", 5);
    tuple.put("c", 6);
    oper.inport2.process(tuple);
    tuple = new HashMap<String, Object>();
    tuple.put("a", 2);
    tuple.put("b", 11);
    tuple.put("c", 12);
    oper.inport2.process(tuple);
    oper.endWindow();
    oper.teardown();
    LOG.debug("{}", sink.collectedTuples);
}
Also used : Condition(org.apache.apex.malhar.lib.streamquery.condition.Condition) JoinColumnEqualCondition(org.apache.apex.malhar.lib.streamquery.condition.JoinColumnEqualCondition) ColumnIndex(org.apache.apex.malhar.lib.streamquery.index.ColumnIndex) HashMap(java.util.HashMap) JoinColumnEqualCondition(org.apache.apex.malhar.lib.streamquery.condition.JoinColumnEqualCondition) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 52 with CollectorTestSink

use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.

the class AbstractMemsqlInputOperatorTest method TestMemsqlPOJOInputOperator.

/*
   * This test can be run against memsql installation on node17.
   */
@Test
public void TestMemsqlPOJOInputOperator() throws SQLException {
    cleanDatabase();
    populateDatabase(createStore(null, true));
    Attribute.AttributeMap attributeMap = new Attribute.AttributeMap.DefaultAttributeMap();
    attributeMap.put(Context.OperatorContext.SPIN_MILLIS, 500);
    OperatorContext context = mockOperatorContext(OPERATOR_ID, attributeMap);
    MemsqlPOJOInputOperator inputOperator = new MemsqlPOJOInputOperator();
    createStore((MemsqlStore) inputOperator.getStore(), true);
    inputOperator.setBatchSize(10);
    inputOperator.setTablename(FQ_TABLE);
    inputOperator.setPrimaryKeyColumn(INDEX_COLUMN);
    ArrayList<String> expressions = new ArrayList<String>();
    expressions.add("id");
    expressions.add("name");
    inputOperator.setExpressions(expressions);
    ArrayList<String> columns = new ArrayList<String>();
    columns.add("data_index");
    columns.add("data2");
    inputOperator.setColumns(columns);
    inputOperator.setQuery("select * from " + FQ_TABLE + ";");
    inputOperator.setOutputClass("org.apache.apex.malhar.contrib.memsql.TestInputPojo");
    CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
    inputOperator.outputPort.setSink(sink);
    inputOperator.setup(context);
    inputOperator.beginWindow(0);
    inputOperator.emitTuples();
    inputOperator.endWindow();
    Assert.assertEquals("rows from db", 100, sink.collectedTuples.size());
    for (int i = 0; i < 10; i++) {
        TestInputPojo object = (TestInputPojo) sink.collectedTuples.get(i);
        Assert.assertEquals("id set in testpojo", i, object.getId());
        Assert.assertEquals("name set in testpojo", "Testname" + i, object.getName());
    }
    sink.clear();
    inputOperator.setQuery("select * from " + FQ_TABLE + " where " + "%p " + ">= " + "%s" + ";");
    inputOperator.setStartRow(10);
    inputOperator.setup(context);
    inputOperator.beginWindow(0);
    inputOperator.emitTuples();
    inputOperator.endWindow();
    Assert.assertEquals("rows from db", 90, sink.collectedTuples.size());
    sink.clear();
    inputOperator.setQuery("select * from " + FQ_TABLE + " where " + "%p " + ">= " + "%s" + " LIMIT " + "%l" + ";");
    inputOperator.setStartRow(1);
    inputOperator.setBatchSize(10);
    inputOperator.setup(context);
    inputOperator.beginWindow(0);
    inputOperator.emitTuples();
    inputOperator.endWindow();
    Assert.assertEquals("rows from db", 10, sink.collectedTuples.size());
}
Also used : Attribute(com.datatorrent.api.Attribute) ArrayList(java.util.ArrayList) 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 53 with CollectorTestSink

use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.

the class FilterKeysMapTest method testNodeProcessing.

/**
 * Test node logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    FilterKeysMap<String, Number> oper = new FilterKeysMap<String, Number>();
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.filter.setSink(sortSink);
    oper.setKey("b");
    oper.clearKeys();
    String[] keys = new String[3];
    keys[0] = "e";
    keys[1] = "f";
    keys[2] = "blah";
    oper.setKey("a");
    oper.setKeys(keys);
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 2);
    input.put("b", 5);
    input.put("c", 7);
    input.put("d", 42);
    input.put("e", 200);
    input.put("f", 2);
    oper.data.process(input);
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    Assert.assertEquals("Total filtered value is ", 204, getTotal(sortSink.collectedTuples.get(0)));
    sortSink.clear();
    input.clear();
    input.put("a", 5);
    oper.data.process(input);
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    Assert.assertEquals("Total filtered value is ", 5, getTotal(sortSink.collectedTuples.get(0)));
    sortSink.clear();
    input.clear();
    input.put("a", 2);
    input.put("b", 33);
    input.put("f", 2);
    oper.data.process(input);
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    Assert.assertEquals("Total filtered value is ", 4, getTotal(sortSink.collectedTuples.get(0)));
    sortSink.clear();
    input.clear();
    input.put("b", 6);
    input.put("a", 2);
    input.put("j", 6);
    input.put("e", 2);
    input.put("dd", 6);
    input.put("blah", 2);
    input.put("another", 6);
    input.put("notmakingit", 2);
    oper.data.process(input);
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    Assert.assertEquals("Total filtered value is ", 6, getTotal(sortSink.collectedTuples.get(0)));
    sortSink.clear();
    input.clear();
    input.put("c", 9);
    oper.setInverse(true);
    oper.data.process(input);
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    Assert.assertEquals("Total filtered value is ", 9, getTotal(sortSink.collectedTuples.get(0)));
    oper.endWindow();
}
Also used : HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 54 with CollectorTestSink

use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.

the class FirstNTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(FirstN oper) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.first.setSink(sortSink);
    oper.setN(3);
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 2);
    oper.data.process(input);
    input.clear();
    input.put("a", 20);
    oper.data.process(input);
    input.clear();
    input.put("a", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 5);
    oper.data.process(input);
    input.clear();
    input.put("a", 20);
    input.put("b", 33);
    oper.data.process(input);
    input.clear();
    input.put("a", 33);
    input.put("b", 34);
    oper.data.process(input);
    input.clear();
    input.put("b", 34);
    input.put("a", 1001);
    oper.data.process(input);
    input.clear();
    input.put("b", 6);
    input.put("a", 1);
    oper.data.process(input);
    input.clear();
    input.put("c", 9);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 7, sortSink.collectedTuples.size());
    int aval = 0;
    int bval = 0;
    int cval = 0;
    for (Object o : sortSink.collectedTuples) {
        for (Map.Entry<String, Number> e : ((HashMap<String, Number>) o).entrySet()) {
            if (e.getKey().equals("a")) {
                aval += e.getValue().intValue();
            } else if (e.getKey().equals("b")) {
                bval += e.getValue().intValue();
            } else if (e.getKey().equals("c")) {
                cval += e.getValue().intValue();
            }
        }
    }
    Assert.assertEquals("Value of \"a\" was ", 1022, aval);
    Assert.assertEquals("Value of \"a\" was ", 101, bval);
    Assert.assertEquals("Value of \"a\" was ", 9, cval);
    log.debug("Done testing round\n");
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 55 with CollectorTestSink

use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.

the class InvertIndexArrayTest method testNodeProcessing.

/**
 * Test oper logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    InvertIndexArray<String, String> oper = new InvertIndexArray<String, String>();
    CollectorTestSink indexSink = new CollectorTestSink();
    Sink inSink = oper.data.getSink();
    oper.index.setSink(indexSink);
    oper.beginWindow(0);
    HashMap<String, ArrayList> input = new HashMap<String, ArrayList>();
    ArrayList<String> alist = new ArrayList<String>();
    alist.add("str");
    alist.add("str1");
    input.put("a", alist);
    input.put("b", alist);
    inSink.put(input);
    alist = new ArrayList<String>();
    input = new HashMap<String, ArrayList>();
    alist.add("blah");
    alist.add("str1");
    input.put("c", alist);
    inSink.put(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 3, indexSink.collectedTuples.size());
    for (Object o : indexSink.collectedTuples) {
        log.debug(o.toString());
        HashMap<String, ArrayList<String>> output = (HashMap<String, ArrayList<String>>) o;
        for (Map.Entry<String, ArrayList<String>> e : output.entrySet()) {
            String key = e.getKey();
            alist = e.getValue();
            if (key.equals("str1")) {
                Assert.assertEquals("Index for \"str1\" contains \"a\"", true, alist.contains("a"));
                Assert.assertEquals("Index for \"str1\" contains \"b\"", true, alist.contains("b"));
                Assert.assertEquals("Index for \"str1\" contains \"c\"", true, alist.contains("c"));
            } else if (key.equals("str")) {
                Assert.assertEquals("Index for \"str1\" contains \"a\"", true, alist.contains("a"));
                Assert.assertEquals("Index for \"str1\" contains \"b\"", true, alist.contains("b"));
                Assert.assertEquals("Index for \"str1\" contains \"c\"", false, alist.contains("c"));
            } else if (key.equals("blah")) {
                Assert.assertEquals("Index for \"str1\" contains \"a\"", false, alist.contains("a"));
                Assert.assertEquals("Index for \"str1\" contains \"b\"", false, alist.contains("b"));
                Assert.assertEquals("Index for \"str1\" contains \"c\"", true, alist.contains("c"));
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Sink(com.datatorrent.api.Sink) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Aggregations

CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)162 Test (org.junit.Test)133 HashMap (java.util.HashMap)56 Map (java.util.Map)33 File (java.io.File)21 ArrayList (java.util.ArrayList)21 OperatorContext (com.datatorrent.api.Context.OperatorContext)19 OperatorContextTestHelper.mockOperatorContext (org.apache.apex.malhar.lib.helper.OperatorContextTestHelper.mockOperatorContext)18 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)15 Path (org.apache.hadoop.fs.Path)15 Attribute (com.datatorrent.api.Attribute)14 ColumnIndex (org.apache.apex.malhar.lib.streamquery.index.ColumnIndex)13 LineByLineFileInputOperator (org.apache.apex.malhar.lib.fs.LineByLineFileInputOperator)12 Kryo (com.esotericsoftware.kryo.Kryo)10 Date (java.util.Date)8 TestPortContext (org.apache.apex.malhar.lib.helper.TestPortContext)8 PortContext (com.datatorrent.stram.engine.PortContext)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 SelectOperator (org.apache.apex.malhar.contrib.misc.streamquery.SelectOperator)6