15
15
*/
16
16
package com .google .cloud .bigtable .mapreduce .hbasesnapshots ;
17
17
18
+ import static org .mockito .Mockito .doAnswer ;
19
+ import static org .mockito .Mockito .when ;
20
+
18
21
import com .google .cloud .bigtable .mapreduce .hbasesnapshots .ImportHBaseSnapshotJob .ScanCounter ;
19
22
import com .google .cloud .bigtable .mapreduce .hbasesnapshots .ImportHBaseSnapshotJob .SnapshotMapper ;
20
- import java .io .IOException ;
21
23
import java .util .ArrayList ;
22
24
import java .util .List ;
23
- import org .apache .hadoop .conf .Configuration ;
24
25
import org .apache .hadoop .hbase .Cell ;
25
26
import org .apache .hadoop .hbase .CellScanner ;
26
27
import org .apache .hadoop .hbase .CellUtil ;
27
28
import org .apache .hadoop .hbase .KeyValue ;
28
29
import org .apache .hadoop .hbase .client .Put ;
29
30
import org .apache .hadoop .hbase .client .Result ;
30
31
import org .apache .hadoop .hbase .io .ImmutableBytesWritable ;
31
- import org .apache .hadoop .hbase .mapreduce .MutationSerialization ;
32
- import org .apache .hadoop .hbase .mapreduce .ResultSerialization ;
33
32
import 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 ;
37
36
import org .junit .Assert ;
38
37
import org .junit .Before ;
38
+ import org .junit .Rule ;
39
39
import 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 ;
40
44
41
45
/** test mapper for snapshot import */
42
46
public class TestSnapshotMapper {
47
+ @ Rule public final MockitoRule mockitoRule = MockitoJUnit .rule ();
48
+ private SnapshotMapper mappUnderTest ;
43
49
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 ;
45
54
46
55
@ 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 ());
58
73
}
59
74
60
75
@ Test
61
- public void testSnapshotMapper () throws IOException {
76
+ public void testSnapshotMapper () throws Exception {
62
77
int rowCount = 20 ;
63
78
int cellCount = 10 ;
64
79
byte [] row = Bytes .toBytes ("row" );
@@ -68,7 +83,6 @@ public void testSnapshotMapper() throws IOException {
68
83
long ts = 123L ;
69
84
70
85
ImmutableBytesWritable key = new ImmutableBytesWritable (row );
71
-
72
86
for (int h = 0 ; h < rowCount ; h ++) {
73
87
List <Cell > cellList = new ArrayList <>();
74
88
for (int i = 0 ; i < cellCount ; i ++) {
@@ -77,11 +91,10 @@ public void testSnapshotMapper() throws IOException {
77
91
}
78
92
79
93
Result res = Result .create (cellList );
80
- mapDriver . addInput ( new Pair <>( key , res ) );
94
+ mappUnderTest . map ( key , res , mockCtx );
81
95
}
82
96
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 ());
85
98
86
99
int cellResCount = 0 ;
87
100
for (Pair <ImmutableBytesWritable , Put > r : resultList ) {
@@ -100,15 +113,12 @@ public void testSnapshotMapper() throws IOException {
100
113
Assert .assertEquals ((rowCount * cellCount ), cellResCount );
101
114
102
115
// 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 ));
108
118
}
109
119
110
120
@ Test
111
- public void testRowExceedingMaxCells () throws IOException {
121
+ public void testRowExceedingMaxCells () throws Exception {
112
122
int cellCount = SnapshotMapper .MAX_CELLS + 100 ;
113
123
byte [] row = Bytes .toBytes ("row" );
114
124
ImmutableBytesWritable key = new ImmutableBytesWritable (row );
@@ -121,10 +131,9 @@ public void testRowExceedingMaxCells() throws IOException {
121
131
}
122
132
123
133
Result res = Result .create (cellList );
124
- Pair <ImmutableBytesWritable , Result > input = new Pair <>(key , res );
125
- mapDriver .addInput (input );
126
134
127
- List <Pair <ImmutableBytesWritable , Put >> resultList = mapDriver .run ();
135
+ mappUnderTest .map (key , res , mockCtx );
136
+
128
137
Assert .assertEquals (2 , resultList .size ());
129
138
130
139
int cellResCount = 0 ;
@@ -137,10 +146,7 @@ public void testRowExceedingMaxCells() throws IOException {
137
146
Assert .assertEquals (cellCount , cellResCount );
138
147
139
148
// 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 ));
145
151
}
146
152
}
0 commit comments