SurveyDataRepository implements SurveyRepository { @VisibleForTesting Observable<List<Transmission>> getSurveyTransmissions(@NonNull String surveyId) { return getFormIds(surveyId) .flatMap(new Function<List<String>, Observable<List<Transmission>>>() { @Override public Observable<List<Transmission>> apply(List<String> formIds) { return Observable.fromIterable(formIds) .flatMap(new Function<String, Observable<List<Transmission>>>() { @Override public Observable<List<Transmission>> apply(String formId) { return getFormTransmissions(formId); } }) .flatMapIterable( new Function<List<Transmission>, List<Transmission>>() { @Override public List<Transmission> apply( List<Transmission> transmissions) { return transmissions; } }) .toList() .toObservable(); } }); } @Inject  SurveyDataRepository(DataSourceFactory dataSourceFactory,
                                DataPointMapper dataPointMapper, RestApi restApi, SurveyMapper surveyMapper,
                                UserMapper userMapper, TransmissionFilenameMapper transmissionFilenameMapper,
                                TransmissionMapper transmissionMapper, FormInstanceMapper formInstanceMapper,
                                FormIdMapper formIdMapper, FormInstanceMetadataMapper formInstanceMetadataMapper,
                                S3RestApi s3RestApi); @Override Observable<List<Survey>> getSurveys(); @Override Observable<List<DataPoint>> getDataPoints(Long surveyGroupId,
            Double latitude, Double longitude, Integer orderBy); @Override Single<DataPoint> getDataPoint(final String datapointId); @Override Observable<Boolean> deleteSurvey(long surveyToDeleteId); @Override Observable<List<User>> getUsers(); @Override Observable<Boolean> editUser(User user); @Override Observable<Boolean> deleteUser(User user); @Override Observable<Long> createUser(String userName); @Override Observable<User> getUser(Long userId); @Override Observable<Boolean> clearResponses(); @Override Observable<Boolean> clearAllData(); @Override Observable<Boolean> unSyncedTransmissionsExist(); @Override Observable<List<String>> getAllTransmissionFileNames(); @Override Observable<List<String>> getFormIds(String surveyId); @Override Observable<List<String>> getFormIds(); @Override Observable<Set<String>> processTransmissions(final String deviceId,
            @NonNull String surveyId); @Override Observable<Set<String>> processTransmissions(final String deviceId); @Nullable @Override Completable setSurveyViewed(long surveyId); @Override Completable cleanDataPoints(Long surveyGroupId); @Override Single<List<InstanceIdUuid>> getSubmittedInstances(); @Override Completable setInstanceStatusToRequested(long id); @Override Single<List<Long>> getPendingSurveyInstances(); @Override Single<FormInstanceMetadata> getFormInstanceData(final Long instanceId,
            final String deviceId); @Override Completable createTransmissions(final Long instanceId, final String formId,
            Set<String> fileNames);  }
@Test public void shouldReturnExpectedSurveyTransmissions() { List<String> formIds = new ArrayList<>(2); formIds.add("1"); formIds.add("2"); when(mockFormIdMapper.mapToFormId(any(Cursor.class))).thenReturn(formIds); List<Transmission> oneTransmissionList = new ArrayList<>(1); oneTransmissionList.add(new Transmission(0L, 0L, "1", null)); List<Transmission> twoTransmissionsList = new ArrayList<>(2); twoTransmissionsList.add(new Transmission(1L, 1L, "2", null)); twoTransmissionsList.add(new Transmission(2L, 2L, "2", null)); when(mockTransmissionMapper.transform(any(Cursor.class))).thenReturn(oneTransmissionList) .thenReturn(twoTransmissionsList); TestObserver observer = new TestObserver<List<Transmission>>(); surveyDataRepository.getSurveyTransmissions("abc").subscribe(observer); observer.assertNoErrors(); observer.assertValueCount(1); assertEquals(3, ((ArrayList) observer.values().get(0)).size()); }
@Test public void shouldReturnEmptySurveyTransmissionsForEmptyFormsList() { when(mockFormIdMapper.mapToFormId(any(Cursor.class))).thenReturn(Collections.emptyList()); TestObserver observer = new TestObserver<List<Transmission>>(); surveyDataRepository.getSurveyTransmissions("").subscribe(observer); observer.assertNoErrors(); observer.assertValueCount(1); assertEquals(0, ((ArrayList) observer.values().get(0)).size()); }
@Test public void shouldReturnEmptyTransmissionsIfNotFound() { List<String> formIds = new ArrayList<>(2); formIds.add("1"); formIds.add("2"); when(mockFormIdMapper.mapToFormId(any(Cursor.class))).thenReturn(formIds); when(mockTransmissionMapper.transform(any(Cursor.class))) .thenReturn(Collections.emptyList()); TestObserver observer = new TestObserver<List<Transmission>>(); surveyDataRepository.getSurveyTransmissions("abc").subscribe(observer); observer.assertNoErrors(); observer.assertValueCount(1); assertEquals(0, ((ArrayList) observer.values().get(0)).size()); }