-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19360 from wordpress-mobile/issue/19165-domains-l…
…ist-endpoint Exposes the Domains List Endpoint (all-domains) api call
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
WordPress/src/main/java/org/wordpress/android/ui/domains/usecases/FetchAllDomainsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.wordpress.android.ui.domains.usecases | ||
|
||
import org.wordpress.android.fluxc.network.rest.wpcom.site.AllDomainsDomain | ||
import org.wordpress.android.fluxc.store.SiteStore | ||
import javax.inject.Inject | ||
|
||
class FetchAllDomainsUseCase @Inject constructor( | ||
private val siteStore: SiteStore, | ||
) { | ||
suspend fun execute(): AllDomains { | ||
val result = siteStore.fetchAllDomains() | ||
return when { | ||
result.isError -> AllDomains.Error | ||
result.domains.isNullOrEmpty() -> return AllDomains.Empty | ||
else -> result.domains?.run { | ||
AllDomains.Success(this) | ||
} ?: AllDomains.Empty | ||
} | ||
} | ||
} | ||
|
||
sealed interface AllDomains { | ||
data class Success( | ||
val domains: List<AllDomainsDomain>, | ||
) : AllDomains | ||
|
||
object Empty : AllDomains | ||
|
||
object Error : AllDomains | ||
} |
89 changes: 89 additions & 0 deletions
89
WordPress/src/test/java/org/wordpress/android/ui/domains/FetchAllDomainsUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.wordpress.android.ui.domains | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.BaseUnitTest | ||
import org.wordpress.android.fluxc.network.rest.wpcom.site.AllDomainsDomain | ||
import org.wordpress.android.fluxc.store.SiteStore | ||
import org.wordpress.android.ui.domains.usecases.AllDomains | ||
import org.wordpress.android.ui.domains.usecases.FetchAllDomainsUseCase | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(MockitoJUnitRunner::class) | ||
class FetchAllDomainsUseCaseTest: BaseUnitTest() { | ||
@Mock | ||
lateinit var store: SiteStore | ||
|
||
private lateinit var fetchAllDomainsUseCase: FetchAllDomainsUseCase | ||
|
||
@Before | ||
fun setUp() { | ||
fetchAllDomainsUseCase = FetchAllDomainsUseCase(store) | ||
} | ||
|
||
@Test | ||
fun `given all-domains call returns results, when the usecase is execute, returns success`() = runTest { | ||
whenever(store.fetchAllDomains()).thenReturn( | ||
SiteStore.FetchedAllDomainsPayload( | ||
listOf( | ||
AllDomainsDomain("test.domain.one"), | ||
AllDomainsDomain("test.domain.two") | ||
) | ||
) | ||
) | ||
|
||
val result = fetchAllDomainsUseCase.execute() | ||
|
||
assertThat(result is AllDomains.Success).isTrue | ||
with(result as AllDomains.Success) { | ||
assertThat(domains.size).isEqualTo(2) | ||
assertThat(domains[0].domain).isEqualTo("test.domain.one") | ||
assertThat(domains[1].domain).isEqualTo("test.domain.two") | ||
} | ||
} | ||
|
||
@Test | ||
fun `given the all-domain call returns error, when usecase is execute, returns error`() = runTest { | ||
whenever(store.fetchAllDomains()).thenReturn( | ||
SiteStore.FetchedAllDomainsPayload( | ||
SiteStore.AllDomainsError( | ||
SiteStore.AllDomainsErrorType.GENERIC_ERROR, | ||
null | ||
) | ||
) | ||
) | ||
|
||
val result = fetchAllDomainsUseCase.execute() | ||
|
||
assertThat(result is AllDomains.Error).isTrue | ||
} | ||
|
||
@Test | ||
fun `given the all-domain call returns empty response, when usecase execute, returns empty`() = runTest { | ||
whenever(store.fetchAllDomains()).thenReturn( | ||
SiteStore.FetchedAllDomainsPayload(listOf()) | ||
) | ||
|
||
val result = fetchAllDomainsUseCase.execute() | ||
|
||
assertThat(result is AllDomains.Empty).isTrue | ||
} | ||
|
||
@Test | ||
fun `given the all-domain call returns null response, when usecase execute, returns empty`() = runTest { | ||
whenever(store.fetchAllDomains()).thenReturn( | ||
SiteStore.FetchedAllDomainsPayload(null) | ||
) | ||
|
||
val result = fetchAllDomainsUseCase.execute() | ||
|
||
assertThat(result is AllDomains.Empty).isTrue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters