Java Code Examples for org.hl7.fhir.dstu3.model.Base#compareDeep()

The following examples show how to use org.hl7.fhir.dstu3.model.Base#compareDeep() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileComparer.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private boolean ruleCompares(ElementDefinition ed, Type vLeft, Type vRight, String path, int nullStatus) throws IOException {
  if (vLeft == null && vRight == null && nullStatus == BOTH_NULL)
    return true;
  if (vLeft == null && vRight == null) {
    messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Must be the same and not null (null/null)", ValidationMessage.IssueSeverity.ERROR));
    status(ed, ProfileUtilities.STATUS_ERROR);
  }
  if (vLeft == null && nullStatus == EITHER_NULL)
    return true;
  if (vRight == null && nullStatus == EITHER_NULL)
    return true;
  if (vLeft == null || vRight == null || !Base.compareDeep(vLeft, vRight, false)) {
    messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Must be the same ("+toString(vLeft)+"/"+toString(vRight)+")", ValidationMessage.IssueSeverity.ERROR));
    status(ed, ProfileUtilities.STATUS_ERROR);
  }
  return true;
}
 
Example 2
Source File: ProfileComparer.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private ElementDefinitionBindingComponent unionBindings(ElementDefinition ed, ProfileComparison outcome, String path, ElementDefinitionBindingComponent left, ElementDefinitionBindingComponent right) throws FHIRFormatError {
  ElementDefinitionBindingComponent union = new ElementDefinitionBindingComponent();
  if (left.getStrength().compareTo(right.getStrength()) < 0)
    union.setStrength(left.getStrength());
  else
    union.setStrength(right.getStrength());
  union.setDescription(mergeText(ed, outcome, path, "binding.description", left.getDescription(), right.getDescription()));
  if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false))
    union.setValueSet(left.getValueSet());
  else {
    ValueSet lvs = resolveVS(outcome.left, left.getValueSet());
    ValueSet rvs = resolveVS(outcome.left, right.getValueSet());
    if (lvs != null && rvs != null)
      union.setValueSet(new Reference().setReference("#"+addValueSet(unite(ed, outcome, path, lvs, rvs))));
    else if (lvs != null)
      union.setValueSet(new Reference().setReference("#"+addValueSet(lvs)));
    else if (rvs != null)
      union.setValueSet(new Reference().setReference("#"+addValueSet(rvs)));
  }
  return union;
}
 
Example 3
Source File: Element.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean equalsDeep(org.hl7.fhir.dstu3.model.Property p, org.hl7.fhir.dstu3.model.Property o) {
  if (o == null || p == null)
    return false;
  if (p.getValues().size() != o.getValues().size())
    return false;
  for (int i = 0; i < p.getValues().size(); i++)
    if (!Base.compareDeep(p.getValues().get(i), o.getValues().get(i), true))
      return false;
  return true;
}
 
Example 4
Source File: ProfileComparer.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean mergeIntoExisting(List<ConceptSetComponent> include, ConceptSetComponent inc) {
  for (ConceptSetComponent dst : include) {
    if (Base.compareDeep(dst,  inc, false))
      return true; // they're actually the same
    if (dst.getSystem().equals(inc.getSystem())) {
      if (inc.hasFilter() || dst.hasFilter()) {
        return false; // just add the new one as a a parallel
      } else if (inc.hasConcept() && dst.hasConcept()) {
        for (ConceptReferenceComponent cc : inc.getConcept()) {
          boolean found = false;
          for (ConceptReferenceComponent dd : dst.getConcept()) {
            if (dd.getCode().equals(cc.getCode()))
              found = true;
            if (found) {
              if (cc.hasDisplay() && !dd.hasDisplay())
                dd.setDisplay(cc.getDisplay());
              break;
            }
          }
          if (!found)
            dst.getConcept().add(cc.copy());
        }
      } else
        dst.getConcept().clear(); // one of them includes the entire code system 
    }
  }
  return false;
}
 
Example 5
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * This is simple: we just create an empty differential, generate the snapshot, and then insist it must match the base 
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testSimple() 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.setBaseDefinition(base.getUrl());
  focus.setType("Patient");
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  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");
}
 
Example 6
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  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");
}
 
Example 7
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * Change one cardinality.
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testCardinalityChange() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.setMin(1);
  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.identifier")) {
          ok = f.getMin() == 1;
          if (ok)
            f.setMin(0);
        }
        ok = ok && Base.compareDeep(b, f, true);
      }
    }
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation chenge cardinality test failed");
  } else 
    System.out.println("Snap shot generation chenge cardinality test passed");
}
 
Example 8
Source File: ResourceUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private static DataElement showDECHeader(StringBuilder b, Bundle bundle) {
  DataElement meta = new DataElement();
  DataElement prototype = (DataElement) bundle.getEntry().get(0).getResource();
  meta.setPublisher(prototype.getPublisher());
  meta.getContact().addAll(prototype.getContact());
  meta.setStatus(prototype.getStatus());
  meta.setDate(prototype.getDate());
  meta.addElement().getType().addAll(prototype.getElement().get(0).getType());

  for (BundleEntryComponent e : bundle.getEntry()) {
    DataElement de = (DataElement) e.getResource();
    if (!Base.compareDeep(de.getPublisherElement(), meta.getPublisherElement(), false))
      meta.setPublisherElement(null);
    if (!Base.compareDeep(de.getContact(), meta.getContact(), false))
      meta.getContact().clear();
    if (!Base.compareDeep(de.getStatusElement(), meta.getStatusElement(), false))
      meta.setStatusElement(null);
    if (!Base.compareDeep(de.getDateElement(), meta.getDateElement(), false))
      meta.setDateElement(null);
    if (!Base.compareDeep(de.getElement().get(0).getType(), meta.getElement().get(0).getType(), false))
      meta.getElement().get(0).getType().clear();
  }
  if (meta.hasPublisher() || meta.hasContact() || meta.hasStatus() || meta.hasDate() /* || meta.hasType() */) {
    b.append("<table class=\"grid\">\r\n"); 
    if (meta.hasPublisher())
      b.append("<tr><td>Publisher:</td><td>"+meta.getPublisher()+"</td></tr>\r\n");
    if (meta.hasContact()) {
      b.append("<tr><td>Contacts:</td><td>");
      boolean firsti = true;
      for (ContactDetail c : meta.getContact()) {
        if (firsti)
          firsti = false;
        else
          b.append("<br/>");
        if (c.hasName())
          b.append(Utilities.escapeXml(c.getName())+": ");
        boolean first = true;
        for (ContactPoint cp : c.getTelecom()) {
          if (first)
            first = false;
          else
            b.append(", ");
          renderContactPoint(b, cp);
        }
      }
      b.append("</td></tr>\r\n");
    }
    if (meta.hasStatus())
      b.append("<tr><td>Status:</td><td>"+meta.getStatus().toString()+"</td></tr>\r\n");
    if (meta.hasDate())
      b.append("<tr><td>Date:</td><td>"+meta.getDateElement().asStringValue()+"</td></tr>\r\n");
    if (meta.getElement().get(0).hasType())
      b.append("<tr><td>Type:</td><td>"+renderType(meta.getElement().get(0).getType())+"</td></tr>\r\n");
    b.append("</table>\r\n"); 
  }  
  return meta;
}
 
Example 9
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * check that documentation appending is working
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testDocumentationAppend() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.setDefinition("... some more doco");
  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.identifier")) {
          ok = f.getDefinition().length() > b.getDefinition().length();
          if (ok) {
            f.setDefinition(null);
            b.setDefinition(null);
          }
        }
        ok = ok && Base.compareDeep(b, f, true);
      }
    }
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation documentation append failed");
  } else 
    System.out.println("Snap shot generation documentation append test passed");
}
 
Example 10
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * check that narrowing types is working
 * this one doesn't rename the path
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void textTypeNarrowing1() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.deceased[x]");
  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 1 failed");
  } else 
    System.out.println("Snap shot generation narrow type 1 test passed");
}
 
Example 11
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  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");
}
 
Example 12
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * check that mapping resolution is working
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testMapping() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.addMapping().setIdentity("rim").setMap("test");
  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.identifier")) {
          ok = f.getMapping().size() > b.getMapping().size();
          if (ok) {
            f.getMapping().clear();
            b.getMapping().clear();
          }
        }
        ok = ok && Base.compareDeep(b, f, true);
      }
    }
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation mapping changes failed");
  } else 
    System.out.println("Snap shot generation mapping changes test passed");
}
 
Example 13
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  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");
}
 
Example 14
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * Walking into a type, without explicitly doing so 
 * 
 * note: this currently fails.
 * 
 * @param context2
 * @
 * @throws EOperationOutcome 
 */
private void testTypeWalk2() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  ElementDefinition 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");
}
 
Example 15
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * we're going to slice Patient.identifier
 */
private void testSlicingSimple() 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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  
  // set the slice up
  ElementDefinition id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator().setPath("use").setType(DiscriminatorType.VALUE);
  
  // first slice: 
  id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.setSliceName("name1");
  id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier.use");
  id.setFixed(new CodeType("usual"));
  
  // second slice:
  id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier");
  id.setSliceName("name2");
  id = focus.getDifferential().addElement();
  id.setPath("Patient.identifier.use");
  id.setFixed(new CodeType("official"));
  
  
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  new ProfileUtilities(context, messages, null).generateSnapshot(base, focus, focus.getUrl(), "Simple Test" );

  // 18 different: identifier + 8 inner children * 2 
  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 18;
  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 + 18);
      if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath())) 
        ok = false;
      else {
        f.setBase(null);
        if (f.getPath().equals("Patient.identifier")) {
          ok = f.hasSlicing();
          if (ok)
            f.setSlicing(null);
        }            
        ok = Base.compareDeep(b, f, true);
      }
    }
  }
  // now, check that the slices we skipped are correct:
  for (int i = 10; i <= 18; i++) {
    if (ok) {
      ElementDefinition d1 = focus.getSnapshot().getElement().get(i);
      ElementDefinition d2 = focus.getSnapshot().getElement().get(i+9);
      if (d1.getPath().equals("Patient.identifier.use")) {
        ok = d1.hasFixed() && d2.hasFixed() && !Base.compareDeep(d1.getFixed(), d2.getFixed(), true);
        if (ok) {
          d1.setFixed(null);
          d2.setFixed(null);
        }
      }
      if (d1.getPath().equals("Patient.identifier")) {
        ok = d1.hasSliceName() && d2.hasSliceName() && !Base.compareDeep(d1.getSliceNameElement(), d2.getSliceNameElement(), true);
        if (ok) {
          d1.setSliceName(null);
          d2.setSliceName(null);
        }
      }
      ok = Base.compareDeep(d1, d2, true);
    }
  }
  // for throughness, we could check against identifier too, but this is not done now.
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation slicing failed");
  } else 
    System.out.println("Snap shot generation slicing passed");
  
}
 
Example 16
Source File: ProfileUtilitiesTests.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
/**
 * we're going to slice Patient.extension and refer to extension by profile
 * 
 * implicit: whether to rely on implicit extension slicing
 */
private void testSlicingExtension(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.setBaseDefinition(base.getUrl());
  focus.setType(base.getType());
  focus.setDerivation(TypeDerivationRule.CONSTRAINT);
  
  // set the slice up
  ElementDefinition id;
  if (!implicit) {
    id = focus.getDifferential().addElement();
    id.setPath("Patient.extension");
    id.getSlicing().setOrdered(false).setRules(SlicingRules.OPEN).addDiscriminator().setPath("url").setType(DiscriminatorType.VALUE);
    id.setMax("3");
  }
  // first slice: 
  id = focus.getDifferential().addElement();
  id.setPath("Patient.extension");
  id.setSliceName("name1");
  id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-birthTime");
  id.setMin(1);
  
  // second slice:
  id = focus.getDifferential().addElement();
  id.setPath("Patient.extension");
  id.setSliceName("name2");
  id.addType().setCode("Extension").setProfile("http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName");    
  
  List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
  ProfileUtilities pu = new ProfileUtilities(context, messages, null);
  pu.generateSnapshot(base, focus, focus.getUrl(), "Simple Test" );

  // 2 different: extension slices 
  boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size() - 2;
  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 + 2);
      if (!f.hasBase() || !b.getPath().equals(f.getBase().getPath())) 
        ok = false;
      else {
        f.setBase(null);
        if (f.getPath().equals("Patient.extension")) {
          ok = f.hasSlicing() && (implicit || f.getMax().equals("3"));
          if (ok) {
            f.setSlicing(null);
            f.setMaxElement(b.getMaxElement());
          }
        }            
        if (!f.getPath().equals("Patient.extension")) // no compare that because the definitions get overwritten 
          ok = Base.compareDeep(b, f, true);
      }
    }
  }
  // now, check that the slices we skipped are correct:
  if (ok) {
    ElementDefinition d1 = focus.getSnapshot().getElement().get(8);
    ElementDefinition d2 = focus.getSnapshot().getElement().get(9);
    ok = d1.hasType() && d1.getType().get(0).hasProfile() && d2.hasType() && d2.getType().get(0).hasProfile() && !Base.compareDeep(d1.getType(), d2.getType(), true) &&
          d1.getMin() == 1 && d2.getMin() == 0 && d1.getMax().equals("1") && d2.getMax().equals("1");
    if (ok) {
      d1.getType().clear();
      d2.getType().clear();
      d1.setSliceName("x");
      d2.setSliceName("x");
      d1.setMin(0);
    }
    ok = Base.compareDeep(d1, d2, true);
    // for throughness, we could check against extension too, but this is not done now.
  }
  
  if (!ok) {
    compareXml(base, focus);
    throw new FHIRException("Snap shot generation slicing extensions simple ("+(implicit ? "implicit" : "not implicit")+") failed");
  } else 
    System.out.println("Snap shot generation slicing extensions simple ("+(implicit ? "implicit" : "not implicit")+") passed");
}