-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation API for SDC #548
Changes from 18 commits
14ad806
bc96a50
962a864
a9f7bf3
8f7be59
6b59724
4049c4a
87fec8e
5d99679
5c3855d
41d335d
167ad15
20e0ad2
70ce98a
803120f
932681e
6a498c1
379a1e4
c01b055
f1a0062
2c9f422
6f50d8b
7e515ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020 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 | ||
* | ||
* 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 com.google.android.fhir.datacapture.validation | ||
|
||
import com.google.android.fhir.datacapture.hasNestedItemsWithinAnswers | ||
import org.hl7.fhir.r4.model.Questionnaire | ||
import org.hl7.fhir.r4.model.QuestionnaireResponse | ||
|
||
object QuestionnaireResponseValidator { | ||
|
||
/** Maps linkId to [ValidationResult]. */ | ||
private val linkIdToValidationResultMap = mutableMapOf<String, MutableList<ValidationResult>>() | ||
|
||
/** | ||
* Validates [questionnaireResponseItemList] using the constraints defined in the | ||
* [questionnaireItemList]. | ||
*/ | ||
fun validate( | ||
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>, | ||
questionnaireResponseItemList: List<QuestionnaireResponse.QuestionnaireResponseItemComponent> | ||
): Map<String, List<ValidationResult>> { | ||
val questionnaireItemListIterator = questionnaireItemList.iterator() | ||
val questionnaireResponseItemListIterator = questionnaireResponseItemList.iterator() | ||
while (questionnaireItemListIterator.hasNext() && | ||
questionnaireResponseItemListIterator.hasNext()) { | ||
val questionnaireItem = questionnaireItemListIterator.next() | ||
val questionnaireResponseItem = questionnaireResponseItemListIterator.next() | ||
linkIdToValidationResultMap[questionnaireItem.linkId] = mutableListOf() | ||
linkIdToValidationResultMap[questionnaireItem.linkId]?.add( | ||
QuestionnaireResponseItemValidator.validate(questionnaireItem, questionnaireResponseItem) | ||
) | ||
if (questionnaireItem.hasNestedItemsWithinAnswers) { | ||
// TODO(https://github.com/google/android-fhir/issues/487): Validates all answers. | ||
validate(questionnaireItem.item, questionnaireResponseItem.answer[0].item) | ||
} | ||
validate(questionnaireItem.item, questionnaireResponseItem.item) | ||
} | ||
return linkIdToValidationResultMap | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't have to do this in this PR - but you can probably create an iterator for questionnaire item + questionnaire response item. this logic is repeated in the questionnaire view model as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure ill put a comment so that it is done while handling repeated answers |
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,159 @@ | ||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||
* Copyright 2020 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 | ||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||
* 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 com.google.android.fhir.datacapture.validation | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
import android.os.Build | ||||||||||||||||||||||||||||||||||||||
import com.google.common.truth.Truth.assertThat | ||||||||||||||||||||||||||||||||||||||
import org.hl7.fhir.r4.model.IntegerType | ||||||||||||||||||||||||||||||||||||||
import org.hl7.fhir.r4.model.Questionnaire | ||||||||||||||||||||||||||||||||||||||
import org.hl7.fhir.r4.model.QuestionnaireResponse | ||||||||||||||||||||||||||||||||||||||
import org.hl7.fhir.r4.model.StringType | ||||||||||||||||||||||||||||||||||||||
import org.junit.Test | ||||||||||||||||||||||||||||||||||||||
import org.junit.runner.RunWith | ||||||||||||||||||||||||||||||||||||||
import org.robolectric.RobolectricTestRunner | ||||||||||||||||||||||||||||||||||||||
import org.robolectric.annotation.Config | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@RunWith(RobolectricTestRunner::class) | ||||||||||||||||||||||||||||||||||||||
@Config(sdk = [Build.VERSION_CODES.P]) | ||||||||||||||||||||||||||||||||||||||
class QuestionnaireResponseValidatorTest { | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||
fun shouldReturnValidResult() { | ||||||||||||||||||||||||||||||||||||||
val questionnaire = | ||||||||||||||||||||||||||||||||||||||
Questionnaire() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
Questionnaire.QuestionnaireItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setMaxLength(3) | ||||||||||||||||||||||||||||||||||||||
.setType(Questionnaire.QuestionnaireItemType.INTEGER) | ||||||||||||||||||||||||||||||||||||||
.setText("Age in years?") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val questionnaireResponse = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setAnswer( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent() | ||||||||||||||||||||||||||||||||||||||
.setValue(IntegerType(3)) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val result = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponseValidator.validate(questionnaire.item, questionnaireResponse.item) | ||||||||||||||||||||||||||||||||||||||
assertThat(result.get("a-question")).isEqualTo(listOf(ValidationResult(true, listOf()))) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||
fun shouldReturnInvalidResultWithMessages() { | ||||||||||||||||||||||||||||||||||||||
val questionnaire = | ||||||||||||||||||||||||||||||||||||||
Questionnaire() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
Questionnaire.QuestionnaireItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setMaxLength(3) | ||||||||||||||||||||||||||||||||||||||
.setType(Questionnaire.QuestionnaireItemType.INTEGER) | ||||||||||||||||||||||||||||||||||||||
.setText("Age in years?") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val questionnaireResponse = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setAnswer( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent() | ||||||||||||||||||||||||||||||||||||||
.setValue(IntegerType(1000)) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val result = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponseValidator.validate(questionnaire.item, questionnaireResponse.item) | ||||||||||||||||||||||||||||||||||||||
assertThat(result.get("a-question")) | ||||||||||||||||||||||||||||||||||||||
.isEqualTo( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
ValidationResult( | ||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||
listOf("The maximum number of characters that are permitted in the answer is: 3") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+87
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||
fun shouldReturnInvalidResultWithMessages_forNestedItems() { | ||||||||||||||||||||||||||||||||||||||
val questionnaire = | ||||||||||||||||||||||||||||||||||||||
Questionnaire() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
Questionnaire.QuestionnaireItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setMaxLength(3) | ||||||||||||||||||||||||||||||||||||||
.setType(Questionnaire.QuestionnaireItemType.INTEGER) | ||||||||||||||||||||||||||||||||||||||
.setText("Age in years?") | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
Questionnaire.QuestionnaireItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-nested-question") | ||||||||||||||||||||||||||||||||||||||
.setMaxLength(3) | ||||||||||||||||||||||||||||||||||||||
.setType(Questionnaire.QuestionnaireItemType.STRING) | ||||||||||||||||||||||||||||||||||||||
.setText("Country code") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val questionnaireResponse = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse() | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-question") | ||||||||||||||||||||||||||||||||||||||
.setAnswer( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent() | ||||||||||||||||||||||||||||||||||||||
.setValue(IntegerType(1000)) | ||||||||||||||||||||||||||||||||||||||
.addItem( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemComponent() | ||||||||||||||||||||||||||||||||||||||
.setLinkId("a-nested-question") | ||||||||||||||||||||||||||||||||||||||
.setAnswer( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent() | ||||||||||||||||||||||||||||||||||||||
.setValue(StringType("ABCD")) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
val result = | ||||||||||||||||||||||||||||||||||||||
QuestionnaireResponseValidator.validate(questionnaire.item, questionnaireResponse.item) | ||||||||||||||||||||||||||||||||||||||
assertThat(result.get("a-question")) | ||||||||||||||||||||||||||||||||||||||
.isEqualTo( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
ValidationResult( | ||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||
listOf("The maximum number of characters that are permitted in the answer is: 3") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
assertThat(result.get("a-nested-question")) | ||||||||||||||||||||||||||||||||||||||
.isEqualTo( | ||||||||||||||||||||||||||||||||||||||
listOf( | ||||||||||||||||||||||||||||||||||||||
ValidationResult( | ||||||||||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||||||||||
listOf("The maximum number of characters that are permitted in the answer is: 3") | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
joiskash marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just change the property to internal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove this getter function altogether!