diff --git a/src/main/java/com/appdirect/sdk/vendorFields/converter/LocaleConverter.java b/src/main/java/com/appdirect/sdk/vendorFields/converter/LocaleConverter.java index d130cb9d..354e7355 100644 --- a/src/main/java/com/appdirect/sdk/vendorFields/converter/LocaleConverter.java +++ b/src/main/java/com/appdirect/sdk/vendorFields/converter/LocaleConverter.java @@ -1,6 +1,7 @@ package com.appdirect.sdk.vendorFields.converter; import java.beans.PropertyEditorSupport; +import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.Locale; @@ -18,6 +19,7 @@ public void setAsText(final String text) throws IllegalArgumentException { .stream() .sorted(Comparator.comparing(Locale.LanguageRange::getWeight).reversed()) .map(localeRange -> Locale.forLanguageTag(localeRange.getRange())) + .filter(locale -> Arrays.asList(Locale.getAvailableLocales()).contains(locale)) .collect(Collectors.toList())); } catch (IllegalArgumentException | NullPointerException e) { throw new PropertyEditorSupportException("Failed to serialize Locale from Accept-Language header with value=%s", text); diff --git a/src/test/java/com/appdirect/sdk/vendorFields/converter/LocaleConverterTest.java b/src/test/java/com/appdirect/sdk/vendorFields/converter/LocaleConverterTest.java index f1956534..b6d23af0 100644 --- a/src/test/java/com/appdirect/sdk/vendorFields/converter/LocaleConverterTest.java +++ b/src/test/java/com/appdirect/sdk/vendorFields/converter/LocaleConverterTest.java @@ -31,7 +31,7 @@ public void setup() { @Test public void testOneLocale() { - String localeToParse = "us"; + String localeToParse = "en"; List expected = Collections.singletonList(Locale.forLanguageTag(localeToParse)); localeConverter.setAsText(localeToParse); @@ -80,6 +80,15 @@ public void testMultipleLocales_orderedByWeight() { assertThat(localeConverter.getValue()).isEqualTo(expected); } + @Test + public void testInvalidLocale_isNotIncludedInList() { + String localeToParse = "us"; + + localeConverter.setAsText(localeToParse); + + assertThat(localeConverter.getValue()).isEqualTo(Collections.emptyList()); + } + @Test public void testNull_throwsPropertyEditorSupportException() { String localeToParse = null;