Merge pull request #1861 from hapifhir/2024-12-gg-tx-tests-move

move tx tests to tx-ecosystem IG
This commit is contained in:
Grahame Grieve 2024-12-17 05:19:02 +11:00 committed by GitHub
commit 9cce17d7b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 107 additions and 129 deletions

View File

@ -41,7 +41,6 @@ public class TxTestsTask extends StandaloneTask{
@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);
@ -50,7 +49,7 @@ public class TxTestsTask extends StandaloneTask{
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();
}

View File

@ -2,7 +2,12 @@ package org.hl7.fhir.validation.special;
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.*;
@ -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>();
@ -50,6 +61,33 @@ public class TxTestData {
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();
}
}

View File

@ -48,6 +48,7 @@ import org.hl7.fhir.utilities.json.JsonException;
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 {
@ -81,10 +82,10 @@ public class TxTester {
}
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());
}
@ -101,9 +102,7 @@ public class TxTester {
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);
}
@ -172,11 +171,11 @@ public class TxTester {
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");
}
}
@ -185,24 +184,6 @@ public class TxTester {
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;
@ -580,90 +561,44 @@ public class TxTester {
}
public static class InternalTxLoader implements ITxTesterLoader {
private String folder;
public InternalTxLoader(String folder) {
this.folder = folder;
TxTestData txtests;
public InternalTxLoader(String version) throws IOException {
load(version);
}
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 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);
}
}

View File

@ -21,9 +21,9 @@ import org.hl7.fhir.r5.formats.JsonParser;
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;
@ -52,8 +52,10 @@ public class ExternalTerminologyServiceTests implements ITxTesterLoader {
@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);
@ -82,6 +84,7 @@ public class ExternalTerminologyServiceTests implements ITxTesterLoader {
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;
@ -106,7 +109,7 @@ public class ExternalTerminologyServiceTests implements ITxTesterLoader {
}
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))
@ -145,11 +148,11 @@ public class ExternalTerminologyServiceTests implements ITxTesterLoader {
@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);
}
}

View File

@ -18,7 +18,6 @@ import org.hl7.fhir.r5.formats.JsonParser;
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.Utilities;
import org.hl7.fhir.utilities.VersionUtil;
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
@ -26,6 +25,7 @@ import org.hl7.fhir.utilities.json.JsonException;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.utilities.tests.TestConfig;
import org.hl7.fhir.validation.special.TxTestData;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.special.TxTester.ITxTesterLoader;
import org.hl7.fhir.validation.tests.utilities.TestUtilities;
@ -62,8 +62,10 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
@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);
@ -94,6 +96,7 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
private static TxTester tester;
private List<String> modes = new ArrayList<>();
private boolean error = false;
private static TxTestData txtests;
public LocalTerminologyServiceTests(String name, JsonObjectPair setup) {
this.setup = setup;
@ -131,7 +134,7 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
}
private String loadVersion() throws JsonException, IOException {
return TxTester.processHistoryMarkdown(TestingUtilities.loadTestResourceBytes("tx", "history.md"));
return txtests.loadVersion();
}
private void logTestSkip(String reason) {
@ -141,7 +144,7 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
}
public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
String contents = TestingUtilities.loadTestResource("tx", filename);
String contents = txtests.load(filename);
Resource res = null;
try (InputStream inputStream = IOUtils.toInputStream(contents, Charsets.UTF_8)) {
if (filename.contains(".json")) {
@ -172,10 +175,10 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
throw new FHIRException("unknown version " + version);
}
}
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
String p = Utilities.path(FhirSettings.getFhirTestCasesPath(), "tx", "r4", filename);
Utilities.createDirectory(Utilities.getDirectoryForFile(p));
new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(p), r4);
// org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
// String p = Utilities.path(FhirSettings.getFhirTestCasesPath(), "tx", "r4", filename);
// Utilities.createDirectory(Utilities.getDirectoryForFile(p));
// new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(p), r4);
return res;
}
@ -186,13 +189,11 @@ public class LocalTerminologyServiceTests implements ITxTesterLoader {
@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);
}
}

View File

@ -18,12 +18,12 @@ import org.hl7.fhir.r5.formats.JsonParser;
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.Utilities;
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.utilities.tests.TestConfig;
import org.hl7.fhir.validation.special.TxTestData;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.special.TxTester.ITxTesterLoader;
import org.hl7.fhir.validation.tests.utilities.TestUtilities;
@ -59,8 +59,10 @@ public class OntoserverTests implements ITxTesterLoader {
@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-ontoserver.csiro.au.json"));
txtests = TxTestData.loadTestDataFromPackage("dev");
String contents = txtests.load("test-cases.json");
externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(txtests.load("messages-ontoserver.csiro.au.json"));
Map<String, JsonObjectPair> examples = new HashMap<String, JsonObjectPair>();
manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents);
@ -94,6 +96,7 @@ public class OntoserverTests implements ITxTesterLoader {
private String version = "5.0.0";
private static TxTester tester;
private List<String> modes = new ArrayList<>();
private static TxTestData txtests;
public OntoserverTests(String name, JsonObjectPair setup) {
this.setup = setup;
@ -127,7 +130,7 @@ public class OntoserverTests implements ITxTesterLoader {
}
public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
String contents = TestingUtilities.loadTestResource("tx", filename);
String contents = txtests.load(filename);
Resource res = null;
try (InputStream inputStream = IOUtils.toInputStream(contents, Charsets.UTF_8)) {
if (filename.contains(".json")) {
@ -158,10 +161,10 @@ public class OntoserverTests implements ITxTesterLoader {
throw new FHIRException("unknown version " + version);
}
}
org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
String p = Utilities.path(FhirSettings.getFhirTestCasesPath(), "tx", "r4", filename);
Utilities.createDirectory(Utilities.getDirectoryForFile(p));
new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(p), r4);
// org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(res);
// String p = Utilities.path(FhirSettings.getFhirTestCasesPath(), "tx", "r4", filename);
// Utilities.createDirectory(Utilities.getDirectoryForFile(p));
// new org.hl7.fhir.r4.formats.JsonParser().compose(ManagedFileAccess.outStream(p), r4);
return res;
}
@ -172,11 +175,11 @@ public class OntoserverTests implements ITxTesterLoader {
@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);
}
}

View File

@ -27,7 +27,6 @@ import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionParameterComponent;
import org.hl7.fhir.r5.terminologies.expansion.ValueSetExpansionOutcome;
import org.hl7.fhir.r5.test.utils.CompareUtilities;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.FhirPublication;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -60,7 +59,7 @@ private static TxTestData testData;
@Parameters(name = "{index}: id {0}")
public static Iterable<Object[]> data() throws IOException {
testData = TxTestData.loadTestDataFromDefaultClassPath();
testData = TxTestData.loadTestDataFromPackage("dev");
return testData.getTestData();
}
@ -95,7 +94,7 @@ private static TxTestData testData;
String reqFile = setup.getTest().asString("request");
Resource req = reqFile == null ? null : loadResource(reqFile);
String fn = setup.getTest().has("response:tx.fhir.org") ? setup.getTest().asString("response:tx.fhir.org") : setup.getTest().asString("response");
String resp = TestingUtilities.loadTestResource("tx", fn);
String resp = testData.load(fn);
String fp = Utilities.path("[tmp]", "tx", fn);
JsonObject ext = testData.getExternals() == null ? null : testData.getExternals().getJsonObject(fn);
File fo = ManagedFileAccess.file(fp);
@ -211,7 +210,7 @@ private static TxTestData testData;
public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
String contents = TestingUtilities.loadTestResource("tx", filename);
String contents = testData.load(filename);
try (InputStream inputStream = IOUtils.toInputStream(contents, Charsets.UTF_8)) {
if (filename.contains(".json")) {
if (Constants.VERSION.equals(version) || "5.0".equals(version))