Search in sources :

Example 96 with CollectorTestSink

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

the class TopNUniqueTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(TopNUnique oper) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.top.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", 1);
    oper.data.process(input);
    input.clear();
    input.put("b", 6);
    input.put("a", 1001);
    oper.data.process(input);
    input.clear();
    input.put("c", 9);
    input.put("a", 5);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 3, sortSink.collectedTuples.size());
    for (Object o : sortSink.collectedTuples) {
        log.debug(o.toString());
        for (Map.Entry<String, ArrayList<HashMap<Number, Integer>>> e : ((HashMap<String, ArrayList<HashMap<Number, Integer>>>) o).entrySet()) {
            if (e.getKey().equals("a")) {
                Assert.assertEquals("emitted value for 'a' was ", 3, e.getValue().size());
            } else if (e.getKey().equals("b")) {
                Assert.assertEquals("emitted tuple for 'b' was ", 3, e.getValue().size());
            } else if (e.getKey().equals("c")) {
                Assert.assertEquals("emitted tuple for 'c' was ", 1, e.getValue().size());
            }
        }
    }
    log.debug("Done testing round\n");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 97 with CollectorTestSink

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

the class BottomNUniqueMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(BottomNUniqueMap oper) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.bottom.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);
    input.put("a", 5);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 3, sortSink.collectedTuples.size());
    for (Object o : sortSink.collectedTuples) {
        log.debug(o.toString());
        for (Map.Entry<String, ArrayList<HashMap<Number, Integer>>> e : ((HashMap<String, ArrayList<HashMap<Number, Integer>>>) o).entrySet()) {
            if (e.getKey().equals("a")) {
                Assert.assertEquals("emitted value for 'a' was ", 3, e.getValue().size());
            } else if (e.getKey().equals("b")) {
                Assert.assertEquals("emitted tuple for 'b' was ", 3, e.getValue().size());
            } else if (e.getKey().equals("c")) {
                Assert.assertEquals("emitted tuple for 'c' was ", 1, e.getValue().size());
            }
        }
    }
    log.debug("Done testing round\n");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 98 with CollectorTestSink

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

the class InsertSortTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(InsertSort oper, String debug) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.sort.setSink(sortSink);
    ArrayList input = new ArrayList();
    oper.beginWindow(0);
    input.add(2);
    oper.datalist.process(input);
    oper.data.process(20);
    input.clear();
    input.add(1000);
    input.add(5);
    input.add(20);
    input.add(33);
    input.add(33);
    input.add(34);
    oper.datalist.process(input);
    input.clear();
    input.add(34);
    input.add(1001);
    input.add(6);
    input.add(1);
    input.add(33);
    input.add(9);
    oper.datalist.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    input = (ArrayList) sortSink.collectedTuples.get(0);
}
Also used : ArrayList(java.util.ArrayList) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 99 with CollectorTestSink

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

the class AppDataSnapshotServerMapTest method simpleTest.

@Test
public void simpleTest() throws Exception {
    AppDataSnapshotServerMap snapshotServer = new AppDataSnapshotServerMap();
    snapshotServer.setSnapshotSchemaJSON(SIMPLE_SCHEMA);
    // // Input Data
    List<Map<String, Object>> dataList = Lists.newArrayList();
    Map<String, Object> data = Maps.newHashMap();
    data.put("word", "a");
    data.put("count", 2);
    Map<String, Object> data1 = Maps.newHashMap();
    data1.put("word", "b");
    data1.put("count", 3);
    dataList.add(data);
    dataList.add(data1);
    // //
    CollectorTestSink<String> resultSink = new CollectorTestSink<String>();
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectorTestSink<Object> tempResultSink = (CollectorTestSink) resultSink;
    snapshotServer.queryResult.setSink(tempResultSink);
    snapshotServer.setup(null);
    snapshotServer.beginWindow(0L);
    snapshotServer.input.put(dataList);
    snapshotServer.endWindow();
    snapshotServer.beginWindow(1L);
    snapshotServer.query.put(SIMPLE_QUERY);
    snapshotServer.endWindow();
    String result = (String) tempResultSink.collectedTuples.get(0);
    Assert.assertEquals("Should get only 1 result back", 1, tempResultSink.collectedTuples.size());
    Assert.assertEquals("Countdown incorrect", 10, new JSONObject(result).getInt("countdown"));
    Assert.assertEquals("Num results", 2, new JSONObject(result).getJSONArray("data").length());
    JSONObject firstRow = new JSONObject(result).getJSONArray("data").getJSONObject(0);
    JSONObject secondRow = new JSONObject(result).getJSONArray("data").getJSONObject(1);
    Assert.assertEquals(2, firstRow.getInt("count"));
    Assert.assertEquals("a", firstRow.getString("word"));
    Assert.assertEquals(3, secondRow.getInt("count"));
    Assert.assertEquals("b", secondRow.getString("word"));
    // Test serialization
    KryoCloneUtils.cloneObject(snapshotServer);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 100 with CollectorTestSink

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

the class ReduceOperatorTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(ReduceOperator<Text, IntWritable, Text, IntWritable> oper) {
    oper.setReduceClass(WordCount.Reduce.class);
    oper.setConfigFile(null);
    oper.setup(null);
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.output.setSink(sortSink);
    oper.beginWindow(0);
    oper.inputCount.process(new KeyHashValPair<Integer, Integer>(1, 1));
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("one"), new IntWritable(1)));
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("one"), new IntWritable(1)));
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("two"), new IntWritable(1)));
    oper.endWindow();
    oper.beginWindow(1);
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("one"), new IntWritable(1)));
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("two"), new IntWritable(1)));
    oper.input.process(new KeyHashValPair<Text, IntWritable>(new Text("two"), new IntWritable(1)));
    oper.inputCount.process(new KeyHashValPair<Integer, Integer>(1, -1));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 2, sortSink.collectedTuples.size());
    for (Object o : sortSink.collectedTuples) {
        logger.debug(o.toString());
    }
    logger.debug("Done testing round\n");
}
Also used : Text(org.apache.hadoop.io.Text) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) IntWritable(org.apache.hadoop.io.IntWritable)

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