Skip to content

Commit

Permalink
Save unpacked response (#3455)
Browse files Browse the repository at this point in the history
* Fix packed response

* Add tests

* spotless
  • Loading branch information
FikriMilano authored Aug 19, 2024
1 parent 70ceb20 commit 115c4f4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ constructor(
val questionnaireItemsMap = questionnaire.item.associateBy { it.linkId }

// Only validate items that are present on both Questionnaire and the QuestionnaireResponse
questionnaireResponse.item.forEach {
questionnaireResponse.copy().item.forEach {
if (questionnaireItemsMap.containsKey(it.linkId)) {
val questionnaireItem = questionnaireItemsMap.getValue(it.linkId)
validQuestionnaireResponseItems.add(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.quest

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import org.hl7.fhir.instance.model.api.IBaseResource
import org.junit.Assert

private val printer: IParser = FhirContext.forR4().newJsonParser()

fun <T : IBaseResource> assertResourceEquals(expected: T, actual: T) {
Assert.assertEquals(
printer.encodeResourceToString(expected),
printer.encodeResourceToString(actual),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ 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.quest.app.fakes.Faker
import org.smartregister.fhircore.quest.assertResourceEquals
import org.smartregister.fhircore.quest.robolectric.RobolectricTest
import org.smartregister.fhircore.quest.ui.questionnaire.QuestionnaireViewModel.Companion.CONTAINED_LIST_TITLE
import org.smartregister.model.practitioner.FhirPractitionerDetails
Expand Down Expand Up @@ -804,7 +805,7 @@ class QuestionnaireViewModelTest : RobolectricTest() {
}

@Test
fun testValidateQuestionnaireResponseWithRepeatsGroup() = runTest {
fun testValidateQuestionnaireResponseWithRepeatedGroup() = runTest {
val questionnaireString =
"""
{
Expand Down Expand Up @@ -901,9 +902,10 @@ class QuestionnaireViewModelTest : RobolectricTest() {
}

@Test
fun testValidateQuestionnaireResponseWithNestedRepeatsGroup() = runTest {
val questionnaireString =
"""
fun testValidateQuestionnaireResponseWithNestedRepeatedGroupShouldNotUpdateTheOriginalQuestionnaireResponse() =
runTest {
val questionnaireString =
"""
{
"resourceType": "Questionnaire",
"item": [
Expand Down Expand Up @@ -949,9 +951,9 @@ class QuestionnaireViewModelTest : RobolectricTest() {
]
}
"""
.trimIndent()
val questionnaireResponseString =
"""
.trimIndent()
val questionnaireResponseString =
"""
{
"resourceType": "QuestionnaireResponse",
"item": [
Expand Down Expand Up @@ -1010,18 +1012,21 @@ class QuestionnaireViewModelTest : RobolectricTest() {
]
}
"""
.trimIndent()
val questionnaire = parser.parseResource(questionnaireString) as Questionnaire
val questionnaireResponse =
parser.parseResource(questionnaireResponseString) as QuestionnaireResponse
val result =
questionnaireViewModel.validateQuestionnaireResponse(
questionnaire,
questionnaireResponse,
context,
)
Assert.assertTrue(result)
}
.trimIndent()
val questionnaire = parser.parseResource(questionnaireString) as Questionnaire
val actualQuestionnaireResponse =
parser.parseResource(questionnaireResponseString) as QuestionnaireResponse
val result =
questionnaireViewModel.validateQuestionnaireResponse(
questionnaire,
actualQuestionnaireResponse,
context,
)
val expectedQuestionnaireResponse =
parser.parseResource(questionnaireResponseString) as QuestionnaireResponse
Assert.assertTrue(result)
assertResourceEquals(expectedQuestionnaireResponse, actualQuestionnaireResponse)
}

@Test
fun testExecuteCqlShouldInvokeRunCqlLibrary() = runTest {
Expand Down

0 comments on commit 115c4f4

Please sign in to comment.