MongoDBDocumentCollectionManager implements DocumentCollectionManager { @Override public DocumentEntity insert(DocumentEntity entity) { Objects.requireNonNull(entity, "entity is required"); String collectionName = entity.getName(); MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName); Document document = getDocument(entity); collection.insertOne(document); boolean hasNotId = entity.getDocuments().stream() .map(jakarta.nosql.document.Document::getName).noneMatch(k -> k.equals(ID_FIELD)); if (hasNotId) { entity.add(Documents.of(ID_FIELD, document.get(ID_FIELD))); } return entity; } MongoDBDocumentCollectionManager(MongoDatabase mongoDatabase); @Override DocumentEntity insert(DocumentEntity entity); @Override DocumentEntity insert(DocumentEntity entity, Duration ttl); @Override Iterable<DocumentEntity> insert(Iterable<DocumentEntity> entities); @Override Iterable<DocumentEntity> insert(Iterable<DocumentEntity> entities, Duration ttl); @Override DocumentEntity update(DocumentEntity entity); @Override Iterable<DocumentEntity> update(Iterable<DocumentEntity> entities); @Override void delete(DocumentDeleteQuery query); @Override Stream<DocumentEntity> select(DocumentQuery query); @Override long count(String documentCollection); @Override void close();  }
@Test public void shouldInsert() { DocumentEntity entity = getEntity(); DocumentEntity documentEntity = entityManager.insert(entity); assertTrue(documentEntity.getDocuments().stream().map(Document::getName).anyMatch(s -> s.equals("_id"))); }
@Test public void shouldThrowExceptionWhenInsertWithTTL() { assertThrows(UnsupportedOperationException.class, () -> entityManager.insert(getEntity(), Duration.ofSeconds(10))); }
@Test public void shouldConvertFromListSubdocumentList() { DocumentEntity entity = createSubdocumentList(); entityManager.insert(entity); }