Speed up unit test

This commit is contained in:
jamesagnew 2016-01-01 17:55:47 -05:00
parent 09b0982df1
commit 6b96fefeb7
1 changed files with 279 additions and 299 deletions

View File

@ -42,9 +42,10 @@ import ca.uhn.fhir.context.FhirContext;
public class FhirInstanceValidatorTest { public class FhirInstanceValidatorTest {
private static final DefaultProfileValidationSupport VALIDATION_SUPPORT = new DefaultProfileValidationSupport();
private static FhirContext ourCtx = FhirContext.forDstu2_1(); private static FhirContext ourCtx = FhirContext.forDstu2_1();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorTest.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirInstanceValidatorTest.class);
private DefaultProfileValidationSupport myDefaultValidationSupport = new DefaultProfileValidationSupport(); private DefaultProfileValidationSupport myDefaultValidationSupport = VALIDATION_SUPPORT;
private FhirInstanceValidator myInstanceVal; private FhirInstanceValidator myInstanceVal;
private IValidationSupport myMockSupport; private IValidationSupport myMockSupport;
@ -63,7 +64,7 @@ public class FhirInstanceValidatorTest {
myVal.setValidateAgainstStandardSchema(false); myVal.setValidateAgainstStandardSchema(false);
myVal.setValidateAgainstStandardSchematron(false); myVal.setValidateAgainstStandardSchematron(false);
myInstanceVal = new FhirInstanceValidator(); myInstanceVal = new FhirInstanceValidator(VALIDATION_SUPPORT);
myVal.registerValidatorModule(myInstanceVal); myVal.registerValidatorModule(myInstanceVal);
mySupportedCodeSystemsForExpansion = new HashMap<String, ValueSet.ValueSetExpansionComponent>(); mySupportedCodeSystemsForExpansion = new HashMap<String, ValueSet.ValueSetExpansionComponent>();
@ -74,7 +75,7 @@ public class FhirInstanceValidatorTest {
when(myMockSupport.expandValueSet(any(FhirContext.class), any(ConceptSetComponent.class))).thenAnswer(new Answer<ValueSetExpansionComponent>() { when(myMockSupport.expandValueSet(any(FhirContext.class), any(ConceptSetComponent.class))).thenAnswer(new Answer<ValueSetExpansionComponent>() {
@Override @Override
public ValueSetExpansionComponent answer(InvocationOnMock theInvocation) throws Throwable { public ValueSetExpansionComponent answer(InvocationOnMock theInvocation) throws Throwable {
ConceptSetComponent arg = (ConceptSetComponent)theInvocation.getArguments()[0]; ConceptSetComponent arg = (ConceptSetComponent) theInvocation.getArguments()[0];
ValueSetExpansionComponent retVal = mySupportedCodeSystemsForExpansion.get(arg.getSystem()); ValueSetExpansionComponent retVal = mySupportedCodeSystemsForExpansion.get(arg.getSystem());
if (retVal == null) { if (retVal == null) {
retVal = myDefaultValidationSupport.expandValueSet(any(FhirContext.class), arg); retVal = myDefaultValidationSupport.expandValueSet(any(FhirContext.class), arg);
@ -91,20 +92,15 @@ public class FhirInstanceValidatorTest {
return retVal; return retVal;
} }
}); });
when(myMockSupport.fetchResource(any(FhirContext.class), any(Class.class), any(String.class))) when(myMockSupport.fetchResource(any(FhirContext.class), any(Class.class), any(String.class))).thenAnswer(new Answer<IBaseResource>() {
.thenAnswer(new Answer<IBaseResource>() {
@Override @Override
public IBaseResource answer(InvocationOnMock theInvocation) throws Throwable { public IBaseResource answer(InvocationOnMock theInvocation) throws Throwable {
IBaseResource retVal = myDefaultValidationSupport.fetchResource( IBaseResource retVal = myDefaultValidationSupport.fetchResource((FhirContext) theInvocation.getArguments()[0], (Class<IBaseResource>) theInvocation.getArguments()[1], (String) theInvocation.getArguments()[2]);
(FhirContext) theInvocation.getArguments()[0], (Class<IBaseResource>) theInvocation.getArguments()[1], ourLog.info("fetchResource({}, {}) : {}", new Object[] { theInvocation.getArguments()[1], theInvocation.getArguments()[2], retVal });
(String) theInvocation.getArguments()[2]);
ourLog.info("fetchResource({}, {}) : {}",
new Object[] { theInvocation.getArguments()[1], theInvocation.getArguments()[2], retVal });
return retVal; return retVal;
} }
}); });
when(myMockSupport.validateCode(any(FhirContext.class), any(String.class), any(String.class), any(String.class))) when(myMockSupport.validateCode(any(FhirContext.class), any(String.class), any(String.class), any(String.class))).thenAnswer(new Answer<CodeValidationResult>() {
.thenAnswer(new Answer<CodeValidationResult>() {
@Override @Override
public CodeValidationResult answer(InvocationOnMock theInvocation) throws Throwable { public CodeValidationResult answer(InvocationOnMock theInvocation) throws Throwable {
FhirContext ctx = (FhirContext) theInvocation.getArguments()[0]; FhirContext ctx = (FhirContext) theInvocation.getArguments()[0];
@ -116,15 +112,14 @@ public class FhirInstanceValidatorTest {
} else { } else {
retVal = myDefaultValidationSupport.validateCode(ctx, system, code, (String) theInvocation.getArguments()[2]); retVal = myDefaultValidationSupport.validateCode(ctx, system, code, (String) theInvocation.getArguments()[2]);
} }
ourLog.info("validateCode({}, {}, {}) : {}", ourLog.info("validateCode({}, {}, {}) : {}", new Object[] { system, code, (String) theInvocation.getArguments()[2], retVal });
new Object[] { system, code, (String) theInvocation.getArguments()[2], retVal });
return retVal; return retVal;
} }
}); });
when(myMockSupport.fetchCodeSystem(any(FhirContext.class), any(String.class))).thenAnswer(new Answer<ValueSet>() { when(myMockSupport.fetchCodeSystem(any(FhirContext.class), any(String.class))).thenAnswer(new Answer<ValueSet>() {
@Override @Override
public ValueSet answer(InvocationOnMock theInvocation) throws Throwable { public ValueSet answer(InvocationOnMock theInvocation) throws Throwable {
ValueSet retVal = myDefaultValidationSupport.fetchCodeSystem((FhirContext) theInvocation.getArguments()[0],(String) theInvocation.getArguments()[1]); ValueSet retVal = myDefaultValidationSupport.fetchCodeSystem((FhirContext) theInvocation.getArguments()[0], (String) theInvocation.getArguments()[1]);
ourLog.info("fetchCodeSystem({}) : {}", new Object[] { (String) theInvocation.getArguments()[1], retVal }); ourLog.info("fetchCodeSystem({}) : {}", new Object[] { (String) theInvocation.getArguments()[1], retVal });
return retVal; return retVal;
} }
@ -137,8 +132,7 @@ public class FhirInstanceValidatorTest {
int index = 0; int index = 0;
for (SingleValidationMessage next : theOutput.getMessages()) { for (SingleValidationMessage next : theOutput.getMessages()) {
ourLog.info("Result {}: {} - {} - {}", ourLog.info("Result {}: {} - {} - {}", new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() });
new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() });
index++; index++;
if (next.getSeverity() != ResultSeverityEnum.INFORMATION) { if (next.getSeverity() != ResultSeverityEnum.INFORMATION) {
@ -154,8 +148,7 @@ public class FhirInstanceValidatorTest {
int index = 0; int index = 0;
for (SingleValidationMessage next : theOutput.getMessages()) { for (SingleValidationMessage next : theOutput.getMessages()) {
ourLog.info("Result {}: {} - {} - {}", ourLog.info("Result {}: {} - {} - {}", new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() });
new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() });
index++; index++;
retVal.add(next); retVal.add(next);
@ -230,8 +223,7 @@ public class FhirInstanceValidatorTest {
ValidationResult output = myVal.validateWithResult(input); ValidationResult output = myVal.validateWithResult(input);
assertThat(output.getMessages().size(), greaterThan(0)); assertThat(output.getMessages().size(), greaterThan(0));
assertEquals("Element '/f:Observation.status': minimum required = 1, but only found 0", assertEquals("Element '/f:Observation.status': minimum required = 1, but only found 0", output.getMessages().get(0).getMessage());
output.getMessages().get(0).getMessage());
} }
@ -240,8 +232,7 @@ public class FhirInstanceValidatorTest {
*/ */
@Test @Test
public void testValidateRawXmlInvalidChoiceName() throws Exception { public void testValidateRawXmlInvalidChoiceName() throws Exception {
String input = IOUtils String input = IOUtils.toString(FhirInstanceValidator.class.getResourceAsStream("/medicationstatement_invalidelement.xml"));
.toString(FhirInstanceValidator.class.getResourceAsStream("/medicationstatement_invalidelement.xml"));
ValidationResult output = myVal.validateWithResult(input); ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> res = logResultsAndReturnAll(output); List<SingleValidationMessage> res = logResultsAndReturnAll(output);
@ -255,7 +246,7 @@ public class FhirInstanceValidatorTest {
// TODO: we should really not have any errors at all here, but for // TODO: we should really not have any errors at all here, but for
// now we aren't validating snomed codes correctly // now we aren't validating snomed codes correctly
// assertEquals(output.toString(), 0, res.size()); // assertEquals(output.toString(), 0, res.size());
} }
@ -273,8 +264,7 @@ public class FhirInstanceValidatorTest {
ValidationResult output = myVal.validateWithResult(input); ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output); List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 1, errors.size()); assertEquals(errors.toString(), 1, errors.size());
assertEquals("StructureDefinition reference \"http://foo/myprofile\" could not be resolved", assertEquals("StructureDefinition reference \"http://foo/myprofile\" could not be resolved", errors.get(0).getMessage());
errors.get(0).getMessage());
} }
@Test @Test
@ -293,11 +283,9 @@ public class FhirInstanceValidatorTest {
ValidationResult output = myVal.validateWithResult(input); ValidationResult output = myVal.validateWithResult(input);
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output); List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertThat(errors.toString(), assertThat(errors.toString(), containsString("Element '/f:Observation.subject': minimum required = 1, but only found 0"));
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 encounter @ /f:Observation: max allowed = 0, but found 1"));
assertThat(errors.toString(), assertThat(errors.toString(), containsString("Element '/f:Observation.device': minimum required = 1, but only found 0"));
containsString("Element '/f:Observation.device': minimum required = 1, but only found 0"));
assertThat(errors.toString(), containsString("")); assertThat(errors.toString(), containsString(""));
} }
@ -316,17 +304,11 @@ public class FhirInstanceValidatorTest {
@Test @Test
public void testValidateResourceWithDefaultValuesetBadCode() { public void testValidateResourceWithDefaultValuesetBadCode() {
String input = "<Observation xmlns=\"http://hl7.org/fhir\">\n" + " <status value=\"notvalidcode\"/>\n" String input = "<Observation xmlns=\"http://hl7.org/fhir\">\n" + " <status value=\"notvalidcode\"/>\n" + " <code>\n" + " <text value=\"No code here!\"/>\n" + " </code>\n" + "</Observation>";
+ " <code>\n" + " <text value=\"No code here!\"/>\n" + " </code>\n" + "</Observation>";
ValidationResult output = myVal.validateWithResult(input); ValidationResult output = myVal.validateWithResult(input);
assertEquals( assertEquals("The value provided is not in the value set http://hl7.org/fhir/ValueSet/observation-status (http://hl7.org/fhir/ValueSet/observation-status, and a code is required from this value set", output.getMessages().get(0).getMessage());
"The value provided is not in the value set http://hl7.org/fhir/ValueSet/observation-status (http://hl7.org/fhir/ValueSet/observation-status, and a code is required from this value set",
output.getMessages().get(0).getMessage());
} }
@Test @Test
public void testValidateResourceWithValuesetExpansionBad() { public void testValidateResourceWithValuesetExpansionBad() {
@ -342,7 +324,6 @@ public class FhirInstanceValidatorTest {
} }
@Test @Test
public void testValidateResourceWithValuesetExpansionGood() { public void testValidateResourceWithValuesetExpansionGood() {
Patient patient = new Patient(); Patient patient = new Patient();
@ -401,7 +382,6 @@ public class FhirInstanceValidatorTest {
List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output); List<SingleValidationMessage> errors = logResultsAndReturnNonInformationalOnes(output);
assertEquals(errors.toString(), 0, errors.size()); assertEquals(errors.toString(), 0, errors.size());
} }
@Test @Test