public class TodoMapperReducerTest {
final static String[] RECORD = {
"{\"title\":\"string\",\"description\":\"string\",\"done\":false,\"dueDate\":{\"start\":\"2021-05-07\"," +
"\"due\":\"2021-05-07\"},\"id\":0}",
"{\"title\":\"string\",\"description\":\"string\",\"done\":false,\"dueDate\":{\"start\":\"2021-05-07\"," +
"\"due\":\"2021-05-07\"},\"id\":1}"
};
MapDriver<LongWritable, Text, Text, IntWritable> mapDriver;
ReduceDriver<Text, IntWritable, Text, IntArrayWritable> reduceDriver;
MapReduceDriver<LongWritable, Text, Text, IntWritable, Text, IntArrayWritable> mapReduceDriver;
@Before
public void setUp() {
TodoMapper mapper = new TodoMapper();
TodoReducer reducer = new TodoReducer();
mapDriver = MapDriver.newMapDriver(mapper); (1)
reduceDriver = ReduceDriver.newReduceDriver(reducer);
mapReduceDriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);
}
@Test
public void shouldVerifyMapper() throws IOException {
mapDriver.withInput(new LongWritable(), new Text(RECORD[0])); (2)
mapDriver.withOutput(new Text("2021-05-07"), new IntWritable(0));
mapDriver.runTest();
}
@Test
public void shouldVerifyReducer() throws IOException {
reduceDriver.withInput(new Text("2021-05-07"), Arrays.asList( (3)
new IntWritable(0), new IntWritable(1)
)
);
reduceDriver.withOutput(new Text("2021-05-07"),
new IntArrayWritable(new Integer[] { 0, 1 }));
reduceDriver.runTest();
}
@Test
public void shouldVerfiyMapAndReduce() throws IOException {
mapReduceDriver.withInput(new LongWritable(), new Text(RECORD[0])); (4)
mapReduceDriver.withInput(new LongWritable(), new Text(RECORD[1]));
mapReduceDriver.withOutput(new Text("2021-05-07"),
new IntArrayWritable(new Integer[] { 0, 1}));
mapReduceDriver.runTest();
}
}