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