Skip to content

Commit

Permalink
change: Corrections and TestOptionsSelectionViewModel unit testing
Browse files Browse the repository at this point in the history
Call modifieed in OptionsSelectionInteractor to only retrieve selected QTSP and not both file and qtsp as it is not needed at that step.
TestOptionsSelectionViewModel created with tests targeting high coverage and test cases commenting, previous test classes removed.

Signed-off-by: Christos Kaitatzis <[email protected]>
  • Loading branch information
ckaitatzis committed Feb 6, 2025
1 parent 00ad2bd commit aa4e041
Show file tree
Hide file tree
Showing 6 changed files with 1,374 additions and 869 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,30 @@ import eu.europa.ec.eudi.rqesui.domain.controller.EudiRqesSetSelectedQtspPartial
import eu.europa.ec.eudi.rqesui.domain.controller.RqesController
import eu.europa.ec.eudi.rqesui.domain.entities.error.EudiRQESUiError
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.CertificateData
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.DocumentData
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.QtspData
import eu.europa.ec.eudi.rqesui.infrastructure.provider.ResourceProvider
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

internal sealed class OptionsSelectionInteractorGetSelectedFileAndQtspPartialState {
internal sealed class OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState {
data class Success(
val selectedFile: DocumentData,
val selectedQtsp: QtspData,
) : OptionsSelectionInteractorGetSelectedFileAndQtspPartialState()
val certificates: List<CertificateData>,
) : OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState()

data class Failure(
val error: EudiRQESUiError
) : OptionsSelectionInteractorGetSelectedFileAndQtspPartialState()
) : OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState()
}

internal sealed class OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState {
internal sealed class OptionsSelectionInteractorGetSelectedQtspPartialState {
data class Success(
val certificates: List<CertificateData>,
) : OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState()
val selectedQtsp: QtspData,
) : OptionsSelectionInteractorGetSelectedQtspPartialState()

data class Failure(
val error: EudiRQESUiError
) : OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState()
) : OptionsSelectionInteractorGetSelectedQtspPartialState()
}

internal interface OptionsSelectionInteractor {
Expand All @@ -63,7 +61,7 @@ internal interface OptionsSelectionInteractor {

fun updateQtspUserSelection(qtspData: QtspData): EudiRqesSetSelectedQtspPartialState

fun getSelectedFileAndQtsp(): OptionsSelectionInteractorGetSelectedFileAndQtspPartialState
fun getSelectedQtsp(): OptionsSelectionInteractorGetSelectedQtspPartialState

suspend fun getServiceAuthorizationUrl(rqesService: RQESService): EudiRqesGetServiceAuthorizationUrlPartialState

Expand Down Expand Up @@ -173,34 +171,23 @@ internal class OptionsSelectionInteractorImpl(
}
}

override fun getSelectedFileAndQtsp(): OptionsSelectionInteractorGetSelectedFileAndQtspPartialState {
override fun getSelectedQtsp(): OptionsSelectionInteractorGetSelectedQtspPartialState {
return runCatching {
when (val getSelectedFileResponse = eudiRqesController.getSelectedFile()) {
is EudiRqesGetSelectedFilePartialState.Failure -> {
return@runCatching OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure(
error = getSelectedFileResponse.error
when (val getSelectedQtspResponse = eudiRqesController.getSelectedQtsp()) {
is EudiRqesGetSelectedQtspPartialState.Failure -> {
return@runCatching OptionsSelectionInteractorGetSelectedQtspPartialState.Failure(
error = getSelectedQtspResponse.error
)
}

is EudiRqesGetSelectedFilePartialState.Success -> {
when (val getSelectedQtspResponse = eudiRqesController.getSelectedQtsp()) {
is EudiRqesGetSelectedQtspPartialState.Failure -> {
return@runCatching OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure(
error = getSelectedQtspResponse.error
)
}

is EudiRqesGetSelectedQtspPartialState.Success -> {
return@runCatching OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Success(
selectedFile = getSelectedFileResponse.file,
selectedQtsp = getSelectedQtspResponse.qtsp,
)
}
}
is EudiRqesGetSelectedQtspPartialState.Success -> {
return@runCatching OptionsSelectionInteractorGetSelectedQtspPartialState.Success(
selectedQtsp = getSelectedQtspResponse.qtsp,
)
}
}
}.getOrElse {
OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure(
OptionsSelectionInteractorGetSelectedQtspPartialState.Failure(
error = EudiRQESUiError(
message = it.localizedMessage ?: genericErrorMsg
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import eu.europa.ec.eudi.rqesui.domain.controller.EudiRqesSetSelectedQtspPartial
import eu.europa.ec.eudi.rqesui.domain.entities.localization.LocalizableKey
import eu.europa.ec.eudi.rqesui.domain.interactor.OptionsSelectionInteractor
import eu.europa.ec.eudi.rqesui.domain.interactor.OptionsSelectionInteractorAuthorizeServiceAndFetchCertificatesPartialState
import eu.europa.ec.eudi.rqesui.domain.interactor.OptionsSelectionInteractorGetSelectedFileAndQtspPartialState
import eu.europa.ec.eudi.rqesui.domain.interactor.OptionsSelectionInteractorGetSelectedQtspPartialState
import eu.europa.ec.eudi.rqesui.domain.serializer.UiSerializer
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.CertificateData
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.DocumentData
Expand Down Expand Up @@ -182,7 +182,7 @@ internal class OptionsSelectionViewModel(

CERTIFICATE_SELECTION_STATE -> {
createQTSPSelectionItemOnSelectCertificateStep(event = event)
createCertificateSelectionItemOnSelectCertificateStep(event = event)
createCertificateSelectionItemOnSelectCertificateStep()
}
}
}
Expand Down Expand Up @@ -333,7 +333,6 @@ internal class OptionsSelectionViewModel(
is EudiRqesGetSelectedFilePartialState.Failure -> {
setState {
copy(
documentSelectionItem = null,
error = ContentErrorConfig(
onRetry = { setEvent(event) },
errorSubTitle = response.error.message,
Expand Down Expand Up @@ -373,7 +372,6 @@ internal class OptionsSelectionViewModel(
is EudiRqesGetSelectedFilePartialState.Failure -> {
setState {
copy(
qtspServiceSelectionItem = null,
error = ContentErrorConfig(
onRetry = { setEvent(event) },
errorSubTitle = response.error.message,
Expand Down Expand Up @@ -406,11 +404,10 @@ internal class OptionsSelectionViewModel(
}

private fun createQTSPSelectionItemOnSelectCertificateStep(event: Event) {
when (val response = optionsSelectionInteractor.getSelectedFileAndQtsp()) {
is OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure -> {
when (val response = optionsSelectionInteractor.getSelectedQtsp()) {
is OptionsSelectionInteractorGetSelectedQtspPartialState.Failure -> {
setState {
copy(
qtspServiceSelectionItem = null,
error = ContentErrorConfig(
onRetry = { setEvent(event) },
errorSubTitle = response.error.message,
Expand All @@ -423,7 +420,7 @@ internal class OptionsSelectionViewModel(
}
}

is OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Success -> {
is OptionsSelectionInteractorGetSelectedQtspPartialState.Success -> {
setState {
copy(
qtspServiceSelectionItem = SelectionOptionUi(
Expand Down Expand Up @@ -466,45 +463,22 @@ internal class OptionsSelectionViewModel(
}
}

private fun createCertificateSelectionItemOnSelectCertificateStep(event: Event) {
when (val response = optionsSelectionInteractor.getSelectedFile()) {
is EudiRqesGetSelectedFilePartialState.Failure -> {
setState {
copy(
certificateSelectionItem = null,
error = ContentErrorConfig(
onRetry = {
setEvent(Event.DismissError)
setEvent(event)
},
errorSubTitle = response.error.message,
onCancel = {
setEvent(Event.DismissError)
setEffect { Effect.Navigation.Finish }
}
)
)
}
}

is EudiRqesGetSelectedFilePartialState.Success -> {
setState {
copy(
certificateSelectionItem = SelectionOptionUi(
overlineText = null,
mainText = resourceProvider.getLocalizedString(LocalizableKey.SelectSigningCertificateTitle),
subtitle = resourceProvider.getLocalizedString(LocalizableKey.SelectCertificateSubtitle),
leadingIcon = AppIcons.StepThree,
trailingIcon = AppIcons.KeyboardArrowRight,
enabled = true,
event = Event.CertificateSelectionItemPressed
)
)
}
setEffect {
Effect.OnCertificateSelectionItemCreated
}
}
private fun createCertificateSelectionItemOnSelectCertificateStep() {
setState {
copy(
certificateSelectionItem = SelectionOptionUi(
overlineText = null,
mainText = resourceProvider.getLocalizedString(LocalizableKey.SelectSigningCertificateTitle),
subtitle = resourceProvider.getLocalizedString(LocalizableKey.SelectCertificateSubtitle),
leadingIcon = AppIcons.StepThree,
trailingIcon = AppIcons.KeyboardArrowRight,
enabled = true,
event = Event.CertificateSelectionItemPressed
)
)
}
setEffect {
Effect.OnCertificateSelectionItemCreated
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class TestOptionsSelectionInteractor {
}
//endregion

//region getSelectedFileAndQtsp
//region getSelectedQtsp
@Test
fun `Given both file and qtsp are selected, When getSelectedFileAndQtsp is called, Then return Success`() {
// Arrange
Expand All @@ -243,31 +243,30 @@ class TestOptionsSelectionInteractor {
.thenReturn(EudiRqesGetSelectedQtspPartialState.Success(qtsp = qtspData))

// Act
val result = interactor.getSelectedFileAndQtsp()
val result = interactor.getSelectedQtsp()

// Assert
assertTrue(result is OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Success)
with(result as OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Success) {
assertEquals(documentData, selectedFile)
assertTrue(result is OptionsSelectionInteractorGetSelectedQtspPartialState.Success)
with(result as OptionsSelectionInteractorGetSelectedQtspPartialState.Success) {
assertEquals(qtspData, selectedQtsp)
}
}

@Test
fun `Given selected file retrieval fails, When getSelectedFileAndQtsp is called, Then return Failure`() {
// Arrange
val error = EudiRQESUiError(message = mockedPlainFailureMessage)
val error = EudiRQESUiError(message = mockedGenericErrorMessage)
whenever(eudiController.getSelectedFile())
.thenReturn(EudiRqesGetSelectedFilePartialState.Failure(error))

// Act
val result = interactor.getSelectedFileAndQtsp()
val result = interactor.getSelectedQtsp()

// Assert
assertTrue(result is OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure)
assertTrue(result is OptionsSelectionInteractorGetSelectedQtspPartialState.Failure)
assertEquals(
error,
(result as OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure).error
error.message,
(result as OptionsSelectionInteractorGetSelectedQtspPartialState.Failure).error.message
)
}

Expand All @@ -281,13 +280,13 @@ class TestOptionsSelectionInteractor {
.thenReturn(EudiRqesGetSelectedQtspPartialState.Failure(error = failureError))

// Act
val result = interactor.getSelectedFileAndQtsp()
val result = interactor.getSelectedQtsp()

// Assert
assertTrue(result is OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure)
assertTrue(result is OptionsSelectionInteractorGetSelectedQtspPartialState.Failure)
assertEquals(
failureError,
(result as OptionsSelectionInteractorGetSelectedFileAndQtspPartialState.Failure).error
(result as OptionsSelectionInteractorGetSelectedQtspPartialState.Failure).error
)
}
//endregion
Expand Down
Loading

0 comments on commit aa4e041

Please sign in to comment.