Search in sources :

Example 16 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method textTypeNarrowing2.

/**
 * check that narrowing types is working
 * this one renames the path
 *
 * @param context2
 * @
 * @throws EOperationOutcome
 */
private void textTypeNarrowing2() throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBase(base.getUrl());
    ElementDefinition id = focus.getDifferential().addElement();
    id.setPath("Patient.deceasedDateTime");
    id.addType().setCode("dateTime");
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.deceasedDateTime")) {
                    ok = f.getType().size() == 1 && f.getType().get(0).getCode().equals("dateTime");
                    if (ok) {
                        f.getType().clear();
                        b.getType().clear();
                        f.setPath(b.getPath());
                    }
                }
                ok = ok && Base.compareDeep(b, f, true);
            }
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation narrow type 2 failed");
    } else
        System.out.println("Snap shot generation narrow type 2 test passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu2.utils.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 17 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testTypeWalk.

/**
 * Walking into a type
 *
 * @param context2
 * @
 * @throws EOperationOutcome
 */
private void testTypeWalk() throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBase(base.getUrl());
    ElementDefinition id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier");
    id.setMustSupport(true);
    id = focus.getDifferential().addElement();
    id.setPath("Patient.identifier.system");
    id.setMustSupport(true);
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    // the derived should be 8 longer
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 8;
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i <= 9 ? i : i + 8);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.identifier")) {
                    ok = f.getMustSupport() && !b.getMustSupport();
                    if (ok) {
                        f.setMustSupportElement(null);
                    }
                }
                ok = Base.compareDeep(b, f, true);
            }
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation simple test failed");
    } else
        System.out.println("Snap shot generation simple test passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu2.utils.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 18 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testSlicingExtensionComplex.

/**
 * we're going to slice Patient.extension and refer to extension by profile. one of the extensions is complex, and we're going to walk into
 * it and make it must support
 *
 * implicit: whether to rely on implicit extension slicing
 */
private void testSlicingExtensionComplex(boolean implicit) throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Patient").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBase(base.getUrl());
    // set the slice up
    ElementDefinition id;
    if (!implicit) {
        id = focus.getDifferential().addElement();
        id.setPath("Patient.extension");
        id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator("url");
    }
    // first slice  - a simple one to get us going:
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.setName("simple");
    id.addType().setCode("Extension").addProfile("http://hl7.org/fhir/StructureDefinition/patient-birthTime");
    // second slice - the complex one
    // we walk into this and fix properties on the inner extensions
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.setName("complex");
    id.addType().setCode("Extension").addProfile("http://hl7.org/fhir/StructureDefinition/patient-nationality");
    if (!implicit) {
        id = focus.getDifferential().addElement();
        id.setPath("Patient.extension.extension");
        id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator("url");
    }
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension.extension");
    id.setName("code");
    id.setMustSupport(true);
    id.addType().setCode("Extension").addProfile("http://hl7.org/fhir/StructureDefinition/patient-nationality#code");
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension.extension");
    id.setName("period");
    id.addType().setCode("Extension").addProfile("http://hl7.org/fhir/StructureDefinition/patient-nationality#period");
    // prohibit this one....
    id.setMax("0");
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    // ok, there's going to 1 (simple) + complex: 1 + id + extnesion.slice + extension.code + (4 inside from that) + extension.period + (4 inside from that) + value + url = 16
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 16;
    // custom checks
    ok = ok && rule(focus.getSnapshot().getElement().get(7).getPath().equals("Patient.extension"), "element 7 (base) path");
    ok = ok && rule(focus.getSnapshot().getElement().get(7).hasSlicing(), "element 7 slicing");
    ok = ok && rule(focus.getSnapshot().getElement().get(8).getPath().equals("Patient.extension"), "element 8 (1st slice) path");
    ok = ok && rule(focus.getSnapshot().getElement().get(8).getName().equals("simple"), "element 8 (1st slice) name");
    ok = ok && rule(focus.getSnapshot().getElement().get(8).getType().get(0).getProfile().get(0).getValue().equals("http://hl7.org/fhir/StructureDefinition/patient-birthTime"), "element 9 (2nd slice) profile name");
    ok = ok && rule(focus.getSnapshot().getElement().get(9).getPath().equals("Patient.extension"), "element 9 (2nd slice) path");
    ok = ok && rule(focus.getSnapshot().getElement().get(9).getName().equals("complex"), "element 8 (1st slice) name");
    ok = ok && rule(focus.getSnapshot().getElement().get(9).getType().get(0).getProfile().get(0).getValue().equals("http://hl7.org/fhir/StructureDefinition/patient-nationality"), "element 9 (2nd slice) profile name");
    ok = ok && rule(focus.getSnapshot().getElement().get(10).getPath().equals("Patient.extension.id"), "element 10 (2nd slice).id path");
    ok = ok && rule(focus.getSnapshot().getElement().get(11).getPath().equals("Patient.extension.extension"), "element 11 (2nd slice).extension path");
    ok = ok && rule(focus.getSnapshot().getElement().get(12).getPath().equals("Patient.extension.extension"), "element 12 (2nd slice).extension path");
    ok = ok && rule(focus.getSnapshot().getElement().get(12).getMustSupport(), "element 12 (2nd slice).extension must support");
    ok = ok && rule(focus.getSnapshot().getElement().get(13).getPath().equals("Patient.extension.extension.id"), "element 13 (2nd slice).extension.id path");
    ok = ok && rule(focus.getSnapshot().getElement().get(14).getPath().equals("Patient.extension.extension.extension"), "element 14 (2nd slice).extension.extension path");
    ok = ok && rule(focus.getSnapshot().getElement().get(15).getPath().equals("Patient.extension.extension.url"), "element 15 (2nd slice).extension.url path");
    ok = ok && rule(focus.getSnapshot().getElement().get(16).getPath().equals("Patient.extension.extension.valueCodeableConcept"), "element 16 (2nd slice).extension.valueCodeableConcept path");
    ok = ok && rule(focus.getSnapshot().getElement().get(17).getPath().equals("Patient.extension.extension"), "element 17 (2nd slice).extension path");
    ok = ok && rule(focus.getSnapshot().getElement().get(17).getMax().equals("0"), "element 17 (2nd slice).extension cardinality");
    ok = ok && rule(focus.getSnapshot().getElement().get(18).getPath().equals("Patient.extension.extension.id"), "element 18 (2nd slice).extension.id path");
    ok = ok && rule(focus.getSnapshot().getElement().get(19).getPath().equals("Patient.extension.extension.extension"), "element 19 (2nd slice).extension.extension path");
    ok = ok && rule(focus.getSnapshot().getElement().get(20).getPath().equals("Patient.extension.extension.url"), "element 20 (2nd slice).extension.url path");
    ok = ok && rule(focus.getSnapshot().getElement().get(21).getPath().equals("Patient.extension.extension.valuePeriod"), "element 21 (2nd slice).extension.valuePeriod path");
    ok = ok && rule(focus.getSnapshot().getElement().get(22).getPath().equals("Patient.extension.url"), "element 22 (2nd slice).url path");
    ok = ok && rule(focus.getSnapshot().getElement().get(23).getPath().equals("Patient.extension.value[x]"), "element 23 (2nd slice).url path");
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i <= 7 ? i : i + 16);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                if (f.getPath().equals("Patient.extension")) {
                    ok = f.hasSlicing();
                    if (ok)
                        f.setSlicing(null);
                }
                if (// no compare that because the definitions get overwritten
                !f.getPath().equals("Patient.extension"))
                    ok = Base.compareDeep(b, f, true);
            }
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation slicing extensions complex (" + (implicit ? "implicit" : "not implicit") + ") failed");
    } else
        System.out.println("Snap shot generation slicing extensions complex (" + (implicit ? "implicit" : "not implicit") + ") passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu2.utils.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 19 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method testSimple2.

/**
 * This is simple: we just create an empty differential, generate the snapshot, and then insist it must match the base. for a different resource with recursion
 *
 * @param context2
 * @
 * @throws EOperationOutcome
 */
private void testSimple2() throws EOperationOutcome, Exception {
    StructureDefinition focus = new StructureDefinition();
    StructureDefinition base = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/ValueSet").copy();
    focus.setUrl(Utilities.makeUuidUrn());
    focus.setBase(base.getUrl());
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test");
    boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
    for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
        if (ok) {
            ElementDefinition b = base.getSnapshot().getElement().get(i);
            ElementDefinition f = focus.getSnapshot().getElement().get(i);
            if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath()))
                ok = false;
            else {
                f.setBase(null);
                ok = Base.compareDeep(b, f, true);
            }
        }
    }
    if (!ok) {
        compareXml(base, focus);
        throw new FHIRException("Snap shot generation simple test failed");
    } else
        System.out.println("Snap shot generation simple test passed");
}
Also used : StructureDefinition(org.hl7.fhir.dstu2.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu2.utils.ProfileUtilities) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.dstu2.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 20 with ProfileUtilities

use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method seeProfile.

public void seeProfile(String url, StructureDefinition p) throws FHIRException {
    if (Utilities.noString(url))
        url = p.getUrl();
    if (!p.hasSnapshot() && p.getKind() != StructureDefinitionKind.LOGICAL) {
        if (!p.hasBaseDefinition())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") has no base and no snapshot");
        StructureDefinition sd = fetchResource(StructureDefinition.class, p.getBaseDefinition());
        if (sd == null)
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + ") base " + p.getBaseDefinition() + " could not be resolved");
        List<ValidationMessage> msgs = new ArrayList<ValidationMessage>();
        List<String> errors = new ArrayList<String>();
        ProfileUtilities pu = new ProfileUtilities(this, msgs, this);
        pu.sortDifferential(sd, p, url, errors);
        for (String err : errors) msgs.add(new ValidationMessage(Source.ProfileValidator, IssueType.EXCEPTION, p.getUserString("path"), "Error sorting Differential: " + err, ValidationMessage.IssueSeverity.ERROR));
        pu.generateSnapshot(sd, p, p.getUrl(), p.getName());
        for (ValidationMessage msg : msgs) {
            if (msg.getLevel() == ValidationMessage.IssueSeverity.ERROR || msg.getLevel() == ValidationMessage.IssueSeverity.FATAL)
                throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot: " + msg.getMessage());
        }
        if (!p.hasSnapshot())
            throw new DefinitionException("Profile " + p.getName() + " (" + p.getUrl() + "). Error generating snapshot");
        pu = null;
    }
    if (structures.containsKey(p.getUrl()) && !allowLoadingDuplicates)
        throw new DefinitionException("Duplicate structures " + p.getUrl());
    structures.put(p.getId(), p);
    structures.put(p.getUrl(), p);
    if (!p.getUrl().equals(url))
        structures.put(url, p);
}
Also used : StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) ArrayList(java.util.ArrayList) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Aggregations

ArrayList (java.util.ArrayList)74 FHIRException (org.hl7.fhir.exceptions.FHIRException)59 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)52 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)48 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 ProfileUtilities (org.hl7.fhir.dstu3.conformance.ProfileUtilities)21 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)20 StructureDefinition (org.hl7.fhir.dstu2.model.StructureDefinition)16 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)16 FileOutputStream (java.io.FileOutputStream)15 ProfileUtilities (org.hl7.fhir.r4b.conformance.ProfileUtilities)14 IOException (java.io.IOException)13 ElementDefinition (org.hl7.fhir.dstu2.model.ElementDefinition)13 ProfileUtilities (org.hl7.fhir.dstu2.utils.ProfileUtilities)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)13 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)13 ProfileUtilities (org.hl7.fhir.r4.conformance.ProfileUtilities)12 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)12 Test (org.junit.jupiter.api.Test)12 FileNotFoundException (java.io.FileNotFoundException)11