Fix more tests 2

This commit is contained in:
dotasek.dev 2024-02-28 18:13:58 -05:00
parent 9e56aeec1b
commit 50a1cb214f
3 changed files with 125 additions and 49 deletions

View File

@ -1,8 +1,10 @@
package org.hl7.fhir.dstu2.hapi.validation;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.support.ConceptValidationOptions;
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
import ca.uhn.fhir.context.support.IValidationSupport;
import ca.uhn.fhir.context.support.ValidationSupportContext;
import ca.uhn.fhir.fhirpath.BaseValidationTestWithInlineMocks;
import ca.uhn.fhir.model.dstu2.composite.PeriodDt;
import ca.uhn.fhir.model.dstu2.resource.Parameters;
@ -26,12 +28,23 @@ import org.hl7.fhir.dstu2.model.Observation.ObservationStatus;
import org.hl7.fhir.dstu2.model.QuestionnaireResponse;
import org.hl7.fhir.dstu2.model.QuestionnaireResponse.QuestionnaireResponseStatus;
import org.hl7.fhir.dstu2.model.StringType;
import org.hl7.fhir.dstu3.model.CodeSystem;
import org.hl7.fhir.utilities.validation.ValidationMessage;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
@ -39,6 +52,11 @@ import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
public class FhirInstanceValidatorDstu2Test extends BaseValidationTestWithInlineMocks {
@ -47,11 +65,62 @@ public class FhirInstanceValidatorDstu2Test extends BaseValidationTestWithInline
private static FhirInstanceValidator ourValidator;
private static FhirContext ourCtxHl7OrgDstu2 = FhirContext.forDstu2Hl7Org();
@BeforeAll
public static void beforeClass() {
private ArrayList<String> myValidConcepts;
private final Set<String> myValidSystems = new HashSet<>();
private void addValidConcept(String theSystem, String theCode) {
myValidSystems.add(theSystem);
myValidConcepts.add(theSystem + "___" + theCode);
}
@BeforeEach
public void beforeEach() {
myValidConcepts = new ArrayList<>();
DefaultProfileValidationSupport defaultProfileValidationSupport = new DefaultProfileValidationSupport(ourCtxDstu2);
IValidationSupport validationSupport = new ValidationSupportChain(defaultProfileValidationSupport, new InMemoryTerminologyServerValidationSupport(ourCtxDstu2));
IValidationSupport mockSupport = mock(IValidationSupport.class, withSettings().strictness(Strictness.LENIENT));
when(mockSupport.getFhirContext()).thenReturn(ourCtxDstu2);
when(mockSupport.isCodeSystemSupported(any(), nullable(String.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock theInvocation) {
String url = (String) theInvocation.getArguments()[1];
boolean retVal = myValidSystems.contains(url);
ourLog.debug("isCodeSystemSupported({}) : {}", new Object[]{url, retVal});
return retVal;
}
});
when(mockSupport.validateCode(any(), any(), nullable(String.class), nullable(String.class), nullable(String.class), nullable(String.class))).thenAnswer(new Answer<IValidationSupport.CodeValidationResult>() {
@Override
public IValidationSupport.CodeValidationResult answer(InvocationOnMock theInvocation) {
ConceptValidationOptions options = theInvocation.getArgument(1, ConceptValidationOptions.class);
String system = theInvocation.getArgument(2, String.class);
String code = theInvocation.getArgument(3, String.class);
String display = theInvocation.getArgument(4, String.class);
String valueSetUrl = theInvocation.getArgument(5, String.class);
IValidationSupport.CodeValidationResult retVal;
if (myValidConcepts.contains(system + "___" + code)) {
retVal = new IValidationSupport.CodeValidationResult().setCode(code);
} else if (myValidSystems.contains(system)) {
return new IValidationSupport.CodeValidationResult().setSeverityCode(ValidationMessage.IssueSeverity.ERROR.toCode()).setMessage("Unknown code");
} else {
retVal = null;
}
ourLog.debug("validateCode({}, {}, {}, {}) : {}", system, code, display, valueSetUrl, retVal);
return retVal;
}
});
IValidationSupport validationSupport = new ValidationSupportChain(
mockSupport,
defaultProfileValidationSupport,
new InMemoryTerminologyServerValidationSupport(ourCtxDstu2));
ourValidator = new FhirInstanceValidator(validationSupport);
}
/**
@ -72,6 +141,8 @@ public class FhirInstanceValidatorDstu2Test extends BaseValidationTestWithInline
@Test
public void testObservation() {
addValidConcept("http://loinc.org", "12345");
Observation o = new Observation();
o.addIdentifier().setSystem("http://acme.org").setValue("1234");
o.setStatus(ObservationStatus.FINAL);

View File

@ -173,14 +173,7 @@ public class FhirInstanceValidatorDstu3Test extends BaseValidationTestWithInline
return retVal;
}
});
// when(mockSupport.isValueSetSupported(any(), nullable(String.class))).thenAnswer(new Answer<Boolean>() {
// @Override
// public Boolean answer(InvocationOnMock theInvocation) {
// String url = (String) theInvocation.getArguments()[1];
// boolean retVal = myValueSets.containsKey(url);
// return retVal;
// }
// });
when(mockSupport.fetchValueSet(any())).thenAnswer(t->{
String url = t.getArgument(0, String.class);
return myValueSets.get(url);

View File

@ -167,6 +167,8 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
myValidConcepts = new ArrayList<>();
addMockedValueSets();
when(myMockSupport.expandValueSet(any(), nullable(ValueSetExpansionOptions.class), any(IBaseResource.class))).thenAnswer(t -> {
ValueSet arg = (ValueSet) t.getArgument(2, IBaseResource.class);
ValueSetExpansionComponent retVal = mySupportedCodeSystemsForExpansion.get(arg.getCompose().getIncludeFirstRep().getSystem());
@ -180,15 +182,6 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
valueset.setExpansion(retVal);
return new ValueSetExpander.ValueSetExpansionOutcome(valueset);
});
when(myMockSupport.isCodeSystemSupported(any(), nullable(String.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock theInvocation) {
String argument = theInvocation.getArgument(1, String.class);
boolean retVal = myValidSystems.contains(argument);
ourLog.debug("isCodeSystemSupported({}) : {}", argument, retVal);
return retVal;
}
});
when(myMockSupport.fetchResource(nullable(Class.class), nullable(String.class))).thenAnswer(new Answer<IBaseResource>() {
@Override
public IBaseResource answer(InvocationOnMock theInvocation) throws Throwable {
@ -206,26 +199,7 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
return retVal;
}
});
when(myMockSupport.validateCode(any(), any(), nullable(String.class), nullable(String.class), nullable(String.class), nullable(String.class))).thenAnswer(new Answer<IValidationSupport.CodeValidationResult>() {
@Override
public IValidationSupport.CodeValidationResult answer(InvocationOnMock theInvocation) {
ConceptValidationOptions options = theInvocation.getArgument(1, ConceptValidationOptions.class);
String system = theInvocation.getArgument(2, String.class);
String code = theInvocation.getArgument(3, String.class);
String display = theInvocation.getArgument(4, String.class);
String valueSetUrl = theInvocation.getArgument(5, String.class);
IValidationSupport.CodeValidationResult retVal;
if (myValidConcepts.contains(system + "___" + code)) {
retVal = new IValidationSupport.CodeValidationResult().setCode(code);
} else if (myValidSystems.contains(system)) {
return new IValidationSupport.CodeValidationResult().setSeverityCode(ValidationMessage.IssueSeverity.ERROR.toCode()).setMessage("Unknown code");
} else {
retVal = myDefaultValidationSupport.validateCode(new ValidationSupportContext(myDefaultValidationSupport), options, system, code, display, valueSetUrl);
}
ourLog.debug("validateCode({}, {}, {}, {}) : {}", system, code, display, valueSetUrl, retVal);
return retVal;
}
});
when(myMockSupport.fetchCodeSystem(nullable(String.class))).thenAnswer(new Answer<CodeSystem>() {
@Override
public CodeSystem answer(InvocationOnMock theInvocation) {
@ -287,6 +261,40 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
}
private void addMockedValueSets() {
when(myMockSupport.isCodeSystemSupported(any(), nullable(String.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock theInvocation) {
String argument = theInvocation.getArgument(1, String.class);
boolean retVal = myValidSystems.contains(argument);
ourLog.debug("isCodeSystemSupported({}) : {}", argument, retVal);
return retVal;
}
});
when(myMockSupport.validateCode(any(), any(), nullable(String.class), nullable(String.class), nullable(String.class), nullable(String.class))).thenAnswer(new Answer<IValidationSupport.CodeValidationResult>() {
@Override
public IValidationSupport.CodeValidationResult answer(InvocationOnMock theInvocation) {
ConceptValidationOptions options = theInvocation.getArgument(1, ConceptValidationOptions.class);
String system = theInvocation.getArgument(2, String.class);
String code = theInvocation.getArgument(3, String.class);
String display = theInvocation.getArgument(4, String.class);
String valueSetUrl = theInvocation.getArgument(5, String.class);
IValidationSupport.CodeValidationResult retVal;
if (myValidConcepts.contains(system + "___" + code)) {
retVal = new IValidationSupport.CodeValidationResult().setCode(code);
} else if (myValidSystems.contains(system)) {
return new IValidationSupport.CodeValidationResult().setSeverityCode(ValidationMessage.IssueSeverity.ERROR.toCode()).setMessage("Unknown code");
} else {
retVal = myDefaultValidationSupport.validateCode(new ValidationSupportContext(myDefaultValidationSupport), options, system, code, display, valueSetUrl);
}
ourLog.debug("validateCode({}, {}, {}, {}) : {}", system, code, display, valueSetUrl, retVal);
return retVal;
}
});
}
private Object defaultString(Integer theLocationLine) {
return theLocationLine != null ? theLocationLine.toString() : "";
}
@ -1697,9 +1705,12 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
@Test
void testValidateCommonCodes_Ucum_ErrorMessageIsPreserved() {
buildValidationSupportWithLogicalAndSupport(false);
addValidConcept("http://loinc.org", "1234");
Observation input = new Observation();
buildValidationSupportWithLogicalAndSupport(false);
addMockedValueSets();
Observation input = createObservationWithDefaultSubjectPerfomerEffective();
input.getText().setDiv(new XhtmlNode().setValue("<div>AA</div>")).setStatus(Narrative.NarrativeStatus.GENERATED);
input.setStatus(ObservationStatus.AMENDED);
input.getCode().addCoding().setSystem("http://loinc.org").setCode("1234").setDisplay("FOO");
@ -1720,12 +1731,12 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
assertEquals("Error processing unit 'MG/DL': The unit 'DL' is unknown' at position 3 (for 'http://unitsofmeasure.org#MG/DL')", result.getMessages().get(0).getMessage());
assertEquals(ResultSeverityEnum.ERROR, result.getMessages().get(0).getSeverity());
assertEquals(15, result.getMessages().get(0).getLocationLine());
assertEquals(22, result.getMessages().get(0).getLocationLine());
assertEquals(4, result.getMessages().get(0).getLocationCol());
assertEquals("Observation.value.ofType(Quantity)", result.getMessages().get(0).getLocationString());
assertEquals("Terminology_PassThrough_TX_Message", result.getMessages().get(0).getMessageId());
assertEquals(15, ((IntegerType) oo.getIssue().get(0).getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/operationoutcome-issue-line").getValue()).getValue());
assertEquals(22, ((IntegerType) oo.getIssue().get(0).getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/operationoutcome-issue-line").getValue()).getValue());
assertEquals(4, ((IntegerType) oo.getIssue().get(0).getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/operationoutcome-issue-col").getValue()).getValue());
assertEquals("Terminology_PassThrough_TX_Message", ((StringType) oo.getIssue().get(0).getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/operationoutcome-message-id").getValue()).getValue());
assertEquals("Error processing unit 'MG/DL': The unit 'DL' is unknown' at position 3 (for 'http://unitsofmeasure.org#MG/DL')", oo.getIssue().get(0).getDiagnostics());
@ -1733,7 +1744,7 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
assertEquals(OperationOutcome.IssueSeverity.ERROR, oo.getIssue().get(0).getSeverity());
assertEquals(2, oo.getIssue().get(0).getLocation().size());
assertEquals("Observation.value.ofType(Quantity)", oo.getIssue().get(0).getLocation().get(0).getValue());
assertEquals("Line[15] Col[4]", oo.getIssue().get(0).getLocation().get(1).getValue());
assertEquals("Line[22] Col[4]", oo.getIssue().get(0).getLocation().get(1).getValue());
}
@Test
@ -1741,6 +1752,7 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
addValidConcept("http://loinc.org", "1234");
buildValidationSupportWithLogicalAndSupport(false);
addMockedValueSets();
Observation input = createObservationWithDefaultSubjectPerfomerEffective();
input.getText().setDiv(new XhtmlNode().setValue("<div>AA</div>")).setStatus(Narrative.NarrativeStatus.GENERATED);
@ -1761,10 +1773,10 @@ public class FhirInstanceValidatorR4Test extends BaseValidationTestWithInlineMoc
OperationOutcome oo = (OperationOutcome) result.toOperationOutcome();
ourLog.info(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
assertEquals("Unknown code 'urn:iso:std:iso:4217#blah' (for 'urn:iso:std:iso:4217#blah')", result.getMessages().get(1).getMessage());
assertEquals(ResultSeverityEnum.ERROR, result.getMessages().get(1).getSeverity());
assertEquals(22, result.getMessages().get(1).getLocationLine());
assertEquals(4, result.getMessages().get(1).getLocationCol());
assertEquals("Unknown code 'urn:iso:std:iso:4217#blah' (for 'urn:iso:std:iso:4217#blah')", result.getMessages().get(0).getMessage());
assertEquals(ResultSeverityEnum.ERROR, result.getMessages().get(0).getSeverity());
assertEquals(22, result.getMessages().get(0).getLocationLine());
assertEquals(4, result.getMessages().get(0).getLocationCol());
assertEquals("Observation.value.ofType(Quantity)", result.getMessages().get(0).getLocationString());
assertEquals("Terminology_PassThrough_TX_Message", result.getMessages().get(0).getMessageId());