From 80bf591c3524006956b89b9e2e178fc9b30ba29d Mon Sep 17 00:00:00 2001 From: Dylan Hall Date: Tue, 16 Jan 2024 16:26:41 -0500 Subject: [PATCH] rename parameter to disable-default-resource-fetcher --- .../fhir/validation/cli/model/CliContext.java | 16 +-- .../cli/services/ValidationService.java | 2 +- .../hl7/fhir/validation/cli/utils/Params.java | 6 +- .../cli/services/ValidationServiceTest.java | 107 ++++++------------ 4 files changed, 45 insertions(+), 86 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 24a862c9c3..ac03d4f353 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,8 +56,8 @@ public class CliContext { private HtmlInMarkdownCheck htmlInMarkdownCheck = HtmlInMarkdownCheck.WARNING; @JsonProperty("allowDoubleQuotesInFHIRPath") private boolean allowDoubleQuotesInFHIRPath = false; - @JsonProperty("doNotFetchUnknownProfiles") - private boolean doNotFetchUnknownProfiles = false; + @JsonProperty("disableDefaultResourceFetcher") + private boolean disableDefaultResourceFetcher = false; @JsonProperty("checkIPSCodes") private boolean checkIPSCodes; @JsonProperty("langTransform") @@ -330,14 +330,14 @@ public void setAllowDoubleQuotesInFHIRPath(boolean allowDoubleQuotesInFHIRPath) this.allowDoubleQuotesInFHIRPath = allowDoubleQuotesInFHIRPath; } - @JsonProperty("doNotFetchUnknownProfiles") - public boolean isDoNotFetchUnknownProfiles() { - return doNotFetchUnknownProfiles; + @JsonProperty("disableDefaultResourceFetcher") + public boolean isDisableDefaultResourceFetcher() { + return disableDefaultResourceFetcher; } - @JsonProperty("doNotFetchUnknownProfiles") - public void setDoNotFetchUnknownProfiles(boolean doNotFetchUnknownProfiles) { - this.doNotFetchUnknownProfiles = doNotFetchUnknownProfiles; + @JsonProperty("disableDefaultResourceFetcher") + public void setDisableDefaultResourceFetcher(boolean doNotFetchUnknownProfiles) { + this.disableDefaultResourceFetcher = doNotFetchUnknownProfiles; } @JsonProperty("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 e0241113e2..7d75435fcb 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 @@ -495,7 +495,7 @@ protected ValidationEngine buildValidationEngine( CliContext cliContext, String validationEngine.setForPublication(cliContext.isForPublication()); validationEngine.setShowTimes(cliContext.isShowTimes()); validationEngine.setAllowExampleUrls(cliContext.isAllowExampleUrls()); - if (!cliContext.isDoNotFetchUnknownProfiles()) { + if (!cliContext.isDisableDefaultResourceFetcher()) { StandAloneValidatorFetcher fetcher = new StandAloneValidatorFetcher(validationEngine.getPcm(), validationEngine.getContext(), validationEngine); validationEngine.setFetcher(fetcher); validationEngine.getContext().setLocator(fetcher); 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 ae0523ef04..52edd45423 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,7 +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 DO_NOT_FETCH_UNKNOWN_PROFILES = "-do-not-fetch-unknown-profiles"; + 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"; @@ -271,8 +271,8 @@ else if (args[i].equals(HTML_OUTPUT)) { cliContext.setNoExtensibleBindingMessages(true); } else if (args[i].equals(ALLOW_DOUBLE_QUOTES)) { cliContext.setAllowDoubleQuotesInFHIRPath(true); - } else if (args[i].equals(DO_NOT_FETCH_UNKNOWN_PROFILES)) { - cliContext.setDoNotFetchUnknownProfiles(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 cb7c969beb..df844ada27 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.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; @@ -29,18 +31,13 @@ import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.model.StructureDefinition; import org.hl7.fhir.r5.test.utils.TestingUtilities; -import org.hl7.fhir.r5.utils.validation.constants.BestPracticeWarningLevel; import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.VersionUtil; -import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.settings.FhirSettings; -import org.hl7.fhir.utilities.validation.ValidationMessage; -import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity; import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.cli.model.CliContext; import org.hl7.fhir.validation.cli.model.FileInfo; import org.hl7.fhir.validation.cli.model.ValidationRequest; -import org.hl7.fhir.validation.cli.model.ValidationResponse; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -223,64 +220,6 @@ public void generateSnapshotMultipleSource() throws Exception { } - @Test - @DisplayName("Test that CliContext.doNotFetchUnknownProfiles is handled correctly") - public void testDoNotFetchUnknownProfiles() throws Exception { - TestingUtilities.injectCorePackageLoader(); - ValidationService myService = new ValidationService(); - - // a Patient resource profiled with http://hl7.org/fhir/us/mcode/StructureDefinition/mcode-cancer-patient - // https://github.com/FHIR/fhir-test-cases/blob/master/validator/mcode-pat.json - String resource = TestingUtilities.loadTestResource("validator", "mcode-pat.json"); - - List filesToValidate = new ArrayList<>(); - filesToValidate.add(new FileInfo().setFileName("test_resource.json") - .setFileContent(resource) - .setFileType(Manager.FhirFormat.JSON.getExtension())); - CliContext cliContext = new CliContext().setTxServer(null) - .setSv("4.0.1") - .setBestPracticeLevel(BestPracticeWarningLevel.Ignore); - - ValidationRequest request = new ValidationRequest().setCliContext(cliContext).setFilesToValidate(filesToValidate); - ValidationResponse response = myService.validateSources(request); - - // Scenario 1: default behavior, doNotFetchUnknownProfiles is "false" - // -- this resource validates with no warnings or errors - response.getOutcomes().get(0).getMessages().forEach(m -> { - if (m.getLevel() == IssueSeverity.WARNING || m.getLevel() == IssueSeverity.ERROR) { - fail(); - } - }); - - // Scenario 2: doNotFetchUnknownProfiles is "true" - // -- now there is a warning that the profile was not fetched - cliContext.setDoNotFetchUnknownProfiles(true); - response = myService.validateSources(request); - - boolean found = false; - for (ValidationMessage m : response.getOutcomes().get(0).getMessages()) { - if (m.getLevel() == IssueSeverity.WARNING && - m.getMessageId() == I18nConstants.VALIDATION_VAL_PROFILE_UNKNOWN_NOT_POLICY) { - // "Profile reference '...' has not been checked because it could not be found, - // and the validator is set to not fetch unknown profiles" - found = true; - break; - } - } - Assertions.assertTrue(found); - - // Scenario 3: doNotFetchUnknownProfiles is "false" and the relevant IG is added to context - // -- this resource again validates with no warnings or errors - cliContext.addIg("hl7.fhir.us.mcode#1.0.0"); - response = myService.validateSources(request); - - response.getOutcomes().get(0).getMessages().forEach(m -> { - if (m.getLevel() == IssueSeverity.WARNING || m.getLevel() == IssueSeverity.ERROR) { - fail(); - } - }); - } - private CliContext getCliContextSingleSource() { CliContext cliContext; cliContext = new CliContext().setSources(Arrays.asList(DUMMY_SOURCE)); @@ -299,17 +238,43 @@ private CliContext getCliContextMultipleSource() { */ @Test public void buildValidationEngineTest() throws IOException, URISyntaxException { + final TimeTracker timeTracker = mock(TimeTracker.class); + final SimpleWorkerContext workerContext = mock(SimpleWorkerContext.class); - final org.hl7.fhir.utilities.TimeTracker timeTracker = mock(org.hl7.fhir.utilities.TimeTracker.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(); + validationService.buildValidationEngine(cliContext, null, timeTracker); + + verify(mockValidationEngine).setFetcher(notNull()); + verify(mockValidationEngineBuilder).withUserAgent(eq("fhir/validator/" + VersionUtil.getVersion())); + } + + @Test + public void buildValidationEngineDoNotFetchUnknownProfilesTest() throws IOException, URISyntaxException { + 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(); + 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); @@ -331,11 +296,5 @@ protected void loadIgsAndExtensions(ValidationEngine validationEngine, CliContex //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