Skip to content

Commit

Permalink
Restricting DatePicker to select date from outside the defined Range …
Browse files Browse the repository at this point in the history
…(Min/Max). Added support for fhir path expression in Min / Max Validators (#1442)

* WIP configured Date picker to pick min and max value dates from extension to make a range

* added cqf-calculated value expression support for date picker

* PR Feedback : refactored methods in MoreQuestionnaireItemComponents

* added unit test cases for fhir path expression for min value validator

* fixed added dyanmic date

* added unit test cases for MoreQuestionnaireItemComponents

* Refactored unit tests for MoreQuestionnaireItemComponents

* added instrumentation test for DatePickerViewFactory

* WIP feedback

* Moved cql parsing related methods to ValidationUtil

* refactored ValidationUtil

* Refactored ValidationUtil

* tests fixed, spotless ran again

* tests updated

* SPOTLESS ran

* spotless rechecked

* WIP fixing feedback

* WIP fixing feedback

* feedback incorporated, updated the unit test cases

* spotless ran

* spotless ran

* added instrumentation test for QuestionnaireItemDatePickerViewHolderFactoryEspressoTest

* fixed a test case handled exception

* fixed imports - feedback

* feedback incorporated

* spotless ran

* throwing an exception when minValue is greater than maxValue

* implementation updated to use compositedatevalidator to restrict min and max dates
  • Loading branch information
aurangzaibumer committed Sep 21, 2022
1 parent eb894de commit 75ac755
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
Expand All @@ -32,11 +35,20 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.TestActivity
import com.google.android.fhir.datacapture.utilities.clickIcon
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.validation.NotValidated
import com.google.android.fhir.datacapture.validation.QuestionnaireResponseItemValidator
import com.google.android.fhir.datacapture.validation.Valid
import com.google.common.truth.Truth.assertThat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import org.hamcrest.CoreMatchers.allOf
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.DateType
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -81,6 +93,188 @@ class QuestionnaireItemDatePickerViewHolderFactoryEspressoTest {
.check(matches(ViewMatchers.isDisplayed()))
}

@Test
fun shouldSetDateInput() {
val questionnaireItemView =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }

onView(withId(R.id.text_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())

val today = DateTimeType.today().valueAsString

assertThat(questionnaireItemView.answers.singleOrNull()?.valueDateType?.valueAsString)
.isEqualTo(today)
}

@Test
fun shouldSetDateInput_withinRange() {
val questionnaireItemView =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/minValue"
val minDate = DateType(Date()).apply { add(Calendar.YEAR, -1) }
setValue(minDate)
}
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/maxValue"
val maxDate = DateType(Date()).apply { add(Calendar.YEAR, 4) }
setValue(maxDate)
}
},
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }

onView(withId(R.id.text_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())

val today = DateTimeType.today().valueAsString
val validationResult =
QuestionnaireResponseItemValidator.validate(
questionnaireItemView.questionnaireItem,
questionnaireItemView.answers,
viewHolder.itemView.context
)

assertThat(questionnaireItemView.answers.singleOrNull()?.valueDateType?.valueAsString)
.isEqualTo(today)
assertThat(validationResult).isEqualTo(Valid)
}

@Test
fun shouldNotSetDateInput_outsideMaxRange() {
val maxDate = DateType(Date()).apply { add(Calendar.YEAR, -2) }
val questionnaireItemView =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/minValue"
val minDate = DateType(Date()).apply { add(Calendar.YEAR, -4) }
setValue(minDate)
}
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/maxValue"
setValue(maxDate)
}
},
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }

onView(withId(R.id.text_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())

val maxDateAllowed = maxDate.valueAsString
val validationResult =
QuestionnaireResponseItemValidator.validate(
questionnaireItemView.questionnaireItem,
questionnaireItemView.answers,
viewHolder.itemView.context
)

assertThat((validationResult as Invalid).getSingleStringValidationMessage())
.isEqualTo("Maximum value allowed is:$maxDateAllowed")
}

@Test
fun shouldNotSetDateInput_outsideMinRange() {
val minDate = DateType(Date()).apply { add(Calendar.YEAR, 1) }
val questionnaireItemView =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/minValue"
setValue(minDate)
}
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/maxValue"
val maxDate = DateType(Date()).apply { add(Calendar.YEAR, 2) }
setValue(maxDate)
}
},
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }

onView(withId(R.id.text_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())

val minDateAllowed = minDate.valueAsString
val validationResult =
QuestionnaireResponseItemValidator.validate(
questionnaireItemView.questionnaireItem,
questionnaireItemView.answers,
viewHolder.itemView.context
)

assertThat((validationResult as Invalid).getSingleStringValidationMessage())
.isEqualTo("Minimum value allowed is:$minDateAllowed")
}

@Test
fun shouldThrowException_whenMinValueRangeIsGreaterThanMaxValueRange() {
val questionnaireItemView =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/minValue"
val minDate = DateType(Date()).apply { add(Calendar.YEAR, 1) }
setValue(minDate)
}
addExtension().apply {
url = "https://hl7.org/fhir/StructureDefinition/maxValue"
val maxDate = DateType(Date()).apply { add(Calendar.YEAR, -1) }
setValue(maxDate)
}
},
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }

val exception =
assertThrows(IllegalArgumentException::class.java) {
onView(withId(R.id.text_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())
}
assertThat(exception.message).isEqualTo("minValue cannot be greater than maxValue")
}

/** Runs code snippet on UI/main thread */
private fun runOnUI(action: () -> Unit) {
activityScenarioRule.scenario.onActivity { activity -> action() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ internal fun Questionnaire.QuestionnaireItemComponent.findVariableExpression(
return variableExpressions.find { it.name == variableName }
}

internal const val CQF_CALCULATED_EXPRESSION_URL: String =
"https://hl7.org/fhir/StructureDefinition/cqf-calculatedValue"

// Item control code, or null
internal val Questionnaire.QuestionnaireItemComponent.itemControl: ItemControlTypes?
get() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google LLC
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,20 +20,31 @@ import android.content.Context
import com.google.android.fhir.compareTo
import com.google.android.fhir.datacapture.R
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.Type

internal const val MAX_VALUE_EXTENSION_URL = "https://hl7.org/fhir/StructureDefinition/maxValue"
/** A validator to check if the value of an answer exceeded the permitted value. */
internal object MaxValueConstraintValidator :
ValueConstraintExtensionValidator(
url = MAX_VALUE_EXTENSION_URL,
predicate = {
extension: Extension,
answer: QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent ->
answer.value > extension.value
answer.value > extension.value?.valueOrCalculateValue()!!
},
{ extension: Extension, context: Context ->
context.getString(R.string.max_value_validation_error_msg, extension.value.primitiveValue())
context.getString(
R.string.max_value_validation_error_msg,
extension.value?.valueOrCalculateValue()?.primitiveValue()
)
}
)
) {

internal const val MAX_VALUE_EXTENSION_URL = "https://hl7.org/fhir/StructureDefinition/maxValue"
fun getMaxValue(questionnaireItemComponent: Questionnaire.QuestionnaireItemComponent): Type? {
return questionnaireItemComponent.extension
.firstOrNull { it.url == MAX_VALUE_EXTENSION_URL }
?.let { it.value?.valueOrCalculateValue() }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google LLC
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,20 +20,33 @@ import android.content.Context
import com.google.android.fhir.compareTo
import com.google.android.fhir.datacapture.R
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.Type

internal const val MIN_VALUE_EXTENSION_URL = "https://hl7.org/fhir/StructureDefinition/minValue"
/** A validator to check if the value of an answer is at least the permitted value. */
internal object MinValueConstraintValidator :
ValueConstraintExtensionValidator(
url = MIN_VALUE_EXTENSION_URL,
predicate = {
extension: Extension,
answer: QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent ->
answer.value < extension.value
answer.value < extension.value?.valueOrCalculateValue()!!
},
{ extension: Extension, context: Context ->
context.getString(R.string.min_value_validation_error_msg, extension.value.primitiveValue())
context.getString(
R.string.min_value_validation_error_msg,
extension.value?.valueOrCalculateValue()?.primitiveValue()
)
}
)
) {

internal const val MIN_VALUE_EXTENSION_URL = "https://hl7.org/fhir/StructureDefinition/minValue"
internal fun getMinValue(
questionnaireItemComponent: Questionnaire.QuestionnaireItemComponent
): Type? {
return questionnaireItemComponent.extension
.firstOrNull { it.url == MIN_VALUE_EXTENSION_URL }
?.let { it.value?.valueOrCalculateValue() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 Google LLC
*
* 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
*
* https://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 com.google.android.fhir.datacapture.validation

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport
import com.google.android.fhir.datacapture.CQF_CALCULATED_EXPRESSION_URL
import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Type
import org.hl7.fhir.r4.utils.FHIRPathEngine

fun Type.valueOrCalculateValue(): Type? {
return if (this.hasExtension()) {
this.extension.firstOrNull { it.url == CQF_CALCULATED_EXPRESSION_URL }?.let {
val expression = (it.value as Expression).expression
fhirPathEngine.evaluate(this, expression).firstOrNull()?.let { it as Type }
}
} else {
this
}
}

private val fhirPathEngine: FHIRPathEngine =
with(FhirContext.forCached(FhirVersionEnum.R4)) {
FHIRPathEngine(HapiWorkerContext(this, DefaultProfileValidationSupport(this)))
}
Loading

0 comments on commit 75ac755

Please sign in to comment.