AbstractMongoDao extends AbstractDao<M,P,K,I> { @Override public M updateAndReturn(K id, Update<P> update, Instant updateAt, I updateBy) { Document upDoc = toUpdateDocument(update, updateAt, updateBy); Document query = new Document(ID_FIELD, id); Document found = getCollection().findOneAndUpdate(query, upDoc); return modelMapper.toObject(found); } AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection, ObjectMapper objectMapper, ObjectMapper extendedJsonMapper);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection,
                            DocumentMapper<M> modelMapper, DocumentMapper<P> partialMapper); MongoCollection<Document> getCollection(); String getCollectionName(); DocumentMapper<M> getModelMapper(); DocumentMapper<P> getPartialMapper(); @Override Optional<M> loadOpt(K id); Optional<P> loadOpt(K id, Iterable<String> fields); @Override Iterable<M> iterateAll(); @Override M create(P partial, Instant createAt, I createBy); @Override void update(K id, Update<P> update, Instant updateAt, I updateBy); @Override M updateAndReturn(K id, Update<P> update, Instant updateAt, I updateBy); @Override void delete(K id); @Override void initTable(); @Override void dropTable(); List<IndexModel> getIndexes(); Update<P> updateOf(P object); Update<P> updateOf(P object, Iterable<String> removeFields); static final String ID_FIELD; }
@Test public void testUpdateAndReturn() throws Exception { TestUser created = dao.create(TestUser.builder().name("Ian").build()); ObjectId updateBy = new ObjectId(); Instant now = Instant.now(); Update<TestUser> update = Update.of( TestUser.builder().name("Bob").build(), ImmutableSet.of("name")); TestUser prev = dao.updateAndReturn(created.getId(), update, now, updateBy); assertEquals(created, prev); TestUser load = dao.load(created.getId()); assertEquals("Bob", load.getName()); assertEquals(updateBy, load.getUpdateId()); assertEquals(now.toEpochMilli(), load.getUpdateAt().toEpochMilli()); }