DriveDatabaseManager { @SuppressLint("CheckResult") public void syncDatabase() { final File filesDir = mContext.getExternalFilesDir(null); if (filesDir != null) { final File dbFile = new File(filesDir, DatabaseConstants.DATABASE_NAME); if (dbFile.exists()) { if (!mIsSyncInProgress.getAndSet(true)) { getSyncDatabaseObservable(dbFile) .observeOn(mObserveOnScheduler) .subscribeOn(mSubscribeOnScheduler) .subscribe(identifier -> { Logger.info(DriveDatabaseManager.this, "Successfully synced our database"); mGoogleDriveSyncMetadata.setDatabaseSyncIdentifier(identifier); mIsSyncInProgress.set(false); }, throwable -> { mIsSyncInProgress.set(false); mAnalytics.record(new ErrorEvent(DriveDatabaseManager.this, throwable)); Logger.error(DriveDatabaseManager.this, "Failed to sync our database", throwable); }); } else { Logger.debug(DriveDatabaseManager.this, "A sync is already in progress. Ignoring subsequent one for now"); } } else { Logger.error(DriveDatabaseManager.this, "Failed to find our main database"); } } else { Logger.error(DriveDatabaseManager.this, "Failed to find our main database storage directory"); } } DriveDatabaseManager(@NonNull Context context, @NonNull DriveStreamsManager driveTaskManager,
                                @NonNull GoogleDriveSyncMetadata googleDriveSyncMetadata,
                                @NonNull Analytics analytics);  DriveDatabaseManager(@NonNull Context context, @NonNull DriveStreamsManager driveTaskManager, @NonNull GoogleDriveSyncMetadata googleDriveSyncMetadata,
                                @NonNull Analytics analytics, @NonNull Scheduler observeOnScheduler, @NonNull Scheduler subscribeOnScheduler); @SuppressLint("CheckResult") void syncDatabase();  }
@Test public void syncDatabaseForTheFirstTime() { final Identifier identifier = new Identifier("newId"); when(mDriveStreamsManager.uploadFileToDrive(mDatabaseFile)).thenReturn(Single.just(identifier)); mDriveDatabaseManager.syncDatabase(); verify(mGoogleDriveSyncMetadata).setDatabaseSyncIdentifier(identifier); }
@Test public void syncExistingDatabase() { final Identifier identifier = new Identifier("oldId"); when(mGoogleDriveSyncMetadata.getDatabaseSyncIdentifier()).thenReturn(identifier); when(mDriveStreamsManager.updateDriveFile(identifier, mDatabaseFile)).thenReturn(Single.just(identifier)); mDriveDatabaseManager.syncDatabase(); verify(mGoogleDriveSyncMetadata).setDatabaseSyncIdentifier(identifier); }
@Test public void syncDatabaseError() { final Exception e = new Exception(); when(mDriveStreamsManager.uploadFileToDrive(mDatabaseFile)).thenReturn(Single.error(e)); mDriveDatabaseManager.syncDatabase(); verify(mGoogleDriveSyncMetadata, never()).setDatabaseSyncIdentifier(any(Identifier.class)); verify(mAnalytics).record(any(ErrorEvent.class)); }