check for existence of issues with a given severity (#4328)

This commit is contained in:
JasonRoberts-smile 2022-12-06 09:14:59 -05:00 committed by GitHub
parent 6ae1672599
commit 248e48072c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -122,6 +122,26 @@ public class OperationOutcomeUtil {
return issueChild.getAccessor().getValues(theOutcome).size();
}
public static boolean hasIssuesOfSeverity(FhirContext theCtx, IBaseOperationOutcome theOutcome, String theSeverity) {
RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome);
BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue");
List<IBase> issues = issueChild.getAccessor().getValues(theOutcome);
if (issues.isEmpty()) {
return false; // if there are no issues at all, there are no issues of the required severity
}
IBase firstIssue = issues.get(0);
BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(firstIssue.getClass());
BaseRuntimeChildDefinition severityChild = issueElement.getChildByName("severity");
return issues.stream()
.flatMap(t -> severityChild.getAccessor().getValues(t).stream())
.map(t -> (IPrimitiveType<?>) t)
.map(IPrimitiveType::getValueAsString)
.anyMatch(theSeverity::equals);
}
public static IBaseOperationOutcome newInstance(FhirContext theCtx) {
RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition("OperationOutcome");
try {

View File

@ -44,4 +44,24 @@ public class OperationOutcomeUtilTest {
assertNotNull(oo.getIssueFirstRep().getDetails(), "OO.issue.details is empty");
}
@Test
public void hasIssuesOfSeverity_noMatchingIssues() {
OperationOutcome oo = new OperationOutcome();
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.WARNING);
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR);
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.INFORMATION);
assertFalse(OperationOutcomeUtil.hasIssuesOfSeverity(myCtx, oo, OperationOutcome.IssueSeverity.FATAL.toCode()));
}
@Test
public void hasIssuesOfSeverity_withMatchingIssues() {
OperationOutcome oo = new OperationOutcome();
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.WARNING);
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR);
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.FATAL);
oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.INFORMATION);
assertTrue(OperationOutcomeUtil.hasIssuesOfSeverity(myCtx, oo, OperationOutcome.IssueSeverity.FATAL.toCode()));
}
}