-
Notifications
You must be signed in to change notification settings - Fork 131
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 #13648 from woocommerce/issue/12440-verify-destina…
…tion-address Verify destination address
- Loading branch information
Showing
9 changed files
with
187 additions
and
13 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
...merce/android/ui/orders/wooshippinglabels/address/destination/VerifyDestinationAddress.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,35 @@ | ||
package com.woocommerce.android.ui.orders.wooshippinglabels.address.destination | ||
|
||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.models.DestinationShippingAddress | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.networking.WooShippingLabelRepository | ||
import com.woocommerce.android.util.CoroutineDispatchers | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class VerifyDestinationAddress @Inject constructor( | ||
private val shippingRepository: WooShippingLabelRepository, | ||
private val selectedSite: SelectedSite, | ||
private val coroutineDispatchers: CoroutineDispatchers | ||
) { | ||
suspend operator fun invoke( | ||
orderId: Long | ||
): Result<DestinationShippingAddress> { | ||
return withContext(coroutineDispatchers.io) { | ||
selectedSite.getOrNull()?.let { | ||
val response = shippingRepository.verifyDestinationAddress(it, orderId) | ||
val result = response.model | ||
when { | ||
response.isError || result == null -> { | ||
val message = | ||
response.error.message | ||
?: if (result == null) "Empty response" else "Unknown error" | ||
Result.failure(Exception(message)) | ||
} | ||
|
||
else -> Result.success(result) | ||
} | ||
} ?: Result.failure(Exception("No site selected")) | ||
} | ||
} | ||
} |
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
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
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
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
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
57 changes: 57 additions & 0 deletions
57
.../android/ui/orders/wooshippinglabels/address/destination/VerifyDestinationAddressTests.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,57 @@ | ||
package com.woocommerce.android.ui.orders.wooshippinglabels.address.destination | ||
|
||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.models.DestinationShippingAddress | ||
import com.woocommerce.android.ui.orders.wooshippinglabels.networking.WooShippingLabelRepository | ||
import com.woocommerce.android.viewmodel.BaseUnitTest | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.UNKNOWN | ||
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError | ||
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.GENERIC_ERROR | ||
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class VerifyDestinationAddressTests : BaseUnitTest() { | ||
private val repository: WooShippingLabelRepository = mock() | ||
private val site: SelectedSite = mock() | ||
|
||
private val sut = VerifyDestinationAddress(repository, site, coroutinesTestRule.testDispatchers) | ||
|
||
private val defaultOrderId = 2L | ||
private val defaultAddressResponse = DestinationShippingAddress.EMPTY | ||
|
||
@Test | ||
fun `when selected site is null then return failure`() = testBlocking { | ||
val result = sut.invoke(defaultOrderId) | ||
assert(result.isFailure) | ||
} | ||
|
||
@Test | ||
fun `when verify address fails then return failure`() = testBlocking { | ||
whenever(site.getOrNull()).thenReturn(SiteModel()) | ||
whenever(repository.verifyDestinationAddress(any(), any())) | ||
.thenReturn(WooResult(WooError(GENERIC_ERROR, UNKNOWN))) | ||
|
||
val result = sut.invoke(defaultOrderId) | ||
|
||
assert(result.isFailure) | ||
} | ||
|
||
@Test | ||
fun `when verify address succeed then return expected data`() = testBlocking { | ||
whenever(site.getOrNull()).thenReturn(SiteModel()) | ||
whenever(repository.verifyDestinationAddress(any(), any())) | ||
.thenReturn(WooResult(defaultAddressResponse)) | ||
|
||
val result = sut.invoke(defaultOrderId) | ||
|
||
assert(result.isSuccess) | ||
assertThat(result.getOrNull()).isEqualTo(defaultAddressResponse) | ||
} | ||
} |
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