
001package org.hl7.fhir.r5.test.utils; 002 003import java.io.File; 004import java.io.IOException; 005import java.io.InputStream; 006import java.util.HashMap; 007import java.util.Locale; 008import java.util.Map; 009 010import org.fhir.ucum.UcumEssenceService; 011import org.hl7.fhir.r5.Constants; 012import org.hl7.fhir.r5.context.IContextResourceLoader; 013import org.hl7.fhir.r5.context.IWorkerContext; 014import org.hl7.fhir.r5.context.SimpleWorkerContext; 015import org.hl7.fhir.r5.model.Parameters; 016import org.hl7.fhir.r5.terminologies.utilities.TerminologyCache; 017import org.hl7.fhir.utilities.Utilities; 018import org.hl7.fhir.utilities.VersionUtilities; 019import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 020import org.hl7.fhir.utilities.npm.BasePackageCacheManager.InputStreamWithSrc; 021import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; 022import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager.IPackageProvider; 023import org.hl7.fhir.utilities.npm.NpmPackage; 024import org.hl7.fhir.utilities.tests.BaseTestingUtilities; 025import org.hl7.fhir.utilities.tests.TestConfig; 026 027public class TestingUtilities extends BaseTestingUtilities { 028 029 public static class PackageProvider implements IPackageProvider { 030 031 @Override 032 public boolean handlesPackage(String id, String version) { 033 return id.equals("hl7.fhir.r5.core"); 034 } 035 036 @Override 037 public InputStreamWithSrc provide(String id, String version) throws IOException { 038 return new InputStreamWithSrc(TestingUtilities.loadR5CorePackageSource(), "Test Case Repository", "5.0.0"); 039 } 040 041 } 042 043 static public Map<String, IWorkerContext> fcontexts; 044 045 final static public String DEFAULT_CONTEXT_VERSION = "5.0.0"; 046 047 /** Get an existing instantiation of a WorkerContext if available 048 * 049 * This uses the DEFAULT_CONTEXT_VERSION 050 * */ 051 public static IWorkerContext getSharedWorkerContext() { 052 return getSharedWorkerContext(DEFAULT_CONTEXT_VERSION); 053 } 054 055 /** 056 * Get an existing instantiation of a WorkerContext if available 057 * 058 * @param version FHIR Version to get context for 059 * @return 060 */ 061 public static IWorkerContext getSharedWorkerContext(String version) { 062 if (!Utilities.existsInList(version, "1.0.2", "3.0.1", "4.0.1", "4.3.0", "5.0.0")) { 063 throw new Error("illegal version: "+version); 064 065 } 066 067 String v = VersionUtilities.getMajMin(version); 068 if (fcontexts == null) { 069 fcontexts = new HashMap<>(); 070 } 071 if (!fcontexts.containsKey(v)) { 072 IWorkerContext fcontext = getWorkerContext(version); 073 fcontexts.put(v, fcontext); 074 } 075 return fcontexts.get(v); 076 } 077 078 public static IWorkerContext getWorkerContext(String version) { 079 080 FilesystemPackageCacheManager pcm; 081 try { 082 pcm = new FilesystemPackageCacheManager.Builder().build(); 083 IWorkerContext fcontext = null; 084 if (VersionUtilities.isR5Ver(version)) { 085 // for purposes of stability, the R5 core package comes from the test case repository 086 fcontext = getWorkerContext(loadR5CorePackage()); 087 } else { 088 fcontext = getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version)); 089 } 090 fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml"))); 091 fcontext.setExpansionParameters(new Parameters()); 092 if (!fcontext.hasPackage("hl7.terminology.r5", null)) { 093 NpmPackage utg = pcm.loadPackage("hl7.terminology.r5"); 094 System.out.println("Loading THO: "+utg.name()+"#"+utg.version()); 095 fcontext.loadFromPackage(utg, new TestPackageLoader(Utilities.strings("CodeSystem", "ValueSet"))); 096 } 097 if (!fcontext.hasPackage("hl7.fhir.uv.extensions", null)) { 098 NpmPackage ext = pcm.loadPackage("hl7.fhir.uv.extensions", Constants.EXTENSIONS_WORKING_VERSION); 099 System.out.println("Loading Extensions: "+ext.name()+"#"+ext.version()); 100 fcontext.loadFromPackage(ext, new TestPackageLoader(Utilities.strings("CodeSystem", "ValueSet", "StructureDefinition"))); 101 } 102 return fcontext; 103 } catch (Exception e) { 104 e.printStackTrace(); 105 throw new Error(e); 106 } 107 } 108 109 public static NpmPackage loadR5CorePackage() throws IOException { 110 return NpmPackage.fromPackage(loadR5CorePackageSource()); 111 } 112 113 private static InputStream loadR5CorePackageSource() throws IOException { 114 return TestingUtilities.loadTestResourceStream("r5", "packages", "hl7.fhir.r5.core.tgz"); 115 } 116 117 public static String getTerminologyCacheDirectory() { 118 return TestConfig.getInstance().getTxCacheDirectory("org.hl7.fhir.r5"); 119 } 120 121 public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage) throws Exception { 122 SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT) 123 .withTerminologyCachePath(getTerminologyCacheDirectory()).fromPackage(npmPackage); 124 TerminologyCache.setCacheErrors(true); 125 return swc; 126 } 127 128 public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage, IContextResourceLoader loader) throws Exception { 129 SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT) 130 .withTerminologyCachePath(getTerminologyCacheDirectory()).fromPackage(npmPackage, loader, true); 131 TerminologyCache.setCacheErrors(true); 132 return swc; 133 } 134 135 static public String fixedpath; 136 static public String contentpath; 137 138 public static String home() throws IOException { 139 if (fixedpath != null) 140 return fixedpath; 141 String s = System.getenv("FHIR_HOME"); 142 if (!Utilities.noString(s)) 143 return s; 144 s = "C:\\work\\org.hl7.fhir\\build"; 145 // #TODO - what should we do with this? 146 s = "/Users/jamesagnew/git/fhir"; 147 if (ManagedFileAccess.file(s).exists()) 148 return s; 149 throw new Error("FHIR Home directory not configured"); 150 } 151 152 153 public static String content() throws IOException { 154 if (contentpath != null) 155 return contentpath; 156 String s = "R:\\fhir\\publish"; 157 if (ManagedFileAccess.file(s).exists()) 158 return s; 159 return Utilities.path(home(), "publish"); 160 } 161 162 // diretory that contains all the US implementation guides 163 public static String us() throws IOException { 164 if (fixedpath != null) 165 return fixedpath; 166 String s = System.getenv("FHIR_HOME"); 167 if (!Utilities.noString(s)) 168 return s; 169 s = "C:\\work\\org.hl7.fhir.us"; 170 if (ManagedFileAccess.file(s).exists()) 171 return s; 172 throw new Error("FHIR US directory not configured"); 173 } 174 175 public static void injectCorePackageLoader() { 176 FilesystemPackageCacheManager.setPackageProvider(new TestingUtilities.PackageProvider()); 177 } 178 179 public static boolean runningAsSurefire() { 180 return "true".equals(System.getProperty("runningAsSurefire") != null ? System.getProperty("runningAsSurefire").toLowerCase(Locale.ENGLISH) : ""); 181 } 182}