From fc36dabc681dd1a8d21aa3d5cf86fbb1e70b83eb Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 31 Aug 2015 09:23:21 -0400 Subject: [PATCH] Fix build --- hapi-fhir-cobertura/pom.xml | 5 + hapi-fhir-jpaserver-base/pom.xml | 1 + .../validation/FhirInstanceValidatorTest.java | 371 +++++++++--------- 3 files changed, 189 insertions(+), 188 deletions(-) diff --git a/hapi-fhir-cobertura/pom.xml b/hapi-fhir-cobertura/pom.xml index d1aa6881852..ddebba6b3d6 100644 --- a/hapi-fhir-cobertura/pom.xml +++ b/hapi-fhir-cobertura/pom.xml @@ -155,6 +155,11 @@ xmlunit test + + org.springframework + spring-test + test + diff --git a/hapi-fhir-jpaserver-base/pom.xml b/hapi-fhir-jpaserver-base/pom.xml index 67b0dc2f89c..e35b8806c27 100644 --- a/hapi-fhir-jpaserver-base/pom.xml +++ b/hapi-fhir-jpaserver-base/pom.xml @@ -178,6 +178,7 @@ org.springframework spring-test + test diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java index 05aaffaf028..d5a517b9e5b 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java @@ -27,31 +27,31 @@ import ca.uhn.fhir.context.FhirContext; public class FhirInstanceValidatorTest { - private static FhirContext ourCtx = FhirContext.forDstu2Hl7Org(); - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorTest.class); - private DefaultProfileValidationSupport myDefaultValidationSupport = new DefaultProfileValidationSupport(); - private FhirInstanceValidator myInstanceVal; + private static FhirContext ourCtx = FhirContext.forDstu2Hl7Org(); + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorTest.class); + private DefaultProfileValidationSupport myDefaultValidationSupport = new DefaultProfileValidationSupport(); + private FhirInstanceValidator myInstanceVal; private IValidationSupport myMockSupport; - - private FhirValidator myVal; + + private FhirValidator myVal; private ArrayList myValidConcepts; private void addValidConcept(String theSystem, String theCode) { myValidConcepts.add(theSystem + "___" + theCode); } - - @SuppressWarnings("unchecked") + + @SuppressWarnings("unchecked") @Before - public void before() { - myVal = ourCtx.newValidator(); - myVal.setValidateAgainstStandardSchema(false); - myVal.setValidateAgainstStandardSchematron(false); - - myInstanceVal = new FhirInstanceValidator(); - myVal.registerValidatorModule(myInstanceVal); - - myValidConcepts = new ArrayList(); - + public void before() { + myVal = ourCtx.newValidator(); + myVal.setValidateAgainstStandardSchema(false); + myVal.setValidateAgainstStandardSchematron(false); + + myInstanceVal = new FhirInstanceValidator(); + myVal.registerValidatorModule(myInstanceVal); + + myValidConcepts = new ArrayList(); + myMockSupport = mock(IValidationSupport.class); when(myMockSupport.isCodeSystemSupported(any(String.class))).thenAnswer(new Answer() { @Override @@ -59,187 +59,182 @@ public class FhirInstanceValidatorTest { boolean retVal = myDefaultValidationSupport.isCodeSystemSupported((String) theInvocation.getArguments()[0]); ourLog.info("isCodeSystemSupported({}) : {}", theInvocation.getArguments()[0], retVal); return retVal; - }}); - when(myMockSupport.fetchResource(any(FhirContext.class), any(Class.class), any(String.class))).thenAnswer(new Answer() { - @Override - public IBaseResource answer(InvocationOnMock theInvocation) throws Throwable { - IBaseResource retVal = myDefaultValidationSupport.fetchResource((FhirContext)theInvocation.getArguments()[0], (Class) theInvocation.getArguments()[1],(String)theInvocation.getArguments()[2]); - ourLog.info("fetchResource({}, {}) : {}", theInvocation.getArguments()[1], theInvocation.getArguments()[2], retVal); - return retVal; - }}); - when(myMockSupport.validateCode(any(String.class), any(String.class),any(String.class))).thenAnswer(new Answer() { - @Override - public org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult answer(InvocationOnMock theInvocation) - throws Throwable { - String system = (String)theInvocation.getArguments()[0]; - String code = (String)theInvocation.getArguments()[1]; - org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult retVal; - if (myValidConcepts.contains(system+"___"+code)) { - retVal = new org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult(new ConceptDefinitionComponent(new CodeType(code))); - } else { - retVal = myDefaultValidationSupport.validateCode(system,code, (String)theInvocation.getArguments()[2]); - } - ourLog.info("validateCode({}, {}, {}) : {}", new Object[] {system,code, (String)theInvocation.getArguments()[2], retVal}); - return retVal; - }}); + } + }); + when(myMockSupport.fetchResource(any(FhirContext.class), any(Class.class), any(String.class))) + .thenAnswer(new Answer() { + @Override + public IBaseResource answer(InvocationOnMock theInvocation) throws Throwable { + IBaseResource retVal = myDefaultValidationSupport.fetchResource( + (FhirContext) theInvocation.getArguments()[0], (Class) theInvocation.getArguments()[1], + (String) theInvocation.getArguments()[2]); + ourLog.info("fetchResource({}, {}) : {}", theInvocation.getArguments()[1], theInvocation.getArguments()[2], + retVal); + return retVal; + } + }); + when(myMockSupport.validateCode(any(String.class), any(String.class), any(String.class))) + .thenAnswer(new Answer() { + @Override + public org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult answer(InvocationOnMock theInvocation) + throws Throwable { + String system = (String) theInvocation.getArguments()[0]; + String code = (String) theInvocation.getArguments()[1]; + org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult retVal; + if (myValidConcepts.contains(system + "___" + code)) { + retVal = new org.hl7.fhir.instance.utils.IWorkerContext.ValidationResult( + new ConceptDefinitionComponent(new CodeType(code))); + } else { + retVal = myDefaultValidationSupport.validateCode(system, code, (String) theInvocation.getArguments()[2]); + } + ourLog.info("validateCode({}, {}, {}) : {}", + new Object[] { system, code, (String) theInvocation.getArguments()[2], retVal }); + return retVal; + } + }); when(myMockSupport.fetchCodeSystem(any(String.class))).thenAnswer(new Answer() { @Override - public ValueSet answer(InvocationOnMock theInvocation) - throws Throwable { - ValueSet retVal = myDefaultValidationSupport.fetchCodeSystem((String)theInvocation.getArguments()[0]); - ourLog.info("fetchCodeSystem({}) : {}", new Object[] {(String)theInvocation.getArguments()[0], retVal}); + public ValueSet answer(InvocationOnMock theInvocation) throws Throwable { + ValueSet retVal = myDefaultValidationSupport.fetchCodeSystem((String) theInvocation.getArguments()[0]); + ourLog.info("fetchCodeSystem({}) : {}", new Object[] { (String) theInvocation.getArguments()[0], retVal }); return retVal; - }}); + } + }); - } - - private List logResultsAndReturnNonInformationalOnes(ValidationResult theOutput) { - List retVal = new ArrayList(); - - int index = 0; - for (SingleValidationMessage next : theOutput.getMessages()) { - ourLog.info("Result {}: {} - {} - {}", index, next.getSeverity(), next.getLocationString(), next.getMessage()); - index++; - - if (next.getSeverity() != ResultSeverityEnum.INFORMATION) { - retVal.add(next); - } - } - - return retVal; } - - @Test - public void testValidateJsonResource() { - //@formatter:off - String input = "{" - + "\"resourceType\":\"Patient\"," - + "\"id\":\"123\"" - + "}"; - //@formatter:on - ValidationResult output = myVal.validateWithResult(input); - assertEquals(output.toString(), 0, output.getMessages().size()); - } + private List logResultsAndReturnNonInformationalOnes(ValidationResult theOutput) { + List retVal = new ArrayList(); - @Test - public void testValidateJsonResourceBadAttributes() { - //@formatter:off - String input = "{" - + "\"resourceType\":\"Patient\"," - + "\"id\":\"123\"," - + "\"foo\":\"123\"" - + "}"; - //@formatter:on + int index = 0; + for (SingleValidationMessage next : theOutput.getMessages()) { + ourLog.info("Result {}: {} - {} - {}", + new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() }); + index++; - 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 testValidateResourceFailingInvariant() { - Observation input = new Observation(); - - // Has a value, but not a status (which is required) - input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345"); - input.setValue(new StringType("AAA")); - - ValidationResult output = myVal.validateWithResult(input); - assertThat(output.getMessages().size(), greaterThan(0)); - assertEquals("Element '/f:Observation.status': minimum required = 1, but only found 0", output.getMessages().get(0).getMessage()); - - } + if (next.getSeverity() != ResultSeverityEnum.INFORMATION) { + retVal.add(next); + } + } - - - @Test - 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); - List 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 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); - List errors = logResultsAndReturnNonInformationalOnes(output); - assertEquals(errors.toString(), 0, errors.size()); - } - - @Test - public void testValidateResourceWithDefaultValueset() { - Observation input = new Observation(); - - input.setStatus(ObservationStatus.FINAL); - input.getCode().setText("No code here!"); - - ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(input)); - - ValidationResult output = myVal.validateWithResult(input); - assertEquals(output.getMessages().size(), 0); - } + return retVal; + } @Test - public void testValidateResourceWithDefaultValuesetBadCode() { - String input = "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - ""; - ValidationResult output = myVal.validateWithResult(input); - assertEquals("Coded value notvalidcode is not in value set http://hl7.org/fhir/ValueSet/observation-status (http://hl7.org/fhir/ValueSet/observation-status)", output.getMessages().get(0).getMessage()); - } + public void testValidateJsonResource() { + // @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 testValidateXmlResource() { - //@formatter:off - String input = "" - + "" - + ""; - //@formatter:on - - ValidationResult output = myVal.validateWithResult(input); - assertEquals(output.toString(), 0, output.getMessages().size()); - } - - @Test - public void testValidateXmlResourceBadAttributes() { - //@formatter:off - String input = "" - + "" - + "" - + ""; - //@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("/f:Patient/f:foo", output.getMessages().get(0).getLocationString()); - assertEquals("Element is unknown or does not match any slice", output.getMessages().get(0).getMessage()); - } + @Test + public void testValidateJsonResourceBadAttributes() { + // @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 testValidateResourceFailingInvariant() { + Observation input = new Observation(); + + // Has a value, but not a status (which is required) + input.getCode().addCoding().setSystem("http://loinc.org").setCode("12345"); + input.setValue(new StringType("AAA")); + + ValidationResult output = myVal.validateWithResult(input); + assertThat(output.getMessages().size(), greaterThan(0)); + assertEquals("Element '/f:Observation.status': minimum required = 1, but only found 0", + output.getMessages().get(0).getMessage()); + + } + + @Test + 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); + List 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 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); + List errors = logResultsAndReturnNonInformationalOnes(output); + assertEquals(errors.toString(), 0, errors.size()); + } + + @Test + public void testValidateResourceWithDefaultValueset() { + Observation input = new Observation(); + + input.setStatus(ObservationStatus.FINAL); + input.getCode().setText("No code here!"); + + ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(input)); + + ValidationResult output = myVal.validateWithResult(input); + assertEquals(output.getMessages().size(), 0); + } + + @Test + public void testValidateResourceWithDefaultValuesetBadCode() { + String input = "\n" + " \n" + + " \n" + " \n" + " \n" + ""; + ValidationResult output = myVal.validateWithResult(input); + assertEquals( + "Coded value notvalidcode is not in value set http://hl7.org/fhir/ValueSet/observation-status (http://hl7.org/fhir/ValueSet/observation-status)", + output.getMessages().get(0).getMessage()); + } + + @Test + public void testValidateXmlResource() { + // @formatter:off + String input = "" + "" + ""; + // @formatter:on + + ValidationResult output = myVal.validateWithResult(input); + assertEquals(output.toString(), 0, output.getMessages().size()); + } + + @Test + public void testValidateXmlResourceBadAttributes() { + // @formatter:off + String input = "" + "" + "" + + ""; + // @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("/f:Patient/f:foo", output.getMessages().get(0).getLocationString()); + assertEquals("Element is unknown or does not match any slice", output.getMessages().get(0).getMessage()); + } }