AbstractMongoDao extends AbstractDao<M,P,K,I> { @Override public M create(P partial, Instant createAt, I createBy) { Document doc = partialMapper.toDocument(partial); if (doc.get(ID_FIELD) == null) { doc.put(ID_FIELD, generateId()); } FieldData fieldData = getFieldData(); if (createAt != null && fieldData.getCreatedAt() != null && doc.get(fieldData.getCreatedAt().getStorageName()) == null) { doc.put(fieldData.getCreatedAt().getStorageName(), Date.from(createAt)); } if (createBy != null && fieldData.getCreatedBy() != null && doc.get(fieldData.getCreatedBy().getStorageName()) == null) { doc.put(fieldData.getCreatedBy().getStorageName(), createBy); } if (createAt != null && fieldData.getUpdatedAt() != null && !fieldData.getUpdatedAt().isOptional() && doc.get(fieldData.getUpdatedAt().getStorageName()) == null) { doc.put(fieldData.getUpdatedAt().getStorageName(), Date.from(createAt)); } if (createBy != null && fieldData.getUpdatedBy() != null && !fieldData.getUpdatedBy().isOptional() && doc.get(fieldData.getUpdatedBy().getStorageName()) == null) { doc.put(fieldData.getUpdatedBy().getStorageName(), createBy); } M model = modelMapper.toObject(doc); getCollection().insertOne(doc); return model; } 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 testCreate() throws Exception { Instant now = Instant.now(); ObjectId creatorId = new ObjectId(); TestUser created = dao.create(TestUser.builder().name("Ian").build(), now, creatorId); assertNotNull(created.getId()); assertEquals("Ian", created.getName()); assertEquals(creatorId, created.getCreateId()); assertEquals(now.toEpochMilli(), created.getCreateAt().toEpochMilli()); }
@Test public void testCreateWithFieldsInPlace() throws Exception { Instant now = Instant.now(); ObjectId creatorId = new ObjectId(); TestUser created = dao.create(TestUser.builder().name("Ian").createAt(now).createId(creatorId).build(), Instant.now(), new ObjectId()); assertNotNull(created.getId()); assertEquals("Ian", created.getName()); assertEquals(creatorId, created.getCreateId()); assertEquals(now.toEpochMilli(), created.getCreateAt().toEpochMilli()); }
@Test public void testCanStoreGeoJson() throws Exception { TestUser created = dao.create(TestUser.builder().location(new Point(-73.9857, 40.7484)).build()); TestUser load = dao.load(created.getId()); assertEquals(-73.9857, load.getLocation().getCoordinates().getLongitude(), .00001); }