MissingAndDeletedDataRepository implements MissingAndDeletedRepository { @Override public Observable<Set<String>> downloadMissingAndDeleted(List<String> formIds, String deviceId) { return getPendingFiles(formIds, deviceId) .concatMap(new Function<FilteredFilesResult, Observable<Set<String>>>() { @Override public Observable<Set<String>> apply(final FilteredFilesResult filtered) { return saveMissing(filtered.getMissingFiles()) .concatMap(new Function<Boolean, Observable<Set<String>>>() { @Override public Observable<Set<String>> apply(Boolean ignored) { return saveDeletedForms(filtered.getDeletedForms()); } }); } }); } @Inject  MissingAndDeletedDataRepository(RestApi restApi, FilesResultMapper filesResultMapper,
            DataSourceFactory dataSourceFactory); @Override Observable<Set<String>> downloadMissingAndDeleted(List<String> formIds, String deviceId);  }
@Test public void testReturnEmptyData() { TestObserver observer = new TestObserver<Set<String>>(); MockResponse response = new MockResponse().setResponseCode(200) .setBody("{\"missingUnknown\":[],\"missingFiles\":[],\"deletedForms\":[]}"); mockWebServer.enqueue(response); List<String> forms = new ArrayList<>(1); forms.add("123"); missingAndDeletedDataRepository.downloadMissingAndDeleted(forms, "123").subscribe(observer); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoErrors(); observer.assertValueCount(1); }
@Test public void testReturnEmptyDataWhenEmptyJson() throws InterruptedException { TestObserver observer = new TestObserver<Set<String>>(); MockResponse response = new MockResponse().setResponseCode(200).setBody("{}"); mockWebServer.enqueue(response); List<String> forms = new ArrayList<>(1); forms.add("123"); missingAndDeletedDataRepository.downloadMissingAndDeleted(forms, "123").subscribe(observer); RecordedRequest request = mockWebServer.takeRequest(2, TimeUnit.SECONDS); assertTrue(request.getPath().startsWith("/devicenotification")); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoErrors(); observer.assertValueCount(1); }
@Test public void testReturnEmptyDataWhenErrorJson() throws InterruptedException { TestObserver observer = new TestObserver<Set<String>>(); MockResponse response = new MockResponse().setResponseCode(200).setBody("{abcd}"); mockWebServer.enqueue(response); List<String> forms = new ArrayList<>(1); forms.add("123"); missingAndDeletedDataRepository.downloadMissingAndDeleted(forms, "123").subscribe(observer); RecordedRequest request = mockWebServer.takeRequest(2, TimeUnit.SECONDS); assertTrue(request.getPath().startsWith("/devicenotification")); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoValues(); assertEquals(1, observer.errorCount()); }
@Test public void testErrorReturnWhenServerError() { TestObserver observer = new TestObserver<Set<String>>(); MockResponse response = new MockResponse().setResponseCode(500).setBody("Server Error"); mockWebServer.enqueue(response); List<String> forms = new ArrayList<>(1); forms.add("123"); missingAndDeletedDataRepository.downloadMissingAndDeleted(forms, "123").subscribe(observer); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoValues(); assertEquals(1, observer.errorCount()); }
@Test public void testErrorReturnWhenTimeout() { TestObserver observer = new TestObserver<Set<String>>(); MockResponse response = new MockResponse().setResponseCode(200) .throttleBody(1024, 1, TimeUnit.SECONDS); mockWebServer.enqueue(response); List<String> forms = new ArrayList<>(1); forms.add("123"); missingAndDeletedDataRepository.downloadMissingAndDeleted(forms, "123").subscribe(observer); observer.awaitTerminalEvent(2, TimeUnit.SECONDS); observer.assertNoValues(); assertEquals(1, observer.errorCount()); }