diff --git a/datacapture/src/test/java/com/google/android/fhir/datacapture/validation/MaxValueValidatorTest.kt b/datacapture/src/test/java/com/google/android/fhir/datacapture/validation/MaxValueValidatorTest.kt index 2c3b1346bd..c9d4137f2b 100644 --- a/datacapture/src/test/java/com/google/android/fhir/datacapture/validation/MaxValueValidatorTest.kt +++ b/datacapture/src/test/java/com/google/android/fhir/datacapture/validation/MaxValueValidatorTest.kt @@ -181,6 +181,91 @@ class MaxValueValidatorTest { .isEqualTo("Maximum value allowed is:${LocalDate.now().minusDays(7)}") } + @Test + fun `should return invalid result when date of birth is in the future and maxValue is today`() = + runTest { + val questionnaireItem = + Questionnaire.QuestionnaireItemComponent().apply { + addExtension( + Extension().apply { + this.url = MAX_VALUE_EXTENSION_URL + this.setValue( + DateType().apply { + addExtension( + Extension( + EXTENSION_CQF_CALCULATED_VALUE_URL, + Expression().apply { + expression = "today()" + language = "text/fhirpath" + }, + ), + ) + }, + ) + }, + ) + } + val tomorrow = + Date.from( + LocalDate.now().plusDays(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(), + ) + val answer = + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { + value = DateType().apply { value = tomorrow } + } + + val validationResult = + MaxValueValidator.validate(questionnaireItem, answer, context) { + TestExpressionValueEvaluator.evaluate(questionnaireItem, it) + } + + assertThat(validationResult.isValid).isFalse() + assertThat(validationResult.errorMessage) + .isEqualTo("Maximum value allowed is:${LocalDate.now()}") + } + + @Test + fun `should return valid result when date of birth is today and maxValue is today`() = + runTest { + val questionnaireItem = + Questionnaire.QuestionnaireItemComponent().apply { + addExtension( + Extension().apply { + this.url = MAX_VALUE_EXTENSION_URL + this.setValue( + DateType().apply { + addExtension( + Extension( + EXTENSION_CQF_CALCULATED_VALUE_URL, + Expression().apply { + expression = "today()" + language = "text/fhirpath" + }, + ), + ) + }, + ) + }, + ) + } + val today = + Date.from( + LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(), + ) + val answer = + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { + value = DateType().apply { value = today } + } + + val validationResult = + MaxValueValidator.validate(questionnaireItem, answer, context) { + TestExpressionValueEvaluator.evaluate(questionnaireItem, it) + } + + assertThat(validationResult.isValid).isTrue() + assertThat(validationResult.errorMessage.isNullOrBlank()).isTrue() + } + @Test fun `should return valid result and removes constraint for an answer value when maxValue cqf-calculatedValue evaluates to empty`() = runTest { diff --git a/demo/src/main/assets/new-patient-registration-paginated.json b/demo/src/main/assets/new-patient-registration-paginated.json index 45c0ba9a72..612afc8c60 100644 --- a/demo/src/main/assets/new-patient-registration-paginated.json +++ b/demo/src/main/assets/new-patient-registration-paginated.json @@ -148,6 +148,22 @@ "definition": "http://hl7.org/fhir/StructureDefinition/Patient#Patient.birthDate", "type": "date", "required": true, + "extension": [ + { + "url": "http://hl7.org/fhir/StructureDefinition/maxValue", + "valueDate": { + "extension": [ + { + "url": "http://hl7.org/fhir/StructureDefinition/cqf-calculatedValue", + "valueExpression": { + "language": "text/fhirpath", + "expression": "today()" + } + } + ] + } + } + ], "text": "Date of Birth", "_text": { "extension": [