1
0
Fork 0

Merge pull request '[dedup] avoid NPEs in the countryInference dedup utility' (#475) from dedup_countryInference_NPE into beta

Reviewed-on: D-Net/dnet-hadoop#475
This commit is contained in:
Claudio Atzori 2024-10-25 10:12:06 +02:00
commit d3764265d5
2 changed files with 11 additions and 3 deletions

View File

@ -90,7 +90,7 @@ public class AbstractPaceFunctions extends PaceCommonUtils {
inferFrom = normalize(inferFrom); inferFrom = normalize(inferFrom);
inferFrom = filterAllStopWords(inferFrom); inferFrom = filterAllStopWords(inferFrom);
Set<String> cities = getCities(inferFrom, 4); Set<String> cities = getCities(inferFrom, 4);
return citiesToCountry(cities).stream().findFirst().orElse("UNKNOWN"); return citiesToCountry(cities).stream().filter(Objects::nonNull).findFirst().orElse("UNKNOWN");
} }
public static String cityInference(String original) { public static String cityInference(String original) {

View File

@ -1,8 +1,7 @@
package eu.dnetlib.pace.common; package eu.dnetlib.pace.common;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
@ -54,8 +53,17 @@ public class PaceFunctionTest extends AbstractPaceFunctions {
System.out.println("Fixed aliases : " + fixAliases(TEST_STRING)); System.out.println("Fixed aliases : " + fixAliases(TEST_STRING));
} }
@Test()
public void countryInferenceTest_NPE() {
assertThrows(
NullPointerException.class,
() -> countryInference("UNKNOWN", null),
"Expected countryInference() to throw an NPE");
}
@Test @Test
public void countryInferenceTest() { public void countryInferenceTest() {
assertEquals("UNKNOWN", countryInference("UNKNOWN", ""));
assertEquals("IT", countryInference("UNKNOWN", "Università di Bologna")); assertEquals("IT", countryInference("UNKNOWN", "Università di Bologna"));
assertEquals("UK", countryInference("UK", "Università di Bologna")); assertEquals("UK", countryInference("UK", "Università di Bologna"));
assertEquals("IT", countryInference("UNKNOWN", "Universiteé de Naples")); assertEquals("IT", countryInference("UNKNOWN", "Universiteé de Naples"));