Bug 1857215 - Part 2:Integrate review checker recommendations
parent
da457de30d
commit
cec1940d38
@ -0,0 +1,50 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping.middleware
|
||||
|
||||
import mozilla.components.concept.engine.shopping.ProductRecommendation
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.RecommendedProductState
|
||||
import java.text.NumberFormat
|
||||
import java.util.Currency
|
||||
import java.util.Locale
|
||||
|
||||
private const val MINIMUM_FRACTION_DIGITS = 0
|
||||
private const val MAXIMUM_FRACTION_DIGITS = 2
|
||||
|
||||
/**
|
||||
* Maps [ProductRecommendation] to [RecommendedProductState].
|
||||
*/
|
||||
fun ProductRecommendation?.toRecommendedProductState(): RecommendedProductState =
|
||||
this?.toRecommendedProduct() ?: RecommendedProductState.Error
|
||||
|
||||
private fun ProductRecommendation.toRecommendedProduct(): RecommendedProductState.Product =
|
||||
RecommendedProductState.Product(
|
||||
aid = aid,
|
||||
name = name,
|
||||
productUrl = url,
|
||||
imageUrl = imageUrl,
|
||||
formattedPrice = price.toDouble().toFormattedAmount(currency),
|
||||
reviewGrade = grade.asEnumOrDefault<ReviewQualityCheckState.Grade>()!!,
|
||||
adjustedRating = adjustedRating.toFloat(),
|
||||
isSponsored = sponsored,
|
||||
analysisUrl = analysisUrl,
|
||||
)
|
||||
|
||||
private fun Double.toFormattedAmount(currencyCode: String): String =
|
||||
mapCurrencyCodeToNumberFormat(currencyCode).apply {
|
||||
minimumFractionDigits = MINIMUM_FRACTION_DIGITS
|
||||
maximumFractionDigits = MAXIMUM_FRACTION_DIGITS
|
||||
}.format(this)
|
||||
|
||||
private fun mapCurrencyCodeToNumberFormat(currencyCode: String): NumberFormat =
|
||||
try {
|
||||
val currency = Currency.getInstance(currencyCode)
|
||||
NumberFormat.getCurrencyInstance(Locale.getDefault()).apply {
|
||||
this.currency = currency
|
||||
}
|
||||
} catch (e: IllegalArgumentException) {
|
||||
NumberFormat.getNumberInstance()
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.helpers
|
||||
|
||||
import org.junit.rules.TestRule
|
||||
import org.junit.runner.Description
|
||||
import org.junit.runners.model.Statement
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* A JUnit [TestRule] that sets the default locale to a given [localeToSet] for the duration of
|
||||
* the test and then resets it to the original locale.
|
||||
*
|
||||
* @param localeToSet The locale to set for the duration of the test.
|
||||
*/
|
||||
class LocaleTestRule(private val localeToSet: Locale) : TestRule {
|
||||
|
||||
private var originalLocale: Locale? = null
|
||||
|
||||
override fun apply(base: Statement, description: Description): Statement =
|
||||
object : Statement() {
|
||||
|
||||
override fun evaluate() {
|
||||
originalLocale = Locale.getDefault()
|
||||
Locale.setDefault(localeToSet)
|
||||
|
||||
try {
|
||||
base.evaluate() // Run the tests
|
||||
} finally {
|
||||
// Reset to the original locale after tests
|
||||
Locale.setDefault(originalLocale!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping
|
||||
|
||||
import mozilla.components.concept.engine.shopping.ProductRecommendation
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
|
||||
|
||||
object ProductRecommendationTestData {
|
||||
|
||||
fun productRecommendation(
|
||||
aid: String = "aid",
|
||||
url: String = "https://test.com",
|
||||
grade: String = "A",
|
||||
adjustedRating: Double = 4.7,
|
||||
sponsored: Boolean = true,
|
||||
analysisUrl: String = "analysisUrl",
|
||||
imageUrl: String = "https://imageurl.com",
|
||||
name: String = "Test Product",
|
||||
price: String = "100",
|
||||
currency: String = "USD",
|
||||
): ProductRecommendation = ProductRecommendation(
|
||||
aid = aid,
|
||||
url = url,
|
||||
grade = grade,
|
||||
adjustedRating = adjustedRating,
|
||||
sponsored = sponsored,
|
||||
analysisUrl = analysisUrl,
|
||||
imageUrl = imageUrl,
|
||||
name = name,
|
||||
price = price,
|
||||
currency = currency,
|
||||
)
|
||||
|
||||
fun product(
|
||||
aid: String = "aid",
|
||||
url: String = "https://test.com",
|
||||
reviewGrade: ReviewQualityCheckState.Grade = ReviewQualityCheckState.Grade.A,
|
||||
adjustedRating: Double = 4.7,
|
||||
sponsored: Boolean = true,
|
||||
analysisUrl: String = "analysisUrl",
|
||||
imageUrl: String = "https://imageurl.com",
|
||||
name: String = "Test Product",
|
||||
formattedPrice: String = "$100",
|
||||
): ReviewQualityCheckState.RecommendedProductState.Product =
|
||||
ReviewQualityCheckState.RecommendedProductState.Product(
|
||||
aid = aid,
|
||||
productUrl = url,
|
||||
reviewGrade = reviewGrade,
|
||||
adjustedRating = adjustedRating.toFloat(),
|
||||
isSponsored = sponsored,
|
||||
analysisUrl = analysisUrl,
|
||||
imageUrl = imageUrl,
|
||||
name = name,
|
||||
formattedPrice = formattedPrice,
|
||||
)
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping.middleware
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.helpers.LocaleTestRule
|
||||
import org.mozilla.fenix.shopping.ProductRecommendationTestData
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
|
||||
import java.util.Locale
|
||||
|
||||
class ProductRecommendationMapperTest {
|
||||
|
||||
@get:Rule
|
||||
val localeTestRule = LocaleTestRule(Locale.US)
|
||||
|
||||
@Test
|
||||
fun `WHEN ProductRecommendation is null THEN it is mapped to Error`() {
|
||||
val actual = null.toRecommendedProductState()
|
||||
val expected = ReviewQualityCheckState.RecommendedProductState.Error
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN ProductRecommendation has data THEN it is mapped to product`() {
|
||||
val productRecommendation = ProductRecommendationTestData.productRecommendation()
|
||||
val actual = productRecommendation.toRecommendedProductState()
|
||||
val expected = ProductRecommendationTestData.product()
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN ProductRecommendation has data with invalid currency code THEN it is mapped to product`() {
|
||||
val productRecommendation = ProductRecommendationTestData.productRecommendation(
|
||||
price = "100",
|
||||
currency = "invalid",
|
||||
)
|
||||
val actual = productRecommendation.toRecommendedProductState()
|
||||
val expected = ProductRecommendationTestData.product(
|
||||
formattedPrice = "100",
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue