Follow-up fixes for unit tests for issue #1048.

This commit is contained in:
Anthony Sute 2018-08-10 11:47:53 -04:00
parent 0fdcad0985
commit 5ef647840b
5 changed files with 250 additions and 40 deletions

View File

@ -48,6 +48,8 @@ import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@ -116,8 +118,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
{
if (metaList.item(j).getNodeName().compareToIgnoreCase("profile") == 0)
{
String[] components = metaList.item(j).getAttributes().item(0).getNodeValue().split("/");
profileNames.add(components[components.length - 1]);
profileNames.add(metaList.item(j).getAttributes().item(0).getNodeValue());
}
}
break;
@ -127,7 +128,26 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
}
private StructureDefinition findStructureDefinitionForResourceName(final FhirContext theCtx, String resourceName) {
String sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
String sdName = null;
try {
// Test if a URL was passed in specifying the structure definition and test if "StructureDefinition" is part of the URL
URL testIfUrl = new URL(resourceName);
if (resourceName.toLowerCase().contains("structuredefinition"))
{
sdName = resourceName;
}
else
{
ourLog.error(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
throw new InternalErrorException(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
}
}
catch (MalformedURLException e)
{
sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
}
StructureDefinition profile = myStructureDefintion != null ? myStructureDefintion : myValidationSupport.fetchStructureDefinition(theCtx, sdName);
return profile;
}
@ -276,6 +296,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, determineResourceName(document));
if (profile != null) {
try {
v.validate(null, messages, document, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else if (theEncoding == EncodingEnum.JSON) {
Gson gson = new GsonBuilder().create();
@ -287,8 +319,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
profiles = json.getAsJsonObject("meta").getAsJsonArray("profile");
for (JsonElement element : profiles)
{
String[] components = element.getAsString().split("/");
resourceNames.add(components[components.length - 1]);
resourceNames.add(element.getAsString());
}
} catch (Exception e) {
resourceNames.add(json.get("resourceType").getAsString());
@ -303,6 +334,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, json.get("resourceType").getAsString());
if (profile != null) {
try {
v.validate(null, messages, json, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else {
throw new IllegalArgumentException("Unknown encoding: " + theEncoding);

View File

@ -56,6 +56,8 @@ import org.xml.sax.InputSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@ -156,8 +158,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
{
if (metaList.item(j).getNodeName().compareToIgnoreCase("profile") == 0)
{
String[] components = metaList.item(j).getAttributes().item(0).getNodeValue().split("/");
profileNames.add(components[components.length - 1]);
profileNames.add(metaList.item(j).getAttributes().item(0).getNodeValue());
}
}
break;
@ -167,7 +168,26 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
}
private StructureDefinition findStructureDefinitionForResourceName(final FhirContext theCtx, String resourceName) {
String sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
String sdName = null;
try {
// Test if a URL was passed in specifying the structure definition and test if "StructureDefinition" is part of the URL
URL testIfUrl = new URL(resourceName);
if (resourceName.toLowerCase().contains("structuredefinition"))
{
sdName = resourceName;
}
else
{
ourLog.error(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
throw new InternalErrorException(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
}
}
catch (MalformedURLException e)
{
sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
}
StructureDefinition profile = myStructureDefintion != null ? myStructureDefintion : myValidationSupport.fetchResource(theCtx, StructureDefinition.class, sdName);
return profile;
}
@ -312,6 +332,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, determineResourceName(document));
if (profile != null) {
try {
v.validate(null, messages, document, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else if (theEncoding == EncodingEnum.JSON) {
Gson gson = new GsonBuilder().create();
@ -323,8 +355,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
profiles = json.getAsJsonObject("meta").getAsJsonArray("profile");
for (JsonElement element : profiles)
{
String[] components = element.getAsString().split("/");
resourceNames.add(components[components.length - 1]);
resourceNames.add(element.getAsString());
}
} catch (Exception e) {
resourceNames.add(json.get("resourceType").getAsString());
@ -339,6 +370,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, json.get("resourceType").getAsString());
if (profile != null) {
try {
v.validate(null, messages, json, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else {
throw new IllegalArgumentException("Unknown encoding: " + theEncoding);

View File

@ -31,6 +31,8 @@ import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -97,8 +99,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
{
if (metaList.item(j).getNodeName().compareToIgnoreCase("profile") == 0)
{
String[] components = metaList.item(j).getAttributes().item(0).getNodeValue().split("/");
profileNames.add(components[components.length - 1]);
profileNames.add(metaList.item(j).getAttributes().item(0).getNodeValue());
}
}
break;
@ -108,7 +109,26 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
}
private StructureDefinition findStructureDefinitionForResourceName(final FhirContext theCtx, String resourceName) {
String sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
String sdName = null;
try {
// Test if a URL was passed in specifying the structure definition and test if "StructureDefinition" is part of the URL
URL testIfUrl = new URL(resourceName);
if (resourceName.toLowerCase().contains("structuredefinition"))
{
sdName = resourceName;
}
else
{
ourLog.error(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
throw new InternalErrorException(String.format("Structure definition URL must contain the text, \"StructureDefinition\", URL=%s",
resourceName));
}
}
catch (MalformedURLException e)
{
sdName = "http://hl7.org/fhir/StructureDefinition/" + resourceName;
}
StructureDefinition profile = myStructureDefintion != null ? myStructureDefintion : myValidationSupport.fetchStructureDefinition(theCtx, sdName);
return profile;
}
@ -247,6 +267,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, determineResourceName(document));
if (profile != null) {
try {
v.validate(null, messages, document, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else if (theEncoding == EncodingEnum.JSON) {
Gson gson = new GsonBuilder().create();
@ -258,8 +290,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
profiles = json.getAsJsonObject("meta").getAsJsonArray("profile");
for (JsonElement element : profiles)
{
String[] components = element.getAsString().split("/");
resourceNames.add(components[components.length - 1]);
resourceNames.add(element.getAsString());
}
} catch (Exception e) {
resourceNames.add(json.get("resourceType").getAsString());
@ -274,6 +305,18 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
else
{
profile = findStructureDefinitionForResourceName(theCtx, json.get("resourceType").getAsString());
if (profile != null) {
try {
v.validate(null, messages, json, profile.getUrl());
} catch (Exception e) {
ourLog.error("Failure during validation", e);
throw new InternalErrorException("Unexpected failure while validating resource", e);
}
}
}
}
} else {
throw new IllegalArgumentException("Unknown encoding: " + theEncoding);

View File

@ -1,5 +1,20 @@
package org.hl7.fhir.dstu3.hapi.validation;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.FhirValidator;
@ -11,19 +26,44 @@ import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.hapi.ctx.HapiWorkerContext;
import org.hl7.fhir.dstu3.hapi.ctx.IValidationSupport;
import org.hl7.fhir.dstu3.hapi.ctx.IValidationSupport.CodeValidationResult;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.Base;
import org.hl7.fhir.dstu3.model.BooleanType;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.CodeSystem;
import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.ContactPoint;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus;
import org.hl7.fhir.dstu3.model.Extension;
import org.hl7.fhir.dstu3.model.Goal;
import org.hl7.fhir.dstu3.model.ImagingStudy;
import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Observation.ObservationStatus;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Period;
import org.hl7.fhir.dstu3.model.Procedure;
import org.hl7.fhir.dstu3.model.Questionnaire;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemType;
import org.hl7.fhir.dstu3.model.Reference;
import org.hl7.fhir.dstu3.model.RelatedPerson;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.dstu3.model.ValueSet;
import org.hl7.fhir.dstu3.model.ValueSet.ConceptSetComponent;
import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent;
import org.hl7.fhir.dstu3.utils.FHIRPathEngine;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
@ -32,16 +72,15 @@ import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
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 java.util.TreeSet;
import java.util.zip.GZIPInputStream;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FhirInstanceValidatorDstu3Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorDstu3Test.class);
@ -845,7 +884,7 @@ public class FhirInstanceValidatorDstu3Test {
addValidConcept("http://loinc.org", "12345");
Observation input = new Observation();
input.getMeta().addProfile("http://foo/myprofile");
input.getMeta().addProfile("http://foo/structuredefinition/myprofile");
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");
input.setStatus(ObservationStatus.FINAL);
@ -854,7 +893,7 @@ public class FhirInstanceValidatorDstu3Test {
ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 1, errors.size());
assertEquals("StructureDefinition reference \"http://foo/myprofile\" could not be resolved", errors.get(0).getMessage());
assertEquals("StructureDefinition reference \"http://foo/structuredefinition/myprofile\" could not be resolved", errors.get(0).getMessage());
}
@Test

View File

@ -1,5 +1,20 @@
package org.hl7.fhir.r4.validation;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.util.TestUtil;
@ -15,20 +30,48 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.conformance.ProfileUtilities;
import org.hl7.fhir.r4.context.IWorkerContext;
import org.hl7.fhir.r4.hapi.ctx.*;
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.hapi.ctx.IValidationSupport.CodeValidationResult;
import org.hl7.fhir.r4.hapi.ctx.PrePopulatedValidationSupport;
import org.hl7.fhir.r4.hapi.ctx.ValidationSupportChain;
import org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.Base64BinaryType;
import org.hl7.fhir.r4.model.BooleanType;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r4.model.CodeSystem;
import org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.Consent;
import org.hl7.fhir.r4.model.ContactPoint;
import org.hl7.fhir.r4.model.DateTimeType;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Media;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Observation.ObservationStatus;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Period;
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.Procedure;
import org.hl7.fhir.r4.model.QuestionnaireResponse;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.RelatedPerson;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.StructureDefinition;
import org.hl7.fhir.r4.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent;
import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent;
import org.hl7.fhir.r4.utils.FHIRPathEngine;
import org.hl7.fhir.r4.utils.IResourceValidator;
import org.hl7.fhir.utilities.validation.ValidationMessage;
import org.junit.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
@ -37,17 +80,16 @@ import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
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 java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FhirInstanceValidatorR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorR4Test.class);
@ -909,7 +951,7 @@ public class FhirInstanceValidatorR4Test {
addValidConcept("http://loinc.org", "12345");
Observation input = new Observation();
input.getMeta().addProfile("http://foo/myprofile");
input.getMeta().addProfile("http://foo/structuredefinition/myprofile");
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");
input.setStatus(ObservationStatus.FINAL);
@ -918,7 +960,7 @@ public class FhirInstanceValidatorR4Test {
ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 1, errors.size());
assertEquals("StructureDefinition reference \"http://foo/myprofile\" could not be resolved", errors.get(0).getMessage());
assertEquals("StructureDefinition reference \"http://foo/structuredefinition/myprofile\" could not be resolved", errors.get(0).getMessage());
}
@Test