|
56 | 56 | import org.springframework.data.mongodb.core.convert.QueryMapper;
|
57 | 57 | import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
|
58 | 58 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 59 | +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; |
| 60 | +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; |
59 | 61 | import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
|
60 | 62 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
61 | 63 | import org.springframework.data.mongodb.core.query.Criteria;
|
|
75 | 77 | import com.mongodb.client.MongoDatabase;
|
76 | 78 | import com.mongodb.client.model.FindOneAndUpdateOptions;
|
77 | 79 | import com.mongodb.client.model.UpdateOptions;
|
| 80 | +import com.mongodb.client.result.UpdateResult; |
78 | 81 |
|
79 | 82 | /**
|
80 | 83 | * Unit tests for {@link MongoTemplate}.
|
@@ -498,6 +501,36 @@ public void mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferent
|
498 | 501 | verify(output, times(1)).limit(1000);
|
499 | 502 | }
|
500 | 503 |
|
| 504 | + @Test // DATAMONGO-1639 |
| 505 | + public void beforeConvertEventForUpdateSeesNextVersion() { |
| 506 | + |
| 507 | + final VersionedEntity entity = new VersionedEntity(); |
| 508 | + entity.id = 1; |
| 509 | + entity.version = 0; |
| 510 | + |
| 511 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 512 | + context.refresh(); |
| 513 | + context.addApplicationListener(new AbstractMongoEventListener<VersionedEntity>() { |
| 514 | + |
| 515 | + @Override |
| 516 | + public void onBeforeConvert(BeforeConvertEvent<VersionedEntity> event) { |
| 517 | + assertThat(event.getSource().version, is(1)); |
| 518 | + } |
| 519 | + }); |
| 520 | + |
| 521 | + template.setApplicationContext(context); |
| 522 | + |
| 523 | + MongoTemplate spy = Mockito.spy(template); |
| 524 | + |
| 525 | + UpdateResult result = mock(UpdateResult.class); |
| 526 | + doReturn(1L).when(result).getModifiedCount(); |
| 527 | + |
| 528 | + doReturn(result).when(spy).doUpdate(anyString(), Mockito.any(Query.class), Mockito.any(Update.class), |
| 529 | + Mockito.any(Class.class), anyBoolean(), anyBoolean()); |
| 530 | + |
| 531 | + spy.save(entity); |
| 532 | + } |
| 533 | + |
501 | 534 | class AutogenerateableId {
|
502 | 535 |
|
503 | 536 | @Id BigInteger id;
|
|
0 commit comments