Skip to content

Commit

Permalink
tests for GetInsuranceForTravelAddonUseCaseImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
panasetskaya committed Dec 16, 2024
1 parent 7b72580 commit 7d21e0e
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package data

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.prop
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.annotations.ApolloExperimental
import com.apollographql.apollo.api.Error
import com.apollographql.apollo.testing.registerTestResponse
import com.hedvig.android.apollo.octopus.test.OctopusFakeResolver
import com.hedvig.android.apollo.test.TestApolloClientRule
import com.hedvig.android.apollo.test.TestNetworkTransportType
import com.hedvig.android.core.common.ErrorMessage
import com.hedvig.android.core.common.test.isLeft
import com.hedvig.android.core.common.test.isRight
import com.hedvig.android.data.contract.ContractGroup
import com.hedvig.android.feature.addon.purchase.data.GetInsuranceForTravelAddonUseCaseImpl
import com.hedvig.android.feature.addon.purchase.data.InsuranceForAddon
import com.hedvig.android.featureflags.flags.Feature
import com.hedvig.android.featureflags.test.FakeFeatureManager2
import com.hedvig.android.logger.TestLogcatLoggingRule
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import octopus.InsurancesForTravelAddonQuery
import octopus.type.buildAgreement
import octopus.type.buildContract
import octopus.type.buildMember
import octopus.type.buildProductVariant
import org.junit.Rule
import org.junit.Test

class GetInsuranceForTravelAddonUseCaseImplTest {
@get:Rule
val testLogcatLogger = TestLogcatLoggingRule()

@get:Rule
val testApolloClientRule = TestApolloClientRule(TestNetworkTransportType.MAP)

val testIds = listOf("testId1")

@OptIn(ApolloExperimental::class)
private val apolloClientWithGoodResponse: ApolloClient
get() = testApolloClientRule.apolloClient.apply {
registerTestResponse(
operation = InsurancesForTravelAddonQuery(),
data = InsurancesForTravelAddonQuery.Data(OctopusFakeResolver) {
currentMember = buildMember {
activeContracts = buildList {
add(
buildContract {
id = testIds[0]
exposureDisplayName = "exposureDisplayName"
currentAgreement = buildAgreement {
productVariant = buildProductVariant {
displayName = "displayName"
typeOfContract = "SE_HOUSE"
}
}
},
)
add(
buildContract {
id = "anotherId"
exposureDisplayName = "exposureDisplayName"
currentAgreement = buildAgreement {
productVariant = buildProductVariant {
displayName = "displayName"
typeOfContract = "SE_HOUSE"
}
}
},
)
}
}
},
)
}

@OptIn(ApolloExperimental::class)
private val apolloClientWithGoodButEmptyResponse: ApolloClient
get() = testApolloClientRule.apolloClient.apply {
registerTestResponse(
operation = InsurancesForTravelAddonQuery(),
data = InsurancesForTravelAddonQuery.Data(OctopusFakeResolver) {
currentMember = buildMember {
activeContracts = listOf()
}
},
)
}

@OptIn(ApolloExperimental::class)
private val apolloClientWithError: ApolloClient
get() = testApolloClientRule.apolloClient.apply {
registerTestResponse(
operation = InsurancesForTravelAddonQuery(),
errors = listOf(Error.Builder(message = "Bad message").build()),
)
}

@Test
fun `if FF for addons is off return ErrorMessage`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to false))
val sut = GetInsuranceForTravelAddonUseCaseImpl(apolloClientWithGoodResponse, featureManager)
val result = sut.invoke(testIds).first()
assertThat(result)
.isLeft()
}

@Test
fun `if quotes list is empty return ErrorMessage with null message`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetInsuranceForTravelAddonUseCaseImpl(apolloClientWithGoodButEmptyResponse, featureManager)
val result = sut.invoke(testIds).first()
assertThat(result)
.isLeft()
.prop(ErrorMessage::message)
}

@Test
fun `if BE gives error return ErrorMessage`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetInsuranceForTravelAddonUseCaseImpl(apolloClientWithError, featureManager)
val result = sut.invoke(testIds).first()
assertThat(result)
.isLeft()
}

@Test
fun `if BE gives correct response but the required ids are not there return ErrorMessage`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetInsuranceForTravelAddonUseCaseImpl(apolloClientWithGoodResponse, featureManager)
val result = sut.invoke(listOf("someotherid")).first()
assertThat(result)
.isLeft()
}

@Test
fun `if BE gives correct response and required ids are not there return correctly mapped list`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetInsuranceForTravelAddonUseCaseImpl(apolloClientWithGoodResponse, featureManager)
val result = sut.invoke(testIds).first()
assertThat(result)
.isRight().isEqualTo(
listOf(
InsuranceForAddon(
id = testIds[0],
displayName = "displayName",
contractExposure = "exposureDisplayName",
contractGroup = ContractGroup.HOUSE,
),
),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package data

import arrow.core.nonEmptyListOf
import arrow.core.raise.either
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import assertk.assertions.prop
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.annotations.ApolloExperimental
import com.apollographql.apollo.api.Error
import com.apollographql.apollo.testing.registerTestResponse
import com.hedvig.android.apollo.octopus.test.OctopusFakeResolver
import com.hedvig.android.apollo.test.TestApolloClientRule
import com.hedvig.android.apollo.test.TestNetworkTransportType
import com.hedvig.android.core.common.ErrorMessage
import com.hedvig.android.core.common.test.isLeft
import com.hedvig.android.core.uidata.UiCurrencyCode
import com.hedvig.android.core.uidata.UiMoney
Expand Down Expand Up @@ -48,7 +53,7 @@ class GetTravelAddonOfferUseCaseImplTest {
get() = testApolloClientRule.apolloClient.apply {
registerTestResponse(
operation = UpsellAddonOfferMutation(testId),
errors = listOf(com.apollographql.apollo.api.Error.Builder(message = "Bad message").build()),
errors = listOf(Error.Builder(message = "Bad message").build()),
)
}

Expand Down Expand Up @@ -219,45 +224,44 @@ class GetTravelAddonOfferUseCaseImplTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to false))
val sut = GetTravelAddonOfferUseCaseImpl(apolloClientWithFullResponseWithCurrentAddon, featureManager)
val result = sut.invoke(testId)
assertk.assertThat(result)
.isLeft().prop(com.hedvig.android.core.common.ErrorMessage::message).isNull()

assertThat(result)
.isLeft().prop(ErrorMessage::message).isNull()
}

@Test
fun `if quotes list is empty return ErrorMessage with null message`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetTravelAddonOfferUseCaseImpl(apolloClientWithFullResponseEmptyQuotes, featureManager)
val result = sut.invoke(testId)
assertk.assertThat(result)
assertThat(result)
.isLeft()
.prop(com.hedvig.android.core.common.ErrorMessage::message).isEqualTo(null)
.prop(ErrorMessage::message).isEqualTo(null)
}

@Test
fun `if BE gives error return ErrorMessage with null message`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetTravelAddonOfferUseCaseImpl(apolloClientWithError, featureManager)
val result = sut.invoke(testId)
assertk.assertThat(result)
.isLeft().prop(com.hedvig.android.core.common.ErrorMessage::message).isEqualTo(null)
assertThat(result)
.isLeft().prop(ErrorMessage::message).isEqualTo(null)
}

@Test
fun `if BE gives UserError return ErrorMessage with proper message`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetTravelAddonOfferUseCaseImpl(apolloClientWithUserError, featureManager)
val result = sut.invoke(testId)
assertk.assertThat(result)
.isLeft().prop(com.hedvig.android.core.common.ErrorMessage::message).isEqualTo("You have 2 insurances")
assertThat(result)
.isLeft().prop(ErrorMessage::message).isEqualTo("You have 2 insurances")
}

@Test
fun `if BE gives data but it's null return ErrorMessage with null message`() = runTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut = GetTravelAddonOfferUseCaseImpl(apolloClientWithNullData, featureManager)
val result = sut.invoke(testId).leftOrNull().toString()
assertk.assertThat(result)
assertThat(result)
.isEqualTo("ErrorMessage(message=null, throwable=null)")
}

Expand All @@ -266,16 +270,15 @@ class GetTravelAddonOfferUseCaseImplTest {
val featureManager = FakeFeatureManager2(fixedMap = mapOf(Feature.TRAVEL_ADDON to true))
val sut1 = GetTravelAddonOfferUseCaseImpl(apolloClientWithFullResponseNoCurrentAddon, featureManager)
val result1 = sut1.invoke(testId)
assertk.assertThat(result1)
assertThat(result1)
.isEqualTo(either { mockWithoutUpgrade })
val sut2 = GetTravelAddonOfferUseCaseImpl(apolloClientWithFullResponseWithCurrentAddon, featureManager)
val result2 = sut2.invoke(testId)
assertk.assertThat(result2)
assertThat(result2)
.isEqualTo(either { mockWithUpgrade })
}
}


private val mockWithoutUpgrade = TravelAddonOffer(
addonOptions = nonEmptyListOf(
TravelAddonQuote(
Expand Down Expand Up @@ -338,4 +341,3 @@ private val mockWithUpgrade = TravelAddonOffer(
listOf("Coverage" to "45 days"),
),
)

0 comments on commit 7d21e0e

Please sign in to comment.