FormInstanceMapper { @NonNull public Pair<FormInstance, Set<String>> getFormInstanceWithMedia(@NonNull String deviceId, Cursor data) { FormInstance formInstance = null; List<Response> responses = new ArrayList<>(); Set<String> imagePaths = new HashSet<>(); if (data != null && data.moveToFirst()) { int answer_col = data.getColumnIndexOrThrow(ResponseColumns.ANSWER); formInstance = getFormInstance(deviceId, data); do { String value = textValueCleaner.sanitizeValue(data.getString(answer_col)); if (!TextUtils.isEmpty(value)) { String filename = getFilename(data); if (!TextUtils.isEmpty(filename)) { imagePaths.add(filename); } Response response = responseMapper.extractResponse(data, value); responses.add(response); } } while (data.moveToNext()); formInstance.getResponses().addAll(responses); } if (data != null) { data.close(); } return new Pair<>(formInstance, imagePaths); } @Inject  FormInstanceMapper(TextValueCleaner textValueCleaner,
            ResponseMapper responseMapper, FileHelper fileHelper); @NonNull List<InstanceIdUuid> getInstanceIdUuids(Cursor cursor); @NonNull List<Long> getInstanceIds(Cursor cursor); @NonNull Pair<FormInstance, Set<String>> getFormInstanceWithMedia(@NonNull String deviceId,
            Cursor data); @NonNull FormInstance getFormInstance(@NonNull String deviceId, Cursor data); Long getFormInstanceId(Cursor cursor);  }
@Test public void getFormInstanceWithMediaShouldReturnEmptyIfNullCursor() { Pair<FormInstance, Set<String>> results = mapper.getFormInstanceWithMedia("123", null); assertNull(results.first); assertTrue(results.second.isEmpty()); }
@Test public void getFormInstanceWithMediaShouldReturnEmptyIfEmptyCursor() { when(mockCursor.moveToFirst()).thenReturn(false); Pair<FormInstance, Set<String>> results = mapper.getFormInstanceWithMedia("12", mockCursor); assertNull(results.first); assertTrue(results.second.isEmpty()); }
@Test public void getFormInstanceWithMediaShouldReturnEmptyResponsesIfEmptyValue() { when(mockCursor.moveToFirst()).thenReturn(true); when(mockCursor.moveToNext()).thenReturn(false); Pair<FormInstance, Set<String>> results = mapper.getFormInstanceWithMedia("12", mockCursor); assertTrue(results.first.getResponses().isEmpty()); assertTrue(results.second.isEmpty()); verify(mockResponseMapper, times(0)).extractResponse(mockCursor, "12"); }
@Test public void getFormInstanceWithMediaShouldReturnEmptyFilenamesIfEmptyValue() { when(mockCursor.moveToFirst()).thenReturn(true); when(mockCursor.moveToNext()).thenReturn(false); when(mockTextValueCleaner.cleanVal(anyString())).thenReturn("abc1"); when(mockTextValueCleaner.sanitizeValue(anyString())).thenReturn("abc"); when(mockFileHelper.getFilenameFromPath(anyString())).thenReturn(""); when(mockResponseMapper.extractResponse(mockCursor, "abc")).thenReturn(mockResponse); Pair<FormInstance, Set<String>> results = mapper.getFormInstanceWithMedia("12", mockCursor); assertFalse(results.first.getResponses().isEmpty()); assertTrue(results.second.isEmpty()); verify(mockResponseMapper, times(1)).extractResponse(mockCursor, "abc"); }
@Test public void getFormInstanceWithMediaShouldReturnValidValues() { when(mockCursor.moveToFirst()).thenReturn(true); when(mockCursor.moveToNext()).thenReturn(false); when(mockTextValueCleaner.sanitizeValue(anyString())).thenReturn("abc"); when(mockFileHelper.getFilenameFromPath(anyString())).thenReturn("file"); when(mockResponseMapper.extractResponse(mockCursor, "abc")).thenReturn(mockResponse); Pair<FormInstance, Set<String>> results = mapper.getFormInstanceWithMedia("12", mockCursor); assertFalse(results.first.getResponses().isEmpty()); assertFalse(results.second.isEmpty()); verify(mockResponseMapper, times(1)).extractResponse(mockCursor, "abc"); }