From 6affe7f040e143c76f618821c686063bf79fe3b2 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Thu, 5 Dec 2019 04:12:02 +1100 Subject: [PATCH] Add NPM Package tests --- .../java/org/hl7/fhir/r5/test/AllR5Tests.java | 1 + .../org/hl7/fhir/r5/test/NpmPackageTests.java | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NpmPackageTests.java diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/AllR5Tests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/AllR5Tests.java index 70ff058f8..b1a0c1b15 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/AllR5Tests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/AllR5Tests.java @@ -20,6 +20,7 @@ import org.junit.runners.Suite.SuiteClasses; BaseDateTimeTypeTest.class, OpenApiGeneratorTest.class, MetadataResourceManagerTester.class, + NpmPackageTests.class, SnapShotGenerationTests.class}) public class AllR5Tests { diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NpmPackageTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NpmPackageTests.java new file mode 100644 index 000000000..27b1b7cd9 --- /dev/null +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NpmPackageTests.java @@ -0,0 +1,100 @@ +package org.hl7.fhir.r5.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.hl7.fhir.r5.test.utils.TestingUtilities; +import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.cache.NpmPackage; +import org.junit.Test; + +import junit.framework.Assert; + +public class NpmPackageTests { + + @Test + public void testOldFolder() throws IOException { + // extract the test + String dst = Utilities.path("[tmp]", "npm", "test.format.old"); + Utilities.clearDirectory(dst); + unzip(TestingUtilities.loadTestResourceStream("npm", "test.format.old.zip"), new File(dst)); + dst = Utilities.path(dst, "test.format.old"); + NpmPackage npm = NpmPackage.fromFolder(dst); + checkNpm(npm); + } + + @Test + public void testNewFolder() throws IOException { + // extract the test + String dst = Utilities.path("[tmp]", "npm", "test.format.new"); + Utilities.clearDirectory(dst); + unzip(TestingUtilities.loadTestResourceStream("npm", "test.format.new.zip"), new File(dst)); + dst = Utilities.path(dst, "test.format.new"); + NpmPackage npm = NpmPackage.fromFolder(dst); + checkNpm(npm); + } + + @Test + public void testOldTgz() throws IOException { + NpmPackage npm = NpmPackage.fromPackage(TestingUtilities.loadTestResourceStream("npm", "test.format.old.tgz")); + checkNpm(npm); + } + + @Test + public void testNewTgz() throws IOException { + NpmPackage npm = NpmPackage.fromPackage(TestingUtilities.loadTestResourceStream("npm", "test.format.new.tgz")); + checkNpm(npm); + } + + private void checkNpm(NpmPackage npm) throws IOException { + Assert.assertEquals(1, npm.list("other").size()); + Assert.assertEquals("help.png", npm.list("other").get(0)); + Assert.assertEquals(1, npm.list("package").size()); + Assert.assertEquals("StructureDefinition-Definition.json", npm.list("package").get(0)); + + } + + private static void unzip(InputStream source, File destDir) throws IOException { + Utilities.createDirectory(destDir.getAbsolutePath()); + + byte[] buffer = new byte[1024]; + + ZipInputStream zis = new ZipInputStream(source); + ZipEntry zipEntry = zis.getNextEntry(); + while (zipEntry != null) { + File newFile = newFile(destDir, zipEntry); + if (zipEntry.isDirectory()) { + Utilities.createDirectory(newFile.getAbsolutePath()); + } else { + FileOutputStream fos = new FileOutputStream(newFile); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + } + zipEntry = zis.getNextEntry(); + } + zis.closeEntry(); + zis.close(); + } + + public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { + File destFile = new File(destinationDir, zipEntry.getName()); + + String destDirPath = destinationDir.getCanonicalPath(); + String destFilePath = destFile.getCanonicalPath(); + if (!destFilePath.startsWith(destDirPath + File.separator)) { + throw new IOException("Entry is outside of the target dir: " + zipEntry.getName()); + } + return destFile; +} +}