Skip to content

Commit

Permalink
Merge pull request #1861 from hapifhir/2024-12-gg-tx-tests-move
Browse files Browse the repository at this point in the history
move tx tests to tx-ecosystem IG
  • Loading branch information
grahamegrieve authored Dec 16, 2024
2 parents 3c20be7 + e310f99 commit 9cce17d
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void printHelp(PrintStream out) {

@Override
public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) throws Exception {
final String source = Params.getParam(args, Params.SOURCE);
String output = Params.getParam(args, Params.OUTPUT);
final String version = Params.getParam(args, Params.VERSION);
final String tx = Params.getParam(args, Params.TERMINOLOGY);
Expand All @@ -50,7 +49,7 @@ public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, Ti
if (output == null ) {
output = Utilities.path("[tmp]");
}
boolean ok = new TxTester(new TxTester.InternalTxLoader(source, output), tx, false, loadExternals(externals)).setOutput(output).execute(version, cliContext.getModeParams(), filter);
boolean ok = new TxTester(new TxTester.InternalTxLoader(version), tx, false, loadExternals(externals)).setOutput(output).execute(cliContext.getModeParams(), filter);
SystemExitManager.setError(ok ? 1 : 0);
SystemExitManager.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import lombok.Getter;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.json.JsonException;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;

import java.io.IOException;
import java.util.*;
Expand All @@ -17,16 +22,22 @@ public class TxTestData {

@Getter
private final List<Object[]> testData;

private final NpmPackage npm;

private TxTestData(List<Object[]> testData, JsonObject manifest, JsonObject externals) throws IOException {
private TxTestData(List<Object[]> testData, JsonObject manifest, JsonObject externals, NpmPackage npm) throws IOException {
this.testData = testData;
this.manifest = manifest;
this.externals = externals;
this.npm = npm;
}

public static TxTestData loadTestDataFromDefaultClassPath() throws IOException {
String contents = TestingUtilities.loadTestResource("tx", "test-cases.json");
String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json");
public static TxTestData loadTestDataFromPackage(String version) throws IOException {
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager.Builder().build();
NpmPackage npm = pcm.loadPackage("hl7.fhir.uv.tx-ecosystem", version);

String contents = TextFile.streamToString(npm.load("tests", "test-cases.json"));
String externalSource = TextFile.streamToString(npm.load("tests", "messages-tx.fhir.org.json"));
JsonObject externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource);

Map<String, TxTestSetup> examples = new HashMap<String, TxTestSetup>();
Expand All @@ -50,6 +61,33 @@ public static TxTestData loadTestDataFromDefaultClassPath() throws IOException {
testData.add(new Object[]{id, examples.get(id)});
}

return new TxTestData(testData, manifest, externals);
return new TxTestData(testData, manifest, externals, npm);
}

public String load(String fn) throws IOException {
return TextFile.streamToString(npm.load("tests", fn));
}

public byte[] loadBytes(String fn) throws IOException {
return TextFile.streamToBytes(npm.load("tests", fn));
}

public boolean hasFile(String filename) throws IOException {
return npm.hasFile("tests", filename);
}

public String loadVersion() throws JsonException, IOException {
return readHistory(loadBytes("history.json"));
}

private String readHistory(byte[] content) throws JsonException, IOException {
JsonObject json = JsonParser.parseObject(content);
return json.getJsonObjects("versions").get(0).asString("version");
}

public String describe() {
return npm.name()+"#"+npm.version();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.hl7.fhir.utilities.json.model.JsonArray;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.hl7.fhir.utilities.npm.NpmPackage;

public class TxTester {

Expand Down Expand Up @@ -81,10 +82,10 @@ public TxTester(ITxTesterLoader loader, String server, boolean tight, JsonObject
}

public static void main(String[] args) throws Exception {
new TxTester(new InternalTxLoader(args[0]), args[1], "true".equals(args[2]), args.length == 5 ? JsonParser.parseObjectFromFile(args[4]) : null).execute(args[2], new ArrayList<>(), args[3]);
new TxTester(new InternalTxLoader(args[0]), args[1], "true".equals(args[2]), args.length == 5 ? JsonParser.parseObjectFromFile(args[4]) : null).execute(new ArrayList<>(), args[3]);
}

public boolean execute(String version, List<String> modes, String filter) throws IOException, URISyntaxException {
public boolean execute(List<String> modes, String filter) throws IOException, URISyntaxException {
if (output == null) {
output = Utilities.path("[tmp]", serverId());
}
Expand All @@ -101,9 +102,7 @@ public boolean execute(String version, List<String> modes, String filter) throws
System.out.println(" Term Service Url: "+server);
System.out.println(" External Strings: "+(externals != null));
System.out.println(" Test Exec Modes: "+modes.toString());
if (version != null) {
System.out.println(" Tx FHIR Version: "+version);
}

if (filter != null) {
System.out.println(" Filter Parameter: "+filter);
}
Expand Down Expand Up @@ -172,11 +171,11 @@ private JsonObject loadTests() throws JsonException, IOException {
return JsonParser.parseObject(loader.loadContent("test-cases.json"));
}

private String loadVersion() throws JsonException, IOException {
public String loadVersion() throws JsonException, IOException {
if (loader.hasContent("history.json")) {
return readHistory(loader.loadContent("history.json"));
} else {
return processHistoryMarkdown(loader.loadContent("history.md"));
throw new Error("history.md is no longer supported");
}
}

Expand All @@ -185,24 +184,6 @@ private String readHistory(byte[] content) throws JsonException, IOException {
return json.getJsonObjects("versions").get(0).asString("version");
}

public static String processHistoryMarkdown(byte[] content) throws IOException {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(content));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.startsWith("## ")) {
return strLine.substring(3).trim();
}
}
} finally {
br.close();
in.close();
}
return "<1.6.0";
}

private ITerminologyClient connectToServer(List<String> modes) throws URISyntaxException, IOException {
System.out.println("Connect to "+server);
software = server;
Expand Down Expand Up @@ -580,90 +561,44 @@ public TxTester setOutput(String output) {
}

public static class InternalTxLoader implements ITxTesterLoader {

private String folder;

public InternalTxLoader(String folder) {
this.folder = folder;
}
TxTestData txtests;

public InternalTxLoader(String source, String local) throws IOException {
if (source.startsWith("http://") || source.startsWith("https://")) {
this.folder = Utilities.path(local, "source");

URL url = new URL(zipUrl(source));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream zip = connection.getInputStream();
unzip(zip);
} else {
this.folder = source;
}
public InternalTxLoader(String version) throws IOException {
load(version);
}

public void unzip(InputStream is) throws IOException {
try (ZipInputStream zipIn = new ZipInputStream(is)) {
for (ZipEntry ze; (ze = zipIn.getNextEntry()) != null; ) {
if (ze.getName().startsWith("fhir-test-cases-master/tx/")) {
Path path = Path.of(Utilities.path(this.folder, ze.getName().substring(26))).normalize();
String pathString = ManagedFileAccess.fromPath(path).getAbsolutePath();
if (!path.startsWith(Path.of(this.folder).normalize())) {
// see: https://snyk.io/research/zip-slip-vulnerability
throw new RuntimeException("Entry with an illegal path: " + ze.getName());
}
if (ze.isDirectory()) {
Utilities.createDirectory(pathString);
} else {
Utilities.createDirectory(Utilities.getDirectoryForFile(pathString));
TextFile.streamToFileNoClose(zipIn, pathString);
}
}
}
}

private void load(String version) throws IOException {
txtests = TxTestData.loadTestDataFromPackage(version);
}


private String zipUrl(String template) {
if (!template.startsWith("https://github.")) {
throw new FHIRException("Cannot refer to source by URL unless referring to a github repository: "+template);
} else if (Utilities.charCount(template, '/') == 4) {
return Utilities.pathURL(template, "archive", "master.zip");
} else if (Utilities.charCount(template, '/') == 6) {
String[] p = template.split("\\/");
return Utilities.pathURL("https://"+p[2], p[3], p[4], "archive", p[6]+".zip");
} else {
throw new FHIRException("Source syntax in URL referring to a github repository was not understood: "+template);
}
}


@Override
public String describe() {
return folder;
return txtests.describe();
}

@Override
public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
Resource res = new org.hl7.fhir.r5.formats.JsonParser().parse(ManagedFileAccess.inStream(Utilities.path(folder, filename)));
try {
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
String p = Utilities.path(folder, "r4", filename);
Utilities.createDirectory(Utilities.getDirectoryForFile(p));
new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(p), r4);
} catch (Exception e) {
// nothing...
}
Resource res = new org.hl7.fhir.r5.formats.JsonParser().parse(txtests.load(filename));
// org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
// String p = Utilities.path(folder, "r4", filename);
// Utilities.createDirectory(Utilities.getDirectoryForFile(p));
// new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(p), r4);
// } catch (Exception e) {
// // nothing...
// }
return res;
}

@Override
public byte[] loadContent(String filename) throws FileNotFoundException, IOException {
return TextFile.fileToBytes(Utilities.path(folder, filename));
return txtests.loadBytes(filename);
}

@Override
public boolean hasContent(String filename) throws IOException {
return new File(Utilities.path(folder, filename)).exists();
return txtests.hasFile(filename);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.model.Constants;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.validation.special.TxTestData;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.special.TxTester.ITxTesterLoader;
import org.junit.Test;
Expand Down Expand Up @@ -52,8 +52,10 @@ public JsonObjectPair(JsonObject suite, JsonObject test) {
@Parameters(name = "{index}: id {0}")
public static Iterable<Object[]> data() throws IOException {

String contents = TestingUtilities.loadTestResource("tx", "test-cases.json");
externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"));
txtests = TxTestData.loadTestDataFromPackage("dev");

String contents = txtests.load("test-cases.json");
externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(txtests.load("messages-tx.fhir.org.json"));

Map<String, JsonObjectPair> examples = new HashMap<String, JsonObjectPair>();
manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents);
Expand Down Expand Up @@ -82,6 +84,7 @@ public static Iterable<Object[]> data() throws IOException {
private String version = "5.0.0";
private static TxTester tester;
private List<String> modes = new ArrayList<>();
private static TxTestData txtests;

public ExternalTerminologyServiceTests(String name, JsonObjectPair setup) {
this.setup = setup;
Expand All @@ -106,7 +109,7 @@ public void test() throws Exception {
}

public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
String contents = TestingUtilities.loadTestResource("tx", filename);
String contents = txtests.load(filename);
try (InputStream inputStream = IOUtils.toInputStream(contents, Charsets.UTF_8)) {
if (filename.contains(".json")) {
if (Constants.VERSION.equals(version) || "5.0".equals(version))
Expand Down Expand Up @@ -145,11 +148,11 @@ public String describe() {

@Override
public byte[] loadContent(String filename) throws FileNotFoundException, IOException {
return TestingUtilities.loadTestResourceBytes("tx", filename);
return txtests.loadBytes(filename);
}

@Override
public boolean hasContent(String filename) throws IOException {
return TestingUtilities.findTestResource("tx", filename);
return txtests.hasFile(filename);
}
}
Loading

0 comments on commit 9cce17d

Please sign in to comment.