SyncDownManager { public void syncDownServerData(Subscriber<SyncProgress> subscriber) { if (isSyncing) { return; } isSyncing = true; Observable.create((Observable.OnSubscribe<SyncProgress>) subscriber1 -> { try { SyncDownPrograms(subscriber1); syncDownService(subscriber1); syncDownReportType(subscriber1); syncDownProducts(subscriber1); syncDownLastMonthStockCards(subscriber1); syncDownRequisition(subscriber1); syncDownRapidTests(subscriber1); isSyncing = false; subscriber1.onCompleted(); } catch (LMISException e) { isSyncing = false; subscriber1.onError(e); } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber); } SyncDownManager(); void syncDownServerData(Subscriber<SyncProgress> subscriber); void syncDownServerData(); void fetchKitChangeProduct(); Observable<Void> saveStockCardsFromLastYear(final List<StockCard> stockCards); Observable<Void> saveStockCards(final List<StockCard> stockCards, Scheduler scheduler);  }
@Test public void shouldSyncDownServerData() throws Exception { mockSyncDownLatestProductResponse(); mockReportResponse(); mockRequisitionResponse(); mockStockCardsResponse(); mockRapidTestsResponse(); mockFetchProgramsResponse(); mockFetchPTVServiceResponse(); SyncServerDataSubscriber subscriber = new SyncServerDataSubscriber(); syncDownManager.syncDownServerData(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); assertThat(subscriber.syncProgresses.get(0), is(SyncingPrograms)); assertThat(subscriber.syncProgresses.get(1), is(ProgramSynced)); assertThat(subscriber.syncProgresses.get(2), is(SyncingServiceList)); assertThat(subscriber.syncProgresses.get(3), is(ServiceSynced)); assertThat(subscriber.syncProgresses.get(4), is(SyncingReportType)); assertThat(subscriber.syncProgresses.get(5), is(ReportTypeSynced)); assertThat(subscriber.syncProgresses.get(6), is(SyncingProduct)); assertThat(subscriber.syncProgresses.get(7), is(ProductSynced)); assertThat(subscriber.syncProgresses.get(8), is(SyncingStockCardsLastMonth)); assertThat(subscriber.syncProgresses.get(9), is(StockCardsLastMonthSynced)); assertThat(subscriber.syncProgresses.get(10), is(SyncingRequisition)); assertThat(subscriber.syncProgresses.get(11), is(RequisitionSynced)); assertThat(subscriber.syncProgresses.get(12), is(SyncingRapidTests)); assertThat(subscriber.syncProgresses.get(13), is(RapidTestsSynced)); }
@Test public void shouldOnlySyncOnceWhenInvokedTwice() throws Exception { mockReportResponse(); mockSyncDownLatestProductResponse(); mockRequisitionResponse(); mockStockCardsResponse(); mockRapidTestsResponse(); mockFetchProgramsResponse(); mockFetchPTVServiceResponse(); CountOnNextSubscriber firstEnterSubscriber = new CountOnNextSubscriber(); CountOnNextSubscriber laterEnterSubscriber = new CountOnNextSubscriber(); syncDownManager.syncDownServerData(firstEnterSubscriber); syncDownManager.syncDownServerData(laterEnterSubscriber); firstEnterSubscriber.awaitTerminalEvent(); firstEnterSubscriber.assertNoErrors(); laterEnterSubscriber.assertNoTerminalEvent(); assertThat(firstEnterSubscriber.syncProgresses.size(), is(14)); assertThat(laterEnterSubscriber.syncProgresses.size(), is(0)); }
@Test public void shouldSyncDownNewLatestProductList() throws Exception { mockReportResponse(); mockSyncDownLatestProductResponse(); mockRequisitionResponse(); mockStockCardsResponse(); mockRapidTestsResponse(); mockFetchProgramsResponse(); mockFetchPTVServiceResponse(); when(productRepository.getByCode(anyString())).thenReturn(new Product()); Program program = new Program(); when(programRepository.queryByCode("PR")).thenReturn(program); SyncServerDataSubscriber subscriber = new SyncServerDataSubscriber(); syncDownManager.syncDownServerData(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); verify(lmisRestApi).fetchLatestProducts(anyString()); verify(productRepository).batchCreateOrUpdateProducts(anyList()); verify(sharedPreferenceMgr).setLastSyncProductTime("today"); }