23
23
import org .junit .Before ;
24
24
import org .junit .Test ;
25
25
import org .mockito .Mock ;
26
+ import static org .mockito .Mockito .mock ;
27
+ import static org .mockito .Mockito .spy ;
28
+ import static org .mockito .Mockito .when ;
29
+ import static org .mockito .Mockito .doAnswer ;
30
+ import static org .mockito .Mockito .verify ;
31
+ import static org .mockito .Mockito .verifyZeroInteractions ;
32
+ import static org .mockito .Mockito .times ;
33
+ import static org .mockito .Mockito .never ;
26
34
import org .mockito .MockitoAnnotations ;
27
35
28
36
import org .springframework .batch .support .transaction .ResourcelessTransactionManager ;
37
+ import org .springframework .data .mapping .context .MappingContext ;
29
38
import org .springframework .data .mongodb .core .BulkOperations ;
30
39
import org .springframework .data .mongodb .core .MongoOperations ;
40
+ import org .springframework .data .mongodb .core .convert .DbRefResolver ;
41
+ import org .springframework .data .mongodb .core .convert .MappingMongoConverter ;
31
42
import org .springframework .data .mongodb .core .convert .MongoConverter ;
43
+ import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
32
44
import org .springframework .data .mongodb .core .query .Query ;
33
45
import org .springframework .transaction .PlatformTransactionManager ;
34
46
import org .springframework .transaction .support .TransactionCallback ;
39
51
import static org .mockito .ArgumentMatchers .any ;
40
52
import static org .mockito .ArgumentMatchers .anyString ;
41
53
import static org .mockito .ArgumentMatchers .eq ;
42
- import static org .mockito .Mockito .when ;
43
- import static org .mockito .Mockito .doAnswer ;
44
- import static org .mockito .Mockito .mock ;
45
- import static org .mockito .Mockito .verify ;
46
- import static org .mockito .Mockito .times ;
47
- import static org .mockito .Mockito .verifyZeroInteractions ;
48
54
49
55
/**
50
56
* @author Michael Minella
@@ -60,15 +66,19 @@ public class MongoItemWriterTests {
60
66
@ Mock
61
67
private BulkOperations bulkOperations ;
62
68
@ Mock
63
- private MongoConverter mongoConverter ;
69
+ DbRefResolver dbRefResolver ;
70
+
64
71
private PlatformTransactionManager transactionManager = new ResourcelessTransactionManager ();
65
72
66
73
@ Before
67
74
public void setUp () throws Exception {
68
75
MockitoAnnotations .initMocks (this );
69
- when (template .bulkOps (any (), anyString ())).thenReturn (bulkOperations );
70
- when (template .bulkOps (any (), any (Class .class ))).thenReturn (bulkOperations );
71
- when (template .getConverter ()).thenReturn (mongoConverter );
76
+ when (this .template .bulkOps (any (), anyString ())).thenReturn (this .bulkOperations );
77
+ when (this .template .bulkOps (any (), any (Class .class ))).thenReturn (this .bulkOperations );
78
+
79
+ MappingContext mappingContext = new MongoMappingContext ();
80
+ MappingMongoConverter mongoConverter = spy (new MappingMongoConverter (this .dbRefResolver , mappingContext ));
81
+ when (this .template .getConverter ()).thenReturn (mongoConverter );
72
82
73
83
writer = new MongoItemWriter <>();
74
84
writer .setTemplate (template );
@@ -91,9 +101,9 @@ public void testAfterPropertiesSet() throws Exception {
91
101
92
102
@ Test
93
103
public void testWriteNoTransactionNoCollection () throws Exception {
94
- List <Object > items = new ArrayList <Object >() {{
95
- add (new Object ( ));
96
- add (new Object ( ));
104
+ List <Item > items = new ArrayList <Item >() {{
105
+ add (new Item ( "Foo" ));
106
+ add (new Item ( "Bar" ));
97
107
}};
98
108
99
109
writer .write (items );
@@ -105,8 +115,8 @@ public void testWriteNoTransactionNoCollection() throws Exception {
105
115
@ Test
106
116
public void testWriteNoTransactionWithCollection () throws Exception {
107
117
List <Object > items = new ArrayList <Object >() {{
108
- add (new Object ( ));
109
- add (new Object ( ));
118
+ add (new Item ( "Foo" ));
119
+ add (new Item ( "Bar" ));
110
120
}};
111
121
112
122
writer .setCollection ("collection" );
@@ -128,8 +138,8 @@ public void testWriteNoTransactionNoItems() throws Exception {
128
138
@ Test
129
139
public void testWriteTransactionNoCollection () throws Exception {
130
140
final List <Object > items = new ArrayList <Object >() {{
131
- add (new Object ( ));
132
- add (new Object ( ));
141
+ add (new Item ( "Foo" ));
142
+ add (new Item ( "Bar" ));
133
143
}};
134
144
135
145
new TransactionTemplate (transactionManager ).execute ((TransactionCallback <Void >) status -> {
@@ -149,8 +159,8 @@ public void testWriteTransactionNoCollection() throws Exception {
149
159
@ Test
150
160
public void testWriteTransactionWithCollection () throws Exception {
151
161
final List <Object > items = new ArrayList <Object >() {{
152
- add (new Object ( ));
153
- add (new Object ( ));
162
+ add (new Item ( "Foo" ));
163
+ add (new Item ( "Bar" ));
154
164
}};
155
165
156
166
writer .setCollection ("collection" );
@@ -172,8 +182,8 @@ public void testWriteTransactionWithCollection() throws Exception {
172
182
@ Test
173
183
public void testWriteTransactionFails () throws Exception {
174
184
final List <Object > items = new ArrayList <Object >() {{
175
- add (new Object ( ));
176
- add (new Object ( ));
185
+ add (new Item ( "Foo" ));
186
+ add (new Item ( "Bar" ));
177
187
}};
178
188
179
189
writer .setCollection ("collection" );
@@ -204,8 +214,8 @@ public void testWriteTransactionFails() throws Exception {
204
214
@ Test
205
215
public void testWriteTransactionReadOnly () throws Exception {
206
216
final List <Object > items = new ArrayList <Object >() {{
207
- add (new Object ( ));
208
- add (new Object ( ));
217
+ add (new Item ( "Foo" ));
218
+ add (new Item ( "Bar" ));
209
219
}};
210
220
211
221
writer .setCollection ("collection" );
@@ -229,34 +239,63 @@ public void testWriteTransactionReadOnly() throws Exception {
229
239
verifyZeroInteractions (bulkOperations );
230
240
}
231
241
242
+ @ Test
243
+ public void testRemoveNoObjectIdNoCollection () throws Exception {
244
+ writer .setDelete (true );
245
+ List <Object > items = new ArrayList <Object >() {{
246
+ add (new Item ("Foo" ));
247
+ add (new Item ("Bar" ));
248
+ }};
249
+
250
+ writer .write (items );
251
+
252
+ verify (template ).bulkOps (any (), any (Class .class ));
253
+ verify (bulkOperations , never ()).remove (any (Query .class ));
254
+ }
255
+
256
+ @ Test
257
+ public void testRemoveNoObjectIdWithCollection () throws Exception {
258
+ writer .setDelete (true );
259
+ List <Object > items = new ArrayList <Object >() {{
260
+ add (new Item ("Foo" ));
261
+ add (new Item ("Bar" ));
262
+ }};
263
+
264
+ writer .setCollection ("collection" );
265
+ writer .write (items );
266
+
267
+ verify (template ).bulkOps (any (), eq ("collection" ));
268
+ verify (bulkOperations , never ()).remove (any (Query .class ));
269
+ }
270
+
232
271
@ Test
233
272
public void testRemoveNoTransactionNoCollection () throws Exception {
234
273
writer .setDelete (true );
235
274
List <Object > items = new ArrayList <Object >() {{
236
- add (new Object ( ));
237
- add (new Object ( ));
275
+ add (new Item ( 1 ));
276
+ add (new Item ( 2 ));
238
277
}};
239
278
240
279
writer .write (items );
241
280
242
- verify (template ).remove ( items . get ( 0 ));
243
- verify (template ) .remove (items . get ( 1 ));
281
+ verify (template ).bulkOps ( any (), any ( Class . class ));
282
+ verify (bulkOperations , times ( 2 )) .remove (any ( Query . class ));
244
283
}
245
284
246
285
@ Test
247
286
public void testRemoveNoTransactionWithCollection () throws Exception {
248
287
writer .setDelete (true );
249
288
List <Object > items = new ArrayList <Object >() {{
250
- add (new Object ( ));
251
- add (new Object ( ));
289
+ add (new Item ( 1 ));
290
+ add (new Item ( 2 ));
252
291
}};
253
292
254
293
writer .setCollection ("collection" );
255
294
256
295
writer .write (items );
257
296
258
- verify (template ).remove ( items . get ( 0 ), "collection" );
259
- verify (template ) .remove (items . get ( 1 ), "collection" );
297
+ verify (template ).bulkOps ( any ( ), eq ( "collection" ) );
298
+ verify (bulkOperations , times ( 2 )) .remove (any ( Query . class ) );
260
299
}
261
300
262
301
// BATCH-2018, test code updated to pass BATCH-3713
@@ -312,3 +351,14 @@ public void testResourceKeyCollision() throws Exception {
312
351
}
313
352
}
314
353
}
354
+
355
+ class Item {
356
+ Integer id ;
357
+ String name ;
358
+ public Item (Integer id ){
359
+ this .id = id ;
360
+ }
361
+ public Item (String name ) {
362
+ this .name = name ;
363
+ }
364
+ }
0 commit comments