Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TECHNICAL] Unit tests for datasources classes - Part 3 #4143

Merged
merged 20 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Summary

* Change - Android library as a module instead of submodule: [#3962](https://github.com/owncloud/android/issues/3962)
* Enhancement - Koin DSL: [#3966](https://github.com/owncloud/android/pull/3966)
* Enhancement - Unit tests for datasources classes - Part 3: [#4072](https://github.com/owncloud/android/issues/4072)
* Enhancement - "Apply to all" when many name conflicts arise: [#4078](https://github.com/owncloud/android/issues/4078)
* Enhancement - "Share to" in oCIS accounts allows upload to any space: [#4088](https://github.com/owncloud/android/issues/4088)
* Enhancement - Use invoke operator to execute usecases: [#4179](https://github.com/owncloud/android/pull/4179)
Expand All @@ -32,6 +33,17 @@ Details

https://github.com/owncloud/android/pull/3966

* Enhancement - Unit tests for datasources classes - Part 3: [#4072](https://github.com/owncloud/android/issues/4072)

Unit tests of the OCFolderBackupLocalDataSource, OCRemoteOAuthDataSource,
OCRemoteShareeDataSource, OCLocalShareDataSource, OCRemoteShareDataSource,
OCLocalSpacesDataSource, OCRemoteSpacesDataSource, OCLocalTransferDataSource,
OCLocalUserDataSource, OCRemoteUserDataSource, OCRemoteWebFingerDatasource classes
have been done and completed.

https://github.com/owncloud/android/issues/4072
https://github.com/owncloud/android/pull/4143

* Enhancement - "Apply to all" when many name conflicts arise: [#4078](https://github.com/owncloud/android/issues/4078)

A new dialog has been created where a checkbox has been added to be able to select all the folders
Expand Down
8 changes: 8 additions & 0 deletions changelog/unreleased/4143
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Enhancement: Unit tests for datasources classes - Part 3

Unit tests of the OCFolderBackupLocalDataSource, OCRemoteOAuthDataSource, OCRemoteShareeDataSource, OCLocalShareDataSource,
OCRemoteShareDataSource, OCLocalSpacesDataSource, OCRemoteSpacesDataSource, OCLocalTransferDataSource, OCLocalUserDataSource,
OCRemoteUserDataSource, OCRemoteWebFingerDatasource classes have been done and completed.

https://github.com/owncloud/android/issues/4072
https://github.com/owncloud/android/pull/4143
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SearchShareesFragmentTest {
private val sharesLiveData = MutableLiveData<Event<UIResult<List<OCShare>>>>()

@Before
fun init() {
fun setUp() {
every { shareViewModel.shares } returns sharesLiveData

stopKoin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import com.owncloud.android.data.capabilities.datasources.LocalCapabilitiesDataS
import com.owncloud.android.data.capabilities.datasources.implementation.OCLocalCapabilitiesDataSource
import com.owncloud.android.data.files.datasources.LocalFileDataSource
import com.owncloud.android.data.files.datasources.implementation.OCLocalFileDataSource
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
import com.owncloud.android.data.folderbackup.datasources.implementation.OCFolderBackupLocalDataSource
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
import com.owncloud.android.data.folderbackup.datasources.implementation.OCLocalFolderBackupDataSource
import com.owncloud.android.data.providers.SharedPreferencesProvider
import com.owncloud.android.data.providers.implementation.OCSharedPreferencesProvider
import com.owncloud.android.data.sharing.shares.datasources.LocalShareDataSource
Expand Down Expand Up @@ -70,7 +70,7 @@ val localDataSourceModule = module {
single<LocalStorageProvider> { ScopedStorageProvider(dataFolder, androidContext()) }

factory<LocalAuthenticationDataSource> { OCLocalAuthenticationDataSource(androidContext(), get(), get(), accountType) }
factoryOf(::OCFolderBackupLocalDataSource) bind FolderBackupLocalDataSource::class
factoryOf(::OCLocalFolderBackupDataSource) bind LocalFolderBackupDataSource::class
factoryOf(::OCLocalAppRegistryDataSource) bind LocalAppRegistryDataSource::class
factoryOf(::OCLocalCapabilitiesDataSource) bind LocalCapabilitiesDataSource::class
factoryOf(::OCLocalFileDataSource) bind LocalFileDataSource::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ import com.owncloud.android.data.capabilities.datasources.implementation.OCLocal
import com.owncloud.android.data.capabilities.datasources.implementation.OCLocalCapabilitiesDataSource.Companion.toModel
import com.owncloud.android.data.capabilities.db.OCCapabilityEntity
import com.owncloud.android.data.files.db.OCFileEntity
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
import com.owncloud.android.data.folderbackup.datasources.implementation.OCFolderBackupLocalDataSource
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
import com.owncloud.android.data.folderbackup.datasources.implementation.OCLocalFolderBackupDataSource
import com.owncloud.android.data.migrations.CameraUploadsMigrationToRoom
import com.owncloud.android.data.providers.SharedPreferencesProvider
import com.owncloud.android.data.providers.implementation.OCSharedPreferencesProvider
Expand Down Expand Up @@ -973,8 +973,8 @@ class FileContentProvider(val executors: Executors = Executors()) : ContentProvi
val pictureUploadsConfiguration = migrationToRoom.getPictureUploadsConfigurationPreferences(pictureUploadsTimestamp)
val videoUploadsConfiguration = migrationToRoom.getVideoUploadsConfigurationPreferences(videoUploadsTimestamp)

val backupLocalDataSource: FolderBackupLocalDataSource =
OCFolderBackupLocalDataSource(OwncloudDatabase.getDatabase(context!!).folderBackUpDao())
val backupLocalDataSource: LocalFolderBackupDataSource =
OCLocalFolderBackupDataSource(OwncloudDatabase.getDatabase(context!!).folderBackUpDao())
// Insert camera uploads configuration in new database
executors.diskIO().execute {
pictureUploadsConfiguration?.let { backupLocalDataSource.saveFolderBackupConfiguration(it) }
Expand Down
JuancaG05 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_BASIC_AUTH
import com.owncloud.android.testutil.OC_USER_INFO
import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION
import io.mockk.every
import io.mockk.mockkClass
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert.assertEquals
Expand All @@ -73,11 +73,11 @@ class OCLocalAuthenticationDataSourceTest {
val instantExecutorRule = InstantTaskExecutorRule()

private lateinit var ocLocalAuthenticationDataSource: OCLocalAuthenticationDataSource
private val accountManager = mockkClass(AccountManager::class)
private val accountManager = mockk<AccountManager>(relaxUnitFun = true)
private val preferencesProvider = spyk<SharedPreferencesProvider>()

@Before
fun init() {
fun setUp() {
val context = InstrumentationRegistry.getInstrumentation().targetContext

ocLocalAuthenticationDataSource = OCLocalAuthenticationDataSource(
Expand All @@ -86,6 +86,13 @@ class OCLocalAuthenticationDataSourceTest {
preferencesProvider,
OC_ACCOUNT.type
)
getAccountsByType(OC_ACCOUNT.type, arrayOf(OC_ACCOUNT))
}

private fun getAccountsByType(accountType: String, accounts: Array<Account>) {
every {
accountManager.getAccountsByType(accountType)
} returns accounts
}

@Test
Expand All @@ -102,7 +109,7 @@ class OCLocalAuthenticationDataSourceTest {
null
)

val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)

// One for checking if the account exists and another one for getting the new account
verifyAccountsByTypeAreGot(newAccount.type, 2)
Expand All @@ -115,9 +122,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotNewException::class)
fun addBasicAccountAlreadyExistsNoUpdate() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

ocLocalAuthenticationDataSource.addBasicAccount(
OC_ACCOUNT_ID,
Expand All @@ -131,17 +135,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun addBasicAccountAlreadyExistsUpdateSameUsername() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

every {
accountManager.setPassword(any(), any())
} returns Unit

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

Expand All @@ -167,10 +160,6 @@ class OCLocalAuthenticationDataSourceTest {
@Test
fun addBasicAccountAlreadyExistsUpdateDifferentUsername() {

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

try {
Expand Down Expand Up @@ -198,10 +187,6 @@ class OCLocalAuthenticationDataSourceTest {
mockRegularAccountCreationFlow()
mockSelectedAccountNameInPreferences()

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

val newAccountName = ocLocalAuthenticationDataSource.addOAuthAccount(
OC_ACCOUNT_ID,
OC_REDIRECTION_PATH.lastPermanentLocation,
Expand All @@ -215,7 +200,7 @@ class OCLocalAuthenticationDataSourceTest {
OC_CLIENT_REGISTRATION
)

val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)

// One for checking if the account exists and another one for getting the new account
verifyAccountsByTypeAreGot(newAccount.type, 2)
Expand All @@ -231,9 +216,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotNewException::class)
fun addOAuthAccountAlreadyExistsNoUpdate() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

ocLocalAuthenticationDataSource.addOAuthAccount(
OC_ACCOUNT_ID,
Expand All @@ -251,17 +233,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun addOAuthAccountAlreadyExistsUpdateSameUsername() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

Expand Down Expand Up @@ -292,14 +263,6 @@ class OCLocalAuthenticationDataSourceTest {
@Test
fun addOAuthAccountAlreadyExistsUpdateDifferentUsername() {

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

try {
Expand Down Expand Up @@ -337,9 +300,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun supportsOAuthOk() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT)

every {
accountManager.getUserData(OC_ACCOUNT, KEY_SUPPORTS_OAUTH2)
Expand All @@ -355,18 +315,13 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotFoundException::class)
fun supportsOAuthAccountNotFound() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf() // That account does not exist
getAccountsByType(OC_ACCOUNT.type, arrayOf())// That account does not exist

ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT.name)
}

@Test
fun getBaseUrlOk() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT)

every {
accountManager.getUserData(OC_ACCOUNT, KEY_OC_BASE_URL)
Expand All @@ -382,9 +337,7 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotFoundException::class)
fun getBaseUrlAccountNotFound() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf() // That account does not exist
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist

ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT.name)
}
Expand All @@ -402,9 +355,7 @@ class OCLocalAuthenticationDataSourceTest {

private fun mockRegularAccountCreationFlow() {
// Step 1: Get accounts to know if the current account exists
every {
accountManager.getAccountsByType("owncloud")
} returns arrayOf() // There's no accounts yet
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // There's no accounts yet

// Step 2: Add new account
every {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
*/
package com.owncloud.android.data.folderbackup

import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
import com.owncloud.android.domain.camerauploads.FolderBackupRepository
import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguration
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
import kotlinx.coroutines.flow.Flow

class OCFolderBackupRepository(
private val folderBackupLocalDataSource: FolderBackupLocalDataSource
private val localFolderBackupDataSource: LocalFolderBackupDataSource
) : FolderBackupRepository {

override fun getCameraUploadsConfiguration(): CameraUploadsConfiguration? =
folderBackupLocalDataSource.getCameraUploadsConfiguration()
localFolderBackupDataSource.getCameraUploadsConfiguration()

override fun getFolderBackupConfigurationByNameAsFlow(name: String): Flow<FolderBackUpConfiguration?> =
folderBackupLocalDataSource.getFolderBackupConfigurationByNameAsFlow(name)
localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(name)

override fun saveFolderBackupConfiguration(folderBackUpConfiguration: FolderBackUpConfiguration) {
folderBackupLocalDataSource.saveFolderBackupConfiguration(folderBackUpConfiguration)
localFolderBackupDataSource.saveFolderBackupConfiguration(folderBackUpConfiguration)
}

override fun resetFolderBackupConfigurationByName(name: String) =
folderBackupLocalDataSource.resetFolderBackupConfigurationByName(name)
localFolderBackupDataSource.resetFolderBackupConfigurationByName(name)

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguratio
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
import kotlinx.coroutines.flow.Flow

interface FolderBackupLocalDataSource {
interface LocalFolderBackupDataSource {
fun getCameraUploadsConfiguration(): CameraUploadsConfiguration?

fun getFolderBackupConfigurationByNameAsFlow(name: String): Flow<FolderBackUpConfiguration?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package com.owncloud.android.data.folderbackup.datasources.implementation

import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
import androidx.annotation.VisibleForTesting
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
import com.owncloud.android.data.folderbackup.db.FolderBackUpEntity
import com.owncloud.android.data.folderbackup.db.FolderBackupDao
import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguration
Expand All @@ -29,9 +30,9 @@ import com.owncloud.android.domain.camerauploads.model.UploadBehavior
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class OCFolderBackupLocalDataSource(
class OCLocalFolderBackupDataSource(
private val folderBackupDao: FolderBackupDao,
) : FolderBackupLocalDataSource {
) : LocalFolderBackupDataSource {

override fun getCameraUploadsConfiguration(): CameraUploadsConfiguration? {
val pictureUploadsConfiguration = folderBackupDao.getFolderBackUpConfigurationByName(pictureUploadsName)
Expand Down Expand Up @@ -59,17 +60,22 @@ class OCFolderBackupLocalDataSource(
/**************************************************************************************************************
************************************************* Mappers ****************************************************
**************************************************************************************************************/
private fun FolderBackUpEntity.toModel() =
FolderBackUpConfiguration(
accountName = accountName,
behavior = UploadBehavior.fromString(behavior),
sourcePath = sourcePath,
uploadPath = uploadPath,
wifiOnly = wifiOnly,
chargingOnly = chargingOnly,
lastSyncTimestamp = lastSyncTimestamp,
name = name
)

companion object {
@VisibleForTesting
fun FolderBackUpEntity.toModel() =
FolderBackUpConfiguration(
accountName = accountName,
behavior = UploadBehavior.fromString(behavior),
sourcePath = sourcePath,
uploadPath = uploadPath,
wifiOnly = wifiOnly,
chargingOnly = chargingOnly,
lastSyncTimestamp = lastSyncTimestamp,
name = name
)
}


private fun FolderBackUpConfiguration.toEntity(): FolderBackUpEntity =
FolderBackUpEntity(
Expand Down
Loading