From f1096a8ebdc200092fe144a2f115975e2d6149e2 Mon Sep 17 00:00:00 2001 From: Dylan Hall Date: Tue, 20 Feb 2024 17:06:55 -0500 Subject: [PATCH] Introduce new validator cliContext option disableDefaultResourceFetcher (#1526) * introduce new cliContext option doNotFetchUnknownProfiles * rename parameter to disable-default-resource-fetcher * missed renaming a couple items * review feedback --- .../fhir/validation/cli/model/CliContext.java | 13 +++++ .../cli/services/ValidationService.java | 8 ++-- .../hl7/fhir/validation/cli/utils/Params.java | 3 ++ .../cli/services/ValidationServiceTest.java | 48 ++++++++++++++----- 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java index 445e8a59b..8b987f826 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java @@ -56,6 +56,8 @@ public class CliContext { private HtmlInMarkdownCheck htmlInMarkdownCheck = HtmlInMarkdownCheck.WARNING; @JsonProperty("allowDoubleQuotesInFHIRPath") private boolean allowDoubleQuotesInFHIRPath = false; + @JsonProperty("disableDefaultResourceFetcher") + private boolean disableDefaultResourceFetcher = false; @JsonProperty("checkIPSCodes") private boolean checkIPSCodes; @JsonProperty("langTransform") @@ -328,6 +330,17 @@ public class CliContext { this.allowDoubleQuotesInFHIRPath = allowDoubleQuotesInFHIRPath; } + @JsonProperty("disableDefaultResourceFetcher") + public boolean isDisableDefaultResourceFetcher() { + return disableDefaultResourceFetcher; + } + + @JsonProperty("disableDefaultResourceFetcher") + public CliContext setDisableDefaultResourceFetcher(boolean disableDefaultResourceFetcher) { + this.disableDefaultResourceFetcher = disableDefaultResourceFetcher; + return this; + } + @JsonProperty("checkIPSCodes") public boolean isCheckIPSCodes() { return checkIPSCodes; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index deb7717aa..25cdc889b 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -524,9 +524,11 @@ public class ValidationService { validationEngine.setForPublication(cliContext.isForPublication()); validationEngine.setShowTimes(cliContext.isShowTimes()); validationEngine.setAllowExampleUrls(cliContext.isAllowExampleUrls()); - StandAloneValidatorFetcher fetcher = new StandAloneValidatorFetcher(validationEngine.getPcm(), validationEngine.getContext(), validationEngine); - validationEngine.setFetcher(fetcher); - validationEngine.getContext().setLocator(fetcher); + if (!cliContext.isDisableDefaultResourceFetcher()) { + StandAloneValidatorFetcher fetcher = new StandAloneValidatorFetcher(validationEngine.getPcm(), validationEngine.getContext(), validationEngine); + validationEngine.setFetcher(fetcher); + validationEngine.getContext().setLocator(fetcher); + } validationEngine.getBundleValidationRules().addAll(cliContext.getBundleValidationRules()); validationEngine.setJurisdiction(CodeSystemUtilities.readCoding(cliContext.getJurisdiction())); TerminologyCache.setNoCaching(cliContext.isNoInternalCaching()); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java index cd70055f4..52edd4542 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Params.java @@ -89,6 +89,7 @@ public class Params { public static final String SRC_LANG = "-src-lang"; public static final String TGT_LANG = "-tgt-lang"; public static final String ALLOW_DOUBLE_QUOTES = "-allow-double-quotes-in-fhirpath"; + public static final String DISABLE_DEFAULT_RESOURCE_FETCHER = "-disable-default-resource-fetcher"; public static final String CHECK_IPS_CODES = "-check-ips-codes"; public static final String BEST_PRACTICE = "-best-practice"; @@ -270,6 +271,8 @@ public class Params { cliContext.setNoExtensibleBindingMessages(true); } else if (args[i].equals(ALLOW_DOUBLE_QUOTES)) { cliContext.setAllowDoubleQuotesInFHIRPath(true); + } else if (args[i].equals(DISABLE_DEFAULT_RESOURCE_FETCHER)) { + cliContext.setDisableDefaultResourceFetcher(true); } else if (args[i].equals(CHECK_IPS_CODES)) { cliContext.setCheckIPSCodes(true); } else if (args[i].equals(NO_UNICODE_BIDI_CONTROL_CHARS)) { diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java index 945c9b537..5346d78e8 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java @@ -10,8 +10,10 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.endsWith; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.ArgumentMatchers.notNull; import static org.mockito.ArgumentMatchers.startsWith; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -236,17 +238,43 @@ class ValidationServiceTest { */ @Test public void buildValidationEngineTest() throws IOException, URISyntaxException { - - final org.hl7.fhir.utilities.TimeTracker timeTracker = mock(org.hl7.fhir.utilities.TimeTracker.class); - + final TimeTracker timeTracker = mock(TimeTracker.class); final SimpleWorkerContext workerContext = mock(SimpleWorkerContext.class); - final ValidationEngine validationEngine = mock(ValidationEngine.class); - when(validationEngine.getContext()).thenReturn(workerContext); + final ValidationEngine mockValidationEngine = mock(ValidationEngine.class); + when(mockValidationEngine.getContext()).thenReturn(workerContext); - final ValidationEngine.ValidationEngineBuilder validationEngineBuilder = mock(ValidationEngine.ValidationEngineBuilder.class);; + final ValidationEngine.ValidationEngineBuilder mockValidationEngineBuilder = mock(ValidationEngine.ValidationEngineBuilder.class);; + final ValidationService validationService = createFakeValidationService(mockValidationEngineBuilder, mockValidationEngine); - final ValidationService validationService = new ValidationService() { + CliContext cliContext = new CliContext(); + validationService.buildValidationEngine(cliContext, null, timeTracker); + + verify(mockValidationEngine).setFetcher(notNull()); + verify(mockValidationEngineBuilder).withUserAgent(eq("fhir/validator/" + VersionUtil.getVersion())); + } + + @Test + public void buildValidationEngineDisableDefaultResourceFetcherTest() throws IOException, URISyntaxException { + final TimeTracker timeTracker = mock(TimeTracker.class); + final SimpleWorkerContext workerContext = mock(SimpleWorkerContext.class); + + final ValidationEngine mockValidationEngine = mock(ValidationEngine.class); + when(mockValidationEngine.getContext()).thenReturn(workerContext); + + final ValidationEngine.ValidationEngineBuilder mockValidationEngineBuilder = mock(ValidationEngine.ValidationEngineBuilder.class);; + final ValidationService validationService = createFakeValidationService(mockValidationEngineBuilder, mockValidationEngine); + + CliContext cliContext = new CliContext(); + cliContext.setDisableDefaultResourceFetcher(true); + validationService.buildValidationEngine(cliContext, null, timeTracker); + + verify(mockValidationEngine, never()).setFetcher(any()); + verify(mockValidationEngineBuilder).withUserAgent(eq("fhir/validator/" + VersionUtil.getVersion())); + } + + private static ValidationService createFakeValidationService(ValidationEngine.ValidationEngineBuilder validationEngineBuilder, ValidationEngine validationEngine) { + return new ValidationService() { @Override protected ValidationEngine.ValidationEngineBuilder getValidationEngineBuilder() { when(validationEngineBuilder.withTHO(anyBoolean())).thenReturn(validationEngineBuilder); @@ -268,11 +296,5 @@ class ValidationServiceTest { //Don't care. Do nothing. } }; - - CliContext cliContext = new CliContext(); - - validationService.buildValidationEngine(cliContext, null, timeTracker); - - verify(validationEngineBuilder).withUserAgent(eq("fhir/validator/" + VersionUtil.getVersion())); } } \ No newline at end of file