Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jamesagnew/hapi-fhir
Browse files Browse the repository at this point in the history
  • Loading branch information
de Beaubien, Bill committed Sep 1, 2015
2 parents c57ec99 + 64cc5d4 commit 6139e49
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ HAPI FHIR - Java API for HL7 FHIR Clients and Servers
[![Coverage Status](https://coveralls.io/repos/jamesagnew/hapi-fhir/badge.svg?branch=master&service=github)](https://coveralls.io/github/jamesagnew/hapi-fhir?branch=master)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/ca.uhn.hapi.fhir/hapi-fhir-base/badge.svg)](http://search.maven.org/#search|ga|1|ca.uhn.hapi.fhir)
[![Dependency Status](https://www.versioneye.com/user/projects/55e1d0d9c6d8f2001c00043e/badge.svg?style=flat)](https://www.versioneye.com/user/projects/55e1d0d9c6d8f2001c00043e)
[![License](https://img.shields.io/badge/license-apache%202.0-ff69b4.svg)](https://github.com/jamesagnew/hapi-fhir/blob/master/LICENSE.txt)
[![License](https://img.shields.io/badge/license-apache%202.0-ff69b4.svg)](http://jamesagnew.github.io/hapi-fhir/license.html)

Complete project documentation is available here:
http://jamesagnew.github.io/hapi-fhir/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void interceptResponse(HttpResponse theResponse) throws IOException {
throw new InternalErrorException(e);
}

myLog.info("Client response body:\n{}", new String(bytes));
myLog.info("Client response body:\n{}", new String(bytes, "UTF-8"));
theResponse.setEntity(new MyEntityWrapper(respEntity, bytes));
} else {
myLog.info("Client response body: (none)");
Expand Down
5 changes: 3 additions & 2 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.codehaus.stax2.io.EscapingWriterFactory;

import com.ctc.wstx.api.WstxInputProperties;
import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.stax.WstxOutputFactory;

/**
Expand Down Expand Up @@ -1550,6 +1549,7 @@ private static XMLInputFactory getOrCreateInputFactory() throws FactoryConfigura
if (ourInputFactory == null) {

try {
// Detect if we're running with the Android lib, and force repackaged Woodstox to be used
Class.forName("ca.uhn.fhir.repackage.javax.xml.stream.XMLInputFactory");
System.setProperty("javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory");
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -1589,6 +1589,7 @@ private static XMLOutputFactory getOrCreateOutputFactory() throws FactoryConfigu
if (ourOutputFactory == null) {

try {
// Detect if we're running with the Android lib, and force repackaged Woodstox to be used
Class.forName("ca.uhn.fhir.repackage.javax.xml.stream.XMLOutputFactory");
System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory");
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -1708,7 +1709,7 @@ public static class MyEscaper implements EscapingWriterFactory {

@Override
public Writer createEscapingWriterFor(OutputStream theOut, String theEnc) throws UnsupportedEncodingException {
return createEscapingWriterFor(new OutputStreamWriter(theOut), theEnc);
return createEscapingWriterFor(new OutputStreamWriter(theOut, theEnc), theEnc);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.w3c.dom.ls.LSInput;
Expand Down Expand Up @@ -203,6 +204,7 @@ public LSInput resolveResource(String theType, String theNamespaceURI, String th

InputStream baseIs = FhirValidator.class.getResourceAsStream(pathToBase);
if (baseIs == null) {
IOUtils.closeQuietly(baseIs);
throw new InternalErrorException("Schema file not found: " + pathToBase);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.oclc.purl.dsdl.svrl.SchematronOutputType;

Expand Down Expand Up @@ -120,12 +121,14 @@ private ISchematronResource getSchematronAndCache(IValidationContext<IBaseResour
return retVal;
}

String pathToBase = myCtx.getVersion().getPathToSchemaDefinitions() + '/' + theCtx.getFhirContext().getResourceDefinition(theCtx.getResource()).getBaseDefinition().getName().toLowerCase()
+ ".sch";
String pathToBase = myCtx.getVersion().getPathToSchemaDefinitions() + '/' + theCtx.getFhirContext().getResourceDefinition(theCtx.getResource()).getBaseDefinition().getName().toLowerCase() + ".sch";
InputStream baseIs = FhirValidator.class.getResourceAsStream(pathToBase);
if (baseIs == null) {
throw new InternalErrorException("No schematron found for resource type: "
+ theCtx.getFhirContext().getResourceDefinition(theCtx.getResource()).getBaseDefinition().getImplementingClass().getCanonicalName());
try {
if (baseIs == null) {
throw new InternalErrorException("No schematron found for resource type: " + theCtx.getFhirContext().getResourceDefinition(theCtx.getResource()).getBaseDefinition().getImplementingClass().getCanonicalName());
}
} finally {
IOUtils.closeQuietly(baseIs);
}

retVal = SchematronResourceSCH.fromClassPath(pathToBase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {

retVal.setPublisher(myPublisher);
retVal.setDate(DateTimeDt.withCurrentTime());
retVal.setFhirVersion("0.5.0"); // TODO: pull from model
retVal.setFhirVersion("1.0.0"); // TODO: pull from model
retVal.setAcceptUnknown(UnknownContentCodeEnum.UNKNOWN_EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Conformance getServerConformance(HttpServletRequest theRequest) {

retVal.setPublisher(myPublisher);
retVal.setDate(new Date());
retVal.setFhirVersion("0.5.0"); // TODO: pull from model
retVal.setFhirVersion("1.0.0"); // TODO: pull from model
retVal.setAcceptUnknown(UnknownContentCode.EXTENSIONS); // TODO: make this configurable - this is a fairly big effort since the parser
// needs to be modified to actually allow it

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ private void checkDeclaredProfiles(List<ValidationMessage> errors, WrapperElemen
String p = stack.addToLiteralPath("meta", "profile", ":"+Integer.toString(i));
if (rule(errors, IssueType.INVALID, element.line(), element.col(), p, !Utilities.noString(ref), "StructureDefinition reference invalid")) {
StructureDefinition pr = context.fetchResource(StructureDefinition.class, ref);
if (warning(errors, IssueType.INVALID, element.line(), element.col(), p, pr != null, "StructureDefinition reference could not be resolved")) {
if (warning(errors, IssueType.INVALID, element.line(), element.col(), p, pr != null, "StructureDefinition reference \"{0}\" could not be resolved", ref)) {
if (rule(errors, IssueType.STRUCTURE, element.line(), element.col(), p, pr.hasSnapshot(), "StructureDefinition has no snapshot - validation is against the snapshot, so it must be provided")) {
validateElement(errors, pr, pr.getSnapshot().getElement().get(0), null, null, element, element.getName(), stack);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.uhn.fhir.validation;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
Expand All @@ -8,6 +9,7 @@
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hl7.fhir.instance.model.CodeType;
Expand All @@ -23,6 +25,8 @@
import org.mockito.stubbing.Answer;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.primitive.IdDt;

public class FhirInstanceValidatorTest {

Expand Down Expand Up @@ -120,26 +124,63 @@ private List<SingleValidationMessage> logResultsAndReturnNonInformationalOnes(Va
}

@Test
public void testValidateJsonResource() {
// @formatter:off
String input = "{" + "\"resourceType\":\"Patient\"," + "\"id\":\"123\"" + "}";
// @formatter:on
public void testValidateRawJsonResource() {
//@formatter:off
String input =
"{" +
"\"resourceType\":\"Patient\"," +
"\"id\":\"123\"" +
"}";
//@formatter:on

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 0, output.getMessages().size());
}

@Test
public void testValidateJsonResourceBadAttributes() {
public void testValidateRawJsonResourceBadAttributes() {
//@formatter:off
String input =
"{" +
"\"resourceType\":\"Patient\"," +
"\"id\":\"123\"," +
"\"foo\":\"123\"" +
"}";
//@formatter:on

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 1, output.getMessages().size());
ourLog.info(output.getMessages().get(0).getLocationString());
ourLog.info(output.getMessages().get(0).getMessage());
assertEquals("/foo", output.getMessages().get(0).getLocationString());
assertEquals("Element is unknown or does not match any slice", output.getMessages().get(0).getMessage());
}

@Test
public void testValidateRawXmlResource() {
//@formatter:off
String input =
"<Patient xmlns=\"http://hl7.org/fhir\">" +
"<id value=\"123\"/>" +
"</Patient>";
//@formatter:on

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 0, output.getMessages().size());
}

@Test
public void testValidateRawXmlResourceBadAttributes() {
// @formatter:off
String input = "{" + "\"resourceType\":\"Patient\"," + "\"id\":\"123\"," + "\"foo\":\"123\"" + "}";
String input = "<Patient xmlns=\"http://hl7.org/fhir\">" + "<id value=\"123\"/>" + "<foo value=\"222\"/>"
+ "</Patient>";
// @formatter:on

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 1, output.getMessages().size());
ourLog.info(output.getMessages().get(0).getLocationString());
ourLog.info(output.getMessages().get(0).getMessage());
assertEquals("/foo", output.getMessages().get(0).getLocationString());
assertEquals("/f:Patient/f:foo", output.getMessages().get(0).getLocationString());
assertEquals("Element is unknown or does not match any slice", output.getMessages().get(0).getMessage());
}

Expand All @@ -159,34 +200,42 @@ public void testValidateResourceFailingInvariant() {
}

@Test
public void testValidateResourceWithExampleBindingCodeValidationFailing() {
public void testValidateResourceContainingProfileDeclarationDoesntResolve() {
addValidConcept("http://loinc.org", "12345");

Observation input = new Observation();

myInstanceVal.setValidationSupport(myMockSupport);

input.setStatus(ObservationStatus.FINAL);
input.getMeta().addProfile("http://foo/myprofile");

input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");
input.setStatus(ObservationStatus.FINAL);

myInstanceVal.setValidationSupport(myMockSupport);
ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 1, errors.size());
assertEquals("Unable to validate code \"12345\" in code system \"http://loinc.org\"", errors.get(0).getMessage());

assertEquals("StructureDefinition reference \"http://foo/myprofile\" could not be resolved", errors.get(0).getMessage());
}

@Test
public void testValidateResourceWithExampleBindingCodeValidationPassing() {
Observation input = new Observation();

myInstanceVal.setValidationSupport(myMockSupport);
public void testValidateResourceContainingProfileDeclaration() {
addValidConcept("http://loinc.org", "12345");

Observation input = new Observation();
input.getMeta().addProfile("http://hl7.org/fhir/StructureDefinition/devicemetricobservation");

input.addIdentifier().setSystem("http://acme").setValue("12345");
input.getEncounter().setReference("http://foo.com/Encounter/9");
input.setStatus(ObservationStatus.FINAL);
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");

myInstanceVal.setValidationSupport(myMockSupport);
ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 0, errors.size());

assertThat(errors.toString(), containsString("Element '/f:Observation.subject': minimum required = 1, but only found 0"));
assertThat(errors.toString(), containsString("Element encounter @ /f:Observation: max allowed = 0, but found 1"));
assertThat(errors.toString(), containsString("Element '/f:Observation.device': minimum required = 1, but only found 0"));
assertThat(errors.toString(), containsString(""));
}

@Test
Expand All @@ -213,27 +262,33 @@ public void testValidateResourceWithDefaultValuesetBadCode() {
}

@Test
public void testValidateXmlResource() {
// @formatter:off
String input = "<Patient xmlns=\"http://hl7.org/fhir\">" + "<id value=\"123\"/>" + "</Patient>";
// @formatter:on
public void testValidateResourceWithExampleBindingCodeValidationFailing() {
Observation input = new Observation();

myInstanceVal.setValidationSupport(myMockSupport);

input.setStatus(ObservationStatus.FINAL);
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 0, output.getMessages().size());
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 1, errors.size());
assertEquals("Unable to validate code \"12345\" in code system \"http://loinc.org\"", errors.get(0).getMessage());

}

@Test
public void testValidateXmlResourceBadAttributes() {
// @formatter:off
String input = "<Patient xmlns=\"http://hl7.org/fhir\">" + "<id value=\"123\"/>" + "<foo value=\"222\"/>"
+ "</Patient>";
// @formatter:on
public void testValidateResourceWithExampleBindingCodeValidationPassing() {
Observation input = new Observation();

myInstanceVal.setValidationSupport(myMockSupport);
addValidConcept("http://loinc.org", "12345");

input.setStatus(ObservationStatus.FINAL);
input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345");

ValidationResult output = myVal.validateWithResult(input);
assertEquals(output.toString(), 1, output.getMessages().size());
ourLog.info(output.getMessages().get(0).getLocationString());
ourLog.info(output.getMessages().get(0).getMessage());
assertEquals("/f:Patient/f:foo", output.getMessages().get(0).getLocationString());
assertEquals("Element is unknown or does not match any slice", output.getMessages().get(0).getMessage());
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 0, errors.size());
}
}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,11 @@
<copy todir="target/site/xref-base">
<fileset dir="hapi-fhir-base/target/site/xref" />
</copy>
<!--
<copy todir="target/site/cobertura">
<fileset dir="hapi-fhir-cobertura/target/site/cobertura" />
</copy>
-->
<copy todir="target/site">
<fileset dir="hapi-fhir-base/target/site" includes="checkstyle.*" />
</copy>
Expand Down Expand Up @@ -1164,7 +1166,9 @@
<module>hapi-fhir-structures-dstu2</module>
<module>hapi-fhir-structures-hl7org-dstu2</module>
<module>hapi-fhir-jpaserver-base</module>
<!--
<module>hapi-fhir-cobertura</module>
-->
<module>examples</module>
</modules>
<reporting>
Expand Down
5 changes: 2 additions & 3 deletions src/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@
</menu>


<menu name="Maven Reports" inherit="bottom">
<menu name="Reports" inherit="bottom">
<item name="Project Developers" href="team-list.html" />
<item name="Cobertura (Test Coverage)" href="cobertura/index.html" />
<item name="Surefire (Test Status)" href="surefire-report.html" />
<item name="Coveralls (Test Coverage)" href="https://coveralls.io/github/jamesagnew/hapi-fhir" />
<item name="FindBugs" href="findbugs.html" />
<item name="Checkstyle" href="checkstyle.html" />
</menu>
Expand Down
2 changes: 2 additions & 0 deletions src/site/xdoc/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<a href="https://maven-badges.herokuapp.com/maven-central/ca.uhn.hapi.fhir/hapi-fhir-base/badge.svg"><img src="https://maven-badges.herokuapp.com/maven-central/ca.uhn.hapi.fhir/hapi-fhir-base/badge.svg" alt="Maven Central"/></a>
<br/>
<a href="https://www.versioneye.com/user/projects/55e1d0d9c6d8f2001c00043e"><img src="https://www.versioneye.com/user/projects/55e1d0d9c6d8f2001c00043e/badge.svg?style=flat" alt="VersionEye"/></a>
<br/>
<a href="http://jamesagnew.github.io/hapi-fhir/license.html"><img src="https://img.shields.io/badge/license-apache%202.0-ff69b4.svg" alt="Apache 2.0 Licensed"/></a>
</p>

<p>
Expand Down

0 comments on commit 6139e49

Please sign in to comment.