diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMap.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMap.java index 85ac2d759..dcfff65e2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMap.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMap.java @@ -23,7 +23,6 @@ import org.apache.commons.text.TextStringBuilder; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * World Map that uses iso-a3 specification of Country codes. @@ -59,8 +58,19 @@ public class WorldMap implements HighChart { private Map toGeoCodeCounts(Map geolocationCounts) { Map geoCodes = getGeoCodes(); - return geolocationCounts.entrySet().stream() - .collect(Collectors.toMap(entry -> geoCodes.get(entry.getKey().toLowerCase()), Map.Entry::getValue)); + Map geoCodeCounts = new HashMap<>(); + + for (Map.Entry entry : geolocationCounts.entrySet()) { + String geolocation = entry.getKey().toLowerCase(); + String geoCode = geoCodes.get(geolocation); + if (geoCode == null) { + continue; + } + + geoCodeCounts.put(geoCode, entry.getValue()); + } + + return geoCodeCounts; } private Map toGeoCodeCounts(List geoLocations) { diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/MySQLTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/MySQLTest.java index b2c86db5c..1d1b68620 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/MySQLTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/MySQLTest.java @@ -75,6 +75,9 @@ public class MySQLTest extends CommonDBTest { UUID firstUuid = UUID.randomUUID(); UUID secondUuid = UUID.randomUUID(); UUID thirdUuid = UUID.randomUUID(); + UUID fourthUuid = UUID.randomUUID(); + UUID fifthUuid = UUID.randomUUID(); + UUID sixthUuid = UUID.randomUUID(); db.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, "")); db.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, "")); @@ -84,6 +87,9 @@ public class MySQLTest extends CommonDBTest { saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5, "3")); saveGeoInfo(secondUuid, new GeoInfo("-", "Sweden", 0, "3")); saveGeoInfo(thirdUuid, new GeoInfo("-", "Denmark", 0, "3")); + saveGeoInfo(fourthUuid, new GeoInfo("-", "Denmark", 0, "3")); + saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0, "3")); + saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0, "3")); Map got = db.query(ServerAggregateQueries.networkGeolocationCounts()); @@ -91,7 +97,9 @@ public class MySQLTest extends CommonDBTest { // first user has a more recent connection from Finland so their country should be counted as Finland. expected.put("Finland", 1); expected.put("Sweden", 1); - expected.put("Denmark", 1); + expected.put("Not Known", 1); + expected.put("Local Machine", 1); + expected.put("Denmark", 2); assertEquals(expected, got); } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/SQLiteTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/SQLiteTest.java index b63b0515f..95a103a37 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/SQLiteTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/SQLiteTest.java @@ -92,6 +92,9 @@ public class SQLiteTest extends CommonDBTest { UUID firstUuid = UUID.randomUUID(); UUID secondUuid = UUID.randomUUID(); UUID thirdUuid = UUID.randomUUID(); + UUID fourthUuid = UUID.randomUUID(); + UUID fifthUuid = UUID.randomUUID(); + UUID sixthUuid = UUID.randomUUID(); db.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, "")); db.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, "")); @@ -101,6 +104,9 @@ public class SQLiteTest extends CommonDBTest { saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5, "3")); saveGeoInfo(secondUuid, new GeoInfo("-", "Sweden", 0, "3")); saveGeoInfo(thirdUuid, new GeoInfo("-", "Denmark", 0, "3")); + saveGeoInfo(fourthUuid, new GeoInfo("-", "Denmark", 0, "3")); + saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0, "3")); + saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0, "3")); Map got = db.query(ServerAggregateQueries.networkGeolocationCounts()); @@ -108,7 +114,9 @@ public class SQLiteTest extends CommonDBTest { // first user has a more recent connection from Finland so their country should be counted as Finland. expected.put("Finland", 1); expected.put("Sweden", 1); - expected.put("Denmark", 1); + expected.put("Not Known", 1); + expected.put("Local Machine", 1); + expected.put("Denmark", 2); assertEquals(expected, got); } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMapTest.java b/Plan/common/src/test/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMapTest.java new file mode 100644 index 000000000..1fe219e4c --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/utilities/html/graphs/special/WorldMapTest.java @@ -0,0 +1,50 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.html.graphs.special; + +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test against exceptions due to unnamed geolocations. + * + * @author Rsl1122 + */ +@RunWith(JUnitPlatform.class) +class WorldMapTest { + + @Test + void toGeolocationCountsCausesNoException() { + Map geolocations = new HashMap<>(); + geolocations.put("Finland", 1); + geolocations.put("Sweden", 1); + geolocations.put("Not Known", 1); + geolocations.put("Local Machine", 1); + geolocations.put("Denmark", 2); + + String expected = "[{'code':'SWE','value':1},{'code':'DNK','value':2},{'code':'FIN','value':1}]"; + String result = new WorldMap(geolocations).toHighChartsSeries(); + assertEquals(expected, result); + } + +} \ No newline at end of file