DriveStreamsManager implements GoogleApiClient.ConnectionCallbacks { @NonNull public Single<SyncState> updateDriveFile(@NonNull final SyncState currentSyncState, @NonNull final java.io.File file) { Preconditions.checkNotNull(currentSyncState); Preconditions.checkNotNull(file); return newBlockUntilConnectedCompletable() .andThen(updateDrive(currentSyncState, file)) .flatMap(driveFile -> Single.just(driveStreamMappings.postUpdateSyncState(driveFile))) .doOnError(throwable -> driveErrorStream.onNext(Optional.of(syncErrorTranslator.get(throwable)))); } DriveStreamsManager(@NonNull Context context,
                               @NonNull DriveServiceHelper driveServiceHelper,
                               @NonNull GoogleDriveSyncMetadata googleDriveSyncMetadata,
                               @NonNull Subject<Optional<Throwable>> driveErrorStream); @VisibleForTesting  DriveStreamsManager(@NonNull DriveDataStreams driveDataStreams,
                        @NonNull DriveStreamMappings driveStreamMappings,
                        @NonNull Subject<Optional<Throwable>> driveErrorStream,
                        @NonNull DriveThrowableToSyncErrorTranslator syncErrorTranslator); @Override void onConnected(@Nullable Bundle bundle); @Override void onConnectionSuspended(int cause); @NonNull Single<List<RemoteBackupMetadata>> getRemoteBackups(); @NonNull synchronized Single<FileList> getAllFiles(); @NonNull synchronized Single<FileList> getFilesInFolder(@NonNull final String folderId); @NonNull synchronized Single<FileList> getFilesInFolder(@NonNull final String driveFolderId, @NonNull final String fileName); @NonNull Single<SyncState> uploadFileToDrive(@NonNull final SyncState currentSyncState, @NonNull final java.io.File file); @NonNull Single<Identifier> uploadFileToDrive(@NonNull final java.io.File file); @NonNull Single<SyncState> updateDriveFile(@NonNull final SyncState currentSyncState, @NonNull final java.io.File file); @NonNull Single<Identifier> updateDriveFile(@NonNull final Identifier currentIdentifier, @NonNull final java.io.File file); @NonNull Single<SyncState> deleteDriveFile(@NonNull final SyncState currentSyncState, final boolean isFullDelete); @NonNull Single<File> renameBackup(@NonNull final Identifier identifier, @NonNull final String newFileName); @NonNull Single<Boolean> delete(@NonNull final Identifier identifier); void clearCachedData(); @NonNull Single<Optional<java.io.File>> download(@NonNull final String fileId, @NonNull final java.io.File downloadLocationFile);  }
@Test public void updateDriveFileWithSyncState() throws Exception { final java.io.File file = new java.io.File("/"); when(currentSyncState.getSyncId(SyncProvider.GoogleDrive)).thenReturn(identifier); when(driveDataStreams.updateFile(identifier, file)).thenReturn(Single.just(driveFile)); when(driveStreamMappings.postUpdateSyncState(driveFile)).thenReturn(newSyncState); final TestObserver<SyncState> testObserver = driveStreamsManager.updateDriveFile(currentSyncState, file).test(); testObserver.assertValue(newSyncState); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void updateDriveFileWithSyncStateWithoutIdentifier() throws Exception { final java.io.File file = new java.io.File("/"); when(currentSyncState.getSyncId(SyncProvider.GoogleDrive)).thenReturn(null); final TestObserver<SyncState> testObserver = driveStreamsManager.updateDriveFile(currentSyncState, file).test(); testObserver.assertNoValues(); testObserver.assertNotComplete(); testObserver.assertError(Exception.class); verify(driveErrorStream).onNext(Optional.of(criticalSyncError)); }
@Test public void updateDriveFileWithIdentifier() throws Exception { final java.io.File file = new java.io.File("/"); final String id = "id"; when(driveDataStreams.updateFile(identifier, file)).thenReturn(Single.just(driveFile)); when(driveFile.getId()).thenReturn(id); final TestObserver<Identifier> testObserver = driveStreamsManager.updateDriveFile(identifier, file).test(); testObserver.assertValue(new Identifier(id)); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void updateDriveFileWithIdentifierErrorIsForwarded() throws Exception { final java.io.File file = new java.io.File("/"); final Exception exception = new Exception("test"); when(driveDataStreams.updateFile(identifier, file)).thenReturn(Single.error(exception)); final TestObserver<Identifier> testObserver = driveStreamsManager.updateDriveFile(identifier, file).test(); testObserver.assertNoValues(); testObserver.assertNotComplete(); testObserver.assertError(Exception.class); verify(driveErrorStream).onNext(Optional.of(criticalSyncError)); }