Search in sources :

Example 1 with ConstraintViolation

use of org.ehrbase.validation.ConstraintViolation in project openEHR_SDK by ehrbase.

the class FhirTerminologyValidation method validateCode.

/**
 * Validates that the code comes from the CodeSystem using <code>validate-code</code> method.
 *
 * @param url        the URL of the CodeSystem
 * @param codePhrase the concept code
 */
private void validateCode(String path, String url, CodePhrase codePhrase) {
    if (!StringUtils.equals(url, codePhrase.getTerminologyId().getValue())) {
        var constraintViolation = new ConstraintViolation(path, MessageFormat.format("The terminology {0} must be {1}", codePhrase.getTerminologyId().getValue(), url));
        throw new ConstraintViolationException(List.of(constraintViolation));
    }
    DocumentContext context;
    try {
        context = internalGet(baseUrl + "/CodeSystem/$validate-code?url=" + url + "&code=" + codePhrase.getCodeString());
    } catch (IOException e) {
        if (failOnError) {
            throw new ExternalTerminologyValidationException("An error occurred while validating the code in CodeSystem", e);
        }
        LOG.warn("An error occurred while validating the code in CodeSystem: {}", e.getMessage());
        return;
    }
    boolean result = context.read("$.parameter[0].valueBoolean", boolean.class);
    if (!result) {
        var message = context.read("$.parameter[1].valueString", String.class);
        var constraintViolation = new ConstraintViolation(path, message);
        throw new ConstraintViolationException(List.of(constraintViolation));
    }
}
Also used : ConstraintViolation(org.ehrbase.validation.ConstraintViolation) ConstraintViolationException(org.ehrbase.validation.ConstraintViolationException) IOException(java.io.IOException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 2 with ConstraintViolation

use of org.ehrbase.validation.ConstraintViolation in project openEHR_SDK by ehrbase.

the class DvCodedTextTest method testValidate_FhirCodeSystem_WrongCode.

@Test
void testValidate_FhirCodeSystem_WrongCode() throws Exception {
    var codePhrase = new CodePhrase(new TerminologyId("http://hl7.org/fhir/observation-status"), "casual");
    // Mockito initialization
    Mockito.when(fhirTerminologyValidationMock.supports("//fhir.hl7.org/CodeSystem?url=http://hl7.org/fhir/observation-status")).thenReturn(true);
    Mockito.doThrow(new ConstraintViolationException(List.of(new ConstraintViolation("/test/dv_coded_text_fhir_code_system", "The specified code 'casual' is not known to belong to the specified code system 'http://hl7.org/fhir/observation-status'")))).when(fhirTerminologyValidationMock).validate("/test/dv_coded_text_fhir_code_system", "//fhir.hl7.org/CodeSystem?url=http://hl7.org/fhir/observation-status", codePhrase);
    var validator = new DvCodedTextValidator(fhirTerminologyValidationMock);
    var node = parseNode("/webtemplate_nodes/dv_codedtext_fhir_codesystem.json");
    var result = validator.validate(new DvCodedText("Casual", codePhrase), node);
    assertEquals(1, result.size());
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) ConstraintViolation(org.ehrbase.validation.ConstraintViolation) ConstraintViolationException(org.ehrbase.validation.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Example 3 with ConstraintViolation

use of org.ehrbase.validation.ConstraintViolation in project openEHR_SDK by ehrbase.

the class DefaultValidator method validate.

private List<ConstraintViolation> validate(Locatable locatable, WebTemplateNode node) {
    List<ConstraintViolation> result = new ArrayList<>();
    node.getChildren().forEach(childNode -> {
        var count = 0;
        for (var item : locatable.itemsAtPath(node.buildRelativePath(childNode))) {
            if (item instanceof Locatable) {
                if (!node.isRelativePathNameDependent(childNode) || Objects.equals(((Locatable) item).getNameAsString(), childNode.getName())) {
                    count++;
                }
            } else {
                count++;
            }
        }
        var interval = getMultiplicityInterval(childNode, node);
        if (!interval.has(count)) {
            String message = RMObjectValidationMessageIds.rm_OCCURRENCE_MISMATCH.getMessage(count, interval.toString());
            result.add(new ConstraintViolation(childNode.getAqlPath(), message));
        }
    });
    return result;
}
Also used : ConstraintViolation(org.ehrbase.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Locatable(com.nedap.archie.rm.archetyped.Locatable)

Example 4 with ConstraintViolation

use of org.ehrbase.validation.ConstraintViolation in project openEHR_SDK by ehrbase.

the class FhirTerminologyValidation method expandValueSet.

/**
 * Validates that the code comes from the ValueSet using <code>$expand</code> method.
 *
 * @param url        the URL of the ValueSet
 * @param codePhrase the concept code
 */
private void expandValueSet(String path, String url, CodePhrase codePhrase) {
    DocumentContext context;
    try {
        context = internalGet(baseUrl + "/ValueSet/$expand?url=" + url);
    } catch (IOException e) {
        if (failOnError) {
            throw new ExternalTerminologyValidationException("An error occurred while expanding the ValueSet", e);
        }
        LOG.warn("An error occurred while expanding the ValueSet: {}", e.getMessage());
        return;
    }
    List<Map<String, String>> codings = context.read("$.expansion.contains[?(@.code=='" + codePhrase.getCodeString() + "')]");
    if (codings.isEmpty()) {
        var constraintViolation = new ConstraintViolation(path, MessageFormat.format("The value {0} does not match any option from value set {1}", codePhrase.getCodeString(), url));
        throw new ConstraintViolationException(List.of(constraintViolation));
    } else if (codings.size() == 1) {
        Map<String, String> coding = codings.get(0);
        if (!StringUtils.equals(coding.get("system"), codePhrase.getTerminologyId().getValue())) {
            var constraintViolation = new ConstraintViolation(path, MessageFormat.format("The terminology {0} must be  {1}", codePhrase.getCodeString(), url));
            throw new ConstraintViolationException(List.of(constraintViolation));
        }
    }
}
Also used : ConstraintViolation(org.ehrbase.validation.ConstraintViolation) ConstraintViolationException(org.ehrbase.validation.ConstraintViolationException) IOException(java.io.IOException) DocumentContext(com.jayway.jsonpath.DocumentContext) Map(java.util.Map)

Example 5 with ConstraintViolation

use of org.ehrbase.validation.ConstraintViolation in project openEHR_SDK by ehrbase.

the class DvCodedTextTest method testValidate_FhirValueSet_WrongTerminologyId.

@Test
void testValidate_FhirValueSet_WrongTerminologyId() throws Exception {
    var codePhrase = new CodePhrase(new TerminologyId("http://snomed.info/sct"), "ANON");
    // Mockito initialization
    Mockito.when(fhirTerminologyValidationMock.supports("//fhir.hl7.org/ValueSet/$expand?url=http://terminology.hl7.org/ValueSet/v3-EntityNameUseR2")).thenReturn(true);
    Mockito.doThrow(new ConstraintViolationException(List.of(new ConstraintViolation("/test/dv_coded_text_fhir_value_set", "The terminology http://snomed.info/sct must be http://terminology.hl7.org/CodeSystem/v3-EntityNameUseR2")))).when(fhirTerminologyValidationMock).validate("/test/dv_coded_text_fhir_value_set", "//fhir.hl7.org/ValueSet/$expand?url=http://terminology.hl7.org/ValueSet/v3-EntityNameUseR2", codePhrase);
    var validator = new DvCodedTextValidator(fhirTerminologyValidationMock);
    var node = parseNode("/webtemplate_nodes/dv_codedtext_fhir_valueset.json");
    var result = validator.validate(new DvCodedText("Anonymous", codePhrase), node);
    assertEquals(1, result.size());
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) ConstraintViolation(org.ehrbase.validation.ConstraintViolation) ConstraintViolationException(org.ehrbase.validation.ConstraintViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

ConstraintViolation (org.ehrbase.validation.ConstraintViolation)9 ConstraintViolationException (org.ehrbase.validation.ConstraintViolationException)7 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)5 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)4 TerminologyId (com.nedap.archie.rm.support.identification.TerminologyId)4 Test (org.junit.jupiter.api.Test)4 DocumentContext (com.jayway.jsonpath.DocumentContext)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Locatable (com.nedap.archie.rm.archetyped.Locatable)1 MessageFormat (java.text.MessageFormat)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ExternalTerminologyValidation (org.ehrbase.validation.terminology.ExternalTerminologyValidation)1 WebTemplateInput (org.ehrbase.webtemplate.model.WebTemplateInput)1 WebTemplateNode (org.ehrbase.webtemplate.model.WebTemplateNode)1