RealmAssetHelper { public void loadDatabaseToStorage(String databaseFolder, String databaseName, IRealmAssetHelperStorageListener listener) throws RuntimeException { mOsUtil.clearCache(); if (mOsUtil.isEmpty(databaseName)) { throw new RuntimeException("The database name is empty"); } if (!mOsUtil.isDatabaseAssetExists(mContext, databaseFolder, databaseName)) { throw new RuntimeException("An asset for requested database doesn't exist"); } Integer currentDbVersion = mOsUtil.getCurrentDbVersion(mContext, databaseName); int assetsDbVersion = mOsUtil.getAssetsDbVersion(mContext, databaseFolder, databaseName); if (currentDbVersion == null) { String path = mOsUtil.loadDatabaseToLocalStorage(mContext, databaseFolder, databaseName); if (mOsUtil.isEmpty(path)) { throw new RuntimeException("Can't find copied file"); } mOsUtil.storeDatabaseVersion(mContext, assetsDbVersion, databaseName); if (listener != null) { listener.onLoadedToStorage(path, RealmAssetHelperStatus.INSTALLED); } } else { if (assetsDbVersion > currentDbVersion) { String path = mOsUtil.loadDatabaseToLocalStorage(mContext, databaseFolder, databaseName); if (mOsUtil.isEmpty(path)) { throw new RuntimeException("Can't find copied file"); } mOsUtil.storeDatabaseVersion(mContext, assetsDbVersion, databaseName); if (listener != null) { listener.onLoadedToStorage(path, RealmAssetHelperStatus.UPDATED); } } else { String path = mOsUtil.getFileNameForDatabase(mContext, databaseName); if (mOsUtil.isEmpty(path)) { throw new RuntimeException("Can't find copied file"); } if (listener != null) { listener.onLoadedToStorage(path, RealmAssetHelperStatus.IGNORED); } } } } protected  RealmAssetHelper(); static RealmAssetHelper getInstance(Context context); void loadDatabaseToStorage(String databaseFolder, String databaseName, IRealmAssetHelperStorageListener listener);  }
@Test(expected = RuntimeException.class) public void onEmptyDatabaseName_shouldThrowRuntimeException() throws RuntimeException { realmAssetHelper.loadDatabaseToStorage(null, null, null); }
@Test public void onDatabaseUpdate_versionShouldBeSetForCorrectDatabase() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(1); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.times(1)).storeDatabaseVersion(context, 2, TestConstants.DB_NAME); }
@Test(expected = RuntimeException.class) public void onLoadDatabaseToStorageFileNotFound_exceptionShouldBeThrown() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(1); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); when(osUtil.loadDatabaseToLocalStorage(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(null); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); }
@Test(expected = RuntimeException.class) public void onMissingDbAsset_shouldThrowRuntimeException() throws RuntimeException { Mockito.when(osUtil.isDatabaseAssetExists(context, TestConstants.DB_FOLDER, TestConstants.DB_NAME)).thenReturn(false); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); }
@Test public void onFreshInstall_databaseVersionShouldBeStored() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(null); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.times(1)).storeDatabaseVersion(context, 2, TestConstants.DB_NAME); }
@Test public void onDatabaseUpdate_databaseVersionShouldBeStored() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(1); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.times(1)).storeDatabaseVersion(context, 2, TestConstants.DB_NAME); }
@Test public void onFreshAppInstall_databaseShouldBeLoadedToInternalStorage() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(null); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.times(1)).loadDatabaseToLocalStorage(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString()); }
@Test public void onDatabaseUpdate_databaseShouldBeLoadedToInternalStorage() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(1); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.times(1)).loadDatabaseToLocalStorage(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString()); }
@Test public void onSameDatabaseVersionArrived_databaseShouldNotBeLoadedToInternalStorage() { Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(2); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, null); Mockito.verify(osUtil, Mockito.never()).loadDatabaseToLocalStorage(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString()); }
@Test public void onFreshAppInstall_relevantCallbackShouldBeTriggered() { IRealmAssetHelperStorageListener listener = Mockito.mock(IRealmAssetHelperStorageListener.class); Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(null); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, listener); Mockito.verify(listener, Mockito.times(1)).onLoadedToStorage(TestConstants.FILE_PATH, RealmAssetHelperStatus.INSTALLED); }
@Test public void onDatabaseUpdate_relevantCallbackShouldBeTriggered() { IRealmAssetHelperStorageListener listener = Mockito.mock(IRealmAssetHelperStorageListener.class); Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(1); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, listener); Mockito.verify(listener, Mockito.times(1)).onLoadedToStorage(TestConstants.FILE_PATH, RealmAssetHelperStatus.UPDATED); }
@Test public void onSameDatabaseVersionArrived_relevantCallbackShouldBeTriggered() { IRealmAssetHelperStorageListener listener = Mockito.mock(IRealmAssetHelperStorageListener.class); Mockito.when(osUtil.getCurrentDbVersion(Mockito.any(Context.class), Mockito.anyString())).thenReturn(2); Mockito.when(osUtil.getAssetsDbVersion(Mockito.any(Context.class), Mockito.anyString(), Mockito.anyString())).thenReturn(2); realmAssetHelper.loadDatabaseToStorage(TestConstants.DB_FOLDER, TestConstants.DB_NAME, listener); Mockito.verify(listener, Mockito.times(1)).onLoadedToStorage(TestConstants.FILE_PATH, RealmAssetHelperStatus.IGNORED); }