|
| 1 | +package org.wordpress.android.fluxc.store |
| 2 | + |
| 3 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 4 | +import kotlinx.coroutines.test.runBlockingTest |
| 5 | +import org.assertj.core.api.Assertions.assertThat |
| 6 | +import org.junit.Before |
| 7 | +import org.junit.Test |
| 8 | +import org.mockito.kotlin.any |
| 9 | +import org.mockito.kotlin.doReturn |
| 10 | +import org.mockito.kotlin.mock |
| 11 | +import org.mockito.kotlin.never |
| 12 | +import org.mockito.kotlin.verify |
| 13 | +import org.mockito.kotlin.whenever |
| 14 | +import org.wordpress.android.fluxc.model.SiteModel |
| 15 | +import org.wordpress.android.fluxc.model.WCShippingMethod |
| 16 | +import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.NOT_FOUND |
| 17 | +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError |
| 18 | +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.EMPTY_RESPONSE |
| 19 | +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload |
| 20 | +import org.wordpress.android.fluxc.network.rest.wpcom.wc.shippingsmethods.ShippingMethodsRestClient |
| 21 | +import org.wordpress.android.fluxc.network.rest.wpcom.wc.shippingsmethods.ShippingMethodsRestClient.ShippingMethodDto |
| 22 | +import org.wordpress.android.fluxc.persistence.dao.ShippingMethodDao |
| 23 | +import org.wordpress.android.fluxc.utils.initCoroutineEngine |
| 24 | + |
| 25 | +@ExperimentalCoroutinesApi |
| 26 | +class WCShippingMethodsStoreTest { |
| 27 | + private lateinit var sut: WCShippingMethodsStore |
| 28 | + private val shippingMethodsRestClient: ShippingMethodsRestClient = mock() |
| 29 | + private val shippingMethodDao: ShippingMethodDao = mock() |
| 30 | + private val coroutineEngine = initCoroutineEngine() |
| 31 | + private val defaultSiteModel = SiteModel().apply { siteId = 1L } |
| 32 | + |
| 33 | + @Before |
| 34 | + fun setup() { |
| 35 | + sut = WCShippingMethodsStore( |
| 36 | + restClient = shippingMethodsRestClient, |
| 37 | + shippingMethodDao = shippingMethodDao, |
| 38 | + coroutineEngine = coroutineEngine |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `when shipping method id is not empty, then the shipping method can be updated`() = |
| 44 | + runBlockingTest { |
| 45 | + val shippingMethod = WCShippingMethod(id = "methodId", title = "methodTitle") |
| 46 | + |
| 47 | + sut.updateShippingMethod(defaultSiteModel, shippingMethod) |
| 48 | + |
| 49 | + verify(shippingMethodDao).insertShippingMethods(any()) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `when shipping method id is empty, then the shipping method CAN'T be updated`() = |
| 54 | + runBlockingTest { |
| 55 | + val shippingMethod = WCShippingMethod(id = "", title = "methodTitle") |
| 56 | + |
| 57 | + sut.updateShippingMethod(defaultSiteModel, shippingMethod) |
| 58 | + |
| 59 | + verify(shippingMethodDao, never()).insertShippingMethods(any()) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `when fetch shipping method success, then response is expected`() = runBlockingTest { |
| 64 | + val methodId = "methodId" |
| 65 | + val methodTitle = "Random Title" |
| 66 | + val expected = WCShippingMethod(methodId, methodTitle) |
| 67 | + |
| 68 | + val response = ShippingMethodDto(id = methodId, title = methodTitle) |
| 69 | + whenever(shippingMethodsRestClient.fetchShippingMethodsById(defaultSiteModel, methodId)) |
| 70 | + .doReturn(WooPayload(response)) |
| 71 | + |
| 72 | + val result = sut.fetchShippingMethod(defaultSiteModel, methodId) |
| 73 | + |
| 74 | + assertThat(result.isError).isFalse() |
| 75 | + assertThat(result.model).isNotNull |
| 76 | + assertThat(result.model).isEqualTo(expected) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `when fetch shipping method is null, then response is error`() = runBlockingTest { |
| 81 | + val methodId = "methodId" |
| 82 | + whenever(shippingMethodsRestClient.fetchShippingMethodsById(defaultSiteModel, methodId)) |
| 83 | + .doReturn(WooPayload(null)) |
| 84 | + |
| 85 | + val result = sut.fetchShippingMethod(defaultSiteModel, methodId) |
| 86 | + |
| 87 | + assertThat(result.isError).isTrue |
| 88 | + assertThat(result.model).isNull() |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `when fetch shipping method fails, then response is error`() = runBlockingTest { |
| 93 | + val methodId = "methodId" |
| 94 | + val error = WooError(EMPTY_RESPONSE, NOT_FOUND) |
| 95 | + whenever(shippingMethodsRestClient.fetchShippingMethodsById(defaultSiteModel, methodId)) |
| 96 | + .doReturn(WooPayload(error)) |
| 97 | + |
| 98 | + val result = sut.fetchShippingMethod(defaultSiteModel, methodId) |
| 99 | + |
| 100 | + assertThat(result.isError).isTrue |
| 101 | + assertThat(result.model).isNull() |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `when fetch shipping methods success, then response is expected`() = runBlockingTest { |
| 106 | + val methodId = "methodId" |
| 107 | + val methodTitle = "Random Title" |
| 108 | + val expected = listOf(WCShippingMethod(methodId, methodTitle)) |
| 109 | + |
| 110 | + val response = listOf(ShippingMethodDto(methodId, methodTitle)) |
| 111 | + whenever(shippingMethodsRestClient.fetchShippingMethods(defaultSiteModel)) |
| 112 | + .doReturn(WooPayload(response)) |
| 113 | + |
| 114 | + val result = sut.fetchShippingMethods(defaultSiteModel) |
| 115 | + |
| 116 | + assertThat(result.isError).isFalse() |
| 117 | + assertThat(result.model).isNotNull |
| 118 | + assertThat(result.model).isEqualTo(expected) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun `when fetch shipping methods is null, then response is error`() = runBlockingTest { |
| 123 | + whenever(shippingMethodsRestClient.fetchShippingMethods(defaultSiteModel)) |
| 124 | + .doReturn(WooPayload(null)) |
| 125 | + |
| 126 | + val result = sut.fetchShippingMethods(defaultSiteModel) |
| 127 | + |
| 128 | + assertThat(result.isError).isTrue |
| 129 | + assertThat(result.model).isNull() |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `when fetch shipping methods fails, then response is error`() = runBlockingTest { |
| 134 | + val error = WooError(EMPTY_RESPONSE, NOT_FOUND) |
| 135 | + whenever(shippingMethodsRestClient.fetchShippingMethods(defaultSiteModel)) |
| 136 | + .doReturn(WooPayload(error)) |
| 137 | + |
| 138 | + val result = sut.fetchShippingMethods(defaultSiteModel) |
| 139 | + |
| 140 | + assertThat(result.isError).isTrue |
| 141 | + assertThat(result.model).isNull() |
| 142 | + } |
| 143 | +} |
0 commit comments