Skip to content

Commit

Permalink
rework txTester to support additional test files, and better R4/R5 lo…
Browse files Browse the repository at this point in the history
…gging
  • Loading branch information
Grahame Grieve committed Jan 12, 2025
1 parent 0cee7d4 commit 6b52ee5
Show file tree
Hide file tree
Showing 17 changed files with 458 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,10 @@ public Parameters translate(Parameters params) throws FHIRException {
return (Parameters) VersionConvertorFactory_10_50.convertResource(client.translate((org.hl7.fhir.dstu2.model.Parameters) VersionConvertorFactory_10_50.convertResource(params)));
}

@Override
public void setConversionLogger(ITerminologyConversionLogger logger) {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,10 @@ public Parameters translate(Parameters params) throws FHIRException {
return (Parameters) VersionConvertorFactory_30_50.convertResource(client.transform((org.hl7.fhir.dstu3.model.Parameters) VersionConvertorFactory_30_50.convertResource(params)));
}

@Override
public void setConversionLogger(ITerminologyConversionLogger logger) {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.utils.client.EFhirClientException;
import org.hl7.fhir.r4.utils.client.FHIRToolingClient;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.JsonParser;
import org.hl7.fhir.r5.model.Bundle;
import org.hl7.fhir.r5.model.CanonicalResource;
import org.hl7.fhir.r5.model.CapabilityStatement;
Expand All @@ -28,9 +30,10 @@

public class TerminologyClientR4 implements ITerminologyClient {

private final FHIRToolingClient client; // todo: use the R2 client
private final FHIRToolingClient client;
private ClientHeaders clientHeaders;
private String id;
private ITerminologyConversionLogger logger;

public TerminologyClientR4(String id, String address, String userAgent) throws URISyntaxException {
this.client = new FHIRToolingClient(address, userAgent);
Expand Down Expand Up @@ -71,7 +74,7 @@ public FhirPublication getActualVersion() {

@Override
public TerminologyCapabilities getTerminologyCapabilities() throws FHIRException {
return (TerminologyCapabilities) VersionConvertorFactory_40_50.convertResource(client.getTerminologyCapabilities());
return (TerminologyCapabilities) convertResource("getTerminologyCapabilities.response", client.getTerminologyCapabilities());
}

@Override
Expand All @@ -81,29 +84,30 @@ public String getAddress() {

@Override
public ValueSet expandValueset(ValueSet vs, Parameters p) throws FHIRException {
org.hl7.fhir.r4.model.ValueSet vs2 = vs == null ? null : (org.hl7.fhir.r4.model.ValueSet) VersionConvertorFactory_40_50.convertResource(vs);
org.hl7.fhir.r4.model.Parameters p2 = p == null ? null : (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(p);
org.hl7.fhir.r4.model.ValueSet vs2 = vs == null ? null : (org.hl7.fhir.r4.model.ValueSet) convertResource("expandValueset.valueset", vs);
org.hl7.fhir.r4.model.Parameters p2 = p == null ? null : (org.hl7.fhir.r4.model.Parameters) convertResource("expandValueset.parameters", p);
try {
vs2 = client.expandValueset(vs2, p2); // todo: second parameter
return (ValueSet) VersionConvertorFactory_40_50.convertResource(vs2);
return (ValueSet) convertResource("expandValueset.response", vs2);
} catch (org.hl7.fhir.r4.utils.client.EFhirClientException e) {
if (e.getServerErrors().size() > 0) {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), (org.hl7.fhir.r5.model.OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0)));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), (org.hl7.fhir.r5.model.OperationOutcome) convertResource("expandValueset.error", e.getServerErrors().get(0)));
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage());
}
}
}


@Override
public Parameters validateCS(Parameters pin) throws FHIRException {
try {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) convertResource("validateCS.request", pin);
p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
return (Parameters) convertResource("validateCS.response", p2);
} catch (EFhirClientException e) {
if (e.getServerErrors().size() == 1) {
OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0));
OperationOutcome op = (OperationOutcome) convertResource("validateCS.error", e.getServerErrors().get(0));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e);
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e);
Expand All @@ -117,12 +121,12 @@ public Parameters validateCS(Parameters pin) throws FHIRException {
@Override
public Parameters subsumes(Parameters pin) throws FHIRException {
try {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) convertResource("subsumes.request", pin);
p2 = client.operateType(org.hl7.fhir.r4.model.CodeSystem.class, "subsumes", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
return (Parameters) convertResource("subsumes.response", p2);
} catch (EFhirClientException e) {
if (e.getServerErrors().size() == 1) {
OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0));
OperationOutcome op = (OperationOutcome) convertResource("subsumes.error", e.getServerErrors().get(0));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e);
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e);
Expand All @@ -135,12 +139,12 @@ public Parameters subsumes(Parameters pin) throws FHIRException {
@Override
public Parameters validateVS(Parameters pin) throws FHIRException {
try {
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(pin);
org.hl7.fhir.r4.model.Parameters p2 = (org.hl7.fhir.r4.model.Parameters) convertResource("validateVS.request", pin);
p2 = client.operateType(org.hl7.fhir.r4.model.ValueSet.class, "validate-code", p2);
return (Parameters) VersionConvertorFactory_40_50.convertResource(p2);
return (Parameters) convertResource("validateVS.response", p2);
} catch (EFhirClientException e) {
if (e.getServerErrors().size() == 1) {
OperationOutcome op = (OperationOutcome) VersionConvertorFactory_40_50.convertResource(e.getServerErrors().get(0));
OperationOutcome op = (OperationOutcome) convertResource("validateVS.error", e.getServerErrors().get(0));
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), op, e);
} else {
throw new org.hl7.fhir.r5.utils.client.EFhirClientException(e.getCode(), e.getMessage(), e);
Expand Down Expand Up @@ -175,22 +179,22 @@ public ITerminologyClient setRetryCount(int retryCount) throws FHIRException {

@Override
public CapabilityStatement getCapabilitiesStatementQuick() throws FHIRException {
return (CapabilityStatement) VersionConvertorFactory_40_50.convertResource(client.getCapabilitiesStatementQuick());
return (CapabilityStatement) convertResource("getCapabilitiesStatementQuick.response", client.getCapabilitiesStatementQuick());
}

@Override
public CapabilityStatement getCapabilitiesStatement() throws FHIRException {
return (CapabilityStatement) VersionConvertorFactory_40_50.convertResource(client.getCapabilitiesStatement());
return (CapabilityStatement) convertResource("getCapabilitiesStatement.response", client.getCapabilitiesStatement());
}

@Override
public Parameters lookupCode(Map<String, String> params) throws FHIRException {
return (Parameters) VersionConvertorFactory_40_50.convertResource(client.lookupCode(params));
return (Parameters) convertResource("lookupCode.response", client.lookupCode(params));
}

@Override
public Parameters lookupCode(Parameters params) throws FHIRException {
return (Parameters) VersionConvertorFactory_40_50.convertResource(client.lookupCode((org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(params)));
return (Parameters) convertResource("lookupCode.response", client.lookupCode((org.hl7.fhir.r4.model.Parameters) convertResource("lookupCode.request", params)));
}

@Override
Expand All @@ -200,8 +204,8 @@ public int getRetryCount() throws FHIRException {

@Override
public Bundle validateBatch(Bundle batch) {
org.hl7.fhir.r4.model.Bundle result = client.transaction((org.hl7.fhir.r4.model.Bundle) VersionConvertorFactory_40_50.convertResource(batch));
return result == null ? null : (Bundle) VersionConvertorFactory_40_50.convertResource(result);
org.hl7.fhir.r4.model.Bundle result = client.transaction((org.hl7.fhir.r4.model.Bundle) convertResource("validateBatch.request", batch));
return result == null ? null : (Bundle) convertResource("validateBatch.response", result);
}

@Override
Expand All @@ -216,7 +220,7 @@ public CanonicalResource read(String type, String id) {
if (r4 == null) {
throw new FHIRException("Unable to fetch resource " + Utilities.pathURL(getAddress(), type, id));
}
org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4);
org.hl7.fhir.r5.model.Resource r5 = convertResource("read.result", r4);
if (r5 == null) {
throw new FHIRException("Unable to convert resource " + Utilities.pathURL(getAddress(), type, id) + " to R5 (internal representation)");
}
Expand Down Expand Up @@ -278,12 +282,55 @@ public int getUseCount() {
@Override
public Bundle search(String type, String criteria) {
org.hl7.fhir.r4.model.Bundle result = client.search(type, criteria);
return result == null ? null : (Bundle) VersionConvertorFactory_40_50.convertResource(result);
return result == null ? null : (Bundle) convertResource("search.result", result);
}

@Override
public Parameters translate(Parameters params) throws FHIRException {
return (Parameters) VersionConvertorFactory_40_50.convertResource(client.translate((org.hl7.fhir.r4.model.Parameters) VersionConvertorFactory_40_50.convertResource(params)));
return (Parameters) convertResource("translate.response", client.translate((org.hl7.fhir.r4.model.Parameters) convertResource("translate.request", params)));
}

private org.hl7.fhir.r4.model.Resource convertResource(String name, org.hl7.fhir.r5.model.Resource resource) {
if (logger != null) {
try {
logger.log(name, resource.fhirType(), "r5", new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).composeBytes(resource));
} catch (IOException e) {
throw new FHIRException(e);
}
}
org.hl7.fhir.r4.model.Resource res = VersionConvertorFactory_40_50.convertResource(resource);
if (logger != null) {
try {
logger.log(name, resource.fhirType(), "r4", new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).composeBytes(res));
} catch (IOException e) {
throw new FHIRException(e);
}
}
return res;
}

private org.hl7.fhir.r5.model.Resource convertResource(String name, org.hl7.fhir.r4.model.Resource resource) {
if (logger != null && name != null) {
try {
logger.log(name, resource.fhirType(), "r4", new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(org.hl7.fhir.r4.formats.IParser.OutputStyle.PRETTY).composeBytes(resource));
} catch (IOException e) {
throw new FHIRException(e);
}
}
org.hl7.fhir.r5.model.Resource res = VersionConvertorFactory_40_50.convertResource(resource);
if (logger != null && name != null) {
try {
logger.log(name, resource.fhirType(), "r5", new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).composeBytes(res));
} catch (IOException e) {
throw new FHIRException(e);
}
}
return res;
}

@Override
public void setConversionLogger(ITerminologyConversionLogger logger) {
this.logger = logger;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ public interface ITerminologyClient {
String getUserAgent();
int getUseCount();
Bundle search(String type, String criteria);

// internal conversion logging
public interface ITerminologyConversionLogger {
void log(String name, String resourceType, String version, byte[] cnt);
}
void setConversionLogger(ITerminologyConversionLogger logger);
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,11 @@ public Parameters translate(Parameters params) throws FHIRException {
return client.translate(params);
}

@Override
public void setConversionLogger(ITerminologyConversionLogger logger) {
// TODO Auto-generated method stub

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ public static File createDirectory(String path) throws IOException {
return ManagedFileAccess.file(path);
}

public static File createDirectoryNC(String path) throws IOException {
ManagedFileAccess.file(path).mkdirs();
return ManagedFileAccess.file(path);
}

public static String changeFileExt(String name, String ext) {
if (name.lastIndexOf('.') > -1)
return name.substring(0, name.lastIndexOf('.')) + ext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public JsonObject add(String name, JsonElement value) throws JsonException {
propMap.put(name, p);
return this;
}

public JsonObject add(int index, String name, JsonElement value) throws JsonException {
check(name != null, "Json Property Name is null");
check(value != null, "Json Property Value is null");
if (get(name) != null) {
check(false, "Name '"+name+"' already exists (value = "+get(name).toString()+")");
}
JsonProperty p = new JsonProperty(name, value);
properties.add(index, p);
propMap.put(name, p);
return this;
}

public JsonObject addIfNotNull(String name, JsonElement value) throws JsonException {
if (value != null) {
Expand Down Expand Up @@ -347,6 +359,16 @@ public JsonArray forceArray(String name) throws JsonException {
}
return getJsonArray(name);
}

public JsonArray forceArray(int index, String name) throws JsonException {
if (has(name) && !hasArray(name)) {
remove(name);
}
if (!has(name)) {
add(index, name, new JsonArray());
}
return getJsonArray(name);
}

public List<JsonObject> getJsonObjects(String name) {
List<JsonObject> res = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
import org.hl7.fhir.validation.ValidationEngine;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ValidationService;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void executeTask(ValidationService validationService, ValidationEngine va
String pid = cliContext.getPackageName();
boolean json = cliContext.getFormat() != FhirFormat.XML;
String output = cliContext.getOutput();
File f = new File(output);
File f = ManagedFileAccess.file(output);
ExpansionPackageGeneratorOutputType t = ExpansionPackageGeneratorOutputType.FOLDER;
if (f.exists() && f.isDirectory()) {
t = ExpansionPackageGeneratorOutputType.FOLDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, Ti
if (output == null ) {
output = Utilities.path("[tmp]");
}
boolean ok = new TxTester(new TxTester.InternalTxLoader(version), tx, false, loadExternals(externals)).setOutput(output).execute(cliContext.getModeParams(), filter);
TxTester txTester = new TxTester(new TxTester.InternalTxLoader(version), tx, false, loadExternals(externals));
for (String input : cliContext.getInputs()) {
txTester.addLoader(new TxTester.InternalTxLoader(input, true));
}
boolean ok = txTester.setOutput(output).execute(cliContext.getModeParams(), filter);
SystemExitManager.setError(ok ? 0 : 1);
SystemExitManager.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ else if (args[i].equals(HTML_OUTPUT)) {
cliContext.setOutputStyle(args[++i]);
} else if (args[i].equals(ADVSIOR_FILE)) {
cliContext.setAdvisorFile(args[++i]);
File f = new File(cliContext.getAdvisorFile());
File f = ManagedFileAccess.file(cliContext.getAdvisorFile());
if (!f.exists()) {
throw new Error("Cannot find advisor file "+cliContext.getAdvisorFile());
} else if (!Utilities.existsInList(Utilities.getFileExtension(f.getName()), "json", "txt")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hl7.fhir.utilities.FhirPublication;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
import org.hl7.fhir.utilities.i18n.I18nConstants;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.validation.ValidationOptions;
Expand Down Expand Up @@ -174,7 +175,7 @@ public static String getDiffForValidation(String id, IWorkerContext context, Str
parameters.addParameter(validationResult.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s));
}
}
if (validationResult.getIssues().size() > 0) {
if (validationResult.getIssues().size() > 0) {
operationOutcome = new OperationOutcome();
operationOutcome.getIssue().addAll(validationResult.getIssues());
parameters.addParameter().setName("issues").setResource(operationOutcome);
Expand Down Expand Up @@ -205,12 +206,12 @@ public static void writeDiffToFileSystem(String testName, String expected, Strin
}
String fullExpected = rootDirectory + "/expected/";
String fullActual = rootDirectory + "/actual/";
File expectedDirectory = new File(fullExpected);
File expectedDirectory = ManagedFileAccess.file(fullExpected);
if (!expectedDirectory.exists()) {
expectedDirectory.mkdirs();
}

File actualDirectory = new File(fullActual);
File actualDirectory = ManagedFileAccess.file(fullActual);
if (!actualDirectory.exists()) {
actualDirectory.mkdirs();
}
Expand Down
Loading

0 comments on commit 6b52ee5

Please sign in to comment.