Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hapi-fhir-android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.google.common.annotations.VisibleForTesting;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
Expand Down Expand Up @@ -565,6 +566,14 @@ boolean validForUpload(IBaseResource theResource) {
return false;
}

if ("CodeSystem".equals(resourceType) && isEmbeddedCodeSystem(theResource)) {
return false;
}

if ("ValueSet".equals(resourceType) && isEmbeddedValueSet(theResource)) {
return false;
}

if (!isValidResourceStatusForPackageUpload(theResource)) {
ourLog.warn(
"Failed to validate resource of type {} with ID {} - Error: Resource status not accepted value.",
Expand All @@ -576,6 +585,34 @@ boolean validForUpload(IBaseResource theResource) {
return true;
}

private final List<String> embeddedCodeSystems = List.of(
CommonCodeSystemsTerminologyService.LANGUAGES_CODESYSTEM_URL,
CommonCodeSystemsTerminologyService.MIMETYPES_CODESYSTEM_URL,
CommonCodeSystemsTerminologyService.CURRENCIES_CODESYSTEM_URL,
CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL,
CommonCodeSystemsTerminologyService.UCUM_CODESYSTEM_URL,
CommonCodeSystemsTerminologyService.USPS_CODESYSTEM_URL);

private final List<String> embeddedValueSets = List.of(
CommonCodeSystemsTerminologyService.LANGUAGES_VALUESET_URL,
CommonCodeSystemsTerminologyService.MIMETYPES_VALUESET_URL,
CommonCodeSystemsTerminologyService.CURRENCIES_VALUESET_URL,
CommonCodeSystemsTerminologyService.UCUM_VALUESET_URL,
CommonCodeSystemsTerminologyService.ALL_LANGUAGES_VALUESET_URL,
CommonCodeSystemsTerminologyService.USPS_VALUESET_URL);

private boolean isEmbeddedValueSet(IBaseResource theResource) {
org.hl7.fhir.r4.model.ValueSet valueSet = myVersionCanonicalizer.valueSetToCanonical(theResource);
if (!valueSet.hasUrl()) return false;
return embeddedValueSets.contains(valueSet.getUrl());
}

private boolean isEmbeddedCodeSystem(IBaseResource theResource) {
org.hl7.fhir.r4.model.CodeSystem codeSystem = myVersionCanonicalizer.codeSystemToCanonical(theResource);
if (!codeSystem.hasUrl()) return false;
return embeddedCodeSystems.contains(codeSystem.getUrl());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommonCodeSystemsTerminologyService already contains the methods isCodeSystemSupported(theValidationSupportContext, theSystem), isValueSetSupported(theValidationSupportContext, theValueSetUrl), getValueSetUrl(theFhirContext, theValueSet) and getCodeSystemUrl(theFhirContext, theCodeSystem); they could be used to avoid duplicating code and risking desynchronizing embeddedCodeSystems and embeddedValueSets with CommonCodeSystemsTerminologyService

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch ... I'll rework it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed with 3b878ff


private boolean isValidSearchParameter(IBaseResource theResource) {
try {
org.hl7.fhir.r5.model.SearchParameter searchParameter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ca.uhn.test.util.LogbackTestExtensionAssert;
import ch.qos.logback.classic.Logger;
import jakarta.annotation.Nonnull;
import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CodeSystem;
import org.hl7.fhir.r4.model.CodeType;
Expand All @@ -34,6 +35,7 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.SearchParameter;
import org.hl7.fhir.r4.model.Subscription;
import org.hl7.fhir.r4.model.ValueSet;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.npm.PackageGenerator;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -166,7 +168,12 @@ public static Stream<Arguments> parametersIsValidForUpload() {
arguments(createDocumentReference(null), false),
arguments(createCommunication(Communication.CommunicationStatus.NOTDONE), true),
arguments(createCommunication(Communication.CommunicationStatus.NULL), false),
arguments(createCommunication(null), false));
arguments(createCommunication(null), false),
arguments(createValueSet("http://test/VS"),true),
arguments(createCodeSystem("http://test/CS"),true),
arguments(createValueSet(CommonCodeSystemsTerminologyService.LANGUAGES_VALUESET_URL),false),
arguments(createCodeSystem(CommonCodeSystemsTerminologyService.LANGUAGES_CODESYSTEM_URL),false)
);
}

@ParameterizedTest
Expand All @@ -175,20 +182,38 @@ public void testValidForUpload_WhenStatusValidationSettingIsEnabled_ValidatesRes
boolean theExpectedResultForStatusValidation) {
if (theResource.fhirType().equals("SearchParameter")) {
setupSearchParameterValidationMocksForSuccess();
when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(true);
}
when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(true);
else if (theResource.fhirType().equals("CodeSystem")) {
when(myVersionCanonicalizerMock.codeSystemToCanonical(any())).thenReturn((CodeSystem) theResource);
}
else if (theResource.fhirType().equals("ValueSet")) {
when(myVersionCanonicalizerMock.valueSetToCanonical(any())).thenReturn((ValueSet) theResource);
}
else
when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(true);

assertEquals(theExpectedResultForStatusValidation, mySvc.validForUpload(theResource));
}

@ParameterizedTest
@MethodSource(value = "parametersIsValidForUpload")
public void testValidForUpload_WhenStatusValidationSettingIsDisabled_DoesNotValidateResourceStatus(IBaseResource theResource) {
public void testValidForUpload_WhenStatusValidationSettingIsDisabled_DoesNotValidateResourceStatus(IBaseResource theResource, boolean theExpectedResultForStatusValidation) {
if (theResource.fhirType().equals("SearchParameter")) {
setupSearchParameterValidationMocksForSuccess();
when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(false);
assertTrue(mySvc.validForUpload(theResource));
}
else if (theResource.fhirType().equals("CodeSystem")) {
when(myVersionCanonicalizerMock.codeSystemToCanonical(any())).thenReturn((CodeSystem) theResource);
assertEquals(theExpectedResultForStatusValidation, mySvc.validForUpload(theResource));
}
when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(false);
//all resources should pass status validation in this case, so expect true always
assertTrue(mySvc.validForUpload(theResource));
else if (theResource.fhirType().equals("ValueSet")) {
when(myVersionCanonicalizerMock.valueSetToCanonical(any())).thenReturn((ValueSet) theResource);
assertEquals(theExpectedResultForStatusValidation, mySvc.validForUpload(theResource));
}
else
assertTrue(mySvc.validForUpload(theResource));
}

@Test
Expand Down Expand Up @@ -227,9 +252,6 @@ public void testValidForUpload_WhenSearchParameterValidatorThrowsAnExceptionOthe
}
}




@Test
public void testDontTryToInstallDuplicateCodeSystem_CodeSystemAlreadyExistsWithDifferentId() throws IOException {
// Setup
Expand All @@ -249,6 +271,9 @@ public void testDontTryToInstallDuplicateCodeSystem_CodeSystemAlreadyExistsWithD

PackageInstallationSpec spec = setupResourceInPackage(existingCs, cs, myCodeSystemDao);

when(myVersionCanonicalizerMock.codeSystemToCanonical(any())).thenReturn(cs);

when(myStorageSettings.isValidateResourceStatusForPackageUpload()).thenReturn(true);
// Test
mySvc.install(spec);

Expand Down Expand Up @@ -396,4 +421,13 @@ private static Communication createCommunication(Communication.CommunicationStat
return communication;
}

private static CodeSystem createCodeSystem(String canonicalUrl) {
return new CodeSystem().setUrl(canonicalUrl).setStatus(Enumerations.PublicationStatus.ACTIVE);
}

private static ValueSet createValueSet(String canonicalUrl) {
return new ValueSet().setUrl(canonicalUrl).setStatus(Enumerations.PublicationStatus.ACTIVE);
}


}
Loading