Search in sources :

Example 16 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class StandAloneValidatorFetcher method resolveURL.

@Override
public boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type) throws IOException, FHIRException {
    if (!Utilities.isAbsoluteUrl(url)) {
        return false;
    }
    if (url.contains("|")) {
        url = url.substring(0, url.lastIndexOf("|"));
    }
    if (type != null && type.equals("uri") && isMappingUri(url)) {
        return true;
    }
    // if we've got to here, it's a reference to a FHIR URL. We're going to try to resolve it on the fly
    String pid = null;
    String ver = null;
    String base = findBaseUrl(url);
    if (base == null) {
        return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
    }
    // the next operations are expensive. we're going to cache them
    if (urlList.containsKey(url)) {
        return urlList.get(url);
    }
    if (base.equals("http://terminology.hl7.org")) {
        pid = "hl7.terminology";
    } else if (url.startsWith("http://hl7.org/fhir")) {
        pid = pcm.getPackageId(base);
    } else {
        if (pidList.containsKey(base)) {
            pid = pidList.get(base);
        } else {
            pid = pcm.findCanonicalInLocalCache(base);
            pidList.put(base, pid);
        }
    }
    ver = url.contains("|") ? url.substring(url.indexOf("|") + 1) : null;
    if (pid == null && Utilities.startsWithInList(url, "http://hl7.org/fhir", "http://terminology.hl7.org")) {
        urlList.put(url, false);
        return false;
    }
    if (url.startsWith("http://hl7.org/fhir")) {
        // first possibility: it's a reference to a version specific URL http://hl7.org/fhir/X.X/...
        VersionURLInfo vu = VersionUtilities.parseVersionUrl(url);
        if (vu != null) {
            NpmPackage pi = pcm.loadPackage(VersionUtilities.packageForVersion(vu.getVersion()), VersionUtilities.getCurrentVersion(vu.getVersion()));
            boolean res = pi.hasCanonical(vu.getUrl());
            urlList.put(url, res);
            return res;
        }
    }
    // ok maybe it's a reference to a package we know
    if (pid != null) {
        if ("sharedhealth.fhir.ca.common".equals(pid)) {
            // special case - optimise this
            return false;
        }
        NpmPackage pi = null;
        if (pidMap.containsKey(pid + "|" + ver)) {
            pi = pidMap.get(pid + "|" + ver);
        } else if (installer.packageExists(pid, ver)) {
            try {
                installer.loadPackage(pid, ver);
                pi = pcm.loadPackage(pid);
                pidMap.put(pid + "|" + ver, pi);
            } catch (Exception e) {
                pidMap.put(pid + "|" + ver, null);
            }
        } else {
            pidMap.put(pid + "|" + ver, null);
        }
        if (pi != null) {
            context.loadFromPackage(pi, null);
            return pi.hasCanonical(url);
        }
    }
    // we don't bother with urls outside fhir space in the standalone validator - we assume they are valid
    return !url.startsWith("http://hl7.org/fhir") && !type.equals("canonical");
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) VersionURLInfo(org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 17 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class PackageCacheTests method testPath.

@Test
public void testPath() throws IOException {
    FilesystemPackageCacheManager cache = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
    cache.clear();
    List<String> list = cache.listPackages();
    if (!list.isEmpty()) {
        System.out.println("remaining packages: " + list.toString());
    }
    Assertions.assertTrue(list.isEmpty(), "List should be true but is " + list.toString());
    NpmPackage npm = cache.loadPackage(CommonPackages.ID_PUBPACK, CommonPackages.VER_PUBPACK);
    npm.loadAllFiles();
    Assertions.assertNotNull(npm);
    File dir = new File(Utilities.path("[tmp]", "cache"));
    if (dir.exists()) {
        Utilities.clearDirectory(dir.getAbsolutePath());
    } else {
        Utilities.createDirectory(dir.getAbsolutePath());
    }
    npm.save(dir);
    NpmPackage npm2 = cache.loadPackage(CommonPackages.ID_PUBPACK, "file:" + dir.getAbsolutePath());
    Assertions.assertNotNull(npm2);
    list = cache.listPackages();
    Assertions.assertFalse(list.isEmpty());
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 18 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, null);
        pu.setNewSlicingProcessing(true);
        for (StructureDefinition sd : test.included) {
            pu.setIds(sd, false);
        }
        for (StructureDefinition sd : test.included) {
            if (!TestingUtilities.context().hasResource(StructureDefinition.class, sd.getUrl())) {
                TestingUtilities.context().cacheResource(sd);
            }
        }
        StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
        }
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition(), context);
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (!TestingUtilities.context().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
        NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
        TestingUtilities.context().loadFromPackage(npm, new TestLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
    }
    pu.setXver(new XVerExtensionManager(TestingUtilities.context()));
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.context(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        TestingUtilities.context().cacheResource(output);
        File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r4b.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) XVerExtensionManager(org.hl7.fhir.r4b.utils.XVerExtensionManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 19 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class NpmPackageTests method testNewTgz.

@Test
public void testNewTgz() throws IOException {
    NpmPackage npm = NpmPackage.fromPackage(TestingUtilities.loadTestResourceStream("npm", "test.format.new.tgz"));
    checkNpm(npm);
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) Test(org.junit.jupiter.api.Test)

Example 20 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class NpmPackageTests method testOldFolder.

@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);
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)34 File (java.io.File)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)15 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)13 IOException (java.io.IOException)11 Test (org.junit.jupiter.api.Test)11 FileNotFoundException (java.io.FileNotFoundException)9 TextFile (org.hl7.fhir.utilities.TextFile)8 FileOutputStream (java.io.FileOutputStream)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 PackageResourceInformation (org.hl7.fhir.utilities.npm.NpmPackage.PackageResourceInformation)6 SimpleWorkerContext (org.hl7.fhir.r5.context.SimpleWorkerContext)4 IniFile (org.hl7.fhir.utilities.IniFile)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 NotImplementedException (org.apache.commons.lang3.NotImplementedException)3 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)3