|
26 | 26 | import org.mockito.quality.Strictness;
|
27 | 27 |
|
28 | 28 | import org.springframework.batch.item.Chunk;
|
| 29 | +import org.springframework.batch.item.data.MongoItemWriter.Mode; |
29 | 30 | import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
30 | 31 | import org.springframework.data.mapping.context.MappingContext;
|
31 | 32 | import org.springframework.data.mongodb.core.BulkOperations;
|
@@ -297,6 +298,155 @@ void testResourceKeyCollision() {
|
297 | 298 | }
|
298 | 299 | }
|
299 | 300 |
|
| 301 | + // BATCH-4149 |
| 302 | + |
| 303 | + @Test |
| 304 | + void testInsertModeNoTransactionNoCollection() throws Exception { |
| 305 | + Chunk<Item> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 306 | + |
| 307 | + writer.setMode(Mode.INSERT); |
| 308 | + writer.write(items); |
| 309 | + |
| 310 | + verify(template).bulkOps(any(), any(Class.class)); |
| 311 | + verify(bulkOperations, times(2)).insert(any(Object.class)); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + void testInsertModeNoTransactionWithCollection() throws Exception { |
| 316 | + Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 317 | + |
| 318 | + writer.setMode(Mode.INSERT); |
| 319 | + writer.setCollection("collection"); |
| 320 | + |
| 321 | + writer.write(items); |
| 322 | + |
| 323 | + verify(template).bulkOps(any(), eq("collection")); |
| 324 | + verify(bulkOperations, times(2)).insert(any(Object.class)); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + void testInsertModeNoTransactionNoItems() throws Exception { |
| 329 | + writer.setMode(Mode.INSERT); |
| 330 | + writer.write(new Chunk<>()); |
| 331 | + |
| 332 | + verifyNoInteractions(template); |
| 333 | + verifyNoInteractions(bulkOperations); |
| 334 | + } |
| 335 | + |
| 336 | + @Test |
| 337 | + void testInsertModeTransactionNoCollection() { |
| 338 | + final Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 339 | + |
| 340 | + writer.setMode(Mode.INSERT); |
| 341 | + |
| 342 | + new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { |
| 343 | + assertDoesNotThrow(() -> writer.write(items)); |
| 344 | + return null; |
| 345 | + }); |
| 346 | + |
| 347 | + verify(template).bulkOps(any(), any(Class.class)); |
| 348 | + verify(bulkOperations, times(2)).insert(any(Object.class)); |
| 349 | + } |
| 350 | + |
| 351 | + @Test |
| 352 | + void testInsertModeTransactionWithCollection() { |
| 353 | + final Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 354 | + |
| 355 | + writer.setMode(Mode.INSERT); |
| 356 | + writer.setCollection("collection"); |
| 357 | + |
| 358 | + new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { |
| 359 | + assertDoesNotThrow(() -> writer.write(items)); |
| 360 | + return null; |
| 361 | + }); |
| 362 | + |
| 363 | + verify(template).bulkOps(any(), eq("collection")); |
| 364 | + verify(bulkOperations, times(2)).insert(any(Object.class)); |
| 365 | + } |
| 366 | + |
| 367 | + @Test |
| 368 | + void testInsertModeTransactionFails() { |
| 369 | + final Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 370 | + |
| 371 | + writer.setMode(Mode.INSERT); |
| 372 | + writer.setCollection("collection"); |
| 373 | + |
| 374 | + Exception exception = assertThrows(RuntimeException.class, |
| 375 | + () -> new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { |
| 376 | + assertDoesNotThrow(() -> writer.write(items)); |
| 377 | + throw new RuntimeException("force rollback"); |
| 378 | + })); |
| 379 | + assertEquals(exception.getMessage(), "force rollback"); |
| 380 | + |
| 381 | + verifyNoInteractions(template); |
| 382 | + verifyNoInteractions(bulkOperations); |
| 383 | + } |
| 384 | + |
| 385 | + @Test |
| 386 | + void testInsertModeTransactionReadOnly() { |
| 387 | + final Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 388 | + |
| 389 | + writer.setMode(Mode.INSERT); |
| 390 | + writer.setCollection("collection"); |
| 391 | + |
| 392 | + TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); |
| 393 | + transactionTemplate.setReadOnly(true); |
| 394 | + transactionTemplate.execute((TransactionCallback<Void>) status -> { |
| 395 | + assertDoesNotThrow(() -> writer.write(items)); |
| 396 | + return null; |
| 397 | + }); |
| 398 | + |
| 399 | + verifyNoInteractions(template); |
| 400 | + verifyNoInteractions(bulkOperations); |
| 401 | + } |
| 402 | + |
| 403 | + @Test |
| 404 | + void testRemoveModeNoObjectIdNoCollection() throws Exception { |
| 405 | + writer.setMode(Mode.REMOVE); |
| 406 | + Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 407 | + |
| 408 | + writer.write(items); |
| 409 | + |
| 410 | + verify(template).bulkOps(any(), any(Class.class)); |
| 411 | + verify(bulkOperations, never()).remove(any(Query.class)); |
| 412 | + } |
| 413 | + |
| 414 | + @Test |
| 415 | + void testRemoveModeNoObjectIdWithCollection() throws Exception { |
| 416 | + writer.setMode(Mode.REMOVE); |
| 417 | + Chunk<Object> items = Chunk.of(new Item("Foo"), new Item("Bar")); |
| 418 | + |
| 419 | + writer.setCollection("collection"); |
| 420 | + writer.write(items); |
| 421 | + |
| 422 | + verify(template).bulkOps(any(), eq("collection")); |
| 423 | + verify(bulkOperations, never()).remove(any(Query.class)); |
| 424 | + } |
| 425 | + |
| 426 | + @Test |
| 427 | + void testRemoveModeNoTransactionNoCollection() throws Exception { |
| 428 | + writer.setMode(Mode.REMOVE); |
| 429 | + Chunk<Object> items = Chunk.of(new Item(1), new Item(2)); |
| 430 | + |
| 431 | + writer.write(items); |
| 432 | + |
| 433 | + verify(template).bulkOps(any(), any(Class.class)); |
| 434 | + verify(bulkOperations, times(2)).remove(any(Query.class)); |
| 435 | + } |
| 436 | + |
| 437 | + @Test |
| 438 | + void testRemoveModeNoTransactionWithCollection() throws Exception { |
| 439 | + writer.setMode(Mode.REMOVE); |
| 440 | + Chunk<Object> items = Chunk.of(new Item(1), new Item(2)); |
| 441 | + |
| 442 | + writer.setCollection("collection"); |
| 443 | + |
| 444 | + writer.write(items); |
| 445 | + |
| 446 | + verify(template).bulkOps(any(), eq("collection")); |
| 447 | + verify(bulkOperations, times(2)).remove(any(Query.class)); |
| 448 | + } |
| 449 | + |
300 | 450 | static class Item {
|
301 | 451 |
|
302 | 452 | Integer id;
|
|
0 commit comments