Skip to content

Commit

Permalink
Adds unit tests for DocumentDetails Interactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatop committed Mar 8, 2024
1 parent e30c3c1 commit 32bcea9
Show file tree
Hide file tree
Showing 11 changed files with 1,000 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import eu.europa.ec.businesslogic.util.toDateFormatted
import eu.europa.ec.businesslogic.util.toList
import eu.europa.ec.commonfeature.model.DocumentUi
import eu.europa.ec.commonfeature.model.toDocumentTypeUi
import eu.europa.ec.commonfeature.model.toUiName
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentDetailsUi
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentJsonKeys
import eu.europa.ec.commonfeature.util.extractFullNameFromDocumentOrEmpty
Expand Down Expand Up @@ -68,13 +69,15 @@ object DocumentDetailsTransformer {
)
}

val documentTypeUi = document.docType.toDocumentTypeUi()

return DocumentUi(
documentId = document.id,
documentName = document.name,
documentType = document.docType.toDocumentTypeUi(),
documentName = documentTypeUi.toUiName(resourceProvider),
documentType = documentTypeUi,
documentExpirationDateFormatted = documentJson.getStringFromJsonOrEmpty(
key = DocumentJsonKeys.EXPIRY_DATE
).toDateFormatted().toString(),
).toDateFormatted() ?: "",
documentImage = documentJson.getStringFromJsonOrEmpty(
key = DocumentJsonKeys.PORTRAIT
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ fun extractFullNameFromDocumentOrEmpty(document: Document): String {
document = document,
key = DocumentJsonKeys.LAST_NAME
)
return "$firstName $lastName"
return if (firstName.isNotBlank() && lastName.isNotBlank()) {
"$firstName $lastName"
} else if (firstName.isNotBlank()) {
firstName
} else if (lastName.isNotBlank()) {
lastName
} else {
""
}
}

fun keyIsBase64(key: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import androidx.annotation.VisibleForTesting
import eu.europa.ec.commonfeature.model.DocumentOptionItemUi
import eu.europa.ec.commonfeature.model.DocumentTypeUi
import eu.europa.ec.commonfeature.model.DocumentUi
import eu.europa.ec.commonfeature.ui.document_details.model.DocumentDetailsUi
import eu.europa.ec.uilogic.component.AppIcons
import eu.europa.ec.uilogic.component.InfoTextWithNameAndImageData
import eu.europa.ec.uilogic.component.InfoTextWithNameAndValueData

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
object TestsConstants {
Expand All @@ -45,6 +48,68 @@ object TestsConstants {
documentDetails = emptyList(),
)

val mockedBasicPidUi = mockedFullPidUi.copy(
documentDetails = listOf(
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "birth_city",
infoValues = arrayOf("KATRINEHOLM")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "gender",
infoValues = arrayOf("male")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_over_18",
infoValues = arrayOf("yes")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_birth_year",
infoValues = arrayOf("1985")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "expiry_date",
infoValues = arrayOf("30 Mar 2050")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "given_name",
infoValues = arrayOf("JAN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "family_name",
infoValues = arrayOf("ANDERSSON")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "age_over_65",
infoValues = arrayOf("no")
)
),
),
userFullName = "JAN ANDERSSON"
)

val mockedFullMdlUi = DocumentUi(
documentId = mockedId2,
documentName = mockedDocUiNameMdl,
Expand All @@ -54,6 +119,74 @@ object TestsConstants {
documentDetails = emptyList(),
)

val mockedBasicMdlUi = mockedFullMdlUi.copy(
documentDetails = listOf(
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "driving_privileges",
infoValues = arrayOf(
"issue_date: 1 Jul 2010\n" +
"expiry_date: 30 Mar 2050\n" +
"vehicle_category_code: A\n" +
"issue_date: 19 May 2008\n" +
"expiry_date: 30 Mar 2050\n" +
"vehicle_category_code: B"
)
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "expiry_date",
infoValues = arrayOf("30 Mar 2050")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "sex",
infoValues = arrayOf("male")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "birth_place",
infoValues = arrayOf("SWEDEN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "given_name",
infoValues = arrayOf("JAN")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "portrait",
infoValues = arrayOf("Shown above")
)
),
DocumentDetailsUi.DefaultItem(
itemData = InfoTextWithNameAndValueData
.create(
title = "family_name",
infoValues = arrayOf("ANDERSSON")
)
),
DocumentDetailsUi.SignatureItem(
itemData = InfoTextWithNameAndImageData(
title = "signature_usual_mark",
base64Image = "SE"
)
),
),
userFullName = "JAN ANDERSSON"
)

val mockedMdlUiWithNoUserNameAndNoUserImage: DocumentUi = mockedFullMdlUi

val mockedMdlUiWithNoExpirationDate: DocumentUi = mockedFullMdlUi.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import eu.europa.ec.commonfeature.util.TestsConstants.mockedUserFirstName
import eu.europa.ec.eudi.wallet.EudiWalletConfig
import eu.europa.ec.resourceslogic.R
import eu.europa.ec.resourceslogic.provider.ResourceProvider
import eu.europa.ec.testfeature.mockDocumentTypeUiToUiNameCall
import eu.europa.ec.testfeature.MockResourceProviderForStringCalls.mockDocumentTypeUiToUiNameCall
import eu.europa.ec.testfeature.mockedExceptionWithMessage
import eu.europa.ec.testfeature.mockedExceptionWithNoMessage
import eu.europa.ec.testfeature.mockedFullDocuments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fun DocumentDetailsScreen(
sheetState = bottomSheetState
) {
SheetContent(
documentTypeUiName = state.documentTypeUiName,
documentTypeUiName = state.document?.documentName,
onEventSent = {
viewModel.setEvent(it)
}
Expand Down Expand Up @@ -274,23 +274,25 @@ private fun Content(

@Composable
private fun SheetContent(
documentTypeUiName: String,
documentTypeUiName: String?,
onEventSent: (event: Event) -> Unit
) {
DialogBottomSheet(
title = stringResource(
id = R.string.document_details_bottom_sheet_delete_title,
documentTypeUiName
),
message = stringResource(
id = R.string.document_details_bottom_sheet_delete_subtitle,
documentTypeUiName
),
positiveButtonText = stringResource(id = R.string.document_details_bottom_sheet_delete_primary_button_text),
negativeButtonText = stringResource(id = R.string.document_details_bottom_sheet_delete_secondary_button_text),
onPositiveClick = { onEventSent(Event.BottomSheet.Delete.PrimaryButtonPressed) },
onNegativeClick = { onEventSent(Event.BottomSheet.Delete.SecondaryButtonPressed) }
)
documentTypeUiName?.let {
DialogBottomSheet(
title = stringResource(
id = R.string.document_details_bottom_sheet_delete_title,
it
),
message = stringResource(
id = R.string.document_details_bottom_sheet_delete_subtitle,
it
),
positiveButtonText = stringResource(id = R.string.document_details_bottom_sheet_delete_primary_button_text),
negativeButtonText = stringResource(id = R.string.document_details_bottom_sheet_delete_secondary_button_text),
onPositiveClick = { onEventSent(Event.BottomSheet.Delete.PrimaryButtonPressed) },
onNegativeClick = { onEventSent(Event.BottomSheet.Delete.SecondaryButtonPressed) }
)
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ package eu.europa.ec.issuancefeature.ui.document.details
import androidx.lifecycle.viewModelScope
import eu.europa.ec.commonfeature.config.IssuanceFlowUiConfig
import eu.europa.ec.commonfeature.model.DocumentUi
import eu.europa.ec.commonfeature.model.toUiName
import eu.europa.ec.issuancefeature.interactor.document.DocumentDetailsInteractor
import eu.europa.ec.issuancefeature.interactor.document.DocumentDetailsInteractorDeleteDocumentPartialState
import eu.europa.ec.issuancefeature.interactor.document.DocumentDetailsInteractorPartialState
import eu.europa.ec.resourceslogic.provider.ResourceProvider
import eu.europa.ec.uilogic.component.AppIcons
import eu.europa.ec.uilogic.component.HeaderData
import eu.europa.ec.uilogic.component.content.ContentErrorConfig
Expand Down Expand Up @@ -53,7 +51,6 @@ data class State(
val isBottomSheetOpen: Boolean = false,

val document: DocumentUi? = null,
val documentTypeUiName: String = "",
val headerData: HeaderData? = null
) : ViewState

Expand Down Expand Up @@ -93,7 +90,6 @@ sealed class Effect : ViewSideEffect {
@KoinViewModel
class DocumentDetailsViewModel(
private val documentDetailsInteractor: DocumentDetailsInteractor,
private val resourceProvider: ResourceProvider,
@InjectedParam private val detailsType: IssuanceFlowUiConfig,
@InjectedParam private val documentId: String,
@InjectedParam private val documentType: String,
Expand Down Expand Up @@ -166,15 +162,13 @@ class DocumentDetailsViewModel(
when (response) {
is DocumentDetailsInteractorPartialState.Success -> {
val documentUi = response.documentUi
val documentTypeUiName = documentUi.documentType.toUiName(resourceProvider)
setState {
copy(
isLoading = false,
error = null,
document = documentUi,
documentTypeUiName = documentTypeUiName,
headerData = HeaderData(
title = documentTypeUiName,
title = documentUi.documentName,
subtitle = documentUi.userFullName.orEmpty(),
base64Image = documentUi.documentImage,
icon = AppIcons.IdStroke
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import eu.europa.ec.commonfeature.util.TestsConstants.mockedMdlOptionItemUi
import eu.europa.ec.commonfeature.util.TestsConstants.mockedPidOptionItemUi
import eu.europa.ec.commonfeature.util.TestsConstants.mockedSampleDataOptionItemUi
import eu.europa.ec.resourceslogic.provider.ResourceProvider
import eu.europa.ec.testfeature.mockDocumentTypeUiToUiNameCall
import eu.europa.ec.testfeature.MockResourceProviderForStringCalls.mockDocumentTypeUiToUiNameCall
import eu.europa.ec.testfeature.mockedExceptionWithMessage
import eu.europa.ec.testfeature.mockedExceptionWithNoMessage
import eu.europa.ec.testfeature.mockedFullMdl
Expand Down
Loading

0 comments on commit 32bcea9

Please sign in to comment.