Skip to content

Commit

Permalink
Merge pull request #19360 from wordpress-mobile/issue/19165-domains-l…
Browse files Browse the repository at this point in the history
…ist-endpoint

Exposes the Domains List Endpoint (all-domains) api call
  • Loading branch information
Antonis Lilis authored Oct 13, 2023
2 parents 57a414e + 88b4e2a commit 4452ba0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
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
}
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
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ext {
automatticTracksVersion = '3.3.0'
gutenbergMobileVersion = 'v1.106.0'
wordPressAztecVersion = 'v1.8.0'
wordPressFluxCVersion = 'trunk-f3ba57a9f1d9be24a4f43f929c6d98bfd8539df2'
wordPressFluxCVersion = 'trunk-21b9cb51949bff38713430b86c31e4620623dcf2'
wordPressLoginVersion = '1.6.0'
wordPressPersistentEditTextVersion = '1.0.2'
wordPressUtilsVersion = '3.10.0'
Expand Down

0 comments on commit 4452ba0

Please sign in to comment.