Search in sources :

Example 6 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method doValidateCode.

public ValidationResult doValidateCode(ValidationOptions options, Coding code, ValueSet vs, boolean implySystem) {
    CacheToken cacheToken = txCache != null ? txCache.generateValidationToken(options, code, vs) : null;
    ValidationResult res = null;
    if (txCache != null)
        res = txCache.getValidation(cacheToken);
    if (res != null)
        return res;
    // ok, first we try to validate locally
    try {
        ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this);
        res = vsc.validateCode(code);
        if (txCache != null)
            txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
        return res;
    } catch (Exception e) {
    }
    // if that failed, we try to validate on the server
    if (noTerminologyServer)
        return new ValidationResult(IssueSeverity.ERROR, "Error validating code: running without terminology services", TerminologyServiceErrorClass.NOSERVICE);
    String csumm = txCache != null ? txCache.summary(code) : null;
    if (txCache != null)
        tlog("$validate " + csumm + " for " + txCache.summary(vs));
    else
        tlog("$validate " + csumm + " before cache exists");
    try {
        Parameters pIn = new Parameters();
        pIn.addParameter().setName("coding").setValue(code);
        if (implySystem)
            pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
        if (options != null)
            setTerminologyOptions(options, pIn);
        res = validateOnServer(vs, pIn);
    } catch (Exception e) {
        res = new ValidationResult(IssueSeverity.ERROR, e.getMessage() == null ? e.getClass().getName() : e.getMessage()).setTxLink(txLog == null ? null : txLog.getLastId());
    }
    if (txCache != null)
        txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    return res;
}
Also used : CacheToken(org.hl7.fhir.r4.context.TerminologyCache.CacheToken) ValueSetCheckerSimple(org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 7 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class ValueSetCheckerSimple method getVs.

private ValueSetCheckerSimple getVs(String url) {
    if (inner.containsKey(url)) {
        return inner.get(url);
    }
    ValueSet vs = context.fetchResource(ValueSet.class, url);
    ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, context, localContext);
    inner.put(url, vsc);
    return vsc;
}
Also used : ValueSet(org.hl7.fhir.r4b.model.ValueSet)

Example 8 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContextTests method testValidateCodingWithServer.

@Test
public void testValidateCodingWithServer() throws IOException {
    ValidationOptions validationOptions = new ValidationOptions().guessSystem().setVersionFlexible(false).noClient();
    ValueSet valueSet = new ValueSet();
    Coding coding = new Coding();
    Mockito.doReturn(cacheToken).when(terminologyCache).generateValidationToken(validationOptions, coding, valueSet);
    Mockito.doReturn(pIn).when(context).constructParameters(validationOptions, coding);
    Mockito.doReturn(expectedValidationResult).when(context).validateOnServer(valueSet, pIn, validationOptions);
    ValidationContextCarrier ctxt = mock(ValidationContextCarrier.class);
    IWorkerContext.ValidationResult actualValidationResult = context.validateCode(validationOptions, coding, valueSet, ctxt);
    assertEquals(expectedValidationResult, actualValidationResult);
    Mockito.verify(valueSetCheckerSimple, times(0)).validateCode(coding);
    Mockito.verify(terminologyCache).getValidation(cacheToken);
    Mockito.verify(terminologyCache).cacheValidation(cacheToken, expectedValidationResult, true);
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationContextCarrier(org.hl7.fhir.r5.utils.validation.ValidationContextCarrier) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ValueSet(org.hl7.fhir.r5.model.ValueSet) Test(org.junit.jupiter.api.Test)

Example 9 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContextTests method testValidateCodingWithCache.

@Test
public void testValidateCodingWithCache() throws IOException {
    ValidationOptions validationOptions = new ValidationOptions().guessSystem().setVersionFlexible(false);
    ValueSet valueSet = new ValueSet();
    Coding coding = new Coding();
    Mockito.doReturn(cacheToken).when(terminologyCache).generateValidationToken(validationOptions, coding, valueSet);
    Mockito.doReturn(expectedValidationResult).when(terminologyCache).getValidation(cacheToken);
    ValidationContextCarrier ctxt = mock(ValidationContextCarrier.class);
    IWorkerContext.ValidationResult actualValidationResult = context.validateCode(validationOptions, coding, valueSet, ctxt);
    assertEquals(expectedValidationResult, actualValidationResult);
    Mockito.verify(valueSetCheckerSimple, times(0)).validateCode(coding);
    Mockito.verify(terminologyCache).getValidation(cacheToken);
    Mockito.verify(terminologyCache, times(0)).cacheValidation(any(), any(), anyBoolean());
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationContextCarrier(org.hl7.fhir.r5.utils.validation.ValidationContextCarrier) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ValueSet(org.hl7.fhir.r5.model.ValueSet) Test(org.junit.jupiter.api.Test)

Example 10 with ValueSetCheckerSimple

use of org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContextTests method testValidateCodableConceptWithServer.

@Test
public void testValidateCodableConceptWithServer() throws IOException {
    CodeableConcept codeableConcept = new CodeableConcept();
    ValueSet valueSet = new ValueSet();
    ValidationOptions validationOptions = CacheTestUtils.validationOptions.noClient();
    Mockito.doReturn(pIn).when(context).constructParameters(validationOptions, codeableConcept);
    Mockito.doReturn(expectedValidationResult).when(context).validateOnServer(valueSet, pIn, validationOptions);
    Mockito.doReturn(cacheToken).when(terminologyCache).generateValidationToken(validationOptions, codeableConcept, valueSet);
    IWorkerContext.ValidationResult validationResultB = context.validateCode(validationOptions, codeableConcept, valueSet);
    assertEquals(expectedValidationResult, validationResultB);
    Mockito.verify(valueSetCheckerSimple, times(0)).validateCode(codeableConcept);
    Mockito.verify(terminologyCache).cacheValidation(cacheToken, expectedValidationResult, true);
    Mockito.verify(context).validateOnServer(valueSet, pIn, validationOptions);
}
Also used : ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeableConcept(org.hl7.fhir.r5.model.CodeableConcept) Test(org.junit.jupiter.api.Test)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)10 IOException (java.io.IOException)10 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)10 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)6 Test (org.junit.jupiter.api.Test)6 ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)5 Coding (org.hl7.fhir.r5.model.Coding)4 Parameters (org.hl7.fhir.r4b.model.Parameters)3 ValueSetCheckerSimple (org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple)3 CodeableConcept (org.hl7.fhir.r5.model.CodeableConcept)3 Parameters (org.hl7.fhir.r5.model.Parameters)3 ValueSetCheckerSimple (org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple)3 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)3 HashSet (java.util.HashSet)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 CacheToken (org.hl7.fhir.r4.context.TerminologyCache.CacheToken)2 ValueSetCheckerSimple (org.hl7.fhir.r4.terminologies.ValueSetCheckerSimple)2