DriveReceiptsManager { public synchronized void handleInsertOrUpdate(@NonNull final Receipt receipt) { if (!mIsInitializing.get()) { handleInsertOrUpdateInternal(receipt); } } DriveReceiptsManager(@NonNull TableController<Receipt> receiptsTableController, @NonNull TripsTable tripsTable,
                                @NonNull ReceiptsTable receiptsTable, @NonNull DriveStreamsManager driveTaskManager,
                                @NonNull DriveDatabaseManager driveDatabaseManager, @NonNull NetworkManager networkManager,
                                @NonNull Analytics analytics);  DriveReceiptsManager(@NonNull TableController<Receipt> receiptsTableController,
                                @NonNull TripsTable tripsTable,
                                @NonNull ReceiptsTable receiptsTable,
                                @NonNull DriveStreamsManager driveTaskManager,
                                @NonNull DriveDatabaseManager driveDatabaseManager,
                                @NonNull NetworkManager networkManager,
                                @NonNull Analytics analytics,
                                @NonNull DriveStreamMappings driveStreamMappings,
                                @NonNull ReceiptBuilderFactoryFactory receiptBuilderFactoryFactory,
                                @NonNull Scheduler observeOnScheduler,
                                @NonNull Scheduler subscribeOnScheduler); @SuppressLint("CheckResult") synchronized void syncReceipts(); synchronized void enable(); synchronized void disable(); synchronized void handleInsertOrUpdate(@NonNull final Receipt receipt); synchronized void handleDelete(@NonNull final Receipt receipt);  }
@Test public void handleInsertOrUpdateWithoutNetwork() { when(driveStreamsManager.uploadFileToDrive(syncState1, file)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(true); when(networkManager.isNetworkAvailable()).thenReturn(false); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test public void handleInsertOrUpdateForNewFile() { when(driveStreamsManager.uploadFileToDrive(syncState1, file)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(true); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); assertNotNull(receiptCaptor.getValue()); assertEquals(receipt1, updatedReceiptCaptor.getValue()); assertEquals(newSyncState1, updatedReceiptCaptor.getValue().getSyncState()); assertEquals(OperationFamilyType.Sync, operationMetadataCaptor.getValue().getOperationFamilyType()); }
@Test public void handleInsertOrUpdateForNewFileButStaleReceipt() { when(driveStreamsManager.uploadFileToDrive(syncState1, file)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(true); when(receiptsTable.findByPrimaryKey(RECEIPT_1_PRIMARY_KEY)).thenReturn(Single.just(mock(Receipt.class))); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test public void handleInsertOrUpdateForNonExistingNewFile() { when(driveStreamMappings.postInsertSyncState()).thenReturn(newSyncState1); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(false); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); assertNotNull(receiptCaptor.getValue()); assertEquals(receipt1, updatedReceiptCaptor.getValue()); assertEquals(newSyncState1, updatedReceiptCaptor.getValue().getSyncState()); assertEquals(OperationFamilyType.Sync, operationMetadataCaptor.getValue().getOperationFamilyType()); }
@Test public void handleInsertOrUpdateForNonExistingNewFileButStaleReceipt() { when(driveStreamMappings.postInsertSyncState()).thenReturn(newSyncState1); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(false); when(receiptsTable.findByPrimaryKey(RECEIPT_1_PRIMARY_KEY)).thenReturn(Single.just(mock(Receipt.class))); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test public void handleInsertWithoutFile() { when(driveStreamMappings.postInsertSyncState()).thenReturn(newSyncState1); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(null); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); assertNotNull(receiptCaptor.getValue()); assertEquals(receipt1, updatedReceiptCaptor.getValue()); assertEquals(newSyncState1, updatedReceiptCaptor.getValue().getSyncState()); assertEquals(OperationFamilyType.Sync, operationMetadataCaptor.getValue().getOperationFamilyType()); }
@Test public void handleInsertWithoutFileButStaleReceipt() { when(driveStreamMappings.postInsertSyncState()).thenReturn(newSyncState1); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(null); when(receiptsTable.findByPrimaryKey(RECEIPT_1_PRIMARY_KEY)).thenReturn(Single.just(mock(Receipt.class))); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test public void handleUpdateWithNewFile() { final Identifier identifier = new Identifier("id"); when(driveStreamsManager.updateDriveFile(syncState1, file)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(identifier); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(true); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); assertNotNull(receiptCaptor.getValue()); assertEquals(receipt1, updatedReceiptCaptor.getValue()); assertEquals(newSyncState1, updatedReceiptCaptor.getValue().getSyncState()); assertEquals(OperationFamilyType.Sync, operationMetadataCaptor.getValue().getOperationFamilyType()); }
@Test public void handleUpdateWithNewFileButStaleReceipt() { final Identifier identifier = new Identifier("id"); when(driveStreamsManager.updateDriveFile(syncState1, file)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(identifier); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(file); when(file.exists()).thenReturn(true); when(receiptsTable.findByPrimaryKey(RECEIPT_1_PRIMARY_KEY)).thenReturn(Single.just(mock(Receipt.class))); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test public void handleUpdateToDeleteExistingFile() { final Identifier identifier = new Identifier("id"); when(driveStreamsManager.deleteDriveFile(syncState1, false)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(identifier); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(null); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); assertNotNull(receiptCaptor.getValue()); assertEquals(receipt1, updatedReceiptCaptor.getValue()); assertEquals(newSyncState1, updatedReceiptCaptor.getValue().getSyncState()); assertEquals(OperationFamilyType.Sync, operationMetadataCaptor.getValue().getOperationFamilyType()); }
@Test public void handleUpdateToDeleteExistingFileButStaleReceipt() { final Identifier identifier = new Identifier("id"); when(driveStreamsManager.deleteDriveFile(syncState1, false)).thenReturn(Single.just(newSyncState1)); when(syncState1.getSyncId(SyncProvider.GoogleDrive)).thenReturn(identifier); when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(false); when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(false); when(receipt1.getFile()).thenReturn(null); when(receiptsTable.findByPrimaryKey(RECEIPT_1_PRIMARY_KEY)).thenReturn(Single.just(mock(Receipt.class))); driveReceiptsManager.handleInsertOrUpdate(receipt1); verify(receiptTableController, never()).update(receiptCaptor.capture(), updatedReceiptCaptor.capture(), operationMetadataCaptor.capture()); }
@Test(expected = IllegalArgumentException.class) public void handleInsertOrUpdateForIllegalSyncState() { when(syncState1.isSynced(SyncProvider.GoogleDrive)).thenReturn(true); driveReceiptsManager.handleInsertOrUpdate(receipt1); }
@Test(expected = IllegalArgumentException.class) public void handleInsertOrUpdateForIllegalMarkedForDeletionState() { when(syncState1.isMarkedForDeletion(SyncProvider.GoogleDrive)).thenReturn(true); driveReceiptsManager.handleInsertOrUpdate(receipt1); }