Search in sources :

Example 6 with CountAndLastTupleTestSink

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

the class QuotientMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testNodeProcessingSchema(QuotientMap oper) throws Exception {
    CountAndLastTupleTestSink quotientSink = new CountAndLastTupleTestSink();
    oper.quotient.setSink(quotientSink);
    oper.setMult_by(2);
    // 
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    int numtuples = 100;
    for (int i = 0; i < numtuples; i++) {
        input.clear();
        input.put("a", 2);
        input.put("b", 20);
        input.put("c", 1000);
        oper.numerator.process(input);
        input.clear();
        input.put("a", 2);
        input.put("b", 40);
        input.put("c", 500);
        oper.denominator.process(input);
    }
    oper.endWindow();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, quotientSink.count);
    HashMap<String, Number> output = (HashMap<String, Number>) quotientSink.tuple;
    for (Map.Entry<String, Number> e : output.entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", 2d, e.getValue());
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", 1d, e.getValue());
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", 4d, e.getValue());
        } else {
            LOG.debug(String.format("key was %s", e.getKey()));
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with CountAndLastTupleTestSink

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

the class LeastFrequentKeyMapTest method testNodeProcessing.

/**
 * Test node logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    LeastFrequentKeyMap<String, Integer> oper = new LeastFrequentKeyMap<String, Integer>();
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    CountAndLastTupleTestSink listSink = new CountAndLastTupleTestSink();
    oper.least.setSink(matchSink);
    oper.list.setSink(listSink);
    oper.beginWindow(0);
    HashMap<String, Integer> amap = new HashMap<String, Integer>(1);
    HashMap<String, Integer> bmap = new HashMap<String, Integer>(1);
    HashMap<String, Integer> cmap = new HashMap<String, Integer>(1);
    int atot = 5;
    int btot = 3;
    int ctot = 6;
    amap.put("a", null);
    bmap.put("b", null);
    cmap.put("c", null);
    for (int i = 0; i < atot; i++) {
        oper.data.process(amap);
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process(bmap);
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process(cmap);
    }
    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(amap);
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process(bmap);
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process(cmap);
    }
    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 8 with CountAndLastTupleTestSink

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

the class MatchMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(MatchMap oper) {
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    oper.match.setSink(matchSink);
    oper.setKey("a");
    oper.setValue(3.0);
    oper.setTypeNEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    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();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    for (Map.Entry<String, Number> e : ((HashMap<String, Number>) matchSink.tuple).entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(2), new Double(e.getValue().doubleValue()));
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(20), new Double(e.getValue().doubleValue()));
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(1000), new Double(e.getValue().doubleValue()));
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) MatchMap(org.apache.apex.malhar.lib.algo.MatchMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with CountAndLastTupleTestSink

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

the class CompareMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(CompareMap oper) {
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    oper.compare.setSink(matchSink);
    oper.setKey("a");
    oper.setValue(3.0);
    oper.setTypeNEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    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();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    for (Map.Entry<String, Number> e : ((HashMap<String, Number>) matchSink.tuple).entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(2), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(20), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(1000), e.getValue().doubleValue(), 0);
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with CountAndLastTupleTestSink

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

the class ExceptMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(ExceptMap oper) {
    CountAndLastTupleTestSink exceptSink = new CountAndLastTupleTestSink();
    oper.except.setSink(exceptSink);
    oper.setKey("a");
    oper.setValue(3.0);
    oper.setTypeEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    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();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, exceptSink.count);
    for (Map.Entry<String, Number> e : ((HashMap<String, Number>) exceptSink.tuple).entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(2), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(20), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(1000), e.getValue().doubleValue(), 0);
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Map(java.util.Map) HashMap(java.util.HashMap)

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