Refactor `CommonCodeSystemTerminologyService` method `validateCode` (#5631)

* Refactored CommonCodeSystemTerminologyService validateCode method and added more tests
This commit is contained in:
Martha Mitran 2024-02-02 14:20:43 -08:00 committed by GitHub
parent 250bc7b031
commit cbd733c340
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 468 additions and 324 deletions

View File

@ -1,6 +1,10 @@
org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport.displayMismatch=Concept Display "{0}" does not match expected "{1}"
org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.unknownCodeInSystem=Unknown code "{0}#{1}"
org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.invalidCodeInSystem=Code {0} is not valid for system: {1}
org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.mismatchCodeSystem=Inappropriate CodeSystem URL "{0}" for ValueSet: {1}
org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.codeNotFoundInValueSet=Code "{0}" is not in valueset: {1}
ca.uhn.fhir.jpa.term.TermReadSvcImpl.expansionRefersToUnknownCs=Unknown CodeSystem URI "{0}" referenced from ValueSet
ca.uhn.fhir.jpa.term.TermReadSvcImpl.valueSetNotYetExpanded=ValueSet "{0}" has not yet been pre-expanded. Performing in-memory expansion without parameters. Current status: {1} | {2}

View File

@ -20,9 +20,9 @@ import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NpmSearchR4Test extends BaseJpaR4Test {
public class JpaPackageCacheSearchR4Test extends BaseJpaR4Test {
private static final Logger ourLog = LoggerFactory.getLogger(NpmSearchR4Test.class);
private static final Logger ourLog = LoggerFactory.getLogger(JpaPackageCacheSearchR4Test.class);
@Autowired
public IPackageInstallerSvc igInstaller;
@Autowired

View File

@ -24,15 +24,11 @@ import ca.uhn.fhir.rest.param.UriParam;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.interceptor.partition.RequestTenantPartitionInterceptor;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import ca.uhn.fhir.test.utilities.JettyUtil;
import ca.uhn.fhir.test.utilities.ProxyUtil;
import ca.uhn.fhir.test.utilities.server.HttpServletExtension;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.JsonUtil;
import ca.uhn.fhir.validation.ValidationResult;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.ee10.servlet.ServletHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Enumerations;
@ -82,9 +78,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@TestMethodOrder(MethodOrderer.MethodName.class)
public class NpmR4Test extends BaseJpaR4Test {
public class PackageInstallerSvcR4Test extends BaseJpaR4Test {
private static final Logger ourLog = LoggerFactory.getLogger(NpmR4Test.class);
private static final Logger ourLog = LoggerFactory.getLogger(PackageInstallerSvcR4Test.class);
@Autowired
@Qualifier("myImplementationGuideDaoR4")
protected IFhirResourceDao<ImplementationGuide> myImplementationGuideDao;

View File

@ -17,7 +17,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.fhir.ucum.UcumEssenceService;
import org.fhir.ucum.UcumException;
import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
@ -30,16 +29,18 @@ import org.hl7.fhir.dstu2.model.ValueSet;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CodeSystem;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@ -65,8 +66,8 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
public static final String UCUM_CODESYSTEM_URL = "http://unitsofmeasure.org";
public static final String UCUM_VALUESET_URL = "http://hl7.org/fhir/ValueSet/ucum-units";
public static final String ALL_LANGUAGES_VALUESET_URL = "http://hl7.org/fhir/ValueSet/all-languages";
private static final String USPS_CODESYSTEM_URL = "https://www.usps.com/";
private static final String USPS_VALUESET_URL = "http://hl7.org/fhir/us/core/ValueSet/us-core-usps-state";
public static final String USPS_CODESYSTEM_URL = "https://www.usps.com/";
public static final String USPS_VALUESET_URL = "http://hl7.org/fhir/us/core/ValueSet/us-core-usps-state";
private static final Logger ourLog = LoggerFactory.getLogger(CommonCodeSystemsTerminologyService.class);
private static final Map<String, String> USPS_CODES = Collections.unmodifiableMap(buildUspsCodes());
private static final Map<String, String> ISO_4217_CODES = Collections.unmodifiableMap(buildIso4217Codes());
@ -81,8 +82,7 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
* Constructor
*/
public CommonCodeSystemsTerminologyService(FhirContext theFhirContext) {
Validate.notNull(theFhirContext);
Objects.requireNonNull(theFhirContext);
myFhirContext = theFhirContext;
myVersionCanonicalizer = new VersionCanonicalizer(theFhirContext);
}
@ -108,136 +108,109 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
public CodeValidationResult validateCode(
@Nonnull ValidationSupportContext theValidationSupportContext,
@Nonnull ConceptValidationOptions theOptions,
String theCodeSystem,
String theCode,
String theDisplay,
String theValueSetUrl) {
final String theCodeSystem,
final String theCode,
final String theDisplay,
final String theValueSetUrl) {
/* **************************************************************************************
* NOTE: Update validation_support_modules.html if any of the support in this module
* NOTE: Update validation_support_modules.md if any of the support in this module
* changes in any way!
* **************************************************************************************/
Map<String, String> handlerMap = null;
String expectSystem = null;
switch (defaultString(theValueSetUrl)) {
String valueSet = defaultString(theValueSetUrl);
String system = defaultString(theCodeSystem);
if (!isBlank(valueSet)) {
final String expectSystem = getCodeSystemForValueSet(valueSet);
if (!isBlank(system) && !system.equals(expectSystem)) {
return getValidateCodeResultInError("mismatchCodeSystem", system, valueSet);
}
system = expectSystem;
}
switch (valueSet) {
case USPS_VALUESET_URL:
handlerMap = USPS_CODES;
expectSystem = USPS_CODESYSTEM_URL;
break;
return validateCodeUsingCodeMap(theCode, system, USPS_CODES);
case CURRENCIES_VALUESET_URL:
handlerMap = ISO_4217_CODES;
expectSystem = CURRENCIES_CODESYSTEM_URL;
break;
return validateCodeUsingCodeMap(theCode, system, ISO_4217_CODES);
case LANGUAGES_VALUESET_URL:
expectSystem = LANGUAGES_CODESYSTEM_URL;
if (!expectSystem.equals(theCodeSystem) && !(theCodeSystem == null && theOptions.isInferSystem())) {
return new CodeValidationResult()
.setSeverity(IssueSeverity.ERROR)
.setMessage("Inappropriate CodeSystem URL \"" + theCodeSystem + "\" for ValueSet: "
+ theValueSetUrl);
}
IBaseResource languagesVs = myLanguagesVs;
if (languagesVs == null) {
languagesVs = theValidationSupportContext
.getRootValidationSupport()
.fetchValueSet("http://hl7.org/fhir/ValueSet/languages");
myLanguagesVs = myVersionCanonicalizer.valueSetToValidatorCanonical(languagesVs);
}
Optional<org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent> match =
myLanguagesVs.getCompose().getInclude().stream()
.flatMap(t -> t.getConcept().stream())
.filter(t -> theCode.equals(t.getCode()))
.findFirst();
if (match.isPresent()) {
return new CodeValidationResult()
.setCode(theCode)
.setDisplay(match.get().getDisplay());
} else {
return new CodeValidationResult()
.setSeverity(IssueSeverity.ERROR)
.setMessage("Code \"" + theCode + "\" is not in valueset: " + theValueSetUrl);
}
return validateLanguageCodeInValueSet(theValidationSupportContext, theCode);
case ALL_LANGUAGES_VALUESET_URL:
expectSystem = LANGUAGES_CODESYSTEM_URL;
if (!expectSystem.equals(theCodeSystem) && !(theCodeSystem == null && theOptions.isInferSystem())) {
return new CodeValidationResult()
.setSeverity(IssueSeverity.ERROR)
.setMessage("Inappropriate CodeSystem URL \"" + theCodeSystem + "\" for ValueSet: "
+ theValueSetUrl);
}
LookupCodeResult outcome = lookupLanguageCode(theCode);
if (outcome.isFound()) {
return new CodeValidationResult().setCode(theCode).setDisplay(outcome.getCodeDisplay());
} else {
return new CodeValidationResult()
.setSeverity(IssueSeverity.ERROR)
.setMessage("Code \"" + theCode + "\" is not in valueset: " + theValueSetUrl);
}
return validateLanguageCode(theCode, valueSet);
case MIMETYPES_VALUESET_URL:
// This is a pretty naive implementation - Should be enhanced in future
return new CodeValidationResult().setCode(theCode).setDisplay(theDisplay);
return getValidateCodeResultOk(theCode, theDisplay);
case UCUM_VALUESET_URL: {
String system = theCodeSystem;
expectSystem = UCUM_CODESYSTEM_URL;
if (system == null && theOptions.isInferSystem()) {
system = expectSystem;
}
CodeValidationResult validationResult =
validateLookupCode(theValidationSupportContext, theCode, system);
if (validationResult != null) {
return validationResult;
}
return validateCodeUsingSystemLookup(theValidationSupportContext, theCode, system);
}
}
if (handlerMap != null) {
String display = handlerMap.get(theCode);
if (display != null) {
if (expectSystem.equals(theCodeSystem) || theOptions.isInferSystem()) {
return new CodeValidationResult().setCode(theCode).setDisplay(display);
}
}
String actualSystem = defaultIfBlank(theCodeSystem, expectSystem);
String unknownCodeMessage = myFhirContext
.getLocalizer()
.getMessage("ca.uhn.fhir.jpa.term.TermReadSvcImpl.unknownCodeInSystem", actualSystem, theCode);
return new CodeValidationResult().setSeverity(IssueSeverity.ERROR).setMessage(unknownCodeMessage);
}
if (isBlank(theValueSetUrl)) {
return validateLookupCode(theValidationSupportContext, theCode, theCodeSystem);
if (isBlank(valueSet)) {
return validateCodeUsingSystemLookup(theValidationSupportContext, theCode, system);
}
return null;
}
@Nullable
public CodeValidationResult validateLookupCode(
ValidationSupportContext theValidationSupportContext, String theCode, String theSystem) {
LookupCodeResult lookupResult =
lookupCode(theValidationSupportContext, new LookupCodeRequest(theSystem, theCode));
CodeValidationResult validationResult = null;
if (lookupResult != null) {
if (lookupResult.isFound()) {
validationResult = new CodeValidationResult()
.setCode(lookupResult.getSearchedForCode())
.setDisplay(lookupResult.getCodeDisplay());
} else if (lookupResult.getErrorMessage() != null) {
validationResult = new CodeValidationResult()
.setSeverity(IssueSeverity.ERROR)
.setMessage(lookupResult.getErrorMessage());
}
private static String getCodeSystemForValueSet(final String theValueSetUrl) {
String theCodeSystem = null;
switch (defaultString(theValueSetUrl)) {
case USPS_VALUESET_URL:
theCodeSystem = USPS_CODESYSTEM_URL;
break;
case CURRENCIES_VALUESET_URL:
theCodeSystem = CURRENCIES_CODESYSTEM_URL;
break;
case LANGUAGES_VALUESET_URL:
case ALL_LANGUAGES_VALUESET_URL:
theCodeSystem = LANGUAGES_CODESYSTEM_URL;
break;
case MIMETYPES_VALUESET_URL:
theCodeSystem = MIMETYPES_CODESYSTEM_URL;
break;
case UCUM_VALUESET_URL:
theCodeSystem = UCUM_CODESYSTEM_URL;
break;
}
return theCodeSystem;
}
return validationResult;
protected CodeValidationResult getValidateCodeResultInError(
final String errorCode, final String theFirstParam, final String theSecondParam) {
String message = getErrorMessage(errorCode, theFirstParam, theSecondParam);
return getValidateCodeResultError(message);
}
protected CodeValidationResult getValidateCodeResultOk(final String theCode, final String theDisplay) {
return new CodeValidationResult().setCode(theCode).setDisplay(theDisplay);
}
protected CodeValidationResult getValidateCodeResultError(final String theMessage) {
return new CodeValidationResult().setSeverity(IssueSeverity.ERROR).setMessage(theMessage);
}
private CodeValidationResult validateCodeUsingCodeMap(
final String theCode, final String theSystem, final Map<String, String> theCodeMap) {
if (theCodeMap.containsKey(theCode)) {
return getValidateCodeResultOk(theCode, theCodeMap.get(theCode));
} else {
return getValidateCodeResultInError("unknownCodeInSystem", theSystem, theCode);
}
}
@Nullable
public CodeValidationResult validateCodeUsingSystemLookup(
final ValidationSupportContext theValidationSupportContext, final String theCode, final String theSystem) {
LookupCodeResult result = lookupCode(theValidationSupportContext, new LookupCodeRequest(theSystem, theCode));
if (result == null) {
return getValidateCodeResultInError("unknownCodeInSystem", theSystem, theCode);
}
if (result.isFound()) {
return getValidateCodeResultOk(theCode, result.getCodeDisplay());
} else if (result.getErrorMessage() != null) {
return getValidateCodeResultError(result.getErrorMessage());
}
return null;
}
@Override
@ -267,85 +240,96 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
return null;
}
String display = map.get(code);
if (isNotBlank(display)) {
LookupCodeResult retVal = new LookupCodeResult();
retVal.setSearchedForCode(code);
retVal.setSearchedForSystem(system);
retVal.setFound(true);
retVal.setCodeDisplay(display);
return retVal;
}
// If we get here it means we know the codesystem but the code was bad
LookupCodeResult retVal = new LookupCodeResult();
retVal.setSearchedForCode(code);
retVal.setSearchedForSystem(system);
retVal.setFound(false);
retVal.setErrorMessage("Code '" + code + "' is not valid for system: " + system);
String display = map.get(code);
if (isNotBlank(display)) {
retVal.setFound(true);
retVal.setCodeDisplay(display);
} else {
// If we get here it means we know the CodeSystem but the code was bad
retVal.setFound(false);
String invalidCodeMessage = getErrorMessage("invalidCodeInSystem", code, system);
retVal.setErrorMessage(invalidCodeMessage);
}
return retVal;
}
private CodeValidationResult validateLanguageCode(final String theCode, final String theValueSetUrl) {
LookupCodeResult outcome = lookupLanguageCode(theCode);
if (outcome.isFound()) {
return getValidateCodeResultOk(theCode, outcome.getCodeDisplay());
} else {
return getValidateCodeResultInError("codeNotFoundInValueSet", theCode, theValueSetUrl);
}
}
private CodeValidationResult validateLanguageCodeInValueSet(
final ValidationSupportContext theValidationSupportContext, final String theCode) {
final String valueSet = LANGUAGES_VALUESET_URL;
if (myLanguagesVs == null) {
IBaseResource languagesVs =
theValidationSupportContext.getRootValidationSupport().fetchValueSet(valueSet);
myLanguagesVs = myVersionCanonicalizer.valueSetToValidatorCanonical(languagesVs);
}
Optional<ConceptReferenceComponent> match = myLanguagesVs.getCompose().getInclude().stream()
.flatMap(t -> t.getConcept().stream())
.filter(t -> theCode.equals(t.getCode()))
.findFirst();
if (match.isPresent()) {
return getValidateCodeResultOk(theCode, match.get().getDisplay());
} else {
return getValidateCodeResultInError("codeNotFoundInValueSet", theCode, valueSet);
}
}
private LookupCodeResult lookupLanguageCode(String theCode) {
if (myLanguagesLanugageMap == null || myLanguagesRegionMap == null) {
initializeBcp47LanguageMap();
}
int langRegionSeparatorIndex = StringUtils.indexOfAny(theCode, '-', '_');
boolean hasRegionAndCodeSegments = langRegionSeparatorIndex > 0;
String language;
String region;
final LookupCodeResult lookupCodeResult = new LookupCodeResult();
lookupCodeResult.setSearchedForSystem(LANGUAGES_CODESYSTEM_URL);
lookupCodeResult.setSearchedForCode(theCode);
final int langRegionSeparatorIndex = StringUtils.indexOfAny(theCode, '-', '_');
final boolean hasRegionAndCodeSegments = langRegionSeparatorIndex > 0;
final boolean found;
final String display;
if (hasRegionAndCodeSegments) {
// we look for languages in lowercase only
// this will allow case insensitivity for language portion of code
language = myLanguagesLanugageMap.get(
String language = myLanguagesLanugageMap.get(
theCode.substring(0, langRegionSeparatorIndex).toLowerCase());
region = myLanguagesRegionMap.get(
String region = myLanguagesRegionMap.get(
theCode.substring(langRegionSeparatorIndex + 1).toUpperCase());
if (language == null || region == null) {
// In case the user provides both a language and a region, they must both be valid for the lookup to
// succeed.
// In case the user provides both a language and a region, they must both be valid for the lookup to
// succeed.
found = language != null && region != null;
display = found ? language + " " + region : null;
if (!found) {
ourLog.warn("Couldn't find a valid bcp47 language-region combination from code: {}", theCode);
return buildNotFoundLookupCodeResult(theCode);
} else {
return buildLookupResultForLanguageAndRegion(theCode, language, region);
}
} else {
// In case user has only provided a language, we build the lookup from only that.
// NB: we only use the lowercase version of the language
language = myLanguagesLanugageMap.get(theCode.toLowerCase());
if (language == null) {
String language = myLanguagesLanugageMap.get(theCode.toLowerCase());
found = language != null;
display = language;
if (!found) {
ourLog.warn("Couldn't find a valid bcp47 language from code: {}", theCode);
return buildNotFoundLookupCodeResult(theCode);
} else {
return buildLookupResultForLanguage(theCode, language);
}
}
}
private LookupCodeResult buildLookupResultForLanguageAndRegion(
@Nonnull String theOriginalCode, @Nonnull String theLanguage, @Nonnull String theRegion) {
LookupCodeResult lookupCodeResult = buildNotFoundLookupCodeResult(theOriginalCode);
lookupCodeResult.setCodeDisplay(theLanguage + " " + theRegion);
lookupCodeResult.setFound(true);
return lookupCodeResult;
}
lookupCodeResult.setFound(found);
lookupCodeResult.setCodeDisplay(display);
private LookupCodeResult buildLookupResultForLanguage(
@Nonnull String theOriginalCode, @Nonnull String theLanguage) {
LookupCodeResult lookupCodeResult = buildNotFoundLookupCodeResult(theOriginalCode);
lookupCodeResult.setCodeDisplay(theLanguage);
lookupCodeResult.setFound(true);
return lookupCodeResult;
}
private LookupCodeResult buildNotFoundLookupCodeResult(@Nonnull String theOriginalCode) {
LookupCodeResult lookupCodeResult = new LookupCodeResult();
lookupCodeResult.setFound(false);
lookupCodeResult.setSearchedForSystem(LANGUAGES_CODESYSTEM_URL);
lookupCodeResult.setSearchedForCode(theOriginalCode);
return lookupCodeResult;
}
@ -404,24 +388,20 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
@Nonnull
private LookupCodeResult lookupUcumCode(String theCode) {
InputStream input = ClasspathUtil.loadResourceAsStream("/ucum-essence.xml");
String outcome;
LookupCodeResult retVal = new LookupCodeResult();
retVal.setSearchedForCode(theCode);
retVal.setSearchedForSystem(UCUM_CODESYSTEM_URL);
try {
try (InputStream input = ClasspathUtil.loadResourceAsStream("/ucum-essence.xml")) {
UcumEssenceService svc = new UcumEssenceService(input);
outcome = svc.analyse(theCode);
String outcome = svc.analyse(theCode);
if (outcome != null) {
retVal.setFound(true);
retVal.setCodeDisplay(outcome);
}
} catch (UcumException e) {
} catch (UcumException | IOException e) {
ourLog.debug("Failed parse UCUM code: {}", theCode, e);
retVal.setErrorMessage(e.getMessage());
} finally {
ClasspathUtil.close(input);
}
return retVal;
}
@ -470,7 +450,7 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
break;
}
Validate.notNull(normalized);
Objects.requireNonNull(normalized);
return normalized;
}
@ -1373,4 +1353,8 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
codes.put("ZWE", "Zimbabwe");
return codes;
}
protected String getErrorMessage(String errorCode, String theFirstParam, String theSecondParam) {
return myFhirContext.getLocalizer().getMessage(getClass(), errorCode, theFirstParam, theSecondParam);
}
}

View File

@ -2,17 +2,35 @@ package org.hl7.fhir.common.hapi.validation.support;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.support.ConceptValidationOptions;
import ca.uhn.fhir.context.support.IValidationSupport;
import ca.uhn.fhir.context.support.ValidationSupportContext;
import ca.uhn.fhir.context.support.IValidationSupport.CodeValidationResult;
import ca.uhn.fhir.context.support.IValidationSupport.IssueSeverity;
import ca.uhn.fhir.context.support.IValidationSupport.LookupCodeResult;
import ca.uhn.fhir.context.support.LookupCodeRequest;
import ca.uhn.fhir.context.support.ValidationSupportContext;
import ca.uhn.fhir.fhirpath.BaseValidationTestWithInlineMocks;
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.EncodingEnum;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CodeSystem;
import org.hl7.fhir.r4.model.ValueSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.ALL_LANGUAGES_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.CURRENCIES_CODESYSTEM_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.CURRENCIES_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.LANGUAGES_CODESYSTEM_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.LANGUAGES_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.MIMETYPES_CODESYSTEM_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.MIMETYPES_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.UCUM_CODESYSTEM_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.UCUM_VALUESET_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.USPS_CODESYSTEM_URL;
import static org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService.USPS_VALUESET_URL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -31,214 +49,356 @@ public class CommonCodeSystemsTerminologyServiceTest extends BaseValidationTestW
mySvc = new CommonCodeSystemsTerminologyService(myCtx);
}
@Test
public void testUcum_LookupCode_Good() {
IValidationSupport.LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest("http://unitsofmeasure.org", "Cel"));
assert outcome != null;
assertTrue(outcome.isFound());
@ParameterizedTest
@CsvSource({"Cel, (degree Celsius)", "kg/m2, (kilogram) / (meter ^ 2)"})
public void testLookupCode_withUnitsOfMeasureWithKnownCode_returnsFound(final String theCode, final String theDisplay) {
final String system = UCUM_CODESYSTEM_URL;
LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, theCode));
lookupCodeResultOk(outcome, theCode, system, theDisplay);
}
@Test
public void testUcum_LookupCode_Good2() {
IValidationSupport.LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest("http://unitsofmeasure.org", "kg/m2"));
assert outcome != null;
assertTrue(outcome.isFound());
public void testLookupCode_withUnitsOfMeasureWithUnknownCode_returnsNotFound() {
final String code = "someCode";
LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest(UCUM_CODESYSTEM_URL, code));
lookupCodeResultError(outcome, code, "Error processing unit '" + code +"': The unit '" + code + "' is unknown' at position 0");
}
@Test
public void testUcum_LookupCode_Bad() {
IValidationSupport.LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest("http://unitsofmeasure.org", "AAAAA"));
assert outcome != null;
assertFalse(outcome.isFound());
}
@Test
public void testUcum_LookupCode_UnknownSystem() {
IValidationSupport.LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest("http://foo", "AAAAA"));
public void testLookupCode_withUnknownSystem_returnsNull() {
LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest("http://foo", "someCode"));
assertNull(outcome);
}
@Test
public void lookupCode_languageOnlyLookup_isCaseInsensitive() {
IValidationSupport.LookupCodeResult outcomeUpper = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "SGN", "Sign Languages", null));
IValidationSupport.LookupCodeResult outcomeLower = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "sgn", "Sign Languages", null));
assertNotNull(outcomeUpper);
assertNotNull(outcomeLower);
assertTrue(outcomeLower.isFound());
assertTrue(outcomeUpper.isFound());
@ParameterizedTest
@CsvSource({"SGN, Sign languages", "sgn, Sign languages", "EN-US, English United States", "en-us, English United States"})
public void testLookupCode_withLanguageOnlyWithKnownCode_returnsFound(final String theCode, final String theDisplay) {
final String system = LANGUAGES_CODESYSTEM_URL;
LookupCodeResult outcome = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, theCode, null, null));
lookupCodeResultOk(outcome, theCode, system, theDisplay);
}
@Test
public void lookupCode_languageAndRegionLookup_isCaseInsensitive() {
IValidationSupport.LookupCodeResult outcomeUpper = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "EN-US", "English", null));
IValidationSupport.LookupCodeResult outcomeLower = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "en-us", "English", null));
assertNotNull(outcomeUpper);
assertNotNull(outcomeLower);
assertTrue(outcomeLower.isFound());
assertTrue(outcomeUpper.isFound());
public void testValidateCode_withUnitsOfMeasureWithKnownCode_returnsValid() {
final String code = "mg";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), UCUM_CODESYSTEM_URL, code, null, UCUM_VALUESET_URL);
validateCodeResultOk(result, code, "(milligram)");
}
@Test
public void testUcum_ValidateCode_Good() {
ValueSet vs = new ValueSet();
vs.setUrl("http://hl7.org/fhir/ValueSet/ucum-units");
IValidationSupport.CodeValidationResult outcome = mySvc.validateCodeInValueSet(newSupport(), newOptions(), "http://unitsofmeasure.org", "mg", null, vs);
assert outcome != null;
assertTrue(outcome.isOk());
assertEquals("(milligram)", outcome.getDisplay());
public void testValidateCodeInValueSet_withUnitsOfMeasureWithKnownCode_returnsValid() {
final ValueSet vs = new ValueSet().setUrl(UCUM_VALUESET_URL);
final String code = "mg";
CodeValidationResult result = mySvc.validateCodeInValueSet(newSupport(), newOptions(), UCUM_CODESYSTEM_URL, code, null, vs);
validateCodeResultOk(result, code, "(milligram)");
}
@Test
public void testUcum_ValidateCode_Good_SystemInferred() {
ValueSet vs = new ValueSet();
vs.setUrl("http://hl7.org/fhir/ValueSet/ucum-units");
IValidationSupport.CodeValidationResult outcome = mySvc.validateCodeInValueSet(newSupport(), newOptions().setInferSystem(true), null, "mg", null, vs);
assert outcome != null;
assertTrue(outcome.isOk());
assertEquals("(milligram)", outcome.getDisplay());
public void testValidateCodeInValueSet_withUnitsOfMeasureWithInferSystem_returnsValid() {
final ValueSet vs = new ValueSet().setUrl(UCUM_VALUESET_URL);
final String code = "mg";
CodeValidationResult result = mySvc.validateCodeInValueSet(newSupport(), newOptions().setInferSystem(true), null, code, null, vs);
validateCodeResultOk(result, code, "(milligram)");
}
@Test
public void testUcum_ValidateCode_Bad() {
ValueSet vs = new ValueSet();
vs.setUrl("http://hl7.org/fhir/ValueSet/ucum-units");
IValidationSupport.CodeValidationResult outcome = mySvc.validateCodeInValueSet(newSupport(), newOptions(), "http://unitsofmeasure.org", "aaaaa", null, vs);
assertNotNull(outcome);
assertFalse(outcome.isOk());
assertEquals("Error processing unit 'aaaaa': The unit 'aaaaa' is unknown' at position 0", outcome.getMessage());
assertEquals("error", outcome.getSeverityCode());
public void testValidateCodeInValueSet_withUnitsOfMeasureWithUnknownCode_returnsInvalid() {
final String code = "FOO";
final ValueSet vs = new ValueSet().setUrl(UCUM_VALUESET_URL);
CodeValidationResult result = mySvc.validateCodeInValueSet(newSupport(), newOptions(), UCUM_CODESYSTEM_URL, code, null, vs);
validateCodeResultError(result, "Error processing unit '" + code +"': The unit '" + code + "' is unknown' at position 0");
}
@ParameterizedTest
@CsvSource({"en-CA, English Canada", "en-US, English United States"})
public void testValidateLookupCode_withLanguagesWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCodeUsingSystemLookup(newSupport(), theCode, LANGUAGES_CODESYSTEM_URL);
validateCodeResultOk(result, theCode, theDisplay);
}
@ParameterizedTest
@CsvSource({"en-CA, English (Canada)", "en-US, English (United States)"})
public void testValidateCode_withLanguagesWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), LANGUAGES_CODESYSTEM_URL, theCode, null, LANGUAGES_VALUESET_URL);
validateCodeResultOk(result, theCode, theDisplay);
}
@Test
public void testLanguagesLanguagesCs_GoodCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateLookupCode(newSupport(), "en-CA", "urn:ietf:bcp:47");
assert outcome != null;
assertTrue(outcome.isOk());
assertEquals("English Canada", outcome.getDisplay());
public void testValidateCode_withLanguagesWithUnknownCode_returnsInvalid() {
final String code = "FOO";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), LANGUAGES_CODESYSTEM_URL, code, null, LANGUAGES_VALUESET_URL);
validateCodeResultError(result, "Code \""+ code +"\" is not in valueset: " + LANGUAGES_VALUESET_URL);
}
@Test
public void testLanguagesLanguagesCs_BadCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateLookupCode(newSupport(), "en-FOO", "urn:ietf:bcp:47");
assertNull(outcome);
public void testValidateCode_withLanguagesWithIncorrectSystem_returnsInvalid() {
final String system = "FOO";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), system, "en-US", null, LANGUAGES_VALUESET_URL);
validateCodeResultError(result, "Inappropriate CodeSystem URL \"" + system + "\" for ValueSet: " + LANGUAGES_VALUESET_URL);
}
@ParameterizedTest
@CsvSource({"en-CA, English Canada", "en-US, English United States"})
public void testValidateCode_withAllLanguagesWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), LANGUAGES_CODESYSTEM_URL, theCode, null, ALL_LANGUAGES_VALUESET_URL);
validateCodeResultOk(result, theCode, theDisplay);
}
@Test
public void testValidateCode_withAllLanguagesWithUnknownCode_returnsInvalid() {
final String code = "FOO";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), LANGUAGES_CODESYSTEM_URL, code, null, ALL_LANGUAGES_VALUESET_URL);
validateCodeResultError(result, "Code \"" + code + "\" is not in valueset: " + ALL_LANGUAGES_VALUESET_URL);
}
@Test
public void testLanguages_CommonLanguagesVs_GoodCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "urn:ietf:bcp:47", "en-US", null, "http://hl7.org/fhir/ValueSet/languages");
assert outcome != null;
assertTrue(outcome.isOk());
assertEquals("English (United States)", outcome.getDisplay());
public void testValidateCode_withAllLanguagesWithIncorrectSystem_returnsInvalid() {
final String system = "FOO";
final String valueSet = ALL_LANGUAGES_VALUESET_URL;
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), system, "en-US", null, valueSet);
validateCodeResultError(result, "Inappropriate CodeSystem URL \"" + system + "\" for ValueSet: " + valueSet);
}
@ParameterizedTest
@CsvSource({"nl, Dutch", "nl-NL, Dutch Netherlands"})
public void testLookupCode_withLanguagesWithKnownLanguageOnlyCode_returnsFound(final String theCode, final String theDisplay) {
final String system = LANGUAGES_CODESYSTEM_URL;
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, theCode));
lookupCodeResultOk(result, theCode, system, theDisplay);
}
@Test
public void testLanguages_CommonLanguagesVs_OnlyLanguage_NoRegion() {
IValidationSupport.LookupCodeResult nl = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "nl"));
assertTrue(nl.isFound());
assertEquals("Dutch", nl.getCodeDisplay());
}
@Test
public void testLanguages_CommonLanguagesVs_LanguageAndRegion() {
IValidationSupport.LookupCodeResult nl = mySvc.lookupCode(newSupport(), new LookupCodeRequest("urn:ietf:bcp:47", "nl-NL"));
assertTrue(nl.isFound());
assertEquals("Dutch Netherlands", nl.getCodeDisplay());
}
@Test
public void testLanguages_CommonLanguagesVs_BadCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "urn:ietf:bcp:47", "FOO", null, "http://hl7.org/fhir/ValueSet/languages");
assert outcome != null;
assertFalse(outcome.isOk());
assertEquals("Code \"FOO\" is not in valueset: http://hl7.org/fhir/ValueSet/languages", outcome.getMessage());
}
@Test
public void testLanguages_CommonLanguagesVs_BadSystem() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "FOO", "en-US", null, "http://hl7.org/fhir/ValueSet/languages");
assert outcome != null;
assertFalse(outcome.isOk());
assertEquals("Inappropriate CodeSystem URL \"FOO\" for ValueSet: http://hl7.org/fhir/ValueSet/languages", outcome.getMessage());
}
@Test
public void testLanguages_AllLanguagesVs_GoodCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "urn:ietf:bcp:47", "en-US", null, "http://hl7.org/fhir/ValueSet/all-languages");
assert outcome != null;
assertTrue(outcome.isOk());
assertEquals("English United States", outcome.getDisplay());
}
@Test
public void testLanguages_AllLanguagesVs_BadCode() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "urn:ietf:bcp:47", "FOO", null, "http://hl7.org/fhir/ValueSet/all-languages");
assert outcome != null;
assertFalse(outcome.isOk());
assertEquals("Code \"FOO\" is not in valueset: http://hl7.org/fhir/ValueSet/all-languages", outcome.getMessage());
}
@Test
public void testLanguages_AllLanguagesVs_BadSystem() {
IValidationSupport.CodeValidationResult outcome = mySvc.validateCode(newSupport(), newOptions(), "FOO", "en-US", null, "http://hl7.org/fhir/ValueSet/all-languages");
assert outcome != null;
assertFalse(outcome.isOk());
assertEquals("Inappropriate CodeSystem URL \"FOO\" for ValueSet: http://hl7.org/fhir/ValueSet/all-languages", outcome.getMessage());
}
@Test
public void testFetchCodeSystemBuiltIn_Iso3166_R4() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem(CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL);
assert cs != null;
assertEquals(498, cs.getConcept().size());
}
@Test
public void testFetchCodeSystemBuiltIn_Iso3166_DSTU3() {
public void testFetchCodeSystem_withCountriesForDSTU3_returnsOk() {
CommonCodeSystemsTerminologyService svc = new CommonCodeSystemsTerminologyService(FhirContext.forDstu3Cached());
org.hl7.fhir.dstu3.model.CodeSystem cs = (org.hl7.fhir.dstu3.model.CodeSystem) svc.fetchCodeSystem(CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL);
assert cs != null;
assertNotNull(cs);
assertEquals(498, cs.getConcept().size());
}
@Test
public void testFetchCodeSystemBuiltIn_Iso3166_R5() {
public void testFetchCodeSystem_withCountriesForR4_returnsOk() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem(CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL);
assertNotNull(cs);
assertEquals(498, cs.getConcept().size());
}
@Test
public void testFetchCodeSystem_withCountriesForR5_returnsOk() {
CommonCodeSystemsTerminologyService svc = new CommonCodeSystemsTerminologyService(FhirContext.forR5Cached());
org.hl7.fhir.r5.model.CodeSystem cs = (org.hl7.fhir.r5.model.CodeSystem) svc.fetchCodeSystem(CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL);
assert cs != null;
assertNotNull(cs);
assertEquals(498, cs.getConcept().size());
}
@Test
public void testFetchCodeSystemBuiltIn_Iso3166_DSTU2() {
public void testFetchCodeSystem_withCountriesForDSTU2_returnsOk() {
CommonCodeSystemsTerminologyService svc = new CommonCodeSystemsTerminologyService(FhirContext.forDstu2Cached());
IBaseResource cs = svc.fetchCodeSystem(CommonCodeSystemsTerminologyService.COUNTRIES_CODESYSTEM_URL);
assertNull(cs);
}
@ParameterizedTest
@CsvSource({"WA, Washington", "PR, Puerto Rico"})
public void testValidateCode_withUSPostalWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), USPS_CODESYSTEM_URL, theCode, null, null);
validateCodeResultOk(result, theCode, theDisplay);
}
@ParameterizedTest
@CsvSource({"WA, Washington", "PR, Puerto Rico"})
public void testValidateCode_withUSPostalValueSetWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), USPS_CODESYSTEM_URL, theCode, null, USPS_VALUESET_URL);
validateCodeResultOk(result, theCode, theDisplay);
}
@Test
public void testFetchCodeSystemBuiltIn_Iso_R4() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem(CommonCodeSystemsTerminologyService.CURRENCIES_CODESYSTEM_URL);
assert cs != null;
public void testValidateCode_withUSPostalValueSetWithUnknownCode_returnsInvalid() {
final String system = USPS_CODESYSTEM_URL;
final String code = "FOO";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), system, code, null, USPS_VALUESET_URL);
validateCodeResultError(result, "Unknown code \"" + system + "#" + code + "\"");
}
@ParameterizedTest
@CsvSource({"WA, Washington", "PR, Puerto Rico"})
public void testLookupCode_withUSPostalWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
final String system = USPS_CODESYSTEM_URL;
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, theCode));
lookupCodeResultOk(result, theCode, system, theDisplay);
}
@Test
public void testLookupCode_withUSPostalWithUnknownCode_returnsNotFound() {
final String system = USPS_CODESYSTEM_URL;
final String code = "invalidUSPS";
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, code));
lookupCodeResultError(result, code, "Code " + code + " is not valid for system: " + system);
}
@ParameterizedTest
@CsvSource({"USD, United States dollar", "CAD, Canadian dollar", "EUR, Euro"})
public void testValidateCode_withCurrenciesWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), CURRENCIES_CODESYSTEM_URL, theCode, null, null);
validateCodeResultOk(result, theCode, theDisplay);
}
@ParameterizedTest
@CsvSource({"USD, United States dollar", "CAD, Canadian dollar", "EUR, Euro"})
public void testValidateCode_withCurrenciesValueSetWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), CURRENCIES_CODESYSTEM_URL, theCode, null, CURRENCIES_VALUESET_URL);
validateCodeResultOk(result, theCode, theDisplay);
}
@Test
public void testValidateCode_withCurrenciesValueSetWithUnknownCode_returnsInvalid() {
final String system = CURRENCIES_CODESYSTEM_URL;
final String code = "invalidCurrency";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), system, code, null, CURRENCIES_VALUESET_URL);
validateCodeResultError(result, "Unknown code \"" + system + "#" + code + "\"");
}
@ParameterizedTest
@CsvSource({"USD, United States dollar", "CAD, Canadian dollar", "EUR, Euro"})
public void testLookupCode_withCurrenciesWithKnownCode_returnsValid(final String theCode, final String theDisplay) {
final String system = CURRENCIES_CODESYSTEM_URL;
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, theCode));
lookupCodeResultOk(result, theCode, system, theDisplay);
}
@Test
public void testLookupCode_withCurrenciesWithUnknownCode_returnsNotFound() {
final String system = CURRENCIES_CODESYSTEM_URL;
final String code = "FOO";
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, code));
lookupCodeResultError(result, code, "Code " + code + " is not valid for system: " + system);
}
@Test
public void testFetchCodeSystem_withCurrencies_returnsOk() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem(CURRENCIES_CODESYSTEM_URL);
assertNotNull(cs);
assertEquals(182, cs.getConcept().size());
}
@Test
public void testFetchCodeSystemBuiltIn_Unknown() {
public void testFetchCodeSystem_withUnknownSystem_returnsNull() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem("http://foo");
assertNull(cs);
}
@Test
public void testFetchCodeSystemUrlDstu3() {
public void testGetCodeSystemUrl_forDSTU3_throwsException() {
try {
CommonCodeSystemsTerminologyService.getCodeSystemUrl(myCtx, new org.hl7.fhir.dstu3.model.CodeSystem());
fail();
} catch (IllegalArgumentException e) {
assertEquals(Msg.code(696) + "Can not handle version: DSTU3", e.getMessage());
}
}
@Test
public void testFetchCodeSystem_withMimeType_returnsOk() {
CodeSystem cs = (CodeSystem) mySvc.fetchCodeSystem(MIMETYPES_CODESYSTEM_URL);
assertNull(cs);
}
@ParameterizedTest
@ValueSource(strings = { EncodingEnum.JSON_PLAIN_STRING, Constants.CT_FHIR_JSON_NEW, Constants.CT_FHIR_JSON })
public void testValidateCode_withMimetypesValueSetWithStandardCode_returnsValid(String theCode) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), MIMETYPES_CODESYSTEM_URL, theCode, null, MIMETYPES_VALUESET_URL);
validateCodeResultOk(result, theCode, null);
}
@ParameterizedTest
@ValueSource(strings = { EncodingEnum.JSON_PLAIN_STRING, Constants.CT_FHIR_JSON_NEW, Constants.CT_FHIR_JSON })
public void testValidateCode_withMimetypesValueSetWithInferSystemWithStandardCode_returnsValid(String theCode) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions().setInferSystem(true), null, theCode, null, MIMETYPES_VALUESET_URL);
validateCodeResultOk(result, theCode, null);
}
@Test
public void testValidateCode_withMimetypesValueSetWithMismatchSystem_returnsInvalid() {
final String system = "someSystem";
final String valueSet = MIMETYPES_VALUESET_URL;
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), system, system, null, valueSet);
validateCodeResultError(result, "Inappropriate CodeSystem URL \"" + system + "\" for ValueSet: " + valueSet);
}
@ParameterizedTest
@ValueSource(strings = { EncodingEnum.JSON_PLAIN_STRING, Constants.CT_FHIR_JSON_NEW, Constants.CT_FHIR_JSON })
public void testValidateCode_withMimetypesWithStandardCode_returnsValid(String theCode) {
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), MIMETYPES_CODESYSTEM_URL, theCode, null, null);
validateCodeResultOk(result, theCode, null);
}
@Test
public void testValidateCode_withMimetypeValueSetWithArbitraryCode_returnsValid() {
final String code = "someCode";
final String display = "displayValue";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), MIMETYPES_CODESYSTEM_URL, code, display, MIMETYPES_VALUESET_URL);
validateCodeResultOk(result, code, display);
}
@Test
public void testValidateCode_withMimetypesWithArbitraryCode_returnsValid() {
final String code = "someCode";
final String display = "displayValue";
CodeValidationResult result = mySvc.validateCode(newSupport(), newOptions(), MIMETYPES_CODESYSTEM_URL, code, display, null);
validateCodeResultOk(result, code, null);
// the display null in result bug is reported here: https://github.com/hapifhir/hapi-fhir/issues/5643
}
@ParameterizedTest
@ValueSource(strings = { EncodingEnum.JSON_PLAIN_STRING, Constants.FORMAT_TURTLE, Constants.CT_FHIR_JSON_NEW, Constants.CT_FHIR_JSON })
public void testLookupCode_withMimetypesWithStandardCode_returnFound(String code) {
final String system = MIMETYPES_CODESYSTEM_URL;
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, code));
lookupCodeResultOk(result, code, system, null);
}
@Test
public void testLookupCode_withMimetypesWithArbitraryCode_returnsFound() {
final String system = MIMETYPES_CODESYSTEM_URL;
final String code = "someCode";
LookupCodeResult result = mySvc.lookupCode(newSupport(), new LookupCodeRequest(system, code));
lookupCodeResultOk(result, code, system, null);
}
private void validateCodeResultOk(final CodeValidationResult theResult, final String theCode, final String theDisplay) {
assertNotNull(theResult);
assertTrue(theResult.isOk());
assertEquals(theCode, theResult.getCode());
assertEquals(theDisplay, theResult.getDisplay());
assertNull(theResult.getSeverity());
assertNull(theResult.getMessage());
}
private void validateCodeResultError(final CodeValidationResult theResult, final String theError) {
assertNotNull(theResult);
assertFalse(theResult.isOk());
assertEquals(IssueSeverity.ERROR, theResult.getSeverity());
assertEquals(theError, theResult.getMessage());
}
private void lookupCodeResultOk(final LookupCodeResult theResult, final String theCode, final String theSystem, final String theDisplay) {
assertNotNull(theResult);
assertEquals(theSystem, theResult.getSearchedForSystem());
assertEquals(theCode, theResult.getSearchedForCode());
assertTrue(theResult.isFound());
assertEquals(theDisplay, theResult.getCodeDisplay());
}
private void lookupCodeResultError(final LookupCodeResult theResult, final String theCode, final String theMessage) {
assertNotNull(theResult);
assertEquals(theCode, theResult.getSearchedForCode());
assertFalse(theResult.isFound());
assertEquals(theMessage, theResult.getErrorMessage());
assertNull(theResult.getCodeDisplay());
}
private ValidationSupportContext newSupport() {
return new ValidationSupportContext(myCtx.getValidationSupport());
}