001package org.hl7.fhir.utilities.tests;
002
003import org.apache.commons.io.IOUtils;
004import org.hl7.fhir.utilities.CSFile;
005import org.hl7.fhir.utilities.TextFile;
006import org.hl7.fhir.utilities.ToolGlobalSettings;
007import org.hl7.fhir.utilities.Utilities;
008
009import java.io.*;
010import java.util.ArrayList;
011import java.util.List;
012
013public class BaseTestingUtilities {
014
015  static public boolean silent;
016
017  public static String loadTestResource(String... paths) throws IOException {
018    /**
019     * This 'if' condition checks to see if the fhir-test-cases project (https://github.com/FHIR/fhir-test-cases) is
020     * installed locally at the same directory level as the core library project is. If so, the test case data is read
021     * directly from that project, instead of the imported maven dependency jar. It is important, that if you want to
022     * test against the dependency imported from sonatype nexus, instead of your local copy, you need to either change
023     * the name of the project directory to something other than 'fhir-test-cases', or move it to another location, not
024     * at the same directory level as the core project.
025     */
026
027    String dir = System.getenv("FHIR-TEST-CASES");
028    if (dir == null && ToolGlobalSettings.hasTestsPath()) {
029      dir = ToolGlobalSettings.getTestsPath();
030    }
031    if (dir != null && new CSFile(dir).exists()) {
032      String n = Utilities.path(dir, Utilities.path(paths));
033      // ok, we'll resolve this locally
034      return TextFile.fileToString(new CSFile(n));
035    } else {
036      // resolve from the package
037      String contents;
038      String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
039      try (InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath)) {
040        if (inputStream == null) {
041          throw new IOException("Can't find file on classpath: " + classpath);
042        }
043        contents = IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.UTF_8);
044      }
045      return contents;
046    }
047  }
048
049  
050  public static InputStream loadTestResourceStream(String... paths) throws IOException {
051    String dir = System.getenv("FHIR-TEST-CASES");
052    if (dir == null && ToolGlobalSettings.hasTestsPath()) {
053      dir = ToolGlobalSettings.getTestsPath();
054    }
055    if (dir != null && new File(dir).exists()) {
056      String n = Utilities.path(dir, Utilities.path(paths));
057      return new FileInputStream(n);
058    } else {
059      String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
060      InputStream s = BaseTestingUtilities.class.getResourceAsStream(classpath);
061      if (s == null) {
062        throw new Error("unable to find resource " + classpath);
063      }
064      return s;
065    }
066  }
067
068  public static byte[] loadTestResourceBytes(String... paths) throws IOException {
069    String dir = System.getenv("FHIR-TEST-CASES");
070    if (dir == null && ToolGlobalSettings.hasTestsPath()) {
071      dir = ToolGlobalSettings.getTestsPath();
072    }
073    if (dir != null && new File(dir).exists()) {
074      String n = Utilities.path(dir, Utilities.path(paths));
075      return TextFile.fileToBytes(n);
076    } else {
077      String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
078      InputStream s = BaseTestingUtilities.class.getResourceAsStream(classpath);
079      if (s == null) {
080        throw new Error("unable to find resource " + classpath);
081      }
082      return TextFile.streamToBytes(s);
083    }
084  }
085
086  public static boolean findTestResource(String... paths) throws IOException {
087    String dir = System.getenv("FHIR-TEST-CASES");
088    if (dir == null && ToolGlobalSettings.hasTestsPath()) {
089      dir = ToolGlobalSettings.getTestsPath();
090    }
091    if (dir != null && new File(dir).exists()) {
092      String n = Utilities.path(dir, Utilities.path(paths));
093      return new File(n).exists();
094    } else {
095      String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
096      try {
097        InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath);
098        return inputStream != null;
099      } catch (Throwable t) {
100        return false;
101      }
102    }
103  }
104}