-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
14ad806
Added link id to validation result
joiskash bc96a50
Merge branch 'master' of https://github.com/google/android-fhir
joiskash 962a864
Added function to validate Questionnaire response items before submit…
joiskash a9f7bf3
Fixed UT
joiskash 8f7be59
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
joiskash 6b59724
Reverted all changes to validation package
joiskash 4049c4a
Reverted changes and added function to return the current Questionnaire
joiskash 87fec8e
Reverted changes to questionnaireFragment
joiskash 5d99679
Added new questionnaire Validator
joiskash 5c3855d
Ran spotless
joiskash 41d335d
UT fix
joiskash 167ad15
Added unit tests
joiskash 20e0ad2
Renamed Class to QuestionnaireResponseValidator.kt
joiskash 70ce98a
Removed function and made questionnaire a property
joiskash 803120f
Changed to isEqualTo
joiskash 932681e
Using assertThat from truth instead of assertEquals
joiskash 6a498c1
Ran spotless
joiskash 379a1e4
Merge branch 'master' into kj/validation-api
joiskash c01b055
Merge branch 'master' into kj/validation-api
joiskash f1a0062
removed getQuestionnaire function
joiskash 2c9f422
added Todo note to cover in the next PR
joiskash 6f50d8b
Updated test
joiskash 7e515ce
Merge branch 'master' into kj/validation-api
joiskash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ain/java/com/google/android/fhir/datacapture/validation/QuestionnaireResponseValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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>> { | ||
/* TODO create an iterator for questionnaire item + questionnaire response item refer to the | ||
questionnaire view model */ | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...java/com/google/android/fhir/datacapture/validation/QuestionnaireResponseValidatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* 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") | ||
) | ||
) | ||
) | ||
} | ||
|
||
@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")) | ||
.containsExactly( | ||
ValidationResult( | ||
false, | ||
listOf("The maximum number of characters that are permitted in the answer is: 3") | ||
) | ||
) | ||
assertThat(result.get("a-nested-question")) | ||
.containsExactly( | ||
ValidationResult( | ||
false, | ||
listOf("The maximum number of characters that are permitted in the answer is: 3") | ||
) | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.