Fix bug in code. Add test. Add changelog
This commit is contained in:
parent
090a8b0821
commit
fd6dcf6363
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
type: fix
|
||||
issue: 2920
|
||||
jira: SMILE-2971
|
||||
title: "Previously, validation against bcp47 (urn:ietf:bcp:47) as a language would fail validation if the region was absent. This has been fixed, and the validate
|
||||
operation will now correctly validate simple languages, e.g. `nl` instead of requiring `nl-DE` or `nl-NL`"
|
|
@ -257,7 +257,60 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
|
|||
Map<String, String> languagesMap = myLanguagesLanugageMap;
|
||||
Map<String, String> regionsMap = myLanguagesRegionMap;
|
||||
if (languagesMap == null || regionsMap == null) {
|
||||
initializeBcp47LanguageMap();
|
||||
}
|
||||
|
||||
int langRegionSeparatorIndex = StringUtils.indexOfAny(theCode, '-', '_');
|
||||
boolean hasRegionAndCodeSegments = langRegionSeparatorIndex > 0;
|
||||
String language;
|
||||
String region;
|
||||
|
||||
if (hasRegionAndCodeSegments) {
|
||||
language = myLanguagesLanugageMap.get(theCode.substring(0, langRegionSeparatorIndex));
|
||||
region = myLanguagesRegionMap.get(theCode.substring(langRegionSeparatorIndex + 1));
|
||||
|
||||
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.
|
||||
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.
|
||||
language = myLanguagesLanugageMap.get(theCode);
|
||||
if (language == null) {
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
private void initializeBcp47LanguageMap() {
|
||||
Map<String, String> regionsMap;
|
||||
Map<String, String> languagesMap;
|
||||
ourLog.info("Loading BCP47 Language Registry");
|
||||
|
||||
String input = ClasspathUtil.loadResource("org/hl7/fhir/common/hapi/validation/support/registry.json");
|
||||
|
@ -292,7 +345,6 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
|
|||
}
|
||||
regionsMap.put(region, description);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ourLog.info("Have {} languages and {} regions", languagesMap.size(), regionsMap.size());
|
||||
|
@ -301,27 +353,6 @@ public class CommonCodeSystemsTerminologyService implements IValidationSupport {
|
|||
myLanguagesRegionMap = regionsMap;
|
||||
}
|
||||
|
||||
int idx = StringUtils.indexOfAny(theCode, '-', '_');
|
||||
String language = null;
|
||||
String region = null;
|
||||
if (idx > 0) {
|
||||
language = languagesMap.get(theCode.substring(0, idx));
|
||||
region = regionsMap.get(theCode.substring(idx + 1));
|
||||
}
|
||||
|
||||
LookupCodeResult retVal = new LookupCodeResult();
|
||||
retVal.setSearchedForCode(theCode);
|
||||
retVal.setSearchedForSystem(LANGUAGES_CODESYSTEM_URL);
|
||||
|
||||
if (language != null && region != null) {
|
||||
String display = language + " " + region;
|
||||
retVal.setFound(true);
|
||||
retVal.setCodeDisplay(display);
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private LookupCodeResult lookupMimetypeCode(String theCode) {
|
||||
// This is a pretty naive implementation - Should be enhanced in future
|
||||
|
|
|
@ -105,6 +105,13 @@ public class CommonCodeSystemsTerminologyServiceTest {
|
|||
assertEquals("English (United States)", outcome.getDisplay());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLanguages_CommonLanguagesVs_OnlyLanguage_NoRegion() {
|
||||
IValidationSupport.LookupCodeResult nl = mySvc.lookupCode(newSupport(), "urn:ietf:bcp:47", "nl");
|
||||
assertTrue(nl.isFound());
|
||||
assertEquals("Dutch", 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");
|
||||
|
|
Loading…
Reference in New Issue