39
39
import java .util .concurrent .BlockingQueue ;
40
40
import java .util .concurrent .LinkedBlockingQueue ;
41
41
import java .util .concurrent .TimeUnit ;
42
+ import java .util .concurrent .atomic .AtomicReference ;
42
43
import java .util .stream .Collectors ;
43
44
import java .util .stream .IntStream ;
44
45
55
56
import org .junit .rules .ExpectedException ;
56
57
import org .junit .runner .RunWith ;
57
58
import org .springframework .beans .factory .annotation .Autowired ;
59
+ import org .springframework .context .ConfigurableApplicationContext ;
58
60
import org .springframework .dao .DataIntegrityViolationException ;
59
61
import org .springframework .dao .DuplicateKeyException ;
60
62
import org .springframework .dao .InvalidDataAccessApiUsageException ;
72
74
import org .springframework .data .mongodb .core .index .GeospatialIndex ;
73
75
import org .springframework .data .mongodb .core .index .Index ;
74
76
import org .springframework .data .mongodb .core .index .IndexOperationsAdapter ;
77
+ import org .springframework .data .mongodb .core .mapping .event .AbstractMongoEventListener ;
78
+ import org .springframework .data .mongodb .core .mapping .event .AfterSaveEvent ;
75
79
import org .springframework .data .mongodb .core .query .Criteria ;
76
80
import org .springframework .data .mongodb .core .query .NearQuery ;
77
81
import org .springframework .data .mongodb .core .query .Query ;
78
82
import org .springframework .data .mongodb .core .query .Update ;
79
83
import org .springframework .data .mongodb .test .util .ReplicaSet ;
84
+ import org .springframework .test .annotation .DirtiesContext ;
80
85
import org .springframework .test .context .ContextConfiguration ;
81
86
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
82
87
@@ -96,6 +101,7 @@ public class ReactiveMongoTemplateTests {
96
101
97
102
@ Autowired SimpleReactiveMongoDatabaseFactory factory ;
98
103
@ Autowired ReactiveMongoTemplate template ;
104
+ @ Autowired ConfigurableApplicationContext context ;
99
105
100
106
@ Before
101
107
public void setUp () {
@@ -1319,6 +1325,50 @@ public void removeShouldConsiderSkipAndSort() {
1319
1325
.verifyComplete ();
1320
1326
}
1321
1327
1328
+ @ Test // DATAMONGO-2189
1329
+ @ DirtiesContext
1330
+ public void afterSaveEventContainsSavedObjectUsingInsert () {
1331
+
1332
+ AtomicReference <ImmutableVersioned > saved = createAfterSaveReference ();
1333
+
1334
+ template .insert (new ImmutableVersioned ()) //
1335
+ .as (StepVerifier ::create ) //
1336
+ .expectNextCount (1 ) //
1337
+ .verifyComplete ();
1338
+
1339
+ assertThat (saved .get ()).isNotNull ();
1340
+ assertThat (saved .get ().id ).isNotNull ();
1341
+ }
1342
+
1343
+ @ Test // DATAMONGO-2189
1344
+ @ DirtiesContext
1345
+ public void afterSaveEventContainsSavedObjectUsingInsertAll () {
1346
+
1347
+ AtomicReference <ImmutableVersioned > saved = createAfterSaveReference ();
1348
+
1349
+ template .insertAll (Collections .singleton (new ImmutableVersioned ())) //
1350
+ .as (StepVerifier ::create ) //
1351
+ .expectNextCount (1 ) //
1352
+ .verifyComplete ();
1353
+
1354
+ assertThat (saved .get ()).isNotNull ();
1355
+ assertThat (saved .get ().id ).isNotNull ();
1356
+ }
1357
+
1358
+ private AtomicReference <ImmutableVersioned > createAfterSaveReference () {
1359
+
1360
+ AtomicReference <ImmutableVersioned > saved = new AtomicReference <>();
1361
+ context .addApplicationListener (new AbstractMongoEventListener <ImmutableVersioned >() {
1362
+
1363
+ @ Override
1364
+ public void onAfterSave (AfterSaveEvent <ImmutableVersioned > event ) {
1365
+ saved .set (event .getSource ());
1366
+ }
1367
+ });
1368
+
1369
+ return saved ;
1370
+ }
1371
+
1322
1372
@ Test // DATAMONGO-2012
1323
1373
public void watchesDatabaseCorrectly () throws InterruptedException {
1324
1374
0 commit comments