CaseService extends RecordService { public Case deleteByRecordId(long recordId) { Case deleteCase = caseDao.getCaseById(recordId); if (deleteCase != null && !deleteCase.isSynced()) { return null; } caseDao.delete(deleteCase); casePhotoDao.deleteByCaseId(recordId); return deleteCase; } CaseService(CaseDao caseDao, CasePhotoDao casePhotoDao, IncidentDao incidentDao); List<Case> getAll(); Case getById(long caseId); Case getByUniqueId(String uniqueId); List<String> getIncidentsByCaseId(String caseUniqueId); List<Long> getAllOrderByDateASC(); List<Long> getAllOrderByDateDES(); List<Long> getAllOrderByAgeASC(); List<Long> getAllOrderByAgeDES(); List<Long> getCPSearchResult(String shortId, String name, int ageFrom, int ageTo,
                                        String caregiver, Date date); List<Long> getAllSyncedRecordsId(); List<Long> getGBVSearchResult(String shortId, String name, String location, Date registrationDate); Case saveOrUpdate(ItemValuesMap itemValues, List<String> photoPaths); Case save(ItemValuesMap itemValues, List<String> photoPaths); void savePhoto(Case child, List<String> photoPaths); Case update(ItemValuesMap itemValues, List<String> photoBitPaths); void updatePhoto(Case child, List<String> photoPaths); Case deleteByRecordId(long recordId); Case getByInternalId(String id); boolean hasSameRev(String id, String rev); static final String TAG; static final String CASE_DISPLAY_ID; static final String CASE_ID; static final String CASE_PRIMARY_ID; }
@Test public void should_delete_when_case_exists_and_synced_yet() throws Exception { long recordId = 123L; Case deleteCase = new Case(recordId); deleteCase.setSynced(true); when(caseDao.getCaseById(recordId)).thenReturn(deleteCase); Case actual = caseService.deleteByRecordId(recordId); verify(caseDao, times(1)).delete(deleteCase); verify(casePhotoDao, times(1)).deleteByCaseId(recordId); assertThat(actual, is(deleteCase)); }
@Test public void should_not_delete_when_case_exist_and_not_synced_yet() throws Exception { long recordId = 123L; Case deleteCase = new Case(recordId); deleteCase.setSynced(false); when(caseDao.getCaseById(recordId)).thenReturn(deleteCase); Case actual = caseService.deleteByRecordId(recordId); verify(caseDao, never()).delete(deleteCase); verify(casePhotoDao, never()).deleteByCaseId(recordId); assertNull(actual); }