public class TestTop extends TestCase {
@Test
public void testTop() throws Exception {
Top top = new Top();
Tuple inputTuple = DefaultTupleFactory.getInstance().newTuple(3);
// set N = 10 i.e retain top 10 tuples
inputTuple.set(0, 10);
// compare tuples by field number 1
inputTuple.set(1, 1);
// set the data bag containing the tuples
DataBag dBag = DefaultBagFactory.getInstance().newDefaultBag();
inputTuple.set(2, dBag);
// generate tuples of the form (group-1, 1), (group-2, 2) ...
for (long i = 0; i < 100; i++) {
Tuple nestedTuple = DefaultTupleFactory.getInstance().newTuple(2);
nestedTuple.set(0, "group-" + i);
nestedTuple.set(1, i);
dBag.add(nestedTuple);
}
DataBag outBag = top.exec(inputTuple);
super.assertEquals(outBag.size(), 10L);
Iterator<Tuple> itr = outBag.iterator();
while (itr.hasNext()) {
Tuple next = itr.next();
Long value = (Long) next.get(1);