Search in sources :

Example 1 with ConstraintViolationException

use of org.ehrbase.validation.ConstraintViolationException 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 ConstraintViolationException

use of org.ehrbase.validation.ConstraintViolationException 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 ConstraintViolationException

use of org.ehrbase.validation.ConstraintViolationException 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 4 with ConstraintViolationException

use of org.ehrbase.validation.ConstraintViolationException 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)

Example 5 with ConstraintViolationException

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

the class DvCodedTextTest method testValidate_FhirValueSet_WrongCode.

@Test
void testValidate_FhirValueSet_WrongCode() throws Exception {
    var codePhrase = new CodePhrase(new TerminologyId("http://terminology.hl7.org/CodeSystem/v3-EntityNameUseR2"), "UKN");
    // 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 value UKN does not match any option from value set http://terminology.hl7.org/ValueSet/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

ConstraintViolationException (org.ehrbase.validation.ConstraintViolationException)7 ConstraintViolation (org.ehrbase.validation.ConstraintViolation)6 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)4 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)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 Map (java.util.Map)1 UnprocessableEntityException (org.ehrbase.api.exception.UnprocessableEntityException)1 ItemStructureVisitor (org.ehrbase.validation.terminology.ItemStructureVisitor)1 WebTemplate (org.ehrbase.webtemplate.model.WebTemplate)1