From 1d926dcef81d8d31fce3c33aef6888035fedf46d Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Mon, 18 Sep 2023 09:29:44 +0200 Subject: [PATCH] Add unit tests for PaymentRepository --- android/lib/payment/build.gradle.kts | 7 ++ .../lib/payment/PaymentRepositoryTest.kt | 117 ++++++++++++++++++ .../mullvadvpn/payment/ExampleUnitTest.kt | 17 --- 3 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepositoryTest.kt delete mode 100644 android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/payment/ExampleUnitTest.kt diff --git a/android/lib/payment/build.gradle.kts b/android/lib/payment/build.gradle.kts index 82fd912f0f26..8b2485ced157 100644 --- a/android/lib/payment/build.gradle.kts +++ b/android/lib/payment/build.gradle.kts @@ -44,4 +44,11 @@ dependencies { // Billing implementation(project(Dependencies.Mullvad.billingLib)) + + // Test dependencies + testImplementation(project(Dependencies.Mullvad.commonTestLib)) + testImplementation(Dependencies.Kotlin.test) + testImplementation(Dependencies.KotlinX.coroutinesTest) + testImplementation(Dependencies.MockK.core) + testImplementation(Dependencies.junit) } diff --git a/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepositoryTest.kt b/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepositoryTest.kt new file mode 100644 index 000000000000..b868fc989c53 --- /dev/null +++ b/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/lib/payment/PaymentRepositoryTest.kt @@ -0,0 +1,117 @@ +package net.mullvad.mullvadvpn.lib.payment + +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertIs +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.runTest +import net.mullvad.mullvadvpn.lib.billing.BillingRepository +import net.mullvad.mullvadvpn.lib.billing.model.BillingProduct +import net.mullvad.mullvadvpn.lib.billing.model.BillingPurchase +import net.mullvad.mullvadvpn.lib.billing.model.PurchaseEvent +import net.mullvad.mullvadvpn.lib.billing.model.PurchaseFlowResult +import net.mullvad.mullvadvpn.lib.billing.model.QueryProductResult +import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +class PaymentRepositoryTest { + @get:Rule val testCoroutineRule = TestCoroutineRule() + + private val mockBillingRepository: BillingRepository = mockk() + + private val purchaseEventFlow = + MutableSharedFlow(replay = 1, extraBufferCapacity = 1) + + private lateinit var paymentRepository: PaymentRepository + + @Before + fun setUp() { + every { mockBillingRepository.purchaseEvents } returns purchaseEventFlow + } + + @After fun tearDown() {} + + @Test + fun testQueryAvailablePaymentShowWebPayment() = runTest { + // Arrange + paymentRepository = + PaymentRepository(billingRepository = mockBillingRepository, showWebPayment = true) + coEvery { mockBillingRepository.queryProducts(any()) } returns + QueryProductResult.ItemUnavailable + + // Act + val result = paymentRepository.queryAvailablePaymentTypes() + + // Assert + assert(result.webPaymentAvailable) + } + + @Test + fun testQueryAvailablePaymentHideWebPayment() = runTest { + // Arrange + paymentRepository = + PaymentRepository(billingRepository = mockBillingRepository, showWebPayment = false) + coEvery { mockBillingRepository.queryProducts(any()) } returns + QueryProductResult.ItemUnavailable + + // Act + val result = paymentRepository.queryAvailablePaymentTypes() + + // Assert + assertFalse(result.webPaymentAvailable) + } + + @Test + fun testQueryAvailablePaymentProductsAvailable() = runTest { + // Arrange + paymentRepository = + PaymentRepository(billingRepository = mockBillingRepository, showWebPayment = false) + val mockProduct: BillingProduct = mockk() + coEvery { mockBillingRepository.queryProducts(any()) } returns + QueryProductResult.Ok(products = listOf(mockProduct)) + + // Act + val result = paymentRepository.queryAvailablePaymentTypes().billingPaymentAvailability + + // Assert + assertIs(result) + assertEquals(mockProduct, result.products.first()) + } + + @Test + fun testQueryAvailablePaymentProductsUnavailable() = runTest { + // Arrange + paymentRepository = + PaymentRepository(billingRepository = mockBillingRepository, showWebPayment = false) + coEvery { mockBillingRepository.queryProducts(any()) } returns + QueryProductResult.ItemUnavailable + + // Act + val result = paymentRepository.queryAvailablePaymentTypes().billingPaymentAvailability + + // Assert + assertIs(result) + } + + @Test + fun testQueryAvailablePaymentBillingUnavailableError() = runTest { + // Arrange + paymentRepository = + PaymentRepository(billingRepository = mockBillingRepository, showWebPayment = false) + coEvery { mockBillingRepository.queryProducts(any()) } returns + QueryProductResult.BillingUnavailable + + // Act + val result = paymentRepository.queryAvailablePaymentTypes().billingPaymentAvailability + + // Assert + assertIs(result) + } +} diff --git a/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/payment/ExampleUnitTest.kt b/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/payment/ExampleUnitTest.kt deleted file mode 100644 index 664284249d31..000000000000 --- a/android/lib/payment/src/test/kotlin/net/mullvad/mullvadvpn/payment/ExampleUnitTest.kt +++ /dev/null @@ -1,17 +0,0 @@ -package net.mullvad.mullvadvpn.payment - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -}