Search in sources :

Example 11 with CountAndLastTupleTestSink

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

the class LeastFrequentKeyTest method testNodeProcessing.

/**
 * Test node logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    LeastFrequentValue<String> oper = new LeastFrequentValue<String>();
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    CountAndLastTupleTestSink listSink = new CountAndLastTupleTestSink();
    oper.least.setSink(matchSink);
    oper.list.setSink(listSink);
    oper.beginWindow(0);
    int atot = 5;
    int btot = 3;
    int ctot = 6;
    for (int i = 0; i < atot; i++) {
        oper.data.process("a");
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process("b");
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process("c");
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    HashMap<String, Integer> tuple = (HashMap<String, Integer>) matchSink.tuple;
    Integer val = tuple.get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    ArrayList<HashMap<String, Integer>> list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    val = list.get(0).get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    matchSink.clear();
    listSink.clear();
    oper.beginWindow(0);
    atot = 5;
    btot = 10;
    ctot = 5;
    for (int i = 0; i < atot; i++) {
        oper.data.process("a");
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process("b");
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process("c");
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    int acount = 0;
    int ccount = 0;
    for (HashMap<String, Integer> h : list) {
        val = h.get("a");
        if (val == null) {
            ccount = h.get("c");
        } else {
            acount = val;
        }
    }
    Assert.assertEquals("Count of a was ", atot, acount);
    Assert.assertEquals("Count of c was ", ctot, ccount);
    HashMap<String, Integer> mtuple = (HashMap<String, Integer>) matchSink.tuple;
    val = mtuple.get("a");
    if (val == null) {
        val = mtuple.get("c");
    }
    Assert.assertEquals("Count of least frequent key was ", ctot, val.intValue());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Test(org.junit.Test)

Example 12 with CountAndLastTupleTestSink

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

the class MostFrequentKeyTest method testNodeProcessing.

/**
 * Test node logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    MostFrequentValue<String> oper = new MostFrequentValue<String>();
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    CountAndLastTupleTestSink listSink = new CountAndLastTupleTestSink();
    oper.most.setSink(matchSink);
    oper.list.setSink(listSink);
    oper.beginWindow(0);
    int atot = 5;
    int btot = 7;
    int ctot = 6;
    for (int i = 0; i < atot; i++) {
        oper.data.process("a");
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process("b");
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process("c");
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    HashMap<String, Integer> tuple = (HashMap<String, Integer>) matchSink.tuple;
    Integer val = tuple.get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    ArrayList<HashMap<String, Integer>> list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    val = list.get(0).get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    matchSink.clear();
    listSink.clear();
    oper.beginWindow(0);
    atot = 5;
    btot = 4;
    ctot = 5;
    for (int i = 0; i < atot; i++) {
        oper.data.process("a");
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process("b");
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process("c");
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    int acount = 0;
    int ccount = 0;
    for (HashMap<String, Integer> h : list) {
        val = h.get("a");
        if (val == null) {
            ccount = h.get("c");
        } else {
            acount = val;
        }
    }
    Assert.assertEquals("Count of a was ", atot, acount);
    Assert.assertEquals("Count of c was ", ctot, ccount);
    HashMap<String, Integer> mtuple = (HashMap<String, Integer>) matchSink.tuple;
    val = mtuple.get("a");
    if (val == null) {
        val = mtuple.get("c");
    }
    Assert.assertEquals("Count of least frequent key was ", ctot, val.intValue());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Test(org.junit.Test)

Example 13 with CountAndLastTupleTestSink

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

the class MatchAllMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(MatchAllMap oper) {
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    oper.all.setSink(matchSink);
    oper.setKey("a");
    oper.setValue(3.0);
    oper.setTypeEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 3);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 3);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    Boolean result = (Boolean) matchSink.tuple;
    Assert.assertEquals("result was false", true, result);
    matchSink.clear();
    oper.beginWindow(0);
    input.put("a", 2);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 3);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    result = (Boolean) matchSink.tuple;
    Assert.assertEquals("result was false", false, result);
    matchSink.clear();
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink)

Example 14 with CountAndLastTupleTestSink

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

the class FirstMatchMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(FirstMatchMap oper) {
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    oper.first.setSink(matchSink);
    oper.setKey("a");
    oper.setValue(3);
    oper.setTypeEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 4);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.put("a", 3);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 2);
    oper.data.process(input);
    input.clear();
    input.put("a", 4);
    input.put("b", 21);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 4);
    input.put("b", 20);
    input.put("c", 5);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    HashMap<String, Number> tuple = (HashMap<String, Number>) matchSink.tuple;
    Number aval = tuple.get("a");
    Assert.assertEquals("Value of a was ", 3, aval.intValue());
    matchSink.clear();
    oper.beginWindow(0);
    input.clear();
    input.put("a", 2);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 5);
    oper.data.process(input);
    oper.endWindow();
    // There should be no emit as all tuples do not match
    Assert.assertEquals("number emitted tuples", 0, matchSink.count);
    matchSink.clear();
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink)

Example 15 with CountAndLastTupleTestSink

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

the class LastMatchMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(LastMatchMap oper) {
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    oper.last.setSink(matchSink);
    oper.setKey("a");
    oper.setValue(3);
    oper.setTypeEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 4);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.put("a", 3);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 2);
    oper.data.process(input);
    input.clear();
    input.put("a", 4);
    input.put("b", 21);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 3);
    input.put("b", 52);
    input.put("c", 5);
    oper.data.process(input);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    HashMap<String, Number> tuple = (HashMap<String, Number>) matchSink.tuple;
    Number aval = tuple.get("a");
    Number bval = tuple.get("b");
    Assert.assertEquals("Value of a was ", 3, aval.intValue());
    Assert.assertEquals("Value of a was ", 52, bval.intValue());
    matchSink.clear();
    oper.beginWindow(0);
    input.clear();
    input.put("a", 2);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 5);
    oper.data.process(input);
    oper.endWindow();
    // There should be no emit as all tuples do not match
    Assert.assertEquals("number emitted tuples", 0, matchSink.count);
    matchSink.clear();
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink)

Aggregations

CountAndLastTupleTestSink (org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink)20 HashMap (java.util.HashMap)16 Map (java.util.Map)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)2 MatchMap (org.apache.apex.malhar.lib.algo.MatchMap)1