Skip to content

Commit ab9d3a6

Browse files
authored
Merge pull request #4209 from owncloud/technical/testing_datasources_fixes
[TECHNICAL] Unit tests for datasources classes - Fixes
2 parents 4400ca9 + e5e6e23 commit ab9d3a6

52 files changed

Lines changed: 1714 additions & 1882 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ownCloud admins and users.
2929

3030
* Change - Android library as a module instead of submodule: [#3962](https://github.com/owncloud/android/issues/3962)
3131
* Enhancement - Koin DSL: [#3966](https://github.com/owncloud/android/pull/3966)
32+
* Enhancement - Unit tests for datasources classes - Part 1 & Fixes: [#4063](https://github.com/owncloud/android/issues/4063)
3233
* Enhancement - Unit tests for datasources classes - Part 3: [#4072](https://github.com/owncloud/android/issues/4072)
3334
* Enhancement - "Apply to all" when many name conflicts arise: [#4078](https://github.com/owncloud/android/issues/4078)
3435
* Enhancement - "Share to" in oCIS accounts allows upload to any space: [#4088](https://github.com/owncloud/android/issues/4088)
@@ -55,6 +56,17 @@ ownCloud admins and users.
5556

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

59+
* Enhancement - Unit tests for datasources classes - Part 1 & Fixes: [#4063](https://github.com/owncloud/android/issues/4063)
60+
61+
Unit tests for OCLocalAppRegistryDataSource, OCRemoteAppRegistryDataSource,
62+
OCLocalAuthenticationDataSource, OCRemoteAuthenticationDataSource,
63+
OCLocalCapabilitiesDataSource and OCRemoteCapabilitiesDataSource classes have been
64+
done and completed, and several fixes have been applied to all existent unit test classes for
65+
datasources.
66+
67+
https://github.com/owncloud/android/issues/4063
68+
https://github.com/owncloud/android/pull/4209
69+
5870
* Enhancement - Unit tests for datasources classes - Part 3: [#4072](https://github.com/owncloud/android/issues/4072)
5971

6072
Unit tests of the OCFolderBackupLocalDataSource, OCRemoteOAuthDataSource,

changelog/unreleased/4209

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Enhancement: Unit tests for datasources classes - Part 1 & Fixes
2+
3+
Unit tests for OCLocalAppRegistryDataSource, OCRemoteAppRegistryDataSource, OCLocalAuthenticationDataSource,
4+
OCRemoteAuthenticationDataSource, OCLocalCapabilitiesDataSource and OCRemoteCapabilitiesDataSource classes have
5+
been done and completed, and several fixes have been applied to all existent unit test classes for
6+
datasources.
7+
8+
https://github.com/owncloud/android/issues/4063
9+
https://github.com/owncloud/android/pull/4209

owncloudApp/src/androidTest/java/com/owncloud/android/files/details/FileDetailsFragmentTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.owncloud.android.presentation.files.operations.FileOperationsViewMode
1515
import com.owncloud.android.sharing.shares.ui.TestShareFileActivity
1616
import com.owncloud.android.testutil.OC_ACCOUNT
1717
import com.owncloud.android.testutil.OC_FILE
18-
import com.owncloud.android.testutil.OC_FILE_OC_AVAILABLE_OFFLINE_FILE
18+
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE
1919
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO
2020
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_SPACE
2121
import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_WITHOUT_PERSONAL_SPACE
@@ -44,7 +44,7 @@ class FileDetailsFragmentTest {
4444
private var currentFileWithoutPersonalSpace: MutableStateFlow<OCFileWithSyncInfo?> =
4545
MutableStateFlow(OC_FILE_WITH_SYNC_INFO_AND_WITHOUT_PERSONAL_SPACE)
4646
private var currentFileSyncInfo: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO)
47-
private var currentFileAvailableOffline: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_OC_AVAILABLE_OFFLINE_FILE)
47+
private var currentFileAvailableOffline: MutableStateFlow<OCFileWithSyncInfo?> = MutableStateFlow(OC_FILE_WITH_SYNC_INFO_AVAILABLE_OFFLINE)
4848

4949
@Before
5050
fun setUp() {
@@ -211,4 +211,4 @@ class FileDetailsFragmentTest {
211211
R.id.fdPathLabel.isDisplayed(displayed = showFdPathLabel)
212212
R.id.fdPath.isDisplayed(displayed = showFdPath)
213213
}
214-
}
214+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import com.owncloud.android.data.spaces.datasources.RemoteSpacesDataSource
4545
import com.owncloud.android.data.spaces.datasources.implementation.OCRemoteSpacesDataSource
4646
import com.owncloud.android.data.user.datasources.RemoteUserDataSource
4747
import com.owncloud.android.data.user.datasources.implementation.OCRemoteUserDataSource
48-
import com.owncloud.android.data.webfinger.datasources.RemoteWebFingerDatasource
49-
import com.owncloud.android.data.webfinger.datasources.implementation.OCRemoteWebFingerDatasource
48+
import com.owncloud.android.data.webfinger.datasources.RemoteWebFingerDataSource
49+
import com.owncloud.android.data.webfinger.datasources.implementation.OCRemoteWebFingerDataSource
5050
import com.owncloud.android.lib.common.ConnectionValidator
5151
import com.owncloud.android.lib.resources.oauth.services.OIDCService
5252
import com.owncloud.android.lib.resources.oauth.services.implementation.OCOIDCService
@@ -77,7 +77,7 @@ val remoteDataSourceModule = module {
7777
singleOf(::OCRemoteShareDataSource) bind RemoteShareDataSource::class
7878
singleOf(::OCRemoteShareeDataSource) bind RemoteShareeDataSource::class
7979
singleOf(::OCRemoteSpacesDataSource) bind RemoteSpacesDataSource::class
80-
singleOf(::OCRemoteWebFingerDatasource) bind RemoteWebFingerDatasource::class
80+
singleOf(::OCRemoteWebFingerDataSource) bind RemoteWebFingerDataSource::class
8181
single<RemoteUserDataSource> { OCRemoteUserDataSource(get(), androidContext().resources.getDimension(R.dimen.file_avatar_size).toInt()) }
8282

8383
factoryOf(::RemoteCapabilityMapper)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ class FileContentProvider(val executors: Executors = Executors()) : ContentProvi
913913

914914
// Insert capability to the new capabilities table in new database
915915
executors.diskIO().execute {
916-
ocLocalCapabilitiesDataSource.insert(
916+
ocLocalCapabilitiesDataSource.insertCapabilities(
917917
listOf(OCCapabilityEntity.fromCursor(cursor)).map {
918918
it.toModel()
919919
}

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

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* ownCloud Android client application
33
*
44
* @author David González Verdugo
5-
* Copyright (C) 2020 ownCloud GmbH.
5+
* @author Juan Carlos Garrote Gascón
6+
*
7+
* Copyright (C) 2023 ownCloud GmbH.
68
*
79
* This program is free software: you can redistribute it and/or modify
810
* it under the terms of the GNU General Public License version 2,
@@ -21,8 +23,6 @@ package com.owncloud.android.data.authentication.datasources.implementation
2123

2224
import android.accounts.Account
2325
import android.accounts.AccountManager
24-
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
25-
import androidx.test.filters.MediumTest
2626
import androidx.test.platform.app.InstrumentationRegistry
2727
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE
2828
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_ID
@@ -47,6 +47,7 @@ import com.owncloud.android.testutil.OC_ACCOUNT
4747
import com.owncloud.android.testutil.OC_ACCOUNT_ID
4848
import com.owncloud.android.testutil.OC_ACCOUNT_NAME
4949
import com.owncloud.android.testutil.OC_AUTH_TOKEN_TYPE
50+
import com.owncloud.android.testutil.OC_BASIC_PASSWORD
5051
import com.owncloud.android.testutil.OC_SECURE_BASE_URL
5152
import com.owncloud.android.testutil.OC_BASIC_USERNAME
5253
import com.owncloud.android.testutil.OC_OAUTH_SUPPORTED_TRUE
@@ -58,23 +59,17 @@ import com.owncloud.android.testutil.OC_USER_INFO
5859
import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION
5960
import io.mockk.every
6061
import io.mockk.mockk
61-
import io.mockk.spyk
6262
import io.mockk.verify
6363
import org.junit.Assert.assertEquals
6464
import org.junit.Assert.assertTrue
6565
import org.junit.Before
66-
import org.junit.Rule
6766
import org.junit.Test
6867

69-
@MediumTest
7068
class OCLocalAuthenticationDataSourceTest {
71-
@Rule
72-
@JvmField
73-
val instantExecutorRule = InstantTaskExecutorRule()
7469

7570
private lateinit var ocLocalAuthenticationDataSource: OCLocalAuthenticationDataSource
7671
private val accountManager = mockk<AccountManager>(relaxUnitFun = true)
77-
private val preferencesProvider = spyk<SharedPreferencesProvider>()
72+
private val preferencesProvider = mockk<SharedPreferencesProvider>(relaxUnitFun = true)
7873

7974
@Before
8075
fun setUp() {
@@ -86,13 +81,7 @@ class OCLocalAuthenticationDataSourceTest {
8681
preferencesProvider,
8782
OC_ACCOUNT.type
8883
)
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
84+
mockGetAccountsByType(OC_ACCOUNT.type, arrayOf(OC_ACCOUNT))
9685
}
9786

9887
@Test
@@ -103,21 +92,20 @@ class OCLocalAuthenticationDataSourceTest {
10392
val newAccountName = ocLocalAuthenticationDataSource.addBasicAccount(
10493
OC_ACCOUNT_ID,
10594
OC_REDIRECTION_PATH.lastPermanentLocation,
106-
"password",
95+
OC_BASIC_PASSWORD,
10796
OC_SECURE_SERVER_INFO_BASIC_AUTH,
10897
OC_USER_INFO,
10998
null
11099
)
111100

112101
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)
113102

103+
assertEquals(newAccount.name, newAccountName)
104+
114105
// One for checking if the account exists and another one for getting the new account
115106
verifyAccountsByTypeAreGot(newAccount.type, 2)
116-
117-
verifyAccountIsExplicitlyAdded(newAccount, "password", 1)
107+
verifyAccountIsExplicitlyAdded(newAccount, OC_BASIC_PASSWORD, 1)
118108
verifyAccountInfoIsUpdated(newAccount, OC_SECURE_SERVER_INFO_BASIC_AUTH, OC_USER_INFO, 1)
119-
120-
assertEquals(newAccount.name, newAccountName)
121109
}
122110

123111
@Test(expected = AccountNotNewException::class)
@@ -126,7 +114,7 @@ class OCLocalAuthenticationDataSourceTest {
126114
ocLocalAuthenticationDataSource.addBasicAccount(
127115
OC_ACCOUNT_ID,
128116
OC_REDIRECTION_PATH.lastPermanentLocation,
129-
"password",
117+
OC_BASIC_PASSWORD,
130118
OC_SECURE_SERVER_INFO_BASIC_AUTH,
131119
OC_USER_INFO.copy(id = OC_ACCOUNT_ID),
132120
null
@@ -138,20 +126,22 @@ class OCLocalAuthenticationDataSourceTest {
138126

139127
mockSelectedAccountNameInPreferences()
140128

141-
ocLocalAuthenticationDataSource.addBasicAccount(
129+
val accountName = ocLocalAuthenticationDataSource.addBasicAccount(
142130
OC_ACCOUNT_ID,
143131
OC_REDIRECTION_PATH.lastPermanentLocation,
144-
"password",
132+
OC_BASIC_PASSWORD,
145133
OC_SECURE_SERVER_INFO_BASIC_AUTH,
146134
OC_USER_INFO.copy(id = OC_ACCOUNT_ID),
147135
OC_ACCOUNT_NAME
148136
)
149137

138+
assertEquals(OC_ACCOUNT_NAME, accountName)
139+
150140
// One for getting account to update
151141
verifyAccountsByTypeAreGot(OC_ACCOUNT.type, 1)
152142

153143
// The account already exists so do not create it
154-
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, "password", 0)
144+
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, OC_BASIC_PASSWORD, 0)
155145

156146
// The account already exists, so update it
157147
verifyAccountInfoIsUpdated(OC_ACCOUNT, OC_SECURE_SERVER_INFO_BASIC_AUTH, OC_USER_INFO, 1)
@@ -166,7 +156,7 @@ class OCLocalAuthenticationDataSourceTest {
166156
ocLocalAuthenticationDataSource.addBasicAccount(
167157
OC_BASIC_USERNAME,
168158
OC_REDIRECTION_PATH.lastPermanentLocation,
169-
"password",
159+
OC_BASIC_PASSWORD,
170160
OC_SECURE_SERVER_INFO_BASIC_AUTH,
171161
OC_USER_INFO,
172162
"NotTheSameAccount"
@@ -175,7 +165,7 @@ class OCLocalAuthenticationDataSourceTest {
175165
assertTrue(exception is AccountNotTheSameException)
176166
} finally {
177167
// The account already exists so do not create a new one
178-
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, "password", 0)
168+
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, OC_BASIC_PASSWORD, 0)
179169

180170
// The account is not the same, so no update needed
181171
verifyAccountInfoIsUpdated(OC_ACCOUNT, OC_SECURE_SERVER_INFO_BASIC_AUTH, OC_USER_INFO, 0)
@@ -236,7 +226,7 @@ class OCLocalAuthenticationDataSourceTest {
236226

237227
mockSelectedAccountNameInPreferences()
238228

239-
ocLocalAuthenticationDataSource.addOAuthAccount(
229+
val accountName = ocLocalAuthenticationDataSource.addOAuthAccount(
240230
OC_ACCOUNT_ID,
241231
OC_REDIRECTION_PATH.lastPermanentLocation,
242232
OC_AUTH_TOKEN_TYPE,
@@ -249,11 +239,13 @@ class OCLocalAuthenticationDataSourceTest {
249239
OC_CLIENT_REGISTRATION
250240
)
251241

242+
assertEquals(OC_ACCOUNT_NAME, accountName)
243+
252244
// One for getting account to update
253245
verifyAccountsByTypeAreGot(OC_ACCOUNT.type, 1)
254246

255247
// The account already exists so do not create it
256-
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, "password", 0)
248+
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, OC_BASIC_PASSWORD, 0)
257249

258250
// The account already exists, so update it
259251
verifyAccountInfoIsUpdated(OC_ACCOUNT, OC_SECURE_SERVER_INFO_BASIC_AUTH, OC_USER_INFO, 1)
@@ -282,7 +274,7 @@ class OCLocalAuthenticationDataSourceTest {
282274
assertTrue(exception is AccountNotTheSameException)
283275
} finally {
284276
// The account already exists so do not create it
285-
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, "password", 0)
277+
verifyAccountIsExplicitlyAdded(OC_ACCOUNT, OC_BASIC_PASSWORD, 0)
286278

287279
// The account already exists, so update it
288280
verifyAccountInfoIsUpdated(OC_ACCOUNT, OC_SECURE_SERVER_INFO_BASIC_AUTH, OC_USER_INFO, 0)
@@ -299,23 +291,23 @@ class OCLocalAuthenticationDataSourceTest {
299291
}
300292

301293
@Test
302-
fun supportsOAuthOk() {
294+
fun supportsOAuth2Ok() {
303295

304296
every {
305297
accountManager.getUserData(OC_ACCOUNT, KEY_SUPPORTS_OAUTH2)
306298
} returns OC_OAUTH_SUPPORTED_TRUE
307299

308300
val supportsOAuth2 = ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT.name)
309301

302+
assertTrue(supportsOAuth2)
303+
310304
verifyAccountsByTypeAreGot(OC_ACCOUNT.type, 1)
311305
verifyUserDataIsGot(OC_ACCOUNT, KEY_SUPPORTS_OAUTH2, 1)
312-
313-
assertEquals(true, supportsOAuth2)
314306
}
315307

316308
@Test(expected = AccountNotFoundException::class)
317309
fun supportsOAuthAccountNotFound() {
318-
getAccountsByType(OC_ACCOUNT.type, arrayOf())// That account does not exist
310+
mockGetAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist
319311

320312
ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT.name)
321313
}
@@ -329,43 +321,41 @@ class OCLocalAuthenticationDataSourceTest {
329321

330322
val baseUrl = ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT.name)
331323

324+
assertEquals(OC_SECURE_BASE_URL, baseUrl)
325+
332326
verifyAccountsByTypeAreGot(OC_ACCOUNT.type, 1)
333327
verifyUserDataIsGot(OC_ACCOUNT, KEY_OC_BASE_URL, 1)
334-
335-
assertEquals(OC_SECURE_BASE_URL, baseUrl)
336328
}
337329

338330
@Test(expected = AccountNotFoundException::class)
339331
fun getBaseUrlAccountNotFound() {
340-
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist
332+
mockGetAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist
341333

342334
ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT.name)
343335
}
344336

337+
private fun mockGetAccountsByType(accountType: String, accounts: Array<Account>) {
338+
every {
339+
accountManager.getAccountsByType(accountType)
340+
} returns accounts
341+
}
342+
345343
private fun mockSelectedAccountNameInPreferences(
346344
selectedAccountName: String = OC_ACCOUNT.name
347345
) {
348346
every {
349347
preferencesProvider.getString(SELECTED_ACCOUNT, any())
350348
} returns selectedAccountName
351-
every {
352-
preferencesProvider.putString(any(), any())
353-
} returns Unit
354349
}
355350

356351
private fun mockRegularAccountCreationFlow() {
357352
// Step 1: Get accounts to know if the current account exists
358-
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // There's no accounts yet
353+
mockGetAccountsByType(OC_ACCOUNT.type, arrayOf()) // There's no accounts yet
359354

360355
// Step 2: Add new account
361356
every {
362357
accountManager.addAccountExplicitly(any(), any(), any())
363358
} returns true
364-
365-
// Step 3: Update just created account
366-
every {
367-
accountManager.setUserData(any(), any(), any())
368-
} returns Unit
369359
}
370360

371361
private fun verifyAccountsByTypeAreGot(accountType: String, exactly: Int) {

owncloudData/src/main/java/com/owncloud/android/data/appregistry/OCAppRegistryRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class OCAppRegistryRepository(
3434
private val localCapabilitiesDataSource: LocalCapabilitiesDataSource,
3535
) : AppRegistryRepository {
3636
override fun refreshAppRegistryForAccount(accountName: String) {
37-
val capabilities = localCapabilitiesDataSource.getCapabilityForAccount(accountName)
37+
val capabilities = localCapabilitiesDataSource.getCapabilitiesForAccount(accountName)
3838
val appUrl = capabilities?.filesAppProviders?.appsUrl?.substring(1)
3939
remoteAppRegistryDataSource.getAppRegistryForAccount(accountName, appUrl).also {
4040
localAppRegistryDataSource.saveAppRegistryForAccount(it)

owncloudData/src/main/java/com/owncloud/android/data/capabilities/datasources/LocalCapabilitiesDataSource.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ interface LocalCapabilitiesDataSource {
2828
accountName: String
2929
): LiveData<OCCapability?>
3030

31-
fun getCapabilityForAccount(
31+
fun getCapabilitiesForAccount(
3232
accountName: String
3333
): OCCapability?
3434

35-
fun insert(ocCapabilities: List<OCCapability>)
35+
fun insertCapabilities(ocCapabilities: List<OCCapability>)
3636

3737
fun deleteCapabilitiesForAccount(accountName: String)
3838
}

owncloudData/src/main/java/com/owncloud/android/data/capabilities/datasources/implementation/OCLocalCapabilitiesDataSource.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class OCLocalCapabilitiesDataSource(
3737
ocCapabilityEntity?.toModel()
3838
}
3939

40-
override fun getCapabilityForAccount(accountName: String): OCCapability? =
40+
override fun getCapabilitiesForAccount(accountName: String): OCCapability? =
4141
ocCapabilityDao.getCapabilitiesForAccount(accountName)?.toModel()
4242

43-
override fun insert(ocCapabilities: List<OCCapability>) {
43+
override fun insertCapabilities(ocCapabilities: List<OCCapability>) {
4444
ocCapabilityDao.replace(
4545
ocCapabilities.map { ocCapability -> ocCapability.toEntity() }
4646
)

0 commit comments

Comments
 (0)