From 3984ea24b08642d036038e7b281f41fefa04850f Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 26 Nov 2019 09:37:52 -0300 Subject: [PATCH] Add validaton test --- .../CustomResourceGenerationTest.java | 52 +++ .../resources/r4/custom-resource-profile.json | 317 ++++++++++++++++++ .../test/resources/r4/custom-resource.json | 6 + 3 files changed, 375 insertions(+) create mode 100644 hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/CustomResourceGenerationTest.java create mode 100644 hapi-fhir-validation/src/test/resources/r4/custom-resource-profile.json create mode 100644 hapi-fhir-validation/src/test/resources/r4/custom-resource.json diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/CustomResourceGenerationTest.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/CustomResourceGenerationTest.java new file mode 100644 index 00000000000..6e2d2cee751 --- /dev/null +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/CustomResourceGenerationTest.java @@ -0,0 +1,52 @@ +package org.hl7.fhir.r4.validation; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.test.BaseTest; +import ca.uhn.fhir.validation.FhirValidator; +import ca.uhn.fhir.validation.ValidationResult; +import org.hl7.fhir.r4.hapi.ctx.DefaultProfileValidationSupport; +import org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator; +import org.hl7.fhir.r4.hapi.validation.PrePopulatedValidationSupport; +import org.hl7.fhir.r4.hapi.validation.ValidationSupportChain; +import org.hl7.fhir.r4.model.StructureDefinition; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class CustomResourceGenerationTest extends BaseTest { + + private static final Logger ourLog = LoggerFactory.getLogger(CustomResourceGenerationTest.class); + private FhirContext myCtx = FhirContext.forR4(); + + @Test + public void testValidateCustomResource() throws IOException { + + StructureDefinition customProfile = loadResource(myCtx, StructureDefinition.class, "/r4/custom-resource-profile.json"); + String customResource = loadResource("/r4/custom-resource.json"); + + PrePopulatedValidationSupport prePopulatedValidationSupport = new PrePopulatedValidationSupport(); + prePopulatedValidationSupport.addStructureDefinition(customProfile); + + DefaultProfileValidationSupport defaultProfileValidationSupport = new DefaultProfileValidationSupport(); + ValidationSupportChain validationSupport = new ValidationSupportChain(defaultProfileValidationSupport, prePopulatedValidationSupport); + + FhirValidator validator = myCtx.newValidator(); + validator.registerValidatorModule(new FhirInstanceValidator(validationSupport)); + + ValidationResult result = validator.validateWithResult(customResource); + + String outcome = myCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(result.toOperationOutcome()); + ourLog.info(outcome); + + assertEquals(2, result.getMessages().size()); + assertEquals("Error parsing JSON: the primitive value must be a boolean", result.getMessages().get(0).getMessage()); + assertEquals("Unrecognised property '@id1'", result.getMessages().get(1).getMessage()); + + } + +} diff --git a/hapi-fhir-validation/src/test/resources/r4/custom-resource-profile.json b/hapi-fhir-validation/src/test/resources/r4/custom-resource-profile.json new file mode 100644 index 00000000000..d04bf07dc33 --- /dev/null +++ b/hapi-fhir-validation/src/test/resources/r4/custom-resource-profile.json @@ -0,0 +1,317 @@ +{ + "resourceType": "StructureDefinition", + "url": "http://hl7.org/fhir/StructureDefinition/CustomResource", + "name": "CustomResource", + "status": "draft", + "fhirVersion": "4.0.0", + "kind": "resource", + "abstract": false, + "type": "CustomResource", + "baseDefinition": "http://hl7.org/fhir/StructureDefinition/DomainResource", + "derivation": "specialization", + "snapshot": { + "element": [ + { + "id": "CustomResource", + "path": "CustomResource", + "short": "Information about an individual or animal receiving health care services", + "definition": "Demographics and other administrative information about an individual or animal receiving care or other health-related services.", + "alias": [ + "SubjectOfCare Client Resident" + ], + "min": 0, + "max": "*", + "constraint": [ + { + "key": "dom-2", + "severity": "error", + "human": "If the resource is contained in another resource, it SHALL NOT contain nested Resources", + "expression": "contained.contained.empty()", + "xpath": "not(parent::f:contained and f:contained)", + "source": "DomainResource" + }, + { + "key": "dom-1", + "severity": "error", + "human": "If the resource is contained in another resource, it SHALL NOT contain any narrative", + "expression": "contained.text.empty()", + "xpath": "not(parent::f:contained and f:text)", + "source": "DomainResource" + }, + { + "key": "dom-4", + "severity": "error", + "human": "If a resource is contained in another resource, it SHALL NOT have a meta.versionId or a meta.lastUpdated", + "expression": "contained.meta.versionId.empty() and contained.meta.lastUpdated.empty()", + "xpath": "not(exists(f:contained/*/f:meta/f:versionId)) and not(exists(f:contained/*/f:meta/f:lastUpdated))", + "source": "DomainResource" + } + ], + "mapping": [ + { + "identity": "rim", + "map": "Entity. Role, or Act" + }, + { + "identity": "rim", + "map": "Patient[classCode=PAT]" + }, + { + "identity": "cda", + "map": "ClinicalDocument.recordTarget.patientRole" + }, + { + "identity": "w5", + "map": "administrative.individual" + } + ] + }, + { + "id": "CustomResource.id", + "path": "CustomResource.id", + "short": "Logical id of this artifact", + "definition": "The logical id of the resource, as used in the URL for the resource. Once assigned, this value never changes.", + "comment": "The only time that a resource does not have an id is when it is being submitted to the server using a create operation.", + "min": 0, + "max": "1", + "base": { + "path": "Resource.id", + "min": 0, + "max": "1" + }, + "type": [ + { + "code": "id" + } + ], + "isSummary": true + }, + { + "id": "CustomResource.meta", + "path": "CustomResource.meta", + "short": "Metadata about the resource", + "definition": "The metadata about the resource. This is content that is maintained by the infrastructure. Changes to the content may not always be associated with version changes to the resource.", + "min": 0, + "max": "1", + "base": { + "path": "Resource.meta", + "min": 0, + "max": "1" + }, + "type": [ + { + "code": "Meta" + } + ], + "isSummary": true + }, + { + "id": "CustomResource.implicitRules", + "path": "CustomResource.implicitRules", + "short": "A set of rules under which this content was created", + "definition": "A reference to a set of rules that were followed when the resource was constructed, and which must be understood when processing the content.", + "comment": "Asserting this rule set restricts the content to be only understood by a limited set of trading partners. This inherently limits the usefulness of the data in the long term. However, the existing health eco-system is highly fractured, and not yet ready to define, collect, and exchange data in a generally computable sense. Wherever possible, implementers and/or specification writers should avoid using this element. \n\nThis element is labelled as a modifier because the implicit rules may provide additional knowledge about the resource that modifies it's meaning or interpretation.", + "min": 0, + "max": "1", + "base": { + "path": "Resource.implicitRules", + "min": 0, + "max": "1" + }, + "type": [ + { + "code": "uri" + } + ], + "isModifier": true, + "isSummary": true + }, + { + "id": "CustomResource.language", + "path": "CustomResource.language", + "short": "Language of the resource content", + "definition": "The base language in which the resource is written.", + "comment": "Language is provided to support indexing and accessibility (typically, services such as text to speech use the language tag). The html language tag in the narrative applies to the narrative. The language tag on the resource may be used to specify the language of other presentations generated from the data in the resource Not all the content has to be in the base language. The Resource.language should not be assumed to apply to the narrative automatically. If a language is specified, it should it also be specified on the div element in the html (see rules in HTML5 for information about the relationship between xml:lang and the html lang attribute).", + "min": 0, + "max": "1", + "base": { + "path": "Resource.language", + "min": 0, + "max": "1" + }, + "type": [ + { + "code": "code" + } + ], + "binding": { + "extension": [ + { + "url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet", + "valueReference": { + "reference": "http://hl7.org/fhir/ValueSet/all-languages" + } + }, + { + "url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-bindingName", + "valueString": "Language" + }, + { + "url": "http://hl7.org/fhir/StructureDefinition/elementdefinition-isCommonBinding", + "valueBoolean": true + } + ], + "strength": "extensible", + "description": "A human language.", + "valueSetReference": { + "reference": "http://hl7.org/fhir/ValueSet/languages" + } + } + }, + { + "id": "CustomResource.text", + "path": "CustomResource.text", + "short": "Text summary of the resource, for human interpretation", + "definition": "A human-readable narrative that contains a summary of the resource, and may be used to represent the content of the resource to a human. The narrative need not encode all the structured data, but is required to contain sufficient detail to make it \"clinically safe\" for a human to just read the narrative. Resource definitions may define what content should be represented in the narrative to ensure clinical safety.", + "comment": "Contained resources do not have narrative. Resources that are not contained SHOULD have a narrative. In some cases, a resource may only have text with little or no additional discrete data (as long as all minOccurs=1 elements are satisfied). This may be necessary for data from legacy systems where information is captured as a \"text blob\" or where text is additionally entered raw or narrated and encoded in formation is added later.", + "alias": [ + "narrative", + "html", + "xhtml", + "display" + ], + "min": 0, + "max": "1", + "base": { + "path": "DomainResource.text", + "min": 0, + "max": "1" + }, + "type": [ + { + "code": "Narrative" + } + ], + "condition": [ + "dom-1" + ], + "mapping": [ + { + "identity": "rim", + "map": "Act.text?" + } + ] + }, + { + "id": "CustomResource.contained", + "path": "CustomResource.contained", + "short": "Contained, inline Resources", + "definition": "These resources do not have an independent existence apart from the resource that contains them - they cannot be identified independently, and nor can they have their own independent transaction scope.", + "comment": "This should never be done when the content can be identified properly, as once identification is lost, it is extremely difficult (and context dependent) to restore it again.", + "alias": [ + "inline resources", + "anonymous resources", + "contained resources" + ], + "min": 0, + "max": "*", + "base": { + "path": "DomainResource.contained", + "min": 0, + "max": "*" + }, + "type": [ + { + "code": "Resource" + } + ], + "mapping": [ + { + "identity": "rim", + "map": "N/A" + } + ] + }, + { + "id": "CustomResource.identifier", + "path": "CustomResource.identifier", + "short": "An identifier for this patient", + "definition": "An identifier for this patient.", + "requirements": "Patients are almost always assigned specific numerical identifiers.", + "min": 0, + "max": "*", + "type": [ + { + "code": "Identifier" + } + ], + "isSummary": true, + "mapping": [ + { + "identity": "v2", + "map": "PID-3" + }, + { + "identity": "rim", + "map": "id" + }, + { + "identity": "cda", + "map": ".id" + }, + { + "identity": "w5", + "map": "id" + } + ] + }, + { + "id": "CustomResource.active", + "path": "CustomResource.active", + "short": "Whether this patient's record is in active use", + "definition": "Whether this patient record is in active use.", + "comment": "Default is true. If a record is inactive, and linked to an active record, then future patient/record updates should occur on the other patient\n\nThis element is labeled as a modifier because when the patient record is marked as not active it is not expected to be used/referenced without being changed back to active.", + "requirements": "Need to be able to mark a patient record as not to be used because it was created in error.", + "min": 0, + "max": "1", + "type": [ + { + "code": "boolean" + } + ], + "defaultValueBoolean": true, + "isModifier": true, + "isSummary": true, + "mapping": [ + { + "identity": "rim", + "map": "statusCode" + }, + { + "identity": "cda", + "map": "n/a" + }, + { + "identity": "w5", + "map": "status" + } + ] + }, + { + "id": "CustomResource.name", + "path": "CustomResource.name", + "short": "A name associated with the CustomResource", + "definition": "A name associated with the individual.", + "comment": "A CustomResource may have multiple names with different uses or applicable periods. For animals, the name is a \"HumanName\" in the sense that is assigned and used by humans and has the same patterns.", + "requirements": "Need to be able to track the patient by multiple names. Examples are your official name and a partner name.", + "min": 0, + "max": "*", + "type": [ + { + "code": "string" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/hapi-fhir-validation/src/test/resources/r4/custom-resource.json b/hapi-fhir-validation/src/test/resources/r4/custom-resource.json new file mode 100644 index 00000000000..a0d20656ba9 --- /dev/null +++ b/hapi-fhir-validation/src/test/resources/r4/custom-resource.json @@ -0,0 +1,6 @@ +{ + "resourceType": "CustomResource", + "id1": "customResource", + "active": "true", + "name": "needle" +} \ No newline at end of file