Search in sources :

Example 1 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method validateAnswerCode.

private void validateAnswerCode(List<ValidationMessage> errors, Element value, NodeStack stack, QuestionnaireWithContext qSrc, String ref, boolean theOpenChoice) {
    ValueSet vs = null;
    if (ref.startsWith("#") && qSrc.container != null) {
        vs = (ValueSet) loadContainedResource(errors, qSrc.containerPath, qSrc.container, ref.substring(1), ValueSet.class);
    } else {
        vs = resolveBindingReference(qSrc.q(), ref, qSrc.q().getUrl());
    }
    if (warning(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), vs != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(ref))) {
        try {
            Coding c = ObjectConverter.readAsCoding(value);
            if (isBlank(c.getCode()) && isBlank(c.getSystem()) && isNotBlank(c.getDisplay())) {
                if (theOpenChoice) {
                    return;
                }
            }
            long t = System.nanoTime();
            ValidationContextCarrier vc = makeValidationContext(errors, qSrc);
            ValidationResult res = context.validateCode(new ValidationOptions(stack.getWorkingLang()), c, vs, vc);
            timeTracker.tx(t, "vc " + c.getSystem() + "#" + c.getCode() + " '" + c.getDisplay() + "'");
            if (!res.isOk()) {
                txRule(errors, res.getTxLink(), IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_BADOPTION, c.getSystem(), c.getCode());
            } else if (res.getSeverity() != null) {
                super.addValidationMessage(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), res.getMessage(), res.getSeverity(), Source.TerminologyEngine, null);
            }
        } catch (Exception e) {
            warning(errors, IssueType.CODEINVALID, value.line(), value.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_CODING, e.getMessage());
        }
    }
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationContextCarrier(org.hl7.fhir.r5.utils.validation.ValidationContextCarrier) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) ValueSet(org.hl7.fhir.r5.model.ValueSet) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier 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 3 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier 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 4 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContextTests method testValidateCodingWithValueSetChecker.

@Test
public void testValidateCodingWithValueSetChecker() 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(valueSetCheckerSimple).when(context).constructValueSetCheckerSimple(any(), any(), any());
    Mockito.doReturn(expectedValidationResult).when(valueSetCheckerSimple).validateCode(any(Coding.class));
    ValidationContextCarrier ctxt = mock(ValidationContextCarrier.class);
    IWorkerContext.ValidationResult actualValidationResult = context.validateCode(validationOptions, coding, valueSet, ctxt);
    assertEquals(expectedValidationResult, actualValidationResult);
    Mockito.verify(valueSetCheckerSimple).validateCode(coding);
    Mockito.verify(terminologyCache).getValidation(cacheToken);
    Mockito.verify(terminologyCache).cacheValidation(cacheToken, expectedValidationResult, false);
}
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 5 with ValidationContextCarrier

use of org.hl7.fhir.r5.utils.validation.ValidationContextCarrier in project org.hl7.fhir.core by hapifhir.

the class BaseWorkerContext method validateCode.

@Override
public ValidationResult validateCode(ValidationOptions options, Coding code, ValueSet vs, ValidationContextCarrier ctxt) {
    if (options == null) {
        options = ValidationOptions.defaults();
    }
    if (code.hasSystem()) {
        codeSystemsUsed.add(code.getSystem());
    }
    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;
    }
    if (options.isUseClient()) {
        // ok, first we try to validate locally
        try {
            ValueSetCheckerSimple vsc = new ValueSetCheckerSimple(options, vs, this, ctxt);
            if (!vsc.isServerSide(code.getSystem())) {
                res = vsc.validateCode(code);
                if (txCache != null) {
                    txCache.cacheValidation(cacheToken, res, TerminologyCache.TRANSIENT);
                }
                return res;
            }
        } catch (Exception e) {
        }
    }
    String codeKey = code.hasVersion() ? code.getSystem() + "|" + code.getVersion() : code.getSystem();
    if (!options.isUseServer()) {
        return new ValidationResult(IssueSeverity.WARNING, formatMessage(I18nConstants.UNABLE_TO_VALIDATE_CODE_WITHOUT_USING_SERVER), TerminologyServiceErrorClass.BLOCKED_BY_OPTIONS);
    }
    if (unsupportedCodeSystems.contains(codeKey)) {
        return new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.TERMINOLOGY_TX_SYSTEM_NOTKNOWN, code.getSystem()), TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED);
    }
    // if that failed, we try to validate on the server
    if (noTerminologyServer) {
        return new ValidationResult(IssueSeverity.ERROR, formatMessage(I18nConstants.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 (options.isGuessSystem()) {
            pIn.addParameter().setName("implySystem").setValue(new BooleanType(true));
        }
        setTerminologyOptions(options, pIn);
        res = validateOnServer(vs, pIn, options);
    } catch (Exception e) {
        res = new ValidationResult(IssueSeverity.ERROR, e.getMessage() == null ? e.getClass().getName() : e.getMessage()).setTxLink(txLog == null ? null : txLog.getLastId()).setErrorClass(TerminologyServiceErrorClass.SERVER_ERROR);
    }
    if (res.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED && !code.hasVersion()) {
        unsupportedCodeSystems.add(codeKey);
    } else if (txCache != null) {
        // we never cache unsuppoted code systems - we always keep trying (but only once per run)
        txCache.cacheValidation(cacheToken, res, TerminologyCache.PERMANENT);
    }
    return res;
}
Also used : Parameters(org.hl7.fhir.r4b.model.Parameters) CacheToken(org.hl7.fhir.r4b.context.TerminologyCache.CacheToken) BooleanType(org.hl7.fhir.r4b.model.BooleanType) ValueSetCheckerSimple(org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) FileNotFoundException(java.io.FileNotFoundException) NoTerminologyServiceException(org.hl7.fhir.exceptions.NoTerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

ValidationOptions (org.hl7.fhir.utilities.validation.ValidationOptions)5 Coding (org.hl7.fhir.r5.model.Coding)4 ValueSet (org.hl7.fhir.r5.model.ValueSet)4 ValidationContextCarrier (org.hl7.fhir.r5.utils.validation.ValidationContextCarrier)4 IOException (java.io.IOException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 Test (org.junit.jupiter.api.Test)3 FileNotFoundException (java.io.FileNotFoundException)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 NoTerminologyServiceException (org.hl7.fhir.exceptions.NoTerminologyServiceException)2 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)2 CacheToken (org.hl7.fhir.r4b.context.TerminologyCache.CacheToken)1 BooleanType (org.hl7.fhir.r4b.model.BooleanType)1 Parameters (org.hl7.fhir.r4b.model.Parameters)1 ValueSetCheckerSimple (org.hl7.fhir.r4b.terminologies.ValueSetCheckerSimple)1 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)1 CacheToken (org.hl7.fhir.r5.context.TerminologyCache.CacheToken)1 Parameters (org.hl7.fhir.r5.model.Parameters)1 ValueSetCheckerSimple (org.hl7.fhir.r5.terminologies.ValueSetCheckerSimple)1