@Test public void testDeleteEntity() { ContainerDocumentAccessor cda = Mockito.spy(testAccessor); Map<String, Object> updateDocCriteria = new HashMap<String, Object>(); updateDocCriteria.put("event", "Tardy"); DBObject pullObject = BasicDBObjectBuilder.start().push("$pull").add("body.attendanceEvent", updateDocCriteria).get(); DBObject resultingAttendanceEvent = createResultAttendance("10-04-2013"); NeutralSchema attendanceSchema = createMockAttendanceSchema(); when(mongoTemplate.getCollection(ATTENDANCE)).thenReturn(mockCollection); when(mockCollection.findAndModify(Mockito.any(DBObject.class), (DBObject) Mockito.isNull(), (DBObject) Mockito.isNull(), Mockito.eq(false), Mockito.eq(pullObject),Mockito.eq(true), Mockito.eq(false))).thenReturn(resultingAttendanceEvent); when(schemaRepo.getSchema(EntityNames.ATTENDANCE)).thenReturn(attendanceSchema); boolean result = cda.deleteContainerNonSubDocs(entity); Mockito.verify(mockCollection, Mockito.times(1)).findAndModify(Mockito.any(DBObject.class), (DBObject) Mockito.isNull(), (DBObject) Mockito.isNull(), Mockito.eq(false), Mockito.eq(pullObject), Mockito.eq(true), Mockito.eq(false)); assertTrue(result); }
@SuppressWarnings("unchecked") public boolean deleteContainerNonSubDocs(final Entity containerDoc) { String collection = containerDoc.getType(); String embeddedDocType = getEmbeddedDocType(collection); List<Map<String, Object>> embeddedDocs = (List<Map<String, Object>>) containerDoc.getBody().get(embeddedDocType); String containerDocId = containerDoc.getEntityId(); final BasicDBObject query = new BasicDBObject(); query.put("_id", containerDocId); DBObject result = null; for (Map<String, Object> docToDelete : embeddedDocs) { Map<String, Object> filteredDocToDelete = filterByNaturalKeys(embeddedDocType, docToDelete); BasicDBObject dBDocToDelete = new BasicDBObject("body." + embeddedDocType, filteredDocToDelete); final BasicDBObject update = new BasicDBObject("$pull", dBDocToDelete); result = this.mongoTemplate.getCollection(collection).findAndModify(query, null, null, false, update, true, false); if (result == null) { LOG.error("Could not delete " + embeddedDocType + " instance from " + collection + " record with id " + containerDocId); return false; } } List<Map<String, Object>> remainingAttendanceEvents = (List<Map<String, Object>>) ((Map<String, Object>) result .get("body")).get(embeddedDocType); if (remainingAttendanceEvents == null || remainingAttendanceEvents.isEmpty()) { Query frQuery = new Query(Criteria.where("_id").is(containerDocId)); Entity deleted = this.mongoTemplate.findAndRemove(frQuery, Entity.class, collection); if (deleted == null) { LOG.error("Could not delete empty " + collection + " record with id " + containerDocId); return false; } } return true; }
ContainerDocumentAccessor { @SuppressWarnings("unchecked") public boolean deleteContainerNonSubDocs(final Entity containerDoc) { String collection = containerDoc.getType(); String embeddedDocType = getEmbeddedDocType(collection); List<Map<String, Object>> embeddedDocs = (List<Map<String, Object>>) containerDoc.getBody().get(embeddedDocType); String containerDocId = containerDoc.getEntityId(); final BasicDBObject query = new BasicDBObject(); query.put("_id", containerDocId); DBObject result = null; for (Map<String, Object> docToDelete : embeddedDocs) { Map<String, Object> filteredDocToDelete = filterByNaturalKeys(embeddedDocType, docToDelete); BasicDBObject dBDocToDelete = new BasicDBObject("body." + embeddedDocType, filteredDocToDelete); final BasicDBObject update = new BasicDBObject("$pull", dBDocToDelete); result = this.mongoTemplate.getCollection(collection).findAndModify(query, null, null, false, update, true, false); if (result == null) { LOG.error("Could not delete " + embeddedDocType + " instance from " + collection + " record with id " + containerDocId); return false; } } List<Map<String, Object>> remainingAttendanceEvents = (List<Map<String, Object>>) ((Map<String, Object>) result .get("body")).get(embeddedDocType); if (remainingAttendanceEvents == null || remainingAttendanceEvents.isEmpty()) { Query frQuery = new Query(Criteria.where("_id").is(containerDocId)); Entity deleted = this.mongoTemplate.findAndRemove(frQuery, Entity.class, collection); if (deleted == null) { LOG.error("Could not delete empty " + collection + " record with id " + containerDocId); return false; } } return true; } }
ContainerDocumentAccessor { @SuppressWarnings("unchecked") public boolean deleteContainerNonSubDocs(final Entity containerDoc) { String collection = containerDoc.getType(); String embeddedDocType = getEmbeddedDocType(collection); List<Map<String, Object>> embeddedDocs = (List<Map<String, Object>>) containerDoc.getBody().get(embeddedDocType); String containerDocId = containerDoc.getEntityId(); final BasicDBObject query = new BasicDBObject(); query.put("_id", containerDocId); DBObject result = null; for (Map<String, Object> docToDelete : embeddedDocs) { Map<String, Object> filteredDocToDelete = filterByNaturalKeys(embeddedDocType, docToDelete); BasicDBObject dBDocToDelete = new BasicDBObject("body." + embeddedDocType, filteredDocToDelete); final BasicDBObject update = new BasicDBObject("$pull", dBDocToDelete); result = this.mongoTemplate.getCollection(collection).findAndModify(query, null, null, false, update, true, false); if (result == null) { LOG.error("Could not delete " + embeddedDocType + " instance from " + collection + " record with id " + containerDocId); return false; } } List<Map<String, Object>> remainingAttendanceEvents = (List<Map<String, Object>>) ((Map<String, Object>) result .get("body")).get(embeddedDocType); if (remainingAttendanceEvents == null || remainingAttendanceEvents.isEmpty()) { Query frQuery = new Query(Criteria.where("_id").is(containerDocId)); Entity deleted = this.mongoTemplate.findAndRemove(frQuery, Entity.class, collection); if (deleted == null) { LOG.error("Could not delete empty " + collection + " record with id " + containerDocId); return false; } } return true; } ContainerDocumentAccessor(final UUIDGeneratorStrategy strategy, final INaturalKeyExtractor extractor,
                                     final MongoTemplate mongoTemplate, final SchemaRepository schemaRepo); }
ContainerDocumentAccessor { @SuppressWarnings("unchecked") public boolean deleteContainerNonSubDocs(final Entity containerDoc) { String collection = containerDoc.getType(); String embeddedDocType = getEmbeddedDocType(collection); List<Map<String, Object>> embeddedDocs = (List<Map<String, Object>>) containerDoc.getBody().get(embeddedDocType); String containerDocId = containerDoc.getEntityId(); final BasicDBObject query = new BasicDBObject(); query.put("_id", containerDocId); DBObject result = null; for (Map<String, Object> docToDelete : embeddedDocs) { Map<String, Object> filteredDocToDelete = filterByNaturalKeys(embeddedDocType, docToDelete); BasicDBObject dBDocToDelete = new BasicDBObject("body." + embeddedDocType, filteredDocToDelete); final BasicDBObject update = new BasicDBObject("$pull", dBDocToDelete); result = this.mongoTemplate.getCollection(collection).findAndModify(query, null, null, false, update, true, false); if (result == null) { LOG.error("Could not delete " + embeddedDocType + " instance from " + collection + " record with id " + containerDocId); return false; } } List<Map<String, Object>> remainingAttendanceEvents = (List<Map<String, Object>>) ((Map<String, Object>) result .get("body")).get(embeddedDocType); if (remainingAttendanceEvents == null || remainingAttendanceEvents.isEmpty()) { Query frQuery = new Query(Criteria.where("_id").is(containerDocId)); Entity deleted = this.mongoTemplate.findAndRemove(frQuery, Entity.class, collection); if (deleted == null) { LOG.error("Could not delete empty " + collection + " record with id " + containerDocId); return false; } } return true; } ContainerDocumentAccessor(final UUIDGeneratorStrategy strategy, final INaturalKeyExtractor extractor,
                                     final MongoTemplate mongoTemplate, final SchemaRepository schemaRepo); boolean isContainerDocument(final String entity); boolean isContainerSubdoc(final String entity); boolean insert(final List<Entity> entityList); String insert(final Entity entity); boolean update(final String type, final String id, Map<String, Object> newValues, String collectionName); String update(final Entity entity); Entity findById(String collectionName, String id); List<Entity> findAll(String collectionName, Query query); boolean delete(final Entity entity); @SuppressWarnings("unchecked") boolean deleteContainerNonSubDocs(final Entity containerDoc); long count(String collectionName, Query query); boolean exists(String collectionName, String id); String getEmbeddedDocType(final String containerDocType); }
ContainerDocumentAccessor { @SuppressWarnings("unchecked") public boolean deleteContainerNonSubDocs(final Entity containerDoc) { String collection = containerDoc.getType(); String embeddedDocType = getEmbeddedDocType(collection); List<Map<String, Object>> embeddedDocs = (List<Map<String, Object>>) containerDoc.getBody().get(embeddedDocType); String containerDocId = containerDoc.getEntityId(); final BasicDBObject query = new BasicDBObject(); query.put("_id", containerDocId); DBObject result = null; for (Map<String, Object> docToDelete : embeddedDocs) { Map<String, Object> filteredDocToDelete = filterByNaturalKeys(embeddedDocType, docToDelete); BasicDBObject dBDocToDelete = new BasicDBObject("body." + embeddedDocType, filteredDocToDelete); final BasicDBObject update = new BasicDBObject("$pull", dBDocToDelete); result = this.mongoTemplate.getCollection(collection).findAndModify(query, null, null, false, update, true, false); if (result == null) { LOG.error("Could not delete " + embeddedDocType + " instance from " + collection + " record with id " + containerDocId); return false; } } List<Map<String, Object>> remainingAttendanceEvents = (List<Map<String, Object>>) ((Map<String, Object>) result .get("body")).get(embeddedDocType); if (remainingAttendanceEvents == null || remainingAttendanceEvents.isEmpty()) { Query frQuery = new Query(Criteria.where("_id").is(containerDocId)); Entity deleted = this.mongoTemplate.findAndRemove(frQuery, Entity.class, collection); if (deleted == null) { LOG.error("Could not delete empty " + collection + " record with id " + containerDocId); return false; } } return true; } ContainerDocumentAccessor(final UUIDGeneratorStrategy strategy, final INaturalKeyExtractor extractor,
                                     final MongoTemplate mongoTemplate, final SchemaRepository schemaRepo); boolean isContainerDocument(final String entity); boolean isContainerSubdoc(final String entity); boolean insert(final List<Entity> entityList); String insert(final Entity entity); boolean update(final String type, final String id, Map<String, Object> newValues, String collectionName); String update(final Entity entity); Entity findById(String collectionName, String id); List<Entity> findAll(String collectionName, Query query); boolean delete(final Entity entity); @SuppressWarnings("unchecked") boolean deleteContainerNonSubDocs(final Entity containerDoc); long count(String collectionName, Query query); boolean exists(String collectionName, String id); String getEmbeddedDocType(final String containerDocType);  }