From 9d164ef153003ca9df19090b5b8d4172cac69dd9 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sun, 15 Sep 2019 20:42:21 +1000 Subject: [PATCH] fix failing test --- .gitignore | 1 + .../CapabilityStatementUtilities.java | 12 +- .../fhir/r5/conformance/ProfileComparer.java | 115 +++++++++++++++--- .../org/hl7/fhir/r5/validation/Validator.java | 4 +- .../tests/ProfileComparisonTests.java | 4 +- 5 files changed, 112 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 8d3be3ddf..a0f8bfa14 100644 --- a/.gitignore +++ b/.gitignore @@ -292,3 +292,4 @@ local.properties /org.hl7.fhir.r4b /org.hl7.fhir.r5/src/test/resources/gen/gen.xml /org.hl7.fhir.r5/src/test/resources/graphql/*.out +/org.hl7.fhir.validation/src/test/resources/comparison/output diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/CapabilityStatementUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/CapabilityStatementUtilities.java index 2d077650f..9e344a776 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/CapabilityStatementUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/CapabilityStatementUtilities.java @@ -117,9 +117,11 @@ public class CapabilityStatementUtilities { this.context = context; this.folder = folder; this.keygen = keygen; - String f = Utilities.path(folder, "comparison.zip"); - download("http://fhir.org/archive/comparison.zip", f); - unzip(f, folder); + if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) { + String f = Utilities.path(folder, "comparison.zip"); + download("https://www.fhir.org/archive/comparison.zip", f); + unzip(f, folder); + } } /** @@ -440,7 +442,7 @@ public class CapabilityStatementUtilities { union.setProfile(sdL.getUrl()); } else if (folder != null) { try { - ProfileComparer pc = new ProfileComparer(context, keygen); + ProfileComparer pc = new ProfileComparer(context, keygen, folder); pc.setId("api-ep."+type); pc.setTitle("Comparison - "+selfName+" vs "+otherName); pc.setLeftName(selfName+": "+sdL.present()); @@ -449,7 +451,7 @@ public class CapabilityStatementUtilities { pc.setRightLink(sdR.getUserString("path")); pc.compareProfiles(sdL, sdR); System.out.println("Generate Comparison between "+pc.getLeftName()+" and "+pc.getRightName()); - pc.generate(folder); + pc.generate(); for (ProfileComparison cmp : pc.getComparisons()) { new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, cmp.getSubset().fhirType()+"-"+cmp.getSubset().getId()+".xml")), cmp.getSubset()); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileComparer.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileComparer.java index b57821c31..0c3a89fa6 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileComparer.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/conformance/ProfileComparer.java @@ -1,5 +1,7 @@ package org.hl7.fhir.r5.conformance; +import java.io.BufferedOutputStream; + /*- * #%L * org.hl7.fhir.r5 @@ -22,7 +24,11 @@ package org.hl7.fhir.r5.conformance; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -32,7 +38,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; - +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.hl7.fhir.exceptions.DefinitionException; import org.hl7.fhir.exceptions.FHIRException; @@ -100,16 +107,96 @@ public class ProfileComparer implements ProfileKnowledgeProvider { private IWorkerContext context; private KeyGenerator keygen; + private String folder; - public ProfileComparer(IWorkerContext context, KeyGenerator keygen) { + public ProfileComparer(IWorkerContext context, KeyGenerator keygen, String folder) throws IOException { super(); this.context = context; this.keygen = keygen; + this.folder = folder; + if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) { + String f = Utilities.path(folder, "comparison.zip"); + download("https://www.fhir.org/archive/comparison.zip", f); + unzip(f, folder); + } } - public ProfileComparer(IWorkerContext context) { + private void download(String address, String filename) throws IOException { + URL url = new URL(address); + URLConnection c = url.openConnection(); + InputStream s = c.getInputStream(); + FileOutputStream f = new FileOutputStream(filename); + transfer(s, f, 1024); + f.close(); + } + + + public static void transfer(InputStream in, OutputStream out, int buffer) throws IOException { + byte[] read = new byte[buffer]; // Your buffer size. + while (0 < (buffer = in.read(read))) + out.write(read, 0, buffer); + } + + /** + * Size of the buffer to read/write data + */ + private static final int BUFFER_SIZE = 4096; + /** + * Extracts a zip file specified by the zipFilePath to a directory specified by + * destDirectory (will be created if does not exists) + * @param zipFilePath + * @param destDirectory + * @throws IOException + */ + public void unzip(String zipFilePath, String destDirectory) throws IOException { + File destDir = new File(destDirectory); + if (!destDir.exists()) { + destDir.mkdir(); + } + ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); + ZipEntry entry = zipIn.getNextEntry(); + // iterates over entries in the zip file + while (entry != null) { + String filePath = destDirectory + File.separator + entry.getName(); + if (!entry.isDirectory()) { + // if the entry is a file, extracts it + extractFile(zipIn, filePath); + } else { + // if the entry is a directory, make the directory + File dir = new File(filePath); + dir.mkdir(); + } + zipIn.closeEntry(); + entry = zipIn.getNextEntry(); + } + zipIn.close(); + } + /** + * Extracts a zip entry (file entry) + * @param zipIn + * @param filePath + * @throws IOException + */ + private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read = 0; + while ((read = zipIn.read(bytesIn)) != -1) { + bos.write(bytesIn, 0, read); + } + bos.close(); + } + + + public ProfileComparer(IWorkerContext context, String folder) throws IOException { super(); this.context = context; + this.folder = folder; + if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) { + String f = Utilities.path(folder, "comparison.zip"); + download("https://www.fhir.org/archive/comparison.zip", f); + unzip(f, folder); + } } private static final int BOTH_NULL = 0; @@ -1431,9 +1518,9 @@ public class ProfileComparer implements ProfileKnowledgeProvider { } - public String generate(String dest) throws IOException { + public String generate() throws IOException { for (ValueSet vs : valuesets) { - vs.setUserData("path", dest+"/"+getId()+"-vs-"+vs.getId()+".html"); + vs.setUserData("path", folder+"/"+getId()+"-vs-"+vs.getId()+".html"); } // ok, all compared; now produce the output // first page we produce is simply the index @@ -1442,8 +1529,8 @@ public class ProfileComparer implements ProfileKnowledgeProvider { vars.put("left", genPCLink(getLeftName(), getLeftLink())); vars.put("right", genPCLink(getRightName(), getRightLink())); vars.put("table", genPCTable()); - vars.put("valuesets", genValueSets(dest+"/"+getId()+"-vs")); - producePage(summaryTemplate(), Utilities.path(dest, getId()+".html"), vars); + vars.put("valuesets", genValueSets(folder+"/"+getId()+"-vs")); + producePage(summaryTemplate(), Utilities.path(folder, getId()+".html"), vars); System.out.println(" .. profiles"); // then we produce a comparison page for each pair @@ -1453,15 +1540,15 @@ public class ProfileComparer implements ProfileKnowledgeProvider { vars.put("left", genPCLink(getLeftName(), getLeftLink())); vars.put("right", genPCLink(getRightName(), getRightLink())); vars.put("messages", genCmpMessages(cmp)); - vars.put("subset", genCompModel(cmp.getSubset(), "intersection", getId()+"."+cmp.getId(), "", dest)); - vars.put("superset", genCompModel(cmp.getSuperset(), "union", getId()+"."+cmp.getId(), "", dest)); - producePage(singleTemplate(), Utilities.path(dest, getId()+"."+cmp.getId()+".html"), vars); + vars.put("subset", genCompModel(cmp.getSubset(), "intersection", getId()+"."+cmp.getId(), "", folder)); + vars.put("superset", genCompModel(cmp.getSuperset(), "union", getId()+"."+cmp.getId(), "", folder)); + producePage(singleTemplate(), Utilities.path(folder, getId()+"."+cmp.getId()+".html"), vars); } // // and also individual pages for each pair outcome // // then we produce value set pages for each value set // // // TODO Auto-generated method stub - return Utilities.path(dest, getId()+".html"); + return Utilities.path(folder, getId()+".html"); } @@ -1480,13 +1567,11 @@ public class ProfileComparer implements ProfileKnowledgeProvider { } private String summaryTemplate() throws IOException { - return TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\source\\template-comparison-set.html"); - // return cachedFetch("04a9d69a-47f2-4250-8645-bf5d880a8eaa-1.fhir-template", "http://build.fhir.org/template-comparison-set.html.template"); + return TextFile.fileToString(Utilities.path(folder, "template-comparison-set.html")); } private String singleTemplate() throws IOException { - return TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\source\\template-comparison.html"); - // return cachedFetch("04a9d69a-47f2-4250-8645-bf5d880a8eaa-1.fhir-template", "http://build.fhir.org/template-comparison-set.html.template"); + return TextFile.fileToString(Utilities.path(folder, "template-comparison.html")); } private String cachedFetch(String id, String source) throws IOException { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java index b1fd274f6..4be4b5027 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java @@ -296,12 +296,12 @@ public class Validator { if (resLeft != null && resRight != null) { if (resLeft instanceof StructureDefinition && resRight instanceof StructureDefinition) { System.out.println("Comparing StructureDefinitions "+left+" to "+right); - ProfileComparer pc = new ProfileComparer(validator.getContext()); + ProfileComparer pc = new ProfileComparer(validator.getContext(), dest); StructureDefinition sdL = (StructureDefinition) resLeft; StructureDefinition sdR = (StructureDefinition) resRight; pc.compareProfiles(sdL, sdR); System.out.println("Generating output to "+dest+"..."); - File htmlFile = new File(pc.generate(dest)); + File htmlFile = new File(pc.generate()); Desktop.getDesktop().browse(htmlFile.toURI()); System.out.println("Done"); } else if (resLeft instanceof CapabilityStatement && resRight instanceof CapabilityStatement) { diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ProfileComparisonTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ProfileComparisonTests.java index cf05e94dc..c83ecd2de 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ProfileComparisonTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ProfileComparisonTests.java @@ -34,7 +34,7 @@ public class ProfileComparisonTests { // ok now set up the comparison StructureDefinition sdL = ve.getContext().fetchResource(StructureDefinition.class, left); - ProfileComparer pc = new ProfileComparer(ve.getContext(), new KeyGenerator("http://fhir.org/temp/"+UUID.randomUUID().toString().toLowerCase())); + ProfileComparer pc = new ProfileComparer(ve.getContext(), new KeyGenerator("http://fhir.org/temp/"+UUID.randomUUID().toString().toLowerCase()), dest); if (sdL == null) { System.out.println("Unable to locate left profile " +left); } else { @@ -47,7 +47,7 @@ public class ProfileComparisonTests { System.out.println("Generating output..."); File htmlFile = null; try { - htmlFile = new File(pc.generate(dest)); + htmlFile = new File(pc.generate()); } catch (Exception e) { e.printStackTrace(); throw e;