CaseService extends RecordService { public Case update(ItemValuesMap itemValues, List<String> photoBitPaths) throws IOException { setCurrentDateIfRegistrationNotExist(itemValues); Gson gson = new Gson(); Blob caseBlob = new Blob(gson.toJson(itemValues.getValues()).getBytes()); Blob audioFileDefault = getAudioBlob(); Case child = caseDao.getCaseByUniqueId(itemValues.getAsString(CASE_ID)); child.setLastUpdatedDate(new Date(Calendar.getInstance().getTimeInMillis())); child.setContent(caseBlob); child.setName(getName(itemValues)); String location = itemValues.has(LOCATION) ? itemValues.getAsString(LOCATION) : ""; child.setLocation(location); int age = itemValues.getAsInt(AGE) != null ? itemValues.getAsInt(AGE) : EMPTY_AGE; child.setAge(age); child.setCaregiver(getCaregiverName(itemValues)); child.setRegistrationDate(Utils.getRegisterDateByYyyyMmDd(itemValues.getAsString(REGISTRATION_DATE))); child.setAudio(audioFileDefault); child.setSynced(false); child = caseDao.update(child); updatePhoto(child, photoBitPaths); return child; } 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_update_case_when_give_item_values() throws Exception { ItemValuesMap itemValues = new ItemValuesMap(); itemValues.addStringItem(CASE_ID, "existedUniqueId"); itemValues.addNumberItem(AGE, 18); itemValues.addStringItem(REGISTRATION_DATE, "2016/12/25"); Case expected = new Case(); when(caseDao.getCaseByUniqueId("existedUniqueId")).thenReturn(expected); expected.setContent(new Blob(new String("").getBytes())); when(caseDao.update(expected)).thenReturn(expected); Case actual = caseService.update(itemValues, Collections.EMPTY_LIST); verify(caseDao, times(1)).update(actual); verify(casePhotoDao, times(1)).getIdsByCaseId(actual.getId()); assertFalse("Sync status should be false", actual.isSynced()); assertThat("Age should be 18", actual.getAge(), is(18)); assertThat("Registration date should be 2016/12/25", actual.getRegistrationDate(), is (Utils.getRegisterDateByYyyyMmDd("2016/12/25"))); }