Add Tests, TxTests, and Compare Tasks

This commit is contained in:
dotasek 2023-06-05 13:19:51 -04:00
parent 949a27f4e6
commit 7922a9947e
7 changed files with 218 additions and 91 deletions

View File

@ -1,10 +1,7 @@
package org.hl7.fhir.validation; package org.hl7.fhir.validation;
import java.io.File;
import java.io.IOException;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -67,28 +64,17 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.terminologies.JurisdictionUtilities; import org.hl7.fhir.r5.terminologies.JurisdictionUtilities;
import org.hl7.fhir.utilities.FileFormat; import org.hl7.fhir.utilities.FileFormat;
import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.JsonException;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.hl7.fhir.utilities.settings.FhirSettings; import org.hl7.fhir.utilities.settings.FhirSettings;
import org.hl7.fhir.validation.cli.model.CliContext; import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ComparisonService;
import org.hl7.fhir.validation.cli.services.ValidationService; import org.hl7.fhir.validation.cli.services.ValidationService;
import org.hl7.fhir.validation.cli.tasks.*; import org.hl7.fhir.validation.cli.tasks.*;
import org.hl7.fhir.validation.cli.utils.Display; import org.hl7.fhir.validation.cli.utils.Display;
import org.hl7.fhir.validation.cli.utils.EngineMode;
import org.hl7.fhir.validation.cli.utils.Params; import org.hl7.fhir.validation.cli.utils.Params;
import org.hl7.fhir.validation.special.R4R5MapTester;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.special.TxTester.InternalTxLoader;
import org.hl7.fhir.validation.testexecutor.TestExecutor;
import org.hl7.fhir.validation.testexecutor.TestExecutorParams;
/** /**
* A executable class that will validate one or more FHIR resources against * A executable class that will validate one or more FHIR resources against
@ -131,7 +117,7 @@ public class ValidatorCli {
protected List<CliTask> getCliTasks() { protected List<CliTask> getCliTasks() {
return List.of( return List.of(
new CompareTask(),
new CompileTask(), new CompileTask(),
new FhirpathTask(), new FhirpathTask(),
new InstallTask(), new InstallTask(),
@ -141,6 +127,8 @@ public class ValidatorCli {
new SnapshotTask(), new SnapshotTask(),
new SpecialTask(), new SpecialTask(),
new SpreadsheetTask(), new SpreadsheetTask(),
new TestsTask(),
new TxTestsTask(),
new TransformTask(), new TransformTask(),
new VersionTask(), new VersionTask(),
defaultCliTask); defaultCliTask);
@ -163,18 +151,13 @@ public class ValidatorCli {
FileFormat.checkCharsetAndWarnIfNotUTF8(System.out); FileFormat.checkCharsetAndWarnIfNotUTF8(System.out);
if (shouldDisplayHelpToUser(args)) { if (shouldDisplayHelpToUser(args)) {
String helpTarget = Params.getParam(args, Params.HELP);
Display.displayHelpDetails("help.txt"); Display.displayHelpDetails("help.txt");
} else if (Params.hasParam(args, Params.COMPARE)) { return;
if (destinationDirectoryValid(Params.getParam(args, Params.DESTINATION))) {
doLeftRightComparison(args, cliContext, tt);
}
} else if (Params.hasParam(args, Params.TEST) || Params.hasParam(args, Params.TX_TESTS)) {
parseTestParamsAndExecute(args);
}
else {
Display.printCliArgumentsAndInfo(args);
doValidation(tt, tts, cliContext, args);
} }
readParamsAndExecuteTask(tt, tts, cliContext, args);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -232,31 +215,6 @@ public class ValidatorCli {
} }
} }
protected static void parseTestParamsAndExecute(String[] args) throws IOException, URISyntaxException {
if (Params.hasParam(args, Params.TX_TESTS)) {
final String source = Params.getParam(args, Params.SOURCE);
final String output = Params.getParam(args, Params.OUTPUT);
final String version = Params.getParam(args, Params.VERSION);
final String tx = Params.getParam(args, Params.TERMINOLOGY);
final String filter = Params.getParam(args, Params.FILTER);
boolean ok = new TxTester(new InternalTxLoader(source, output), tx).setOutput(output).execute(version, filter);
System.exit(ok ? 1 : 0);
} else {
final String testModuleParam = Params.getParam(args, Params.TEST_MODULES);
final String testClassnameFilter = Params.getParam(args, Params.TEST_NAME_FILTER);
final String testCasesDirectory = Params.getParam(args, Params.TEST);
final String txCacheDirectory = Params.getParam(args, Params.TERMINOLOGY_CACHE);
assert TestExecutorParams.isValidModuleParam(testModuleParam) : "Invalid test module param: " + testModuleParam;
final String[] moduleNamesArg = TestExecutorParams.parseModuleParam(testModuleParam);
assert TestExecutorParams.isValidClassnameFilterParam(testClassnameFilter) : "Invalid regex for test classname filter: " + testClassnameFilter;
new TestExecutor(moduleNamesArg).executeTests(testClassnameFilter, txCacheDirectory, testCasesDirectory);
System.exit(0);
}
}
private static String[] addAdditionalParamsForIpsParam(String[] args) { private static String[] addAdditionalParamsForIpsParam(String[] args) {
// ips$branch --> -version 4.0 -ig hl7.fhir.uv.ips#current$connectathon-2 -profile http://hl7.org/fhir/uv/ips/StructureDefinition/Bundle-uv-ips // ips$branch --> -version 4.0 -ig hl7.fhir.uv.ips#current$connectathon-2 -profile http://hl7.org/fhir/uv/ips/StructureDefinition/Bundle-uv-ips
List<String> res = new ArrayList<>(); List<String> res = new ArrayList<>();
@ -286,21 +244,9 @@ public class ValidatorCli {
return r; return r;
} }
private static boolean destinationDirectoryValid(String dest) {
if (dest == null) {
System.out.println("no -dest parameter provided");
return false;
} else if (!new File(dest).isDirectory()) {
System.out.println("Specified destination (-dest parameter) is not valid: \"" + dest + "\")");
return false;
} else {
System.out.println("Valid destination directory provided: \"" + dest + "\")");
return true;
}
}
private static boolean shouldDisplayHelpToUser(String[] args) { private static boolean shouldDisplayHelpToUser(String[] args) {
String helpTarget = Params.getParam(args, Params.HELP);
return (args.length == 0 return (args.length == 0
|| Params.hasParam(args, Params.HELP) || Params.hasParam(args, Params.HELP)
|| Params.hasParam(args, "?") || Params.hasParam(args, "?")
@ -308,31 +254,14 @@ public class ValidatorCli {
|| Params.hasParam(args, "/?")); || Params.hasParam(args, "/?"));
} }
private static void doLeftRightComparison(String[] args, CliContext cliContext, TimeTracker tt) throws Exception { private void readParamsAndExecuteTask(TimeTracker tt, TimeTracker.Session tts, CliContext cliContext, String[] params) throws Exception {
Display.printCliArgumentsAndInfo(args); Display.printCliParamsAndInfo(params);
if (cliContext.getSv() == null) {
cliContext.setSv(validationService.determineVersion(cliContext));
}
String v = VersionUtilities.getCurrentVersion(cliContext.getSv());
String definitions = VersionUtilities.packageForVersion(v) + "#" + v;
ValidationEngine validator = validationService.initializeValidator(cliContext, definitions, tt);
validator.loadPackage(CommonPackages.ID_PUBPACK, null);
ComparisonService.doLeftRightComparison(args, Params.getParam(args, Params.DESTINATION), validator);
}
private void doValidation(TimeTracker tt, TimeTracker.Session tts, CliContext cliContext, String[] params) throws Exception {
if (cliContext.getSv() == null) { if (cliContext.getSv() == null) {
cliContext.setSv(myValidationService.determineVersion(cliContext)); cliContext.setSv(myValidationService.determineVersion(cliContext));
} }
CliTask cliTask = null; final CliTask cliTask = selectCliTask(cliContext, params);
for(CliTask candidateTask : cliTasks) {
if (candidateTask.shouldExecuteTask(cliContext, params)) {
cliTask = candidateTask;
}
}
if (cliTask == null)
cliTask = defaultCliTask;
if (cliTask instanceof ValidationServiceTask) { if (cliTask instanceof ValidationServiceTask) {
ValidationEngine validationEngine = getValidationEngine(tt, cliContext); ValidationEngine validationEngine = getValidationEngine(tt, cliContext);
@ -345,6 +274,18 @@ public class ValidatorCli {
System.out.println("Done. " + tt.report()+". Max Memory = "+Utilities.describeSize(Runtime.getRuntime().maxMemory())); System.out.println("Done. " + tt.report()+". Max Memory = "+Utilities.describeSize(Runtime.getRuntime().maxMemory()));
} }
private CliTask selectCliTask(CliContext cliContext, String[] params) {
CliTask cliTask = null;
for(CliTask candidateTask : cliTasks) {
if (candidateTask.shouldExecuteTask(cliContext, params)) {
cliTask = candidateTask;
}
}
if (cliTask == null)
cliTask = defaultCliTask;
return cliTask;
}
private ValidationEngine getValidationEngine(TimeTracker tt, CliContext cliContext) throws Exception { private ValidationEngine getValidationEngine(TimeTracker tt, CliContext cliContext) throws Exception {
ValidationEngine validationEngine; ValidationEngine validationEngine;
System.out.println(" Locale: "+Locale.getDefault().getDisplayCountry()+"/"+Locale.getDefault().getCountry()); System.out.println(" Locale: "+Locale.getDefault().getDisplayCountry()+"/"+Locale.getDefault().getCountry());

View File

@ -9,9 +9,6 @@ import java.io.IOException;
public abstract class CliTask { public abstract class CliTask {
public abstract String getName(); public abstract String getName();
public final String getTaskParam() {
return "-" + getName();
}
public abstract boolean shouldExecuteTask(CliContext cliContext, String[] args); public abstract boolean shouldExecuteTask(CliContext cliContext, String[] args);
public abstract void printHelp(java.io.PrintStream out); public abstract void printHelp(java.io.PrintStream out);

View File

@ -0,0 +1,60 @@
package org.hl7.fhir.validation.cli.tasks;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.hl7.fhir.validation.ValidationEngine;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ComparisonService;
import org.hl7.fhir.validation.cli.services.ValidationService;
import org.hl7.fhir.validation.cli.utils.Display;
import org.hl7.fhir.validation.cli.utils.Params;
import java.io.File;
import java.io.PrintStream;
public class CompareTask extends ValidationServiceTask{
@Override
public String getName() {
return "compare";
}
@Override
public boolean shouldExecuteTask(CliContext cliContext, String[] args) {
return Params.hasParam(args, Params.COMPARE);
}
@Override
public void printHelp(PrintStream out) {
}
@Override
public void executeTask(ValidationService validationService, ValidationEngine validationEngine, CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) throws Exception {
Display.printCliParamsAndInfo(args);
if (!destinationDirectoryValid(Params.getParam(args, Params.DESTINATION))) {
return;
}
if (cliContext.getSv() == null) {
cliContext.setSv(validationService.determineVersion(cliContext));
}
String v = VersionUtilities.getCurrentVersion(cliContext.getSv());
String definitions = VersionUtilities.packageForVersion(v) + "#" + v;
ValidationEngine validator = validationService.initializeValidator(cliContext, definitions, tt);
validator.loadPackage(CommonPackages.ID_PUBPACK, null);
ComparisonService.doLeftRightComparison(args, Params.getParam(args, Params.DESTINATION), validator);
}
private boolean destinationDirectoryValid(String dest) {
if (dest == null) {
System.out.println("no -dest parameter provided");
return false;
} else if (!new File(dest).isDirectory()) {
System.out.println("Specified destination (-dest parameter) is not valid: \"" + dest + "\")");
return false;
} else {
System.out.println("Valid destination directory provided: \"" + dest + "\")");
return true;
}
}
}

View File

@ -0,0 +1,45 @@
package org.hl7.fhir.validation.cli.tasks;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.utils.Params;
import org.hl7.fhir.validation.special.R4R5MapTester;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.testexecutor.TestExecutor;
import org.hl7.fhir.validation.testexecutor.TestExecutorParams;
import java.io.File;
import java.io.PrintStream;
public class TestsTask extends StandaloneTask{
@Override
public String getName() {
return "tests";
}
@Override
public boolean shouldExecuteTask(CliContext cliContext, String[] args) {
return Params.hasParam(args, Params.TEST);
}
@Override
public void printHelp(PrintStream out) {
}
@Override
public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) throws Exception {
final String testModuleParam = Params.getParam(args, Params.TEST_MODULES);
final String testClassnameFilter = Params.getParam(args, Params.TEST_NAME_FILTER);
final String testCasesDirectory = Params.getParam(args, Params.TEST);
final String txCacheDirectory = Params.getParam(args, Params.TERMINOLOGY_CACHE);
assert TestExecutorParams.isValidModuleParam(testModuleParam) : "Invalid test module param: " + testModuleParam;
final String[] moduleNamesArg = TestExecutorParams.parseModuleParam(testModuleParam);
assert TestExecutorParams.isValidClassnameFilterParam(testClassnameFilter) : "Invalid regex for test classname filter: " + testClassnameFilter;
new TestExecutor(moduleNamesArg).executeTests(testClassnameFilter, txCacheDirectory, testCasesDirectory);
System.exit(0);
}
}

View File

@ -0,0 +1,38 @@
package org.hl7.fhir.validation.cli.tasks;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.utils.Params;
import org.hl7.fhir.validation.special.TxTester;
import org.hl7.fhir.validation.testexecutor.TestExecutor;
import org.hl7.fhir.validation.testexecutor.TestExecutorParams;
import java.io.PrintStream;
public class TxTestsTask extends StandaloneTask{
@Override
public String getName() {
return "txTests";
}
@Override
public boolean shouldExecuteTask(CliContext cliContext, String[] args) {
return Params.hasParam(args, Params.TX_TESTS);
}
@Override
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);
final String output = Params.getParam(args, Params.OUTPUT);
final String version = Params.getParam(args, Params.VERSION);
final String tx = Params.getParam(args, Params.TERMINOLOGY);
final String filter = Params.getParam(args, Params.FILTER);
boolean ok = new TxTester(new TxTester.InternalTxLoader(source, output), tx).setOutput(output).execute(version, filter);
System.exit(ok ? 1 : 0);
}
}

View File

@ -19,7 +19,7 @@ public class Display {
return Long.toString(maxMemory / (1024 * 1024)); return Long.toString(maxMemory / (1024 * 1024));
} }
public static void printCliArgumentsAndInfo(String[] args) throws IOException { public static void printCliParamsAndInfo(String[] args) throws IOException {
System.out.println(" Paths: Current = " + System.getProperty("user.dir") + ", Package Cache = " + new FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager.FilesystemPackageCacheMode.USER).getFolder()); System.out.println(" Paths: Current = " + System.getProperty("user.dir") + ", Package Cache = " + new FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager.FilesystemPackageCacheMode.USER).getFolder());
System.out.print(" Params:"); System.out.print(" Params:");
for (String s : args) { for (String s : args) {

View File

@ -1,13 +1,11 @@
package org.hl7.fhir.validation; package org.hl7.fhir.validation;
import org.apache.commons.compress.archivers.sevenz.CLI;
import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.validation.cli.model.CliContext; import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ValidationService; import org.hl7.fhir.validation.cli.services.ValidationService;
import org.hl7.fhir.validation.cli.tasks.*; import org.hl7.fhir.validation.cli.tasks.*;
import org.hl7.fhir.validation.cli.utils.Params; import org.hl7.fhir.validation.cli.utils.Params;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -31,6 +29,9 @@ public class ValidatorCliTests {
@Mock @Mock
ValidationEngine validationEngine; ValidationEngine validationEngine;
@Spy
CompareTask compareTask;
@Spy @Spy
CompileTask compileTask; CompileTask compileTask;
@ -49,6 +50,18 @@ public class ValidatorCliTests {
SnapshotTask snapshotTask; SnapshotTask snapshotTask;
@Spy @Spy
SpreadsheetTask spreadsheetTask; SpreadsheetTask spreadsheetTask;
@Spy
TestsTask testsTask = new TestsTask() {
@Override
public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) {}
};
@Spy
TxTestsTask txTestsTask = new TxTestsTask() {
@Override
public void executeTask(CliContext cliContext, String[] args, TimeTracker tt, TimeTracker.Session tts) {}
};
@Spy @Spy
TransformTask transformTask; TransformTask transformTask;
@ -76,6 +89,7 @@ public class ValidatorCliTests {
protected List<CliTask> getCliTasks() { protected List<CliTask> getCliTasks() {
return List.of( return List.of(
compareTask,
compileTask, compileTask,
fhirpathTask, fhirpathTask,
installTask, installTask,
@ -85,6 +99,8 @@ public class ValidatorCliTests {
snapshotTask, snapshotTask,
specialTask, specialTask,
spreadsheetTask, spreadsheetTask,
testsTask,
txTestsTask,
transformTask, transformTask,
versionTask, versionTask,
//validate is the default //validate is the default
@ -241,4 +257,34 @@ public class ValidatorCliTests {
Mockito.verify(specialTask).executeTask(same(cliContext), eq(args), any(TimeTracker.class), any(TimeTracker.Session.class)); Mockito.verify(specialTask).executeTask(same(cliContext), eq(args), any(TimeTracker.class), any(TimeTracker.Session.class));
} }
@Test
public void compareTest() throws Exception {
final String[] args = new String[]{"-compare"};
CliContext cliContext = Params.loadCliContext(args);
ValidatorCli cli = mockValidatorCliWithService(cliContext);
cli.readParamsAndExecuteTask(cliContext, args);
Mockito.verify(validationService).determineVersion(same(cliContext));
Mockito.verify(compareTask).executeTask(same(validationService), same(validationEngine), same(cliContext), eq(args), any(TimeTracker.class), any(TimeTracker.Session.class));
}
@Test
public void txTestsTest() throws Exception {
final String[] args = new String[]{"-txTests"};
CliContext cliContext = Params.loadCliContext(args);
ValidatorCli cli = mockValidatorCli();
cli.readParamsAndExecuteTask(cliContext, args);
Mockito.verify(txTestsTask).executeTask(same(cliContext), eq(args), any(TimeTracker.class), any(TimeTracker.Session.class));
}
@Test
public void testsTest() throws Exception {
final String[] args = new String[]{"-tests"};
CliContext cliContext = Params.loadCliContext(args);
ValidatorCli cli = mockValidatorCli();
cli.readParamsAndExecuteTask(cliContext, args);
Mockito.verify(testsTask).executeTask(same(cliContext), eq(args), any(TimeTracker.class), any(TimeTracker.Session.class));
}
} }