TransmissionMapper { public List<Transmission> transform(@Nullable Cursor cursor) { int size = cursor == null ? 0 : cursor.getCount(); List<Transmission> transmissions = new ArrayList<>(size); if (cursor != null) { if (cursor.moveToFirst()) { final int idCol = cursor.getColumnIndexOrThrow(TransmissionColumns._ID); final int formIdCol = cursor.getColumnIndexOrThrow(TransmissionColumns.SURVEY_ID); final int surveyInstanceCol = cursor .getColumnIndexOrThrow(TransmissionColumns.SURVEY_INSTANCE_ID); final int fileCol = cursor.getColumnIndexOrThrow(TransmissionColumns.FILENAME); do { String filename = cursor.getString(fileCol); S3File s3File = fileMapper.transform(filename); if (s3File != null) { Transmission trans = new Transmission(cursor.getLong(idCol), cursor.getLong(surveyInstanceCol), cursor.getString(formIdCol), s3File); transmissions.add(trans); } else { Timber.e("Transmission error: file " + filename + " could not be processed"); } } while (cursor.moveToNext()); } cursor.close(); } return transmissions; } @Inject  TransmissionMapper(S3FileMapper fileMapper); List<Transmission> transform(@Nullable Cursor cursor);  }
@Test public void shouldReturnEmptyForNullCursor() { TransmissionMapper mapper = new TransmissionMapper(mockS3Mapper); List<Transmission> transmissions = mapper.transform(null); assertTrue(transmissions.isEmpty()); }
@Test public void shouldReturnEmptyForEmptyCursor() { TransmissionMapper mapper = new TransmissionMapper(mockS3Mapper); given(mockCursor.getCount()).willReturn(0); given(mockCursor.moveToFirst()).willReturn(false); doNothing().when(mockCursor).close(); List<Transmission> transmissions = mapper.transform(mockCursor); assertTrue(transmissions.isEmpty()); }
@Test public void shouldOmitTransmissionsWithNullS3Files() { TransmissionMapper mapper = new TransmissionMapper(mockS3Mapper); given(mockCursor.getCount()).willReturn(1); given(mockCursor.moveToFirst()).willReturn(true).willReturn(false); given(mockCursor.moveToNext()).willReturn(false); doNothing().when(mockCursor).close(); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns._ID)).willReturn(0); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.SURVEY_ID)).willReturn(1); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.SURVEY_INSTANCE_ID)).willReturn(2); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.FILENAME)).willReturn(3); given(mockCursor.getString(3)).willReturn(""); given(mockS3Mapper.transform(anyString())).willReturn(null); List<Transmission> transmissions = mapper.transform(mockCursor); assertTrue(transmissions.isEmpty()); }
@Test public void shouldReturnCorrectTransmissions() { TransmissionMapper mapper = new TransmissionMapper(mockS3Mapper); given(mockCursor.getCount()).willReturn(2); given(mockCursor.moveToFirst()).willReturn(true).willReturn(false); given(mockCursor.moveToNext()).willReturn(true).willReturn(false); doNothing().when(mockCursor).close(); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns._ID)).willReturn(0); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.SURVEY_ID)).willReturn(1); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.SURVEY_INSTANCE_ID)) .willReturn(2); given(mockCursor.getColumnIndexOrThrow(TransmissionColumns.FILENAME)).willReturn(3); given(mockCursor.getString(3)).willReturn(""); given(mockS3Mapper.transform(anyString())).willReturn(mockS3File); List<Transmission> transmissions = mapper.transform(mockCursor); assertEquals(2, transmissions.size()); }