Skip to content

Commit

Permalink
rename parameter to disable-default-resource-fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dehall committed Jan 16, 2024
1 parent 8bfba3d commit 80bf591
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<FileInfo> 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));
Expand All @@ -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);
Expand All @@ -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()));
}
}

0 comments on commit 80bf591

Please sign in to comment.