Skip to content

Commit 9bbef94

Browse files
authored
Merge pull request #4143 from owncloud/technical/testing_datasources_3
[TECHNICAL] Unit tests for datasources classes - Part 3
2 parents 7f47c1c + 475412b commit 9bbef94

File tree

33 files changed

+1614
-399
lines changed

33 files changed

+1614
-399
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Summary
1010

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

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

36+
* Enhancement - Unit tests for datasources classes - Part 3: [#4072](https://github.com/owncloud/android/issues/4072)
37+
38+
Unit tests of the OCFolderBackupLocalDataSource, OCRemoteOAuthDataSource,
39+
OCRemoteShareeDataSource, OCLocalShareDataSource, OCRemoteShareDataSource,
40+
OCLocalSpacesDataSource, OCRemoteSpacesDataSource, OCLocalTransferDataSource,
41+
OCLocalUserDataSource, OCRemoteUserDataSource, OCRemoteWebFingerDatasource classes
42+
have been done and completed.
43+
44+
https://github.com/owncloud/android/issues/4072
45+
https://github.com/owncloud/android/pull/4143
46+
3547
* Enhancement - "Apply to all" when many name conflicts arise: [#4078](https://github.com/owncloud/android/issues/4078)
3648

3749
A new dialog has been created where a checkbox has been added to be able to select all the folders

changelog/unreleased/4143

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Enhancement: Unit tests for datasources classes - Part 3
2+
3+
Unit tests of the OCFolderBackupLocalDataSource, OCRemoteOAuthDataSource, OCRemoteShareeDataSource, OCLocalShareDataSource,
4+
OCRemoteShareDataSource, OCLocalSpacesDataSource, OCRemoteSpacesDataSource, OCLocalTransferDataSource, OCLocalUserDataSource,
5+
OCRemoteUserDataSource, OCRemoteWebFingerDatasource classes have been done and completed.
6+
7+
https://github.com/owncloud/android/issues/4072
8+
https://github.com/owncloud/android/pull/4143

owncloudApp/src/androidTest/java/com/owncloud/android/sharing/sharees/ui/SearchShareesFragmentTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SearchShareesFragmentTest {
5454
private val sharesLiveData = MutableLiveData<Event<UIResult<List<OCShare>>>>()
5555

5656
@Before
57-
fun init() {
57+
fun setUp() {
5858
every { shareViewModel.shares } returns sharesLiveData
5959

6060
stopKoin()

owncloudApp/src/main/java/com/owncloud/android/dependecyinjection/LocalDataSourceModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import com.owncloud.android.data.capabilities.datasources.LocalCapabilitiesDataS
3434
import com.owncloud.android.data.capabilities.datasources.implementation.OCLocalCapabilitiesDataSource
3535
import com.owncloud.android.data.files.datasources.LocalFileDataSource
3636
import com.owncloud.android.data.files.datasources.implementation.OCLocalFileDataSource
37-
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
38-
import com.owncloud.android.data.folderbackup.datasources.implementation.OCFolderBackupLocalDataSource
37+
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
38+
import com.owncloud.android.data.folderbackup.datasources.implementation.OCLocalFolderBackupDataSource
3939
import com.owncloud.android.data.providers.SharedPreferencesProvider
4040
import com.owncloud.android.data.providers.implementation.OCSharedPreferencesProvider
4141
import com.owncloud.android.data.sharing.shares.datasources.LocalShareDataSource
@@ -70,7 +70,7 @@ val localDataSourceModule = module {
7070
single<LocalStorageProvider> { ScopedStorageProvider(dataFolder, androidContext()) }
7171

7272
factory<LocalAuthenticationDataSource> { OCLocalAuthenticationDataSource(androidContext(), get(), get(), accountType) }
73-
factoryOf(::OCFolderBackupLocalDataSource) bind FolderBackupLocalDataSource::class
73+
factoryOf(::OCLocalFolderBackupDataSource) bind LocalFolderBackupDataSource::class
7474
factoryOf(::OCLocalAppRegistryDataSource) bind LocalAppRegistryDataSource::class
7575
factoryOf(::OCLocalCapabilitiesDataSource) bind LocalCapabilitiesDataSource::class
7676
factoryOf(::OCLocalFileDataSource) bind LocalFileDataSource::class

owncloudApp/src/main/java/com/owncloud/android/providers/FileContentProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ import com.owncloud.android.data.capabilities.datasources.implementation.OCLocal
5555
import com.owncloud.android.data.capabilities.datasources.implementation.OCLocalCapabilitiesDataSource.Companion.toModel
5656
import com.owncloud.android.data.capabilities.db.OCCapabilityEntity
5757
import com.owncloud.android.data.files.db.OCFileEntity
58-
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
59-
import com.owncloud.android.data.folderbackup.datasources.implementation.OCFolderBackupLocalDataSource
58+
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
59+
import com.owncloud.android.data.folderbackup.datasources.implementation.OCLocalFolderBackupDataSource
6060
import com.owncloud.android.data.migrations.CameraUploadsMigrationToRoom
6161
import com.owncloud.android.data.providers.SharedPreferencesProvider
6262
import com.owncloud.android.data.providers.implementation.OCSharedPreferencesProvider
@@ -973,8 +973,8 @@ class FileContentProvider(val executors: Executors = Executors()) : ContentProvi
973973
val pictureUploadsConfiguration = migrationToRoom.getPictureUploadsConfigurationPreferences(pictureUploadsTimestamp)
974974
val videoUploadsConfiguration = migrationToRoom.getVideoUploadsConfigurationPreferences(videoUploadsTimestamp)
975975

976-
val backupLocalDataSource: FolderBackupLocalDataSource =
977-
OCFolderBackupLocalDataSource(OwncloudDatabase.getDatabase(context!!).folderBackUpDao())
976+
val backupLocalDataSource: LocalFolderBackupDataSource =
977+
OCLocalFolderBackupDataSource(OwncloudDatabase.getDatabase(context!!).folderBackUpDao())
978978
// Insert camera uploads configuration in new database
979979
executors.diskIO().execute {
980980
pictureUploadsConfiguration?.let { backupLocalDataSource.saveFolderBackupConfiguration(it) }

owncloudData/src/androidTest/java/com/owncloud/android/data/authentication/datasources/implementation/OCLocalAuthenticationDataSourceTest.kt

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import com.owncloud.android.testutil.OC_SECURE_SERVER_INFO_BASIC_AUTH
5757
import com.owncloud.android.testutil.OC_USER_INFO
5858
import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION
5959
import io.mockk.every
60-
import io.mockk.mockkClass
60+
import io.mockk.mockk
6161
import io.mockk.spyk
6262
import io.mockk.verify
6363
import org.junit.Assert.assertEquals
@@ -73,11 +73,11 @@ class OCLocalAuthenticationDataSourceTest {
7373
val instantExecutorRule = InstantTaskExecutorRule()
7474

7575
private lateinit var ocLocalAuthenticationDataSource: OCLocalAuthenticationDataSource
76-
private val accountManager = mockkClass(AccountManager::class)
76+
private val accountManager = mockk<AccountManager>(relaxUnitFun = true)
7777
private val preferencesProvider = spyk<SharedPreferencesProvider>()
7878

7979
@Before
80-
fun init() {
80+
fun setUp() {
8181
val context = InstrumentationRegistry.getInstrumentation().targetContext
8282

8383
ocLocalAuthenticationDataSource = OCLocalAuthenticationDataSource(
@@ -86,6 +86,13 @@ class OCLocalAuthenticationDataSourceTest {
8686
preferencesProvider,
8787
OC_ACCOUNT.type
8888
)
89+
getAccountsByType(OC_ACCOUNT.type, arrayOf(OC_ACCOUNT))
90+
}
91+
92+
private fun getAccountsByType(accountType: String, accounts: Array<Account>) {
93+
every {
94+
accountManager.getAccountsByType(accountType)
95+
} returns accounts
8996
}
9097

9198
@Test
@@ -102,7 +109,7 @@ class OCLocalAuthenticationDataSourceTest {
102109
null
103110
)
104111

105-
val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
112+
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)
106113

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

116123
@Test(expected = AccountNotNewException::class)
117124
fun addBasicAccountAlreadyExistsNoUpdate() {
118-
every {
119-
accountManager.getAccountsByType(OC_ACCOUNT.type)
120-
} returns arrayOf(OC_ACCOUNT) // The account is already there
121125

122126
ocLocalAuthenticationDataSource.addBasicAccount(
123127
OC_ACCOUNT_ID,
@@ -131,17 +135,6 @@ class OCLocalAuthenticationDataSourceTest {
131135

132136
@Test
133137
fun addBasicAccountAlreadyExistsUpdateSameUsername() {
134-
every {
135-
accountManager.getAccountsByType(OC_ACCOUNT.type)
136-
} returns arrayOf(OC_ACCOUNT) // The account is already there
137-
138-
every {
139-
accountManager.setPassword(any(), any())
140-
} returns Unit
141-
142-
every {
143-
accountManager.setUserData(any(), any(), any())
144-
} returns Unit
145138

146139
mockSelectedAccountNameInPreferences()
147140

@@ -167,10 +160,6 @@ class OCLocalAuthenticationDataSourceTest {
167160
@Test
168161
fun addBasicAccountAlreadyExistsUpdateDifferentUsername() {
169162

170-
every {
171-
accountManager.setUserData(any(), any(), any())
172-
} returns Unit
173-
174163
mockSelectedAccountNameInPreferences()
175164

176165
try {
@@ -198,10 +187,6 @@ class OCLocalAuthenticationDataSourceTest {
198187
mockRegularAccountCreationFlow()
199188
mockSelectedAccountNameInPreferences()
200189

201-
every {
202-
accountManager.setAuthToken(any(), any(), any())
203-
} returns Unit
204-
205190
val newAccountName = ocLocalAuthenticationDataSource.addOAuthAccount(
206191
OC_ACCOUNT_ID,
207192
OC_REDIRECTION_PATH.lastPermanentLocation,
@@ -215,7 +200,7 @@ class OCLocalAuthenticationDataSourceTest {
215200
OC_CLIENT_REGISTRATION
216201
)
217202

218-
val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
203+
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)
219204

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

232217
@Test(expected = AccountNotNewException::class)
233218
fun addOAuthAccountAlreadyExistsNoUpdate() {
234-
every {
235-
accountManager.getAccountsByType(OC_ACCOUNT.type)
236-
} returns arrayOf(OC_ACCOUNT) // The account is already there
237219

238220
ocLocalAuthenticationDataSource.addOAuthAccount(
239221
OC_ACCOUNT_ID,
@@ -251,17 +233,6 @@ class OCLocalAuthenticationDataSourceTest {
251233

252234
@Test
253235
fun addOAuthAccountAlreadyExistsUpdateSameUsername() {
254-
every {
255-
accountManager.getAccountsByType(OC_ACCOUNT.type)
256-
} returns arrayOf(OC_ACCOUNT) // The account is already there
257-
258-
every {
259-
accountManager.setUserData(any(), any(), any())
260-
} returns Unit
261-
262-
every {
263-
accountManager.setAuthToken(any(), any(), any())
264-
} returns Unit
265236

266237
mockSelectedAccountNameInPreferences()
267238

@@ -292,14 +263,6 @@ class OCLocalAuthenticationDataSourceTest {
292263
@Test
293264
fun addOAuthAccountAlreadyExistsUpdateDifferentUsername() {
294265

295-
every {
296-
accountManager.setUserData(any(), any(), any())
297-
} returns Unit
298-
299-
every {
300-
accountManager.setAuthToken(any(), any(), any())
301-
} returns Unit
302-
303266
mockSelectedAccountNameInPreferences()
304267

305268
try {
@@ -337,9 +300,6 @@ class OCLocalAuthenticationDataSourceTest {
337300

338301
@Test
339302
fun supportsOAuthOk() {
340-
every {
341-
accountManager.getAccountsByType(OC_ACCOUNT.type)
342-
} returns arrayOf(OC_ACCOUNT)
343303

344304
every {
345305
accountManager.getUserData(OC_ACCOUNT, KEY_SUPPORTS_OAUTH2)
@@ -355,18 +315,13 @@ class OCLocalAuthenticationDataSourceTest {
355315

356316
@Test(expected = AccountNotFoundException::class)
357317
fun supportsOAuthAccountNotFound() {
358-
every {
359-
accountManager.getAccountsByType(OC_ACCOUNT.type)
360-
} returns arrayOf() // That account does not exist
318+
getAccountsByType(OC_ACCOUNT.type, arrayOf())// That account does not exist
361319

362320
ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT.name)
363321
}
364322

365323
@Test
366324
fun getBaseUrlOk() {
367-
every {
368-
accountManager.getAccountsByType(OC_ACCOUNT.type)
369-
} returns arrayOf(OC_ACCOUNT)
370325

371326
every {
372327
accountManager.getUserData(OC_ACCOUNT, KEY_OC_BASE_URL)
@@ -382,9 +337,7 @@ class OCLocalAuthenticationDataSourceTest {
382337

383338
@Test(expected = AccountNotFoundException::class)
384339
fun getBaseUrlAccountNotFound() {
385-
every {
386-
accountManager.getAccountsByType(OC_ACCOUNT.type)
387-
} returns arrayOf() // That account does not exist
340+
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist
388341

389342
ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT.name)
390343
}
@@ -402,9 +355,7 @@ class OCLocalAuthenticationDataSourceTest {
402355

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

409360
// Step 2: Add new account
410361
every {

owncloudData/src/main/java/com/owncloud/android/data/folderbackup/OCFolderBackupRepository.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@
1818
*/
1919
package com.owncloud.android.data.folderbackup
2020

21-
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
21+
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
2222
import com.owncloud.android.domain.camerauploads.FolderBackupRepository
2323
import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguration
2424
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
2525
import kotlinx.coroutines.flow.Flow
2626

2727
class OCFolderBackupRepository(
28-
private val folderBackupLocalDataSource: FolderBackupLocalDataSource
28+
private val localFolderBackupDataSource: LocalFolderBackupDataSource
2929
) : FolderBackupRepository {
3030

3131
override fun getCameraUploadsConfiguration(): CameraUploadsConfiguration? =
32-
folderBackupLocalDataSource.getCameraUploadsConfiguration()
32+
localFolderBackupDataSource.getCameraUploadsConfiguration()
3333

3434
override fun getFolderBackupConfigurationByNameAsFlow(name: String): Flow<FolderBackUpConfiguration?> =
35-
folderBackupLocalDataSource.getFolderBackupConfigurationByNameAsFlow(name)
35+
localFolderBackupDataSource.getFolderBackupConfigurationByNameAsFlow(name)
3636

3737
override fun saveFolderBackupConfiguration(folderBackUpConfiguration: FolderBackUpConfiguration) {
38-
folderBackupLocalDataSource.saveFolderBackupConfiguration(folderBackUpConfiguration)
38+
localFolderBackupDataSource.saveFolderBackupConfiguration(folderBackUpConfiguration)
3939
}
4040

4141
override fun resetFolderBackupConfigurationByName(name: String) =
42-
folderBackupLocalDataSource.resetFolderBackupConfigurationByName(name)
42+
localFolderBackupDataSource.resetFolderBackupConfigurationByName(name)
4343

4444
}

owncloudData/src/main/java/com/owncloud/android/data/folderbackup/datasources/FolderBackupLocalDataSource.kt renamed to owncloudData/src/main/java/com/owncloud/android/data/folderbackup/datasources/LocalFolderBackupDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguratio
2222
import com.owncloud.android.domain.camerauploads.model.FolderBackUpConfiguration
2323
import kotlinx.coroutines.flow.Flow
2424

25-
interface FolderBackupLocalDataSource {
25+
interface LocalFolderBackupDataSource {
2626
fun getCameraUploadsConfiguration(): CameraUploadsConfiguration?
2727

2828
fun getFolderBackupConfigurationByNameAsFlow(name: String): Flow<FolderBackUpConfiguration?>

owncloudData/src/main/java/com/owncloud/android/data/folderbackup/datasources/implementation/OCFolderBackupLocalDataSource.kt renamed to owncloudData/src/main/java/com/owncloud/android/data/folderbackup/datasources/implementation/OCLocalFolderBackupDataSource.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919
package com.owncloud.android.data.folderbackup.datasources.implementation
2020

21-
import com.owncloud.android.data.folderbackup.datasources.FolderBackupLocalDataSource
21+
import androidx.annotation.VisibleForTesting
22+
import com.owncloud.android.data.folderbackup.datasources.LocalFolderBackupDataSource
2223
import com.owncloud.android.data.folderbackup.db.FolderBackUpEntity
2324
import com.owncloud.android.data.folderbackup.db.FolderBackupDao
2425
import com.owncloud.android.domain.camerauploads.model.CameraUploadsConfiguration
@@ -29,9 +30,9 @@ import com.owncloud.android.domain.camerauploads.model.UploadBehavior
2930
import kotlinx.coroutines.flow.Flow
3031
import kotlinx.coroutines.flow.map
3132

32-
class OCFolderBackupLocalDataSource(
33+
class OCLocalFolderBackupDataSource(
3334
private val folderBackupDao: FolderBackupDao,
34-
) : FolderBackupLocalDataSource {
35+
) : LocalFolderBackupDataSource {
3536

3637
override fun getCameraUploadsConfiguration(): CameraUploadsConfiguration? {
3738
val pictureUploadsConfiguration = folderBackupDao.getFolderBackUpConfigurationByName(pictureUploadsName)
@@ -59,17 +60,22 @@ class OCFolderBackupLocalDataSource(
5960
/**************************************************************************************************************
6061
************************************************* Mappers ****************************************************
6162
**************************************************************************************************************/
62-
private fun FolderBackUpEntity.toModel() =
63-
FolderBackUpConfiguration(
64-
accountName = accountName,
65-
behavior = UploadBehavior.fromString(behavior),
66-
sourcePath = sourcePath,
67-
uploadPath = uploadPath,
68-
wifiOnly = wifiOnly,
69-
chargingOnly = chargingOnly,
70-
lastSyncTimestamp = lastSyncTimestamp,
71-
name = name
72-
)
63+
64+
companion object {
65+
@VisibleForTesting
66+
fun FolderBackUpEntity.toModel() =
67+
FolderBackUpConfiguration(
68+
accountName = accountName,
69+
behavior = UploadBehavior.fromString(behavior),
70+
sourcePath = sourcePath,
71+
uploadPath = uploadPath,
72+
wifiOnly = wifiOnly,
73+
chargingOnly = chargingOnly,
74+
lastSyncTimestamp = lastSyncTimestamp,
75+
name = name
76+
)
77+
}
78+
7379

7480
private fun FolderBackUpConfiguration.toEntity(): FolderBackUpEntity =
7581
FolderBackUpEntity(

0 commit comments

Comments
 (0)