SyncUpManager { public boolean syncStockCards() { try { List<StockMovementItem> stockMovementItems = stockMovementRepository.listUnSynced(); if (stockMovementItems.isEmpty()) { return false; } final String facilityId = UserInfoMgr.getInstance().getUser().getFacilityId(); List<StockMovementEntry> movementEntriesToSync = convertStockMovementItemsToStockMovementEntriesForSync(facilityId, stockMovementItems); if (movementEntriesToSync == null || movementEntriesToSync.isEmpty()) { new LMISException("SyncUpManager.movementEntriesToSync").reportToFabric(); return false; } SyncUpStockMovementDataSplitResponse response = lmisRestApi.syncUpStockMovementDataSplit(facilityId, movementEntriesToSync); List<StockMovementItem> shouldMarkSyncedItems = new ArrayList<>(); Set<String> syncFailedProduct = Sets.newHashSet(response.getErrorProductCodes()); for (StockMovementItem stockMovementItem : stockMovementItems) { if (CollectionUtils.isEmpty(syncFailedProduct) || !syncFailedProduct.contains(stockMovementItem.getStockCard().getProduct().getCode())) { shouldMarkSyncedItems.add(stockMovementItem); } } markStockDataSynced(shouldMarkSyncedItems); syncErrorsRepository.deleteBySyncTypeAndObjectId(SyncType.StockCards, 0L); if (!response.getErrorProductCodes().isEmpty()) { syncErrorsRepository.deleteBySyncTypeAndObjectId(SyncType.SyncMovement, 2L); saveStockMovementErrors(response); return false; } sharedPreferenceMgr.setStockMovementSyncError(""); Log.d(TAG, "===> SyncStockMovement : synced"); return true; } catch (LMISException exception) { new LMISException(exception, "SyncUpManager.syncStockCards").reportToFabric(); syncErrorsRepository.save(new SyncError(exception.getMessage(), SyncType.StockCards, 0L)); Log.e(TAG, "===> SyncStockMovement : synced failed ->" + exception.getMessage()); return false; } } SyncUpManager(); boolean syncRnr(); boolean fakeSyncRnr(); boolean syncStockCards(); boolean fakeSyncStockCards(); void syncUpUnSyncedStockCardCodes(); void fakeSyncUpUnSyncedStockCardCodes(); void syncAppVersion(); void syncArchivedProducts(); void syncUpCmms(); void fakeSyncUpCmms(); boolean syncDeleteMovement(); void syncRapidTestForms(); void fakeSyncRapidTestForms();  }
@Test public void shouldSyncUnSyncedStockMovementData() throws LMISException, ParseException { StockCard stockCard = createTestStockCardData(); SyncUpStockMovementDataSplitResponse response = new SyncUpStockMovementDataSplitResponse(); response.setErrorProductCodes(newArrayList()); when(mockedLmisRestApi.syncUpStockMovementDataSplit(any(String.class), any(List.class))).thenReturn(response); syncUpManager.syncStockCards(); stockRepository.refresh(stockCard); List<StockMovementItem> items = newArrayList(stockCard.getForeignStockMovementItems()); assertThat(items.size(), is(2)); assertThat(items.get(0).isSynced(), is(true)); assertThat(items.get(1).isSynced(), is(true)); verify(mockedSyncErrorsRepository).deleteBySyncTypeAndObjectId(any(SyncType.class), anyLong()); }
@Test public void shouldDisplaySyncedErrorProduct() throws LMISException, ParseException { StockCard stockCard = createTestStockCardData(); SyncUpStockMovementDataSplitResponse response = new SyncUpStockMovementDataSplitResponse(); response.setErrorProductCodes(newArrayList("product1", "product2")); when(mockedLmisRestApi.syncUpStockMovementDataSplit(any(String.class), any(List.class))).thenReturn(response); syncUpManager.syncStockCards(); stockRepository.refresh(stockCard); List<StockMovementItem> items = newArrayList(stockCard.getForeignStockMovementItems()); assertThat(items.size(), is(2)); assertThat(items.get(0).isSynced(), is(true)); assertThat(items.get(1).isSynced(), is(true)); verify(mockedSyncErrorsRepository, times(2)).deleteBySyncTypeAndObjectId(any(SyncType.class), anyLong()); }
@Test public void shouldSaveSyncErrorWhenUnSyncedStockMovementDataFail() throws LMISException, SQLException, ParseException { createTestStockCardData(); doThrow(new LMISException("mocked exception")).when(mockedLmisRestApi).syncUpStockMovementDataSplit(anyString(), anyList()); syncUpManager.syncStockCards(); verify(mockedSyncErrorsRepository).save(any(SyncError.class)); }
@Test public void shouldNotMarkAsSyncedWhenStockMovementSyncFailed() throws LMISException, ParseException { StockCard stockCard = createTestStockCardData(); doThrow(new RuntimeException("Sync Failed")).when(mockedLmisRestApi).syncUpStockMovementDataSplit(anyString(), anyList()); try { syncUpManager.syncStockCards(); } catch (RuntimeException e) { } stockRepository.refresh(stockCard); List<StockMovementItem> items = newArrayList(stockCard.getForeignStockMovementItems()); assertThat(items.size(), is(2)); assertThat(items.get(0).isSynced(), is(false)); assertThat(items.get(1).isSynced(), is(false)); }