1515 */
1616package com .google .cloud .bigtable .mapreduce .hbasesnapshots ;
1717
18+ import static org .mockito .Mockito .doAnswer ;
19+ import static org .mockito .Mockito .when ;
20+
1821import com .google .cloud .bigtable .mapreduce .hbasesnapshots .ImportHBaseSnapshotJob .ScanCounter ;
1922import com .google .cloud .bigtable .mapreduce .hbasesnapshots .ImportHBaseSnapshotJob .SnapshotMapper ;
20- import java .io .IOException ;
2123import java .util .ArrayList ;
2224import java .util .List ;
23- import org .apache .hadoop .conf .Configuration ;
2425import org .apache .hadoop .hbase .Cell ;
2526import org .apache .hadoop .hbase .CellScanner ;
2627import org .apache .hadoop .hbase .CellUtil ;
2728import org .apache .hadoop .hbase .KeyValue ;
2829import org .apache .hadoop .hbase .client .Put ;
2930import org .apache .hadoop .hbase .client .Result ;
3031import org .apache .hadoop .hbase .io .ImmutableBytesWritable ;
31- import org .apache .hadoop .hbase .mapreduce .MutationSerialization ;
32- import org .apache .hadoop .hbase .mapreduce .ResultSerialization ;
3332import org .apache .hadoop .hbase .util .Bytes ;
34- import org .apache .hadoop .mapreduce . Counters ;
35- import org .apache .hadoop .mrunit . mapreduce .MapDriver ;
36- import org .apache .hadoop .mrunit . types . Pair ;
33+ import org .apache .hadoop .hbase . util . Pair ;
34+ import org .apache .hadoop .mapreduce .Counter ;
35+ import org .apache .hadoop .mapreduce . Mapper ;
3736import org .junit .Assert ;
3837import org .junit .Before ;
38+ import org .junit .Rule ;
3939import org .junit .Test ;
40+ import org .mockito .Mock ;
41+ import org .mockito .Mockito ;
42+ import org .mockito .junit .MockitoJUnit ;
43+ import org .mockito .junit .MockitoRule ;
4044
4145/** test mapper for snapshot import */
4246public class TestSnapshotMapper {
47+ @ Rule public final MockitoRule mockitoRule = MockitoJUnit .rule ();
48+ private SnapshotMapper mappUnderTest ;
4349
44- private MapDriver <ImmutableBytesWritable , Result , ImmutableBytesWritable , Put > mapDriver ;
50+ @ Mock private Mapper <ImmutableBytesWritable , Result , ImmutableBytesWritable , Put >.Context mockCtx ;
51+ @ Mock private Counter rowCounter ;
52+ @ Mock private Counter cellCounter ;
53+ private List <Pair <ImmutableBytesWritable , Put >> resultList ;
4554
4655 @ Before
47- public void setup () {
48- SnapshotMapper snapshotMapper = new SnapshotMapper ();
49- mapDriver = MapDriver .newMapDriver (snapshotMapper );
50- Configuration conf = new Configuration ();
51- mapDriver
52- .getConfiguration ()
53- .setStrings (
54- "io.serializations" ,
55- conf .get ("io.serializations" ),
56- MutationSerialization .class .getName (),
57- ResultSerialization .class .getName ());
56+ public void setup () throws Exception {
57+ mappUnderTest = new SnapshotMapper ();
58+
59+ when (mockCtx .getCounter (Mockito .eq (ScanCounter .NUM_ROWS ))).thenReturn (rowCounter );
60+ when (mockCtx .getCounter (Mockito .eq (ScanCounter .NUM_CELLS ))).thenReturn (cellCounter );
61+
62+ resultList = new ArrayList <>();
63+ doAnswer (
64+ invocationOnMock -> {
65+ resultList .add (
66+ Pair .newPair (
67+ (ImmutableBytesWritable ) invocationOnMock .getArguments ()[0 ],
68+ (Put ) invocationOnMock .getArguments ()[1 ]));
69+ return null ;
70+ })
71+ .when (mockCtx )
72+ .write (Mockito .any (), Mockito .any ());
5873 }
5974
6075 @ Test
61- public void testSnapshotMapper () throws IOException {
76+ public void testSnapshotMapper () throws Exception {
6277 int rowCount = 20 ;
6378 int cellCount = 10 ;
6479 byte [] row = Bytes .toBytes ("row" );
@@ -68,7 +83,6 @@ public void testSnapshotMapper() throws IOException {
6883 long ts = 123L ;
6984
7085 ImmutableBytesWritable key = new ImmutableBytesWritable (row );
71-
7286 for (int h = 0 ; h < rowCount ; h ++) {
7387 List <Cell > cellList = new ArrayList <>();
7488 for (int i = 0 ; i < cellCount ; i ++) {
@@ -77,11 +91,10 @@ public void testSnapshotMapper() throws IOException {
7791 }
7892
7993 Result res = Result .create (cellList );
80- mapDriver . addInput ( new Pair <>( key , res ) );
94+ mappUnderTest . map ( key , res , mockCtx );
8195 }
8296
83- List <Pair <ImmutableBytesWritable , Put >> resultList = mapDriver .run ();
84- Assert .assertEquals (rowCount , resultList .size ());
97+ Mockito .verify (mockCtx , Mockito .times (rowCount )).write (Mockito .any (), Mockito .any ());
8598
8699 int cellResCount = 0 ;
87100 for (Pair <ImmutableBytesWritable , Put > r : resultList ) {
@@ -100,15 +113,12 @@ public void testSnapshotMapper() throws IOException {
100113 Assert .assertEquals ((rowCount * cellCount ), cellResCount );
101114
102115 // verify counters
103- Counters counters = mapDriver .getCounters ();
104- long numRows = counters .findCounter (ScanCounter .NUM_ROWS ).getValue ();
105- long numCells = counters .findCounter (ScanCounter .NUM_CELLS ).getValue ();
106- Assert .assertEquals (rowCount , numRows );
107- Assert .assertEquals ((rowCount * cellCount ), numCells );
116+ Mockito .verify (rowCounter , Mockito .times (rowCount )).increment (Mockito .eq (1L ));
117+ Mockito .verify (cellCounter , Mockito .times (rowCount )).increment (Mockito .eq ((long ) cellCount ));
108118 }
109119
110120 @ Test
111- public void testRowExceedingMaxCells () throws IOException {
121+ public void testRowExceedingMaxCells () throws Exception {
112122 int cellCount = SnapshotMapper .MAX_CELLS + 100 ;
113123 byte [] row = Bytes .toBytes ("row" );
114124 ImmutableBytesWritable key = new ImmutableBytesWritable (row );
@@ -121,10 +131,9 @@ public void testRowExceedingMaxCells() throws IOException {
121131 }
122132
123133 Result res = Result .create (cellList );
124- Pair <ImmutableBytesWritable , Result > input = new Pair <>(key , res );
125- mapDriver .addInput (input );
126134
127- List <Pair <ImmutableBytesWritable , Put >> resultList = mapDriver .run ();
135+ mappUnderTest .map (key , res , mockCtx );
136+
128137 Assert .assertEquals (2 , resultList .size ());
129138
130139 int cellResCount = 0 ;
@@ -137,10 +146,7 @@ public void testRowExceedingMaxCells() throws IOException {
137146 Assert .assertEquals (cellCount , cellResCount );
138147
139148 // verify counters
140- Counters counters = mapDriver .getCounters ();
141- long numRows = counters .findCounter (ScanCounter .NUM_ROWS ).getValue ();
142- long numCells = counters .findCounter (ScanCounter .NUM_CELLS ).getValue ();
143- Assert .assertEquals (1 , numRows );
144- Assert .assertEquals (cellCount , numCells );
149+ Mockito .verify (rowCounter , Mockito .times (1 )).increment (Mockito .eq (1L ));
150+ Mockito .verify (cellCounter , Mockito .times (1 )).increment (Mockito .eq ((long ) cellCount ));
145151 }
146152}
0 commit comments