CaseService extends RecordService { public Case save(ItemValuesMap itemValues, List<String> photoPaths) throws IOException { String uniqueId = generateUniqueId(); String username = PrimeroAppConfiguration.getCurrentUser().getUsername(); itemValues.addStringItem(CASE_DISPLAY_ID, getShortUUID(uniqueId)); itemValues.addStringItem(CASE_ID, uniqueId); itemValues.addStringItem(RECORD_OWNED_BY, username); itemValues.addStringItem(RECORD_CREATED_BY, username); itemValues.addStringItem(PREVIOUS_OWNER, username); setCurrentDateIfRegistrationNotExist(itemValues); Gson gson = new Gson(); Date date = new Date(System.currentTimeMillis()); Blob blob = new Blob(gson.toJson(itemValues.getValues()).getBytes()); Blob audioFileDefault = getAudioBlob(); Case child = new Case(); child.setUniqueId(uniqueId); child.setShortId(getShortUUID(uniqueId)); child.setCreateDate(date); child.setLastUpdatedDate(date); child.setContent(blob); child.setName(getName(itemValues)); 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.setCreatedBy(username); child.setOwnedBy(username); String location = itemValues.has(LOCATION) ? itemValues.getAsString(LOCATION) : ""; child.setLocation(location); child.setServerUrl(TextUtils.lintUrl(PrimeroAppConfiguration.getApiBaseUrl())); caseDao.save(child); savePhoto(child, photoPaths); 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_save_case_when_give_item_values() throws Exception { CaseService caseServiceSpy = spy(caseService); String uuid = UUID.randomUUID().toString(); when(caseServiceSpy.generateUniqueId()).thenReturn(uuid); ItemValuesMap itemValues = new ItemValuesMap(); Case actual = caseServiceSpy.save(itemValues, Collections.EMPTY_LIST); when(caseDao.save(any(Case.class))).thenReturn(actual); assertThat("Should have save url", actual.getServerUrl(), is(TextUtils.lintUrl("https: assertThat("Should have save uuid.", actual.getUniqueId(), is(uuid)); verify(caseDao, times(1)).save(any(Case.class)); }