From d4701a2cad7f030767510d45bd3f0033cdfe6333 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 21 Jan 2020 11:37:01 +1100 Subject: [PATCH] track issues with sorting diffs into qa.html --- .../fhir/r5/conformance/ProfileUtilities.java | 29 +++++++++++++++++-- .../fhir/r5/context/SimpleWorkerContext.java | 2 +- .../fhir/r5/test/SnapShotGenerationTests.java | 6 ++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java index 1a81b130a..dd2a8de0e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileUtilities.java @@ -2583,7 +2583,7 @@ public class ProfileUtilities extends TranslatingUtilities { } } } - sortDifferential(base, derived, derived.getName(), new ArrayList()); + sortDifferential(base, derived, derived.getName(), new ArrayList(), false); } private void closeChildren(StructureDefinition base, ElementDefinition edb, StructureDefinition derived, ElementDefinition edm) { @@ -4350,7 +4350,9 @@ public class ProfileUtilities extends TranslatingUtilities { } - public void sortDifferential(StructureDefinition base, StructureDefinition diff, String name, List errors) throws FHIRException { + public void sortDifferential(StructureDefinition base, StructureDefinition diff, String name, List errors, boolean errorIfChanges) throws FHIRException { + List original = new ArrayList<>(); + original.addAll(diff.getDifferential().getElement()); final List diffList = diff.getDifferential().getElement(); int lastCount = diffList.size(); // first, we move the differential elements into a tree @@ -4390,13 +4392,34 @@ public class ProfileUtilities extends TranslatingUtilities { sortElements(edh, cmp, errors); // now, we serialise them back to a list + List newDiff = new ArrayList<>(); + writeElements(edh, newDiff); + if (errorIfChanges) { + compareDiffs(original, newDiff, errors); + } diffList.clear(); - writeElements(edh, diffList); + diffList.addAll(newDiff); if (lastCount != diffList.size()) errors.add("Sort failed: counts differ; at least one of the paths in the differential is illegal"); } + private void compareDiffs(List diffList, List newDiff, List errors) { + if (diffList.size() != newDiff.size()) { + errors.add("The diff list size changed when sorting - was "+diffList.size()+" is now "+newDiff.size()); + } else { + for (int i = 0; i < Integer.min(diffList.size(), newDiff.size()); i++) { + ElementDefinition e = diffList.get(i); + ElementDefinition n = newDiff.get(i); + if (!n.getPath().equals(e.getPath())) { + errors.add("The element "+e.getPath()+" is out of order (and maybe others after it)"); + return; + } + } + } + } + + private int processElementsIntoTree(ElementDefinitionHolder edh, int i, List list) { String path = edh.getSelf().getPath(); final String prefix = path + "."; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index 8ae8fc1db..df051f7ec 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -662,7 +662,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon ProfileUtilities pu = new ProfileUtilities(this, msgs, this); pu.setThrowException(false); if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) { - pu.sortDifferential(sd, p, p.getUrl(), errors); + pu.sortDifferential(sd, p, p.getUrl(), errors, true); } pu.setDebug(false); for (String err : errors) diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/SnapShotGenerationTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/SnapShotGenerationTests.java index 076e9adec..7f134c8cf 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/SnapShotGenerationTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/SnapShotGenerationTests.java @@ -458,7 +458,7 @@ public class SnapShotGenerationTests { ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), null, null); pu.setIds(test.getSource(), false); List errors = new ArrayList(); - pu.sortDifferential(base, test.getOutput(), test.getOutput().getUrl(), errors); + pu.sortDifferential(base, test.getOutput(), test.getOutput().getUrl(), errors, false); if (!errors.isEmpty()) throw new FHIRException(errors.get(0)); IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId()+"-expected.xml"), new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId()+"-expected.xml"))); @@ -490,7 +490,7 @@ public class SnapShotGenerationTests { if (test.isSort()) { List errors = new ArrayList(); int lastCount = output.getDifferential().getElement().size(); - pu.sortDifferential(base, output, test.getSource().getName(), errors); + pu.sortDifferential(base, output, test.getSource().getName(), errors, false); if (errors.size() > 0) throw new FHIRException("Sort failed: "+errors.toString()); } @@ -537,7 +537,7 @@ public class SnapShotGenerationTests { ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages , new TestPKP()); pu.setNewSlicingProcessing(true); List errors = new ArrayList(); - pu.sortDifferential(base, sd, url, errors); + pu.sortDifferential(base, sd, url, errors, false); if (!errors.isEmpty()) throw new FHIRException(errors.get(0)); pu.setIds(sd, false);