From 28fba9734b23de1f852935def95569464a9dcb99 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Thu, 19 Dec 2019 14:58:32 -0500 Subject: [PATCH] Test fix --- .../PrePopulatedValidationSupport.java | 47 ++++++++++++- .../r4/validation/HapiWorkerContextTest.java | 15 +++- .../uscore/valueset/ValueSet-birthsex.json | 70 ++++++++++++++++++- 3 files changed, 128 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/hapi/validation/PrePopulatedValidationSupport.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/hapi/validation/PrePopulatedValidationSupport.java index 8487ada17a8..6d30f08a13f 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/hapi/validation/PrePopulatedValidationSupport.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/hapi/validation/PrePopulatedValidationSupport.java @@ -4,6 +4,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.api.Constants; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.hapi.ctx.DefaultProfileValidationSupport; import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext; import org.hl7.fhir.r4.hapi.ctx.IValidationSupport; import org.hl7.fhir.r4.model.CodeSystem; @@ -11,6 +12,7 @@ import org.hl7.fhir.r4.model.CodeType; import org.hl7.fhir.r4.model.MetadataResource; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.StructureDefinition; +import org.hl7.fhir.r4.model.UriType; import org.hl7.fhir.r4.model.ValueSet; import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent; import org.hl7.fhir.r4.terminologies.ValueSetExpander; @@ -18,9 +20,12 @@ import org.hl7.fhir.r4.terminologies.ValueSetExpanderSimple; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import static org.apache.commons.lang3.StringUtils.defaultString; import static org.apache.commons.lang3.StringUtils.isNotBlank; /** @@ -32,6 +37,7 @@ public class PrePopulatedValidationSupport implements IValidationSupport { private Map myCodeSystems; private Map myStructureDefinitions; private Map myValueSets; + private DefaultProfileValidationSupport myDefaultProfileValidationSupport = new DefaultProfileValidationSupport(); /** * Constructor @@ -134,7 +140,43 @@ public class PrePopulatedValidationSupport implements IValidationSupport { @Override public ValueSetExpander.ValueSetExpansionOutcome expandValueSet(FhirContext theContext, ConceptSetComponent theInclude) { - return null; + ValueSetExpander.ValueSetExpansionOutcome retVal = new ValueSetExpander.ValueSetExpansionOutcome(new ValueSet()); + + Set wantCodes = new HashSet<>(); + for (ValueSet.ConceptReferenceComponent next : theInclude.getConcept()) { + wantCodes.add(next.getCode()); + } + + CodeSystem system = fetchCodeSystem(theContext, theInclude.getSystem()); + if (system != null) { + List concepts = system.getConcept(); + addConcepts(theInclude, retVal.getValueset().getExpansion(), wantCodes, concepts); + } + + for (UriType next : theInclude.getValueSet()) { + ValueSet vs = myValueSets.get(defaultString(next.getValueAsString())); + if (vs != null) { + for (ConceptSetComponent nextInclude : vs.getCompose().getInclude()) { + ValueSetExpander.ValueSetExpansionOutcome contents = expandValueSet(theContext, nextInclude); + retVal.getValueset().getExpansion().getContains().addAll(contents.getValueset().getExpansion().getContains()); + } + } + } + + return retVal; + } + + private void addConcepts(ConceptSetComponent theInclude, ValueSet.ValueSetExpansionComponent theRetVal, Set theWantCodes, List theConcepts) { + for (CodeSystem.ConceptDefinitionComponent next : theConcepts) { + if (theWantCodes.isEmpty() || theWantCodes.contains(next.getCode())) { + theRetVal + .addContains() + .setSystem(theInclude.getSystem()) + .setCode(next.getCode()) + .setDisplay(next.getDisplay()); + } + addConcepts(theInclude, theRetVal, theWantCodes, next.getConcept()); + } } @Override @@ -210,7 +252,8 @@ public class PrePopulatedValidationSupport implements IValidationSupport { vs.getCompose().addInclude().setSystem(theCodeSystem); } - ValueSetExpanderSimple expander = new ValueSetExpanderSimple(new HapiWorkerContext(theContext, this)); + IValidationSupport support = new ValidationSupportChain(this, myDefaultProfileValidationSupport); + ValueSetExpanderSimple expander = new ValueSetExpanderSimple(new HapiWorkerContext(theContext, support)); ValueSetExpander.ValueSetExpansionOutcome expansion = expander.expand(vs, new Parameters()); for (ValueSet.ValueSetExpansionContainsComponent nextExpansionCode : expansion.getValueset().getExpansion().getContains()) { diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/HapiWorkerContextTest.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/HapiWorkerContextTest.java index 6a6e8e38bc5..42ee3cd2a30 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/HapiWorkerContextTest.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/HapiWorkerContextTest.java @@ -53,8 +53,11 @@ public class HapiWorkerContextTest extends BaseTest { HapiWorkerContext workerCtx = new HapiWorkerContext(ctx, validationSupportChain); ValueSet vs = new ValueSet(); - vs.setUrl("http://hl7.org/fhir/ValueSet/fm-status"); + IWorkerContext.ValidationResult outcome; + // Built-in Codes + + vs.setUrl("http://hl7.org/fhir/ValueSet/fm-status"); IWorkerContext.ValidationResult outcome = workerCtx.validateCode(new TerminologyServiceOptions(), "active", vs); assertEquals(outcome.getMessage(), true, outcome.isOk()); @@ -62,6 +65,16 @@ public class HapiWorkerContextTest extends BaseTest { assertEquals(outcome.getMessage(), false, outcome.isOk()); assertEquals("Unknown code[active2] in system[(none)]", outcome.getMessage()); + // PrePopulated codes + + vs.setUrl("http://hl7.org/fhir/us/core/ValueSet/birthsex"); + outcome = workerCtx.validateCode(new TerminologyServiceOptions(), "F", vs); + assertEquals(outcome.getMessage(), true, outcome.isOk()); + + outcome = workerCtx.validateCode(new TerminologyServiceOptions(), "F2", vs); + assertEquals(outcome.getMessage(), false, outcome.isOk()); + assertEquals("Unknown code[F2] in system[(none)]", outcome.getMessage()); + } diff --git a/hapi-fhir-validation/src/test/resources/r4/carin/uscore/valueset/ValueSet-birthsex.json b/hapi-fhir-validation/src/test/resources/r4/carin/uscore/valueset/ValueSet-birthsex.json index 53861876f6a..d436db302b6 100644 --- a/hapi-fhir-validation/src/test/resources/r4/carin/uscore/valueset/ValueSet-birthsex.json +++ b/hapi-fhir-validation/src/test/resources/r4/carin/uscore/valueset/ValueSet-birthsex.json @@ -1 +1,69 @@ -{"resourceType":"ValueSet","id":"birthsex","text":{"status":"generated","div":"

Birth Sex

Codes for assigning sex at birth as specified by the Office of the National Coordinator for Health IT (ONC)

\n

This value set includes codes from the following code systems:

  • Include these codes as defined in http://terminology.hl7.org/CodeSystem/v3-AdministrativeGender
    CodeDisplay
    FFemaleFemale
    MMaleMale
  • Include these codes as defined in http://terminology.hl7.org/CodeSystem/v3-NullFlavor
    CodeDisplay
    UNKUnknownDescription:A proper value is applicable, but not known.
    \n \n Usage Notes: This means the actual value is not known. If the only thing that is unknown is how to properly express the value in the necessary constraints (value set, datatype, etc.), then the OTH or UNC flavor should be used. No properties should be included for a datatype with this property unless:
    \n \n Those properties themselves directly translate to a semantic of "unknown". (E.g. a local code sent as a translation that conveys 'unknown')\n Those properties further qualify the nature of what is unknown. (E.g. specifying a use code of "H" and a URL prefix of "tel:" to convey that it is the home phone number that is unknown.)
"},"url":"http://hl7.org/fhir/us/core/ValueSet/birthsex","identifier":[{"system":"urn:ietf:rfc:3986","value":"urn:oid:2.16.840.1.113762.1.4.1021.24"}],"version":"3.1.0","name":"BirthSex","title":"Birth Sex","status":"active","date":"2019-05-21T00:00:00+00:00","publisher":"HL7 US Realm Steering Committee","contact":[{"telecom":[{"system":"other","value":"http://hl7.org/fhir"}]}],"description":"Codes for assigning sex at birth as specified by the [Office of the National Coordinator for Health IT (ONC)](https://www.healthit.gov/newsroom/about-onc)","jurisdiction":[{"coding":[{"system":"urn:iso:std:iso:3166","code":"US","display":"United States of America"}]}],"compose":{"include":[{"system":"http://terminology.hl7.org/CodeSystem/v3-AdministrativeGender","concept":[{"code":"F","display":"Female"},{"code":"M","display":"Male"}]},{"system":"http://terminology.hl7.org/CodeSystem/v3-NullFlavor","concept":[{"code":"UNK","display":"Unknown"}]}]}} \ No newline at end of file +{ + "resourceType": "ValueSet", + "id": "birthsex", + "text": { + "status": "generated", + "div": "

Birth Sex

Codes for assigning sex at birth as specified by the Office of the National Coordinator for Health IT (ONC)

\n

This value set includes codes from the following code systems:

  • Include these codes as defined in http://terminology.hl7.org/CodeSystem/v3-AdministrativeGender
    CodeDisplay
    FFemaleFemale
    MMaleMale
  • Include these codes as defined in http://terminology.hl7.org/CodeSystem/v3-NullFlavor
    CodeDisplay
    UNKUnknownDescription:A proper value is applicable, but not known.
    \n \n Usage Notes: This means the actual value is not known. If the only thing that is unknown is how to properly express the value in the necessary constraints (value set, datatype, etc.), then the OTH or UNC flavor should be used. No properties should be included for a datatype with this property unless:
    \n \n Those properties themselves directly translate to a semantic of "unknown". (E.g. a local code sent as a translation that conveys 'unknown')\n Those properties further qualify the nature of what is unknown. (E.g. specifying a use code of "H" and a URL prefix of "tel:" to convey that it is the home phone number that is unknown.)
" + }, + "url": "http://hl7.org/fhir/us/core/ValueSet/birthsex", + "identifier": [ + { + "system": "urn:ietf:rfc:3986", + "value": "urn:oid:2.16.840.1.113762.1.4.1021.24" + } + ], + "version": "3.1.0", + "name": "BirthSex", + "title": "Birth Sex", + "status": "active", + "date": "2019-05-21T00:00:00+00:00", + "publisher": "HL7 US Realm Steering Committee", + "contact": [ + { + "telecom": [ + { + "system": "other", + "value": "http://hl7.org/fhir" + } + ] + } + ], + "description": "Codes for assigning sex at birth as specified by the [Office of the National Coordinator for Health IT (ONC)](https://www.healthit.gov/newsroom/about-onc)", + "jurisdiction": [ + { + "coding": [ + { + "system": "urn:iso:std:iso:3166", + "code": "US", + "display": "United States of America" + } + ] + } + ], + "compose": { + "include": [ + { + "system": "http://terminology.hl7.org/CodeSystem/v3-AdministrativeGender", + "concept": [ + { + "code": "F", + "display": "Female" + }, + { + "code": "M", + "display": "Male" + } + ] + }, + { + "system": "http://terminology.hl7.org/CodeSystem/v3-NullFlavor", + "concept": [ + { + "code": "UNK", + "display": "Unknown" + } + ] + } + ] + } +}