Skip to content

Commit 804803c

Browse files
authored
Merge pull request #21 from codice/npe
Added null safety to CCs that do not have numeric mapping
2 parents 9a8315a + 24b5aca commit 804803c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

converter/src/test/java/org/codice/countrycode/CountryCodeSimpleTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertThat;
2525

2626
import java.util.Set;
27+
import org.codice.countrycode.CountryCodeSimple.StandardFormat;
2728
import org.junit.Ignore;
2829
import org.junit.Test;
2930

@@ -101,6 +102,14 @@ public void testConversionNoNumericCode() {
101102
assertThat("IP", equalTo(result.iterator().next()));
102103
}
103104

105+
@Test
106+
public void testConversionNonExistentCode() {
107+
Set<String> result =
108+
CountryCodeSimple.convert(
109+
"ZZ", StandardFormat.ISO_3166_1_ALPHA2, StandardFormat.GENC_3_0_0_ALPHA3);
110+
assertThat(0, equalTo(result.size()));
111+
}
112+
104113
// todo this demonstrates a bug where the input format is ignored by the converter impl
105114
// we should fix this bug
106115
@Test

standards/common/src/main/java/org/codice/countrycode/standards/common/StandardUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package org.codice.countrycode.standards.common;
1515

16+
import java.util.Objects;
1617
import org.codice.countrycode.standard.CountryCode;
1718

1819
public class StandardUtils {
@@ -24,6 +25,8 @@ public static boolean containsFormatValue(CountryCode countryCode, String value)
2425
.getStandard()
2526
.getFormatNames()
2627
.stream()
27-
.anyMatch(formatName -> countryCode.getAsFormat(formatName).equalsIgnoreCase(value));
28+
.map(formatName -> countryCode.getAsFormat(formatName))
29+
.filter(Objects::nonNull)
30+
.anyMatch(formatName -> formatName.equalsIgnoreCase(value));
2831
}
2932
}

0 commit comments

Comments
 (0)