### Question:
EntityPersistHandler extends AbstractIngestionHandler<SimpleEntity, Entity> implements InitializingBean { public void setEntityRepository(Repository<Entity> entityRepository) { this.entityRepository = entityRepository; }  @Override void afterPropertiesSet(); void setEntityRepository(Repository<Entity> entityRepository); EntityConfigFactory getEntityConfigurations(); void setEntityConfigurations(EntityConfigFactory entityConfigurations); @Override String getStageName(); static final Logger LOG; }### Answer:
@Test public void testCreateStudentEntity() { MongoEntityRepository entityRepository = mock(MongoEntityRepository.class); NeutralQuery query = new NeutralQuery(); query.addCriteria(new NeutralCriteria(METADATA_BLOCK + "." + REGION_ID_FIELD, NeutralCriteria.OPERATOR_EQUAL, REGION_ID, false)); query.addCriteria(new NeutralCriteria(METADATA_BLOCK + "." + EXTERNAL_ID_FIELD, NeutralCriteria.OPERATOR_EQUAL, STUDENT_ID, false)); SimpleEntity studentEntity = createStudentEntity(true); List<Entity> le = new ArrayList<Entity>(); le.add(studentEntity); when(entityRepository.findAll(eq("student"), any(NeutralQuery.class))).thenReturn(le); when(entityRepository.updateWithRetries(studentEntity.getType(), studentEntity, totalRetries)).thenReturn(true); entityPersistHandler.setEntityRepository(entityRepository); AbstractMessageReport errorReport = new DummyMessageReport(); ReportStats reportStats = new SimpleReportStats(); entityPersistHandler.handle(studentEntity, errorReport, reportStats); verify(entityRepository).updateWithRetries(studentEntity.getType(), studentEntity, totalRetries); le.clear(); SimpleEntity studentEntity2 = createStudentEntity(false); le.add(studentEntity2); entityPersistHandler.handle(studentEntity2, errorReport, reportStats); verify(entityRepository).createWithRetries(studentEntity.getType(), null, studentEntity.getBody(), studentEntity.getMetaData(), "student", totalRetries); Assert.assertFalse("Error report should not contain errors", reportStats.hasErrors()); }
@Test public void testCreateAndDeleteStudentEntity() { MongoEntityRepository entityRepository = mock(MongoEntityRepository.class); NeutralQuery query = new NeutralQuery(); query.addCriteria(new NeutralCriteria(METADATA_BLOCK + "." + REGION_ID_FIELD, NeutralCriteria.OPERATOR_EQUAL, REGION_ID, false)); query.addCriteria(new NeutralCriteria(METADATA_BLOCK + "." + EXTERNAL_ID_FIELD, NeutralCriteria.OPERATOR_EQUAL, STUDENT_ID, false)); SimpleEntity studentEntity = createStudentEntity(true); List<Entity> le = new ArrayList<Entity>(); le.add(studentEntity); when(entityRepository.findAll(eq("student"), any(NeutralQuery.class))).thenReturn(le); when(entityRepository.updateWithRetries(studentEntity.getType(), studentEntity, totalRetries)).thenReturn(true); CascadeResult mockCascadeResult = new CascadeResult(); mockCascadeResult.setStatus(CascadeResult.Status.SUCCESS); when(entityRepository.safeDelete(eq(studentEntity), eq(studentEntity.getEntityId()), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyInt(), any(AccessibilityCheck.class))).thenReturn(mockCascadeResult); entityPersistHandler.setEntityRepository(entityRepository); AbstractMessageReport errorReport = new DummyMessageReport(); ReportStats reportStats = new SimpleReportStats(); entityPersistHandler.handle(studentEntity, errorReport, reportStats); verify(entityRepository).updateWithRetries(studentEntity.getType(), studentEntity, totalRetries); studentEntity.setAction( ActionVerb.CASCADE_DELETE); studentEntity.setActionAttributes(ImmutableMap.of( "Force", "true", "LogViolations", "true" )); entityPersistHandler.handle( studentEntity, errorReport, reportStats); verify(entityRepository).safeDelete(studentEntity, studentEntity.getEntityId(), true, false, true, true, null, null); Assert.assertFalse("Error report should not contain errors", reportStats.hasErrors()); }