use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.
the class TailFsInputOperatorTest method testDelimiter.
@Test
public void testDelimiter() throws Exception {
FileWriter fstream = new FileWriter(filePath);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
TailFsInputOperator oper = new TailFsInputOperator();
oper.setFilePath(filePath);
oper.setDelimiter('|');
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
oper.output.setSink(sink);
oper.setDelay(1);
oper.setNumberOfTuples(10);
oper.setup(null);
oper.activate(null);
oper.beginWindow(0);
oper.emitTuples();
oper.endWindow();
fstream = new FileWriter(filePath, true);
out = new BufferedWriter(fstream);
out.write("Hello Java|");
out.close();
oper.beginWindow(1);
oper.emitTuples();
oper.endWindow();
oper.deactivate();
Assert.assertEquals(1, sink.collectedTuples.size());
Assert.assertEquals("Hello JavaHello Java", sink.collectedTuples.get(0));
out.close();
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
}
use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.
the class TailFsInputOperatorTest method testTruncationWithSameFileSize.
/**
* This tests the case when the file is rotated and new file has same size as old file
*
* @throws Exception
*/
@Test
public void testTruncationWithSameFileSize() throws Exception {
FileWriter fstream = new FileWriter(filePath);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java\n");
out.close();
TailFsInputOperator oper = new TailFsInputOperator();
oper.setFilePath(filePath);
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
oper.output.setSink(sink);
oper.setDelay(1);
oper.setEnd(true);
oper.setNumberOfTuples(10);
oper.setup(null);
oper.activate(null);
File file = new File(filePath);
if (file.exists()) {
file.renameTo(new File(filePath + ".bk"));
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// fixme
}
fstream = new FileWriter(filePath);
out = new BufferedWriter(fstream);
out.write("Hello abcd\n");
out.close();
oper.beginWindow(0);
oper.emitTuples();
oper.endWindow();
oper.deactivate();
file = new File(filePath);
if (file.exists()) {
file.delete();
}
file = new File(filePath + ".bk");
if (file.exists()) {
file.delete();
}
Assert.assertEquals(1, sink.collectedTuples.size());
Assert.assertEquals("Hello abcd", sink.collectedTuples.get(0));
}
use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.
the class SumTest method testNodeSchemaProcessing.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testNodeSchemaProcessing(Sum oper) {
CollectorTestSink sumSink = new CollectorTestSink();
oper.sum.setSink(sumSink);
//
oper.beginWindow(0);
Double a = 2.0;
Double b = 20.0;
Double c = 1000.0;
oper.data.process(a);
oper.data.process(b);
oper.data.process(c);
a = 1.0;
oper.data.process(a);
a = 10.0;
oper.data.process(a);
b = 5.0;
oper.data.process(b);
b = 12.0;
oper.data.process(b);
c = 22.0;
oper.data.process(c);
c = 14.0;
oper.data.process(c);
a = 46.0;
oper.data.process(a);
b = 2.0;
oper.data.process(b);
a = 23.0;
oper.data.process(a);
//
oper.endWindow();
// payload should be 1 bag of tuples with keys "a", "b", "c", "d", "e"
Assert.assertEquals("number emitted tuples", 1, sumSink.collectedTuples.size());
for (Object o : sumSink.collectedTuples) {
// sum is 1157
Double val = ((Number) o).doubleValue();
Assert.assertEquals("emitted sum value was was ", new Double(1157.0), val);
}
}
use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.
the class BottomNMapTest method testNodeProcessingSchema.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testNodeProcessingSchema(BottomNMap 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);
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<Number>> e : ((HashMap<String, ArrayList<Number>>) 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");
}
use of org.apache.apex.malhar.lib.testbench.CollectorTestSink in project apex-malhar by apache.
the class MergeSortNumberTest method testNodeProcessing.
/**
* Test node logic emits correct results
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
MergeSortNumber<Integer> oper = new MergeSortNumber<Integer>();
CollectorTestSink sink = new CollectorTestSink();
oper.sort.setSink(sink);
oper.setup(null);
oper.beginWindow(1);
Random rand = new Random();
ArrayList<Integer> tuple = new ArrayList<Integer>();
tuple.add(rand.nextInt(50));
tuple.add(50 + rand.nextInt(50));
oper.process(tuple);
tuple = new ArrayList<Integer>();
tuple.add(rand.nextInt(50));
tuple.add(50 + rand.nextInt(50));
oper.process(tuple);
oper.endWindow();
oper.teardown();
assertTrue("Tuples in sink", sink.collectedTuples.size() == 1);
Iterator iter = sink.collectedTuples.iterator();
if (!iter.hasNext()) {
return;
}
tuple = (ArrayList<Integer>) iter.next();
assertTrue("Tuple size 4", tuple.size() == 4);
Integer val = tuple.get(0);
for (int i = 1; i < 4; i++) {
assertTrue("Values must be sorted " + tuple, val <= tuple.get(i));
val = tuple.get(i);
}
}
Aggregations