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