From 5af63adc84d1f3286fc7786533c5e8a7b6858cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E2=89=A1ZRS?= <12814349+LZRS@users.noreply.github.com> Date: Fri, 18 Oct 2024 18:19:50 +0300 Subject: [PATCH] Separate validationRequest listener to unblock main thread during questionnaire response submission --- .../fhircore/engine/di/FhirValidatorModule.kt | 12 ++ .../util/extension/FhirValidatorExtension.kt | 76 ---------- .../validation/ResourceValidationRequest.kt | 94 +++++++++++++ .../ResourceValidationRequestTest.kt} | 54 +++++--- .../questionnaire/QuestionnaireViewModel.kt | 130 ++++++++++-------- .../QuestionnaireViewModelTest.kt | 20 +-- 6 files changed, 229 insertions(+), 157 deletions(-) delete mode 100644 android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt create mode 100644 android/engine/src/main/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequest.kt rename android/engine/src/test/java/org/smartregister/fhircore/engine/util/{extension/FhirValidatorExtensionTest.kt => validation/ResourceValidationRequestTest.kt} (65%) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.kt index 53af79a8a9..4e49ded6ba 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.kt @@ -20,6 +20,7 @@ import ca.uhn.fhir.context.FhirContext import ca.uhn.fhir.context.support.DefaultProfileValidationSupport import ca.uhn.fhir.context.support.IValidationSupport import ca.uhn.fhir.validation.FhirValidator +import dagger.Lazy import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -30,6 +31,8 @@ import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerVali import org.hl7.fhir.common.hapi.validation.support.UnknownCodeSystemWarningValidationSupport import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator +import org.smartregister.fhircore.engine.util.DispatcherProvider +import org.smartregister.fhircore.engine.util.validation.ResourceValidationRequestHandler @Module @InstallIn(SingletonComponent::class) @@ -52,4 +55,13 @@ class FhirValidatorModule { instanceValidator.invalidateCaches() return fhirContext.newValidator().apply { registerValidatorModule(instanceValidator) } } + + @Provides + @Singleton + fun provideResourceValidationRequestHandler( + fhirValidatorProvider: Lazy, + dispatcherProvider: DispatcherProvider, + ): ResourceValidationRequestHandler { + return ResourceValidationRequestHandler(fhirValidatorProvider.get(), dispatcherProvider) + } } diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt deleted file mode 100644 index 3a7c28f60f..0000000000 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtension.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2021-2024 Ona Systems, Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.smartregister.fhircore.engine.util.extension - -import ca.uhn.fhir.validation.FhirValidator -import ca.uhn.fhir.validation.ResultSeverityEnum -import ca.uhn.fhir.validation.ValidationResult -import kotlin.coroutines.coroutineContext -import kotlinx.coroutines.withContext -import org.hl7.fhir.r4.model.Resource -import org.smartregister.fhircore.engine.BuildConfig -import timber.log.Timber - -data class ResourceValidationResult( - val resource: Resource, - val validationResult: ValidationResult, -) { - val errorMessages - get() = buildString { - val messages = - validationResult.messages.filter { - it.severity.ordinal >= ResultSeverityEnum.WARNING.ordinal - } - - for (validationMsg in messages) { - appendLine( - "${validationMsg.message} - ${validationMsg.locationString} -- (${validationMsg.severity})", - ) - } - } -} - -data class FhirValidatorResultsWrapper(val results: List = emptyList()) { - val errorMessages = results.map { it.errorMessages } -} - -suspend fun FhirValidator.checkResourceValid( - vararg resource: Resource, - isDebug: Boolean = BuildConfig.DEBUG, -): FhirValidatorResultsWrapper { - if (!isDebug) return FhirValidatorResultsWrapper() - - return withContext(coroutineContext) { - FhirValidatorResultsWrapper( - results = - resource.map { - val result = this@checkResourceValid.validateWithResult(it) - ResourceValidationResult(it, result) - }, - ) - } -} - -fun FhirValidatorResultsWrapper.logErrorMessages() { - results.forEach { - if (it.errorMessages.isNotBlank()) { - Timber.tag("$TAG (${it.resource.referenceValue()})").e(it.errorMessages) - } - } -} - -private const val TAG = "FhirValidator" diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequest.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequest.kt new file mode 100644 index 0000000000..ba398a5bf8 --- /dev/null +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequest.kt @@ -0,0 +1,94 @@ +/* + * Copyright 2021-2024 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.engine.util.validation + +import ca.uhn.fhir.validation.FhirValidator +import ca.uhn.fhir.validation.ResultSeverityEnum +import ca.uhn.fhir.validation.ValidationResult +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch +import org.hl7.fhir.r4.model.Resource +import org.smartregister.fhircore.engine.util.DispatcherProvider +import org.smartregister.fhircore.engine.util.extension.referenceValue +import timber.log.Timber + +data class ResourceValidationRequest(val resources: List) { + constructor(vararg resource: Resource) : this(resource.toList()) +} + +class ResourceValidationRequestHandler( + private val fhirValidator: FhirValidator, + private val dispatcherProvider: DispatcherProvider, +) { + fun handleResourceValidationRequest(request: ResourceValidationRequest) { + CoroutineScope(dispatcherProvider.io()).launch { + val resources = request.resources + fhirValidator.checkResources(resources).logErrorMessages() + } + } +} + +internal data class ResourceValidationResult( + val resource: Resource, + val validationResult: ValidationResult, +) { + val errorMessages + get() = buildString { + val messages = + validationResult.messages.filter { + it.severity.ordinal >= ResultSeverityEnum.WARNING.ordinal + } + if (messages.isNotEmpty()) { + appendLine(resource.referenceValue()) + } + for (validationMsg in messages) { + appendLine( + "${validationMsg.locationString} - ${validationMsg.message} -- (${validationMsg.severity})", + ) + } + } +} + +internal class FhirValidatorResultsWrapper( + val results: List = emptyList(), +) { + val errorMessages = results.map { it.errorMessages } + + fun logErrorMessages() { + results.forEach { + if (it.errorMessages.isNotBlank()) { + Timber.tag(TAG).e(it.errorMessages) + } + } + } + + companion object { + private const val TAG = "FhirValidatorResult" + } +} + +internal fun FhirValidator.checkResources( + resources: List, +): FhirValidatorResultsWrapper { + return FhirValidatorResultsWrapper( + results = + resources.map { + val result = this@checkResources.validateWithResult(it) + ResourceValidationResult(it, result) + }, + ) +} diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtensionTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequestTest.kt similarity index 65% rename from android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtensionTest.kt rename to android/engine/src/test/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequestTest.kt index f2dff7fe0d..0e3d8d2a8f 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/FhirValidatorExtensionTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/validation/ResourceValidationRequestTest.kt @@ -14,16 +14,16 @@ * limitations under the License. */ -package org.smartregister.fhircore.engine.util.extension +package org.smartregister.fhircore.engine.util.validation import ca.uhn.fhir.validation.FhirValidator import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest -import io.mockk.spyk +import io.mockk.mockkObject +import io.mockk.unmockkObject import io.mockk.verify import javax.inject.Inject import kotlinx.coroutines.test.runTest -import org.hl7.fhir.instance.model.api.IBaseResource import org.hl7.fhir.r4.model.CarePlan import org.hl7.fhir.r4.model.Patient import org.hl7.fhir.r4.model.Reference @@ -32,33 +32,51 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.smartregister.fhircore.engine.robolectric.RobolectricTest +import timber.log.Timber @HiltAndroidTest -class FhirValidatorExtensionTest : RobolectricTest() { - +class ResourceValidationRequestTest : RobolectricTest() { @get:Rule var hiltRule = HiltAndroidRule(this) @Inject lateinit var validator: FhirValidator + @Inject lateinit var resourceValidationRequestHandler: ResourceValidationRequestHandler + @Before fun setUp() { hiltRule.inject() } @Test - fun testCheckResourceValidRunsNoValidationWhenBuildTypeIsNotDebug() = runTest { - val basicResource = CarePlan() - val fhirValidatorSpy = spyk(validator) - val resultsWrapper = fhirValidatorSpy.checkResourceValid(basicResource, isDebug = false) - Assert.assertTrue(resultsWrapper.results.isEmpty()) - verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any()) } - verify(exactly = 0) { fhirValidatorSpy.validateWithResult(any()) } + fun testHandleResourceValidationRequestValidatesInvalidResourceLoggingErrors() = runTest { + mockkObject(Timber) + val resource = + CarePlan().apply { + id = "test-careplan" + status = CarePlan.CarePlanStatus.ACTIVE + intent = CarePlan.CarePlanIntent.PLAN + subject = Reference("f4bd3e29-f0f8-464e-97af-923b83664ccc") + } + val validationRequest = ResourceValidationRequest(resource) + resourceValidationRequestHandler.handleResourceValidationRequest(validationRequest) + verify { + Timber.e( + withArg { + Assert.assertTrue( + it.contains( + "CarePlan.subject - The syntax of the reference 'f4bd3e29-f0f8-464e-97af-923b83664ccc' looks incorrect, and it should be checked -- (WARNING)", + ), + ) + }, + ) + } + unmockkObject(Timber) } @Test fun testCheckResourceValidValidatesResourceStructureWhenCarePlanResourceInvalid() = runTest { val basicCarePlan = CarePlan() - val resultsWrapper = validator.checkResourceValid(basicCarePlan) + val resultsWrapper = validator.checkResources(listOf(basicCarePlan)) Assert.assertTrue( resultsWrapper.errorMessages.any { it.contains( @@ -85,13 +103,13 @@ class FhirValidatorExtensionTest : RobolectricTest() { intent = CarePlan.CarePlanIntent.PLAN subject = Reference("Task/unknown") } - val resultsWrapper = validator.checkResourceValid(carePlan) + val resultsWrapper = validator.checkResources(listOf(carePlan)) Assert.assertEquals(1, resultsWrapper.errorMessages.size) Assert.assertTrue( resultsWrapper.errorMessages .first() .contains( - "The type 'Task' implied by the reference URL Task/unknown is not a valid Target for this element (must be one of [Group, Patient]) - CarePlan.subject", + "CarePlan.subject - The type 'Task' implied by the reference URL Task/unknown is not a valid Target for this element (must be one of [Group, Patient])", ignoreCase = true, ), ) @@ -105,13 +123,13 @@ class FhirValidatorExtensionTest : RobolectricTest() { intent = CarePlan.CarePlanIntent.PLAN subject = Reference("unknown") } - val resultsWrapper = validator.checkResourceValid(carePlan) + val resultsWrapper = validator.checkResources(listOf(carePlan)) Assert.assertEquals(1, resultsWrapper.errorMessages.size) Assert.assertTrue( resultsWrapper.errorMessages .first() .contains( - "The syntax of the reference 'unknown' looks incorrect, and it should be checked - CarePlan.subject", + "CarePlan.subject - The syntax of the reference 'unknown' looks incorrect, and it should be checked", ignoreCase = true, ), ) @@ -126,7 +144,7 @@ class FhirValidatorExtensionTest : RobolectricTest() { intent = CarePlan.CarePlanIntent.PLAN subject = Reference(patient) } - val resultsWrapper = validator.checkResourceValid(carePlan) + val resultsWrapper = validator.checkResources(listOf(carePlan)) Assert.assertEquals(1, resultsWrapper.errorMessages.size) Assert.assertTrue(resultsWrapper.errorMessages.first().isBlank()) } diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt index f01d407979..363802a72c 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt @@ -23,7 +23,6 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import ca.uhn.fhir.context.FhirContext -import ca.uhn.fhir.validation.FhirValidator import com.google.android.fhir.datacapture.extensions.logicalId import com.google.android.fhir.datacapture.mapping.ResourceMapper import com.google.android.fhir.datacapture.mapping.StructureMapExtractionContext @@ -34,12 +33,12 @@ import com.google.android.fhir.db.ResourceNotFoundException import com.google.android.fhir.search.Search import com.google.android.fhir.search.filter.TokenParamFilterCriterion import com.google.android.fhir.workflow.FhirOperator +import dagger.Lazy import dagger.hilt.android.lifecycle.HiltViewModel import java.util.Date import java.util.LinkedList import java.util.UUID import javax.inject.Inject -import javax.inject.Provider import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -83,7 +82,6 @@ import org.smartregister.fhircore.engine.util.extension.appendPractitionerInfo import org.smartregister.fhircore.engine.util.extension.appendRelatedEntityLocation import org.smartregister.fhircore.engine.util.extension.asReference import org.smartregister.fhircore.engine.util.extension.batchedSearch -import org.smartregister.fhircore.engine.util.extension.checkResourceValid import org.smartregister.fhircore.engine.util.extension.clearText import org.smartregister.fhircore.engine.util.extension.cqfLibraryUrls import org.smartregister.fhircore.engine.util.extension.extractByStructureMap @@ -92,13 +90,14 @@ import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid import org.smartregister.fhircore.engine.util.extension.find import org.smartregister.fhircore.engine.util.extension.generateMissingId import org.smartregister.fhircore.engine.util.extension.isIn -import org.smartregister.fhircore.engine.util.extension.logErrorMessages import org.smartregister.fhircore.engine.util.extension.packRepeatedGroups import org.smartregister.fhircore.engine.util.extension.prepopulateWithComputedConfigValues import org.smartregister.fhircore.engine.util.extension.showToast import org.smartregister.fhircore.engine.util.extension.updateLastUpdated import org.smartregister.fhircore.engine.util.fhirpath.FhirPathDataExtractor import org.smartregister.fhircore.engine.util.helper.TransformSupportServices +import org.smartregister.fhircore.engine.util.validation.ResourceValidationRequest +import org.smartregister.fhircore.engine.util.validation.ResourceValidationRequestHandler import org.smartregister.fhircore.quest.R import timber.log.Timber @@ -113,7 +112,7 @@ constructor( val transformSupportServices: TransformSupportServices, val sharedPreferencesHelper: SharedPreferencesHelper, val fhirOperator: FhirOperator, - val fhirValidatorProvider: Provider, + val fhirValidatorRequestHandlerProvider: Lazy, val fhirPathDataExtractor: FhirPathDataExtractor, val configurationRegistry: ConfigurationRegistry, ) : ViewModel() { @@ -265,9 +264,17 @@ constructor( } } - suspend fun validateWithFhirValidator(vararg resource: Resource) { - val fhirValidator = fhirValidatorProvider.get() - fhirValidator.checkResourceValid(*resource).logErrorMessages() + fun validateWithFhirValidator(vararg resource: Resource) { + if (BuildConfig.DEBUG) { + fhirValidatorRequestHandlerProvider + .get() + .handleResourceValidationRequest( + request = + ResourceValidationRequest( + *resource, + ), + ) + } } suspend fun retireUsedQuestionnaireUniqueId( @@ -346,6 +353,7 @@ constructor( ) { questionnaireResponse.subject = this.logicalId.asReference(subjectType) } + if (questionnaireConfig.isEditable()) { if (resourceType == subjectType) { this.id = questionnaireResponse.subject.extractId() @@ -361,19 +369,23 @@ constructor( .fhirPathExpression val currentResourceIdentifier = - fhirPathDataExtractor.extractValue( - base = this, - expression = fhirPathExpression, - ) + withContext(dispatcherProvider.default()) { + fhirPathDataExtractor.extractValue( + base = this@run, + expression = fhirPathExpression, + ) + } // Search for resource with property value matching extracted value val resource = previouslyExtractedResources.getValue(resourceType).find { val extractedValue = - fhirPathDataExtractor.extractValue( - base = it, - expression = fhirPathExpression, - ) + withContext(dispatcherProvider.default()) { + fhirPathDataExtractor.extractValue( + base = it, + expression = fhirPathExpression, + ) + } extractedValue.isNotEmpty() && extractedValue.equals(currentResourceIdentifier, true) } @@ -629,25 +641,27 @@ constructor( ): Bundle = kotlin .runCatching { - if (extractByStructureMap) { - ResourceMapper.extract( - questionnaire = questionnaire, - questionnaireResponse = questionnaireResponse, - structureMapExtractionContext = - StructureMapExtractionContext( - transformSupportServices = transformSupportServices, - structureMapProvider = { structureMapUrl: String?, _: IWorkerContext -> - structureMapUrl?.substringAfterLast("/")?.let { - defaultRepository.loadResource(it) - } - }, - ), - ) - } else { - ResourceMapper.extract( - questionnaire = questionnaire, - questionnaireResponse = questionnaireResponse, - ) + withContext(dispatcherProvider.default()) { + if (extractByStructureMap) { + ResourceMapper.extract( + questionnaire = questionnaire, + questionnaireResponse = questionnaireResponse, + structureMapExtractionContext = + StructureMapExtractionContext( + transformSupportServices = transformSupportServices, + structureMapProvider = { structureMapUrl: String?, _: IWorkerContext -> + structureMapUrl?.substringAfterLast("/")?.let { + defaultRepository.loadResource(it) + } + }, + ), + ) + } else { + ResourceMapper.extract( + questionnaire = questionnaire, + questionnaireResponse = questionnaireResponse, + ) + } } } .onFailure { exception -> @@ -755,18 +769,20 @@ constructor( } } - return QuestionnaireResponseValidator.validateQuestionnaireResponse( - questionnaire = Questionnaire().apply { item = validQuestionnaireItems }, - questionnaireResponse = - QuestionnaireResponse().apply { - item = validQuestionnaireResponseItems - packRepeatedGroups() - }, - context = context, - ) - .values - .flatten() - .all { it is Valid || it is NotValidated } + return withContext(dispatcherProvider.default()) { + QuestionnaireResponseValidator.validateQuestionnaireResponse( + questionnaire = Questionnaire().apply { item = validQuestionnaireItems }, + questionnaireResponse = + QuestionnaireResponse().apply { + item = validQuestionnaireResponseItems + packRepeatedGroups() + }, + context = context, + ) + .values + .flatten() + .all { it is Valid || it is NotValidated } + } } suspend fun executeCql( @@ -1071,10 +1087,12 @@ constructor( launchContextResources(resourceType, resourceIdentifier, actionParameters) // Populate questionnaire with initial default values - ResourceMapper.populate( - questionnaire, - launchContexts = launchContextResources.associateBy { it.resourceType.name.lowercase() }, - ) + withContext(dispatcherProvider.default()) { + ResourceMapper.populate( + questionnaire, + launchContexts = launchContextResources.associateBy { it.resourceType.name.lowercase() }, + ) + } questionnaire.prepopulateWithComputedConfigValues( questionnaireConfig, @@ -1088,10 +1106,12 @@ constructor( computedValues, ) - fhirPathDataExtractor.extractValue( - base = uniqueIdResource, - expression = uniqueIdAssignmentConfig.idFhirPathExpression, - ) + withContext(dispatcherProvider.default()) { + fhirPathDataExtractor.extractValue( + base = uniqueIdResource, + expression = uniqueIdAssignmentConfig.idFhirPathExpression, + ) + } }, ) diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt index 578a91120f..33bd7d5d67 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt @@ -19,7 +19,6 @@ package org.smartregister.fhircore.quest.ui.questionnaire import android.app.Application import androidx.test.core.app.ApplicationProvider import ca.uhn.fhir.parser.IParser -import ca.uhn.fhir.validation.FhirValidator import com.google.android.fhir.FhirEngine import com.google.android.fhir.SearchResult import com.google.android.fhir.datacapture.extensions.logicalId @@ -29,6 +28,7 @@ import com.google.android.fhir.get import com.google.android.fhir.knowledge.KnowledgeManager import com.google.android.fhir.search.Search import com.google.android.fhir.workflow.FhirOperator +import dagger.Lazy import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import io.mockk.coEvery @@ -46,7 +46,6 @@ import java.io.File import java.util.Date import java.util.UUID import javax.inject.Inject -import javax.inject.Provider import kotlin.test.assertEquals import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -57,6 +56,7 @@ import org.hl7.fhir.r4.model.Attachment import org.hl7.fhir.r4.model.Basic import org.hl7.fhir.r4.model.BooleanType import org.hl7.fhir.r4.model.Bundle +import org.hl7.fhir.r4.model.CarePlan import org.hl7.fhir.r4.model.CodeableConcept import org.hl7.fhir.r4.model.Coding import org.hl7.fhir.r4.model.Consent @@ -114,6 +114,7 @@ import org.smartregister.fhircore.engine.util.extension.isToday import org.smartregister.fhircore.engine.util.extension.valueToString import org.smartregister.fhircore.engine.util.extension.yesterday import org.smartregister.fhircore.engine.util.fhirpath.FhirPathDataExtractor +import org.smartregister.fhircore.engine.util.validation.ResourceValidationRequestHandler import org.smartregister.fhircore.quest.app.fakes.Faker import org.smartregister.fhircore.quest.assertResourceEquals import org.smartregister.fhircore.quest.robolectric.RobolectricTest @@ -129,7 +130,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { @Inject lateinit var sharedPreferencesHelper: SharedPreferencesHelper - @Inject lateinit var fhirValidatorProvider: Provider + @Inject lateinit var fhirValidatorRequestHandlerProvider: Lazy @Inject lateinit var configService: ConfigService @@ -210,7 +211,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { resourceDataRulesExecutor = resourceDataRulesExecutor, transformSupportServices = mockk(), sharedPreferencesHelper = sharedPreferencesHelper, - fhirValidatorProvider = fhirValidatorProvider, + fhirValidatorRequestHandlerProvider = fhirValidatorRequestHandlerProvider, fhirOperator = fhirOperator, fhirPathDataExtractor = fhirPathDataExtractor, configurationRegistry = configurationRegistry, @@ -650,7 +651,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { transformSupportServices = mockk(), sharedPreferencesHelper = sharedPreferencesHelper, fhirOperator = fhirOperator, - fhirValidatorProvider = fhirValidatorProvider, + fhirValidatorRequestHandlerProvider = fhirValidatorRequestHandlerProvider, fhirPathDataExtractor = fhirPathDataExtractor, configurationRegistry = configurationRegistry, ) @@ -1103,6 +1104,9 @@ class QuestionnaireViewModelTest : RobolectricTest() { fun testGenerateCarePlan() = runTest { val bundle = Bundle().apply { addEntry(Bundle.BundleEntryComponent().apply { resource = patient }) } + coEvery { + fhirCarePlanGenerator.generateOrUpdateCarePlan(any(), any(), any(), any()) + } returns CarePlan() val questionnaireConfig = questionnaireConfig.copy(planDefinitions = listOf("planDefId")) questionnaireViewModel.generateCarePlan(patient, bundle, questionnaireConfig) @@ -1829,7 +1833,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { transformSupportServices = mockk(), sharedPreferencesHelper = sharedPreferencesHelper, fhirOperator = fhirOperator, - fhirValidatorProvider = fhirValidatorProvider, + fhirValidatorRequestHandlerProvider = fhirValidatorRequestHandlerProvider, fhirPathDataExtractor = fhirPathDataExtractor, configurationRegistry = configurationRegistry, ) @@ -1891,7 +1895,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { transformSupportServices = mockk(), sharedPreferencesHelper = sharedPreferencesHelper, fhirOperator = fhirOperator, - fhirValidatorProvider = fhirValidatorProvider, + fhirValidatorRequestHandlerProvider = fhirValidatorRequestHandlerProvider, fhirPathDataExtractor = fhirPathDataExtractor, configurationRegistry = configurationRegistry, ) @@ -1966,7 +1970,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { transformSupportServices = mockk(), sharedPreferencesHelper = sharedPreferencesHelper, fhirOperator = fhirOperator, - fhirValidatorProvider = fhirValidatorProvider, + fhirValidatorRequestHandlerProvider = fhirValidatorRequestHandlerProvider, fhirPathDataExtractor = fhirPathDataExtractor, configurationRegistry = configurationRegistry, )