001package org.hl7.fhir.r5.test.utils; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileNotFoundException; 006import java.io.IOException; 007import java.io.InputStream; 008import java.util.ArrayList; 009import java.util.HashMap; 010import java.util.List; 011import java.util.Map; 012 013import javax.xml.parsers.DocumentBuilder; 014import javax.xml.parsers.DocumentBuilderFactory; 015 016import org.apache.commons.codec.binary.Base64; 017import org.fhir.ucum.UcumEssenceService; 018import org.hl7.fhir.r5.context.IWorkerContext; 019import org.hl7.fhir.r5.context.SimpleWorkerContext; 020import org.hl7.fhir.r5.context.TerminologyCache; 021import org.hl7.fhir.r5.model.Parameters; 022import org.hl7.fhir.r5.utils.R5Hacker; 023import org.hl7.fhir.utilities.CSFile; 024import org.hl7.fhir.utilities.TextFile; 025import org.hl7.fhir.utilities.ToolGlobalSettings; 026import org.hl7.fhir.utilities.Utilities; 027import org.hl7.fhir.utilities.VersionUtilities; 028import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; 029import org.hl7.fhir.utilities.npm.NpmPackage; 030import org.hl7.fhir.utilities.npm.ToolsVersion; 031import org.hl7.fhir.utilities.tests.BaseTestingUtilities; 032import org.hl7.fhir.utilities.tests.TestConfig; 033import org.w3c.dom.Document; 034import org.w3c.dom.Element; 035import org.w3c.dom.NamedNodeMap; 036import org.w3c.dom.Node; 037 038/* 039 Copyright (c) 2011+, HL7, Inc. 040 All rights reserved. 041 042 Redistribution and use in source and binary forms, with or without modification, 043 are permitted provided that the following conditions are met: 044 045 * Redistributions of source code must retain the above copyright notice, this 046 list of conditions and the following disclaimer. 047 * Redistributions in binary form must reproduce the above copyright notice, 048 this list of conditions and the following disclaimer in the documentation 049 and/or other materials provided with the distribution. 050 * Neither the name of HL7 nor the names of its contributors may be used to 051 endorse or promote products derived from this software without specific 052 prior written permission. 053 054 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 055 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 056 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 057 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 058 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 059 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 060 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 061 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 062 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 063 POSSIBILITY OF SUCH DAMAGE. 064 065 */ 066 067 068import com.google.gson.JsonArray; 069import com.google.gson.JsonElement; 070import com.google.gson.JsonNull; 071import com.google.gson.JsonObject; 072import com.google.gson.JsonPrimitive; 073import com.google.gson.JsonSyntaxException; 074 075public class TestingUtilities extends BaseTestingUtilities { 076 077 static public Map<String, IWorkerContext> fcontexts; 078 079 final static public String DEFAULT_CONTEXT_VERSION = "5.0.0"; 080 081 /** Get an existing instantiation of a WorkerContext if available 082 * 083 * This uses the DEFAULT_CONTEXT_VERSION 084 * */ 085 public static IWorkerContext getSharedWorkerContext() { 086 return getSharedWorkerContext(DEFAULT_CONTEXT_VERSION); 087 } 088 089 /** 090 * Get an existing instantiation of a WorkerContext if available 091 * 092 * @param version FHIR Version to get context for 093 * @return 094 */ 095 public static IWorkerContext getSharedWorkerContext(String version) { 096 if (!Utilities.existsInList(version, "1.0.2", "3.0.1", "4.0.1", "4.3.0", "5.0.0")) { 097 throw new Error("illegal version: "+version); 098 099 } 100 101 String v = VersionUtilities.getMajMin(version); 102 if (fcontexts == null) { 103 fcontexts = new HashMap<>(); 104 } 105 if (!fcontexts.containsKey(v)) { 106 IWorkerContext fcontext = getWorkerContext(version); 107 fcontexts.put(v, fcontext); 108 } 109 return fcontexts.get(v); 110 } 111 112 public static IWorkerContext getWorkerContext(String version) { 113 114 FilesystemPackageCacheManager pcm; 115 try { 116 pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION); 117 IWorkerContext fcontext = getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version)); 118 fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml"))); 119 fcontext.setExpansionProfile(new Parameters()); 120 if (!fcontext.hasPackage("hl7.terminology.r5", null)) { 121 NpmPackage utg = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage("hl7.terminology.r5"); 122 System.out.println("Loading THO: "+utg.name()+"#"+utg.version()); 123 fcontext.loadFromPackage(utg, new TestPackageLoader(new String[]{"CodeSystem", "ValueSet"})); 124 } 125 R5Hacker.fixR5BrokenResources(fcontext); 126 return fcontext; 127 } catch (Exception e) { 128 e.printStackTrace(); 129 throw new Error(e); 130 } 131 } 132 133 public static String getTerminologyCacheDirectory() { 134 return TestConfig.getInstance().getTxCacheDirectory("org.hl7.fhir.r5"); 135 } 136 137 public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage) throws Exception { 138 SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT) 139 .withTerminologyCachePath(getTerminologyCacheDirectory()).fromPackage(npmPackage); 140 TerminologyCache.setCacheErrors(true); 141 return swc; 142 } 143 144 public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage, IWorkerContext.IContextResourceLoader loader) throws Exception { 145 SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT) 146 .withTerminologyCachePath(getTerminologyCacheDirectory()).fromPackage(npmPackage, loader); 147 TerminologyCache.setCacheErrors(true); 148 return swc; 149 } 150 151 static public String fixedpath; 152 static public String contentpath; 153 154 public static String home() { 155 if (fixedpath != null) 156 return fixedpath; 157 String s = System.getenv("FHIR_HOME"); 158 if (!Utilities.noString(s)) 159 return s; 160 s = "C:\\work\\org.hl7.fhir\\build"; 161 // FIXME: change this back 162 s = "/Users/jamesagnew/git/fhir"; 163 if (new File(s).exists()) 164 return s; 165 throw new Error("FHIR Home directory not configured"); 166 } 167 168 169 public static String content() throws IOException { 170 if (contentpath != null) 171 return contentpath; 172 String s = "R:\\fhir\\publish"; 173 if (new File(s).exists()) 174 return s; 175 return Utilities.path(home(), "publish"); 176 } 177 178 // diretory that contains all the US implementation guides 179 public static String us() { 180 if (fixedpath != null) 181 return fixedpath; 182 String s = System.getenv("FHIR_HOME"); 183 if (!Utilities.noString(s)) 184 return s; 185 s = "C:\\work\\org.hl7.fhir.us"; 186 if (new File(s).exists()) 187 return s; 188 throw new Error("FHIR US directory not configured"); 189 } 190 191 192}