IncidentService extends RecordService { public Incident save(ItemValuesMap itemValues) throws IOException { if (!itemValues.has(DATE_OF_INTERVIEW)) { itemValues.addStringItem(DATE_OF_INTERVIEW, getCurrentRegistrationDateAsString()); } String uniqueId = generateUniqueId(); itemValues.addStringItem(INCIDENT_DISPLAY_ID, getShortUUID(uniqueId)); itemValues.addStringItem(INCIDENT_ID, uniqueId); String username = PrimeroAppConfiguration.getCurrentUser().getUsername(); itemValues.addStringItem(MODULE, MODULE_GBV_CASE); itemValues.addStringItem(RECORD_OWNED_BY, username); itemValues.addStringItem(RECORD_CREATED_BY, username); itemValues.addStringItem(PREVIOUS_OWNER, username); Gson gson = new Gson(); Date date = new Date(Calendar.getInstance().getTimeInMillis()); Blob tracingBlob = new Blob(gson.toJson(itemValues.getValues()).getBytes()); Incident incident = new Incident(); incident.setUniqueId(uniqueId); incident.setShortId(getShortUUID(uniqueId)); incident.setCreateDate(date); incident.setLastUpdatedDate(date); incident.setContent(tracingBlob); incident.setName(getName(itemValues)); incident.setSurvivorCode(getSurvivorCode(itemValues)); incident.setTypeOfViolence(getTypeOfViolence(itemValues)); incident.setLocation(getLocation(itemValues)); incident.setServerUrl(TextUtils.lintUrl(PrimeroAppConfiguration.getApiBaseUrl())); int age = itemValues.getAsInt(AGE) != null ? itemValues.getAsInt(AGE) : EMPTY_AGE; incident.setAge(age); incident.setCaregiver(getCaregiverName(itemValues)); incident.setRegistrationDate(Utils.getRegisterDateByYyyyMmDd(itemValues.getAsString(DATE_OF_INTERVIEW))); incident.setCreatedBy(username); incident.setOwnedBy(username); String caseId = itemValues.has(CASE_ID) ? itemValues.getAsString(CASE_ID) : EMPTY_ID; incident.setCaseUniqueId(caseId); incidentDao.save(incident); return incident; } IncidentService();  IncidentService(IncidentDao incidentDao); Incident getById(long incidentId); Incident getByUniqueId(String uniqueId); List<Incident> getAll(); List<Long> getAllOrderByDateASC(); List<Long> getAllOrderByDateDES(); List<Long> getAllOrderByAgeASC(); List<Long> getAllOrderByAgeDES(); List<Long> getSearchResult(String uniqueId, String survivorCode, int ageFrom, int ageTo, String
            typeOfViolence, String location); Incident saveOrUpdate(ItemValuesMap itemValues); Incident save(ItemValuesMap itemValues); Incident update(ItemValuesMap itemValues); Incident deleteByRecordId(long recordId); Incident getByInternalId(String id); boolean hasSameRev(String id, String rev); List<Long> getAllSyncedRecordsId(); static final String TAG; static final String INCIDENT_DISPLAY_ID; static final String INCIDENT_ID; static final String INCIDENT_PRIMARY_ID; static final String EMPTY_ID; }
@Test public void should_save_incident_when_give_item_values_and_item_values_map_have_register_date() throws IOException { String url = "http: Mockito.when(PrimeroAppConfiguration.getApiBaseUrl()).thenReturn(url); String uniqueId = "test save incident"; IncidentService incidentServiceSpy = spy(incidentService); when(incidentServiceSpy.generateUniqueId()).thenReturn(uniqueId); User user = new User("userName"); when(PrimeroAppConfiguration.getCurrentUser()).thenReturn(user); ItemValuesMap itemValuesMap = new ItemValuesMap(); itemValuesMap.addStringItem(DATE_OF_INTERVIEW, "1111/1111/11"); Incident incident = incidentServiceSpy.save(itemValuesMap); when(incidentDao.save(any(Incident.class))).thenReturn(incident); assertThat(incident.getAge(), is(-1)); assertThat(incident.getUniqueId(), is(uniqueId)); assertThat(incident.getServerUrl(), is(TextUtils.lintUrl(url))); assertThat(incident.getRegistrationDate(), is(Utils.getRegisterDateByYyyyMmDd("1111/1111/11"))); assertThat(incident.getRegistrationDate(), is(Utils.getRegisterDateByYyyyMmDd("1111/1111/11"))); }
@Test public void should_save_incident_when_give_item_values_and_item_values_map_has_not_register_date() throws IOException { String url = "http: Mockito.when(PrimeroAppConfiguration.getApiBaseUrl()).thenReturn(url); String uniqueId = "test save incident"; IncidentService incidentServiceSpy = spy(incidentService); when(incidentServiceSpy.generateUniqueId()).thenReturn(uniqueId); User user = new User("userName"); Mockito.when(PrimeroAppConfiguration.getCurrentUser()).thenReturn(user); ItemValuesMap itemValuesMap = new ItemValuesMap(); Incident incident = incidentServiceSpy.save(itemValuesMap); when(incidentDao.save(any(Incident.class))).thenReturn(incident); assertThat(incident.getAge(), is(-1)); assertThat(incident.getServerUrl(), is(TextUtils.lintUrl(url))); assertThat(incident.getUniqueId(), is(uniqueId)); }