|
| 1 | +package com.woocommerce.android.ui.woopos.common.data.models |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper |
| 5 | +import org.assertj.core.api.Assertions.assertThat |
| 6 | +import org.junit.Test |
| 7 | +import org.mockito.kotlin.mock |
| 8 | +import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId |
| 9 | +import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId |
| 10 | +import org.wordpress.android.fluxc.model.WCProductModel |
| 11 | +import java.math.BigDecimal |
| 12 | + |
| 13 | +class WCProductToWooPosProductModelMapperTest { |
| 14 | + private val logger: WooPosLogWrapper = mock() |
| 15 | + private val wooPosProductModelMapper = WooPosProductModelVersion2Mapper(logger) |
| 16 | + private val sut: WCProductToWooPosProductModelMapper = |
| 17 | + WCProductToWooPosProductModelMapper(wooPosProductModelMapper, logger) |
| 18 | + |
| 19 | + @Test |
| 20 | + fun `when mapping WCProductModel, then all attributes are correctly mapped`() { |
| 21 | + val imagesJson = """[{"id": 1, "src": "https://example.com/image.jpg", "name": "Image", "alt": "Alt text"}]""" |
| 22 | + .trimIndent() |
| 23 | + val categoriesJson = """[{"id": 10, "name": "Electronics", "slug": "electronics"}]""".trimIndent() |
| 24 | + val tagsJson = """[{"id": 20, "name": "Featured", "slug": "featured"}]""".trimIndent() |
| 25 | + |
| 26 | + val attributes = arrayOf( |
| 27 | + WCProductModel.ProductAttribute( |
| 28 | + id = 100L, |
| 29 | + name = "Color", |
| 30 | + variation = true, |
| 31 | + visible = true, |
| 32 | + options = listOf("Red", "Blue") |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + val wcProduct = createFullWCProductModel(imagesJson, attributes, categoriesJson, tagsJson) |
| 37 | + |
| 38 | + val result = sut.map(wcProduct) |
| 39 | + |
| 40 | + assertThat(result.remoteId).isEqualTo(123L) |
| 41 | + assertThat(result.parentId).isEqualTo(456L) |
| 42 | + assertThat(result.name).isEqualTo("Test Product") |
| 43 | + assertThat(result.sku).isEqualTo("TEST-SKU") |
| 44 | + assertThat(result.globalUniqueId).isEqualTo("global-123") |
| 45 | + assertThat(result.description).isEqualTo("Full description") |
| 46 | + assertThat(result.shortDescription).isEqualTo("Short description") |
| 47 | + assertThat(result.isDownloadable).isTrue() |
| 48 | + assertThat(result.lastModified).isEqualTo("2024-01-15T10:00:00Z") |
| 49 | + |
| 50 | + assertThat(result.type).isEqualTo(WooPosProductModelVersion2.WooPosProductType.SIMPLE) |
| 51 | + assertThat(result.status).isEqualTo(WooPosProductModelVersion2.WooPosProductStatus.PUBLISH) |
| 52 | + |
| 53 | + assertThat(result.pricing).isInstanceOf(WooPosProductModelVersion2.WooPosPricing.SalePricing::class.java) |
| 54 | + val pricing = result.pricing as WooPosProductModelVersion2.WooPosPricing.SalePricing |
| 55 | + assertThat(pricing.regularPrice).isEqualTo(BigDecimal("24.99")) |
| 56 | + assertThat(pricing.salePrice).isEqualTo(BigDecimal("19.99")) |
| 57 | + |
| 58 | + assertThat(result.images).hasSize(1) |
| 59 | + assertThat(result.images[0].id).isEqualTo(1L) |
| 60 | + assertThat(result.images[0].url).isEqualTo("https://example.com/image.jpg") |
| 61 | + assertThat(result.images[0].name).isEqualTo("Image") |
| 62 | + assertThat(result.images[0].alt).isEqualTo("Alt text") |
| 63 | + |
| 64 | + assertThat(result.attributes).hasSize(1) |
| 65 | + assertThat(result.attributes[0].id).isEqualTo(100L) |
| 66 | + assertThat(result.attributes[0].name).isEqualTo("Color") |
| 67 | + assertThat(result.attributes[0].options).containsExactly("Red", "Blue") |
| 68 | + assertThat(result.attributes[0].isVisible).isTrue() |
| 69 | + assertThat(result.attributes[0].isVariation).isTrue() |
| 70 | + |
| 71 | + assertThat(result.categories).hasSize(1) |
| 72 | + assertThat(result.categories[0].id).isEqualTo(10L) |
| 73 | + assertThat(result.categories[0].name).isEqualTo("Electronics") |
| 74 | + assertThat(result.categories[0].slug).isEqualTo("electronics") |
| 75 | + |
| 76 | + assertThat(result.tags).hasSize(1) |
| 77 | + assertThat(result.tags[0].id).isEqualTo(20L) |
| 78 | + assertThat(result.tags[0].name).isEqualTo("Featured") |
| 79 | + assertThat(result.tags[0].slug).isEqualTo("featured") |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `when parent id is zero, then mapped parent id is null`() { |
| 84 | + val wcProduct = WCProductModel( |
| 85 | + localSiteId = LocalId(1), |
| 86 | + remoteId = RemoteId(123L), |
| 87 | + parentId = 0L |
| 88 | + ) |
| 89 | + |
| 90 | + val result = sut.map(wcProduct) |
| 91 | + |
| 92 | + assertThat(result.parentId).isNull() |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `when parent id is non-zero, then mapped parent id has value`() { |
| 97 | + val wcProduct = WCProductModel( |
| 98 | + localSiteId = LocalId(1), |
| 99 | + remoteId = RemoteId(123L), |
| 100 | + parentId = 789L |
| 101 | + ) |
| 102 | + |
| 103 | + val result = sut.map(wcProduct) |
| 104 | + |
| 105 | + assertThat(result.parentId).isEqualTo(789L) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `when product has empty collections, then mapped collections are empty`() { |
| 110 | + val wcProduct = WCProductModel( |
| 111 | + localSiteId = LocalId(1), |
| 112 | + remoteId = RemoteId(123L), |
| 113 | + images = "", |
| 114 | + attributes = "[]", |
| 115 | + categories = "", |
| 116 | + tags = "" |
| 117 | + ) |
| 118 | + |
| 119 | + val result = sut.map(wcProduct) |
| 120 | + |
| 121 | + assertThat(result.images).isEmpty() |
| 122 | + assertThat(result.attributes).isEmpty() |
| 123 | + assertThat(result.categories).isEmpty() |
| 124 | + assertThat(result.tags).isEmpty() |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `when product has no price, then pricing is NoPricing`() { |
| 129 | + val wcProduct = WCProductModel( |
| 130 | + localSiteId = LocalId(1), |
| 131 | + remoteId = RemoteId(123L), |
| 132 | + price = "", |
| 133 | + regularPrice = "", |
| 134 | + salePrice = "", |
| 135 | + onSale = false |
| 136 | + ) |
| 137 | + |
| 138 | + val result = sut.map(wcProduct) |
| 139 | + |
| 140 | + assertThat(result.pricing).isEqualTo(WooPosProductModelVersion2.WooPosPricing.NoPricing) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun `when product has regular price only, then pricing is RegularPricing`() { |
| 145 | + val wcProduct = WCProductModel( |
| 146 | + localSiteId = LocalId(1), |
| 147 | + remoteId = RemoteId(123L), |
| 148 | + price = "", |
| 149 | + regularPrice = "29.99", |
| 150 | + salePrice = "", |
| 151 | + onSale = false |
| 152 | + ) |
| 153 | + |
| 154 | + val result = sut.map(wcProduct) |
| 155 | + |
| 156 | + assertThat(result.pricing).isInstanceOf(WooPosProductModelVersion2.WooPosPricing.RegularPricing::class.java) |
| 157 | + val pricing = result.pricing as WooPosProductModelVersion2.WooPosPricing.RegularPricing |
| 158 | + assertThat(pricing.price).isEqualTo(BigDecimal("29.99")) |
| 159 | + } |
| 160 | + |
| 161 | + private fun createFullWCProductModel( |
| 162 | + imagesJson: String, |
| 163 | + attributes: Array<WCProductModel.ProductAttribute>, |
| 164 | + categoriesJson: String, |
| 165 | + tagsJson: String |
| 166 | + ): WCProductModel { |
| 167 | + val wcProduct = WCProductModel( |
| 168 | + localSiteId = LocalId(1), |
| 169 | + remoteId = RemoteId(123L), |
| 170 | + parentId = 456L, |
| 171 | + name = "Test Product", |
| 172 | + sku = "TEST-SKU", |
| 173 | + globalUniqueId = "global-123", |
| 174 | + type = "simple", |
| 175 | + status = "publish", |
| 176 | + price = "19.99", |
| 177 | + regularPrice = "24.99", |
| 178 | + salePrice = "19.99", |
| 179 | + onSale = true, |
| 180 | + description = "Full description", |
| 181 | + shortDescription = "Short description", |
| 182 | + downloadable = true, |
| 183 | + dateModified = "2024-01-15T10:00:00Z", |
| 184 | + images = imagesJson, |
| 185 | + attributes = Gson().toJson(attributes), |
| 186 | + categories = categoriesJson, |
| 187 | + tags = tagsJson |
| 188 | + ) |
| 189 | + return wcProduct |
| 190 | + } |
| 191 | +} |
0 commit comments