Skip to content

Commit

Permalink
Add missing geocodes for countries
Browse files Browse the repository at this point in the history
Affects issues:
- Fixed #3843
  • Loading branch information
AuroraLS3 committed Nov 30, 2024
1 parent 7de29e7 commit a884e05
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 8 deletions.
34 changes: 32 additions & 2 deletions Plan/common/src/main/resources/assets/plan/geocodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"hong kong": "HKG",
"armenia": "ARM",
"vietnam": "VNM",
"vatican city": "VAT",
"australia": "AUS",
"laos": "LAO",
"aruba": "ABW",
"solomon islands": "SLB",
"turkey": "TUR",
"türkiye": "TUR",
"ukraine": "UKR",
"austria": "AUT",
"united states": "USA",
Expand All @@ -33,7 +35,8 @@
"greece": "GRC",
"paraguay": "PRY",
"palau": "PLW",
"congo, republic of the": "COG",
"congo republic": "COD",
"dr congo": "COG",
"vanuatu": "VUT",
"cyprus": "CYP",
"colombia": "COL",
Expand Down Expand Up @@ -93,6 +96,7 @@
"zimbabwe": "ZWE",
"el salvador": "SLV",
"macedonia": "MKD",
"north macedonia": "MKD",
"saint lucia": "LCA",
"bolivia": "BOL",
"china": "CHN",
Expand Down Expand Up @@ -187,6 +191,7 @@
"tanzania": "TZA",
"grenada": "GRD",
"netherlands": "NLD",
"the netherlands": "NLD",
"sao tome and principe": "STP",
"guam": "GUM",
"eritrea": "ERI",
Expand Down Expand Up @@ -221,5 +226,30 @@
"saint vincent and the grenadines": "VCT",
"tonga": "TON",
"barbados": "BRB",
"new caledonia": "NCL"
"new caledonia": "NCL",
"réunion": "REU",
"mayotte": "MYT",
"martinique": "MTQ",
"guadeloupe": "GLP",
"french guiana": "GUF",
"eswatini": "SWZ",
"cocos (keeling) islands": "CCK",
"macao": "MAC",
"st kitts and nevis": "KNA",
"saint barthélemy": "BLM",
"st vincent and grenadines": "VCT",
"bahamas": "BHS",
"myanmar": "MMR",
"tokelau": "TKL",
"wallis and futuna": "WLF",
"ivory coast": "CIV",
"côte d'ivoire": "CIV",
"gambia": "GMB",
"nauru": "NRU",
"federated states of micronesia": "FSM",
"curaçao": "CUW",
"são tomé and príncipe": "STP",
"turks and caicos islands": "TCA",
"norfolk island": "NFK",
"pitcairn islands": "PCN"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,34 @@
package com.djrapitops.plan.gathering.geolocation;

import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.delivery.rendering.json.graphs.Graphs;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.ConfigSystem;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.DataGatheringSettings;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import extension.FullSystemExtension;
import net.playeranalytics.plugin.server.PluginLogger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import utilities.TestErrorLogger;
import utilities.TestPluginLogger;
import utilities.TestResources;
import utilities.mocks.TestProcessing;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -118,4 +124,38 @@ void callsToCachedIPsReturnCachedEntries() {
assertEquals(expIp, countryThirdCall);
}
}

// Test utility for reading https://cable.ayra.ch/ip/data/countries.json for getting first IP of each country
// Have to manually remove 3 first ones and the IPv6 addresses at the end.
public static void main(String[] args) throws URISyntaxException, IOException {
File testResourceFile = TestResources.getTestResourceFile("countries.json", GeolocationTest.class);
String read = Files.readString(testResourceFile.toPath());
Map<String, Map<String, List<String>>> contents = new Gson().fromJson(new StringReader(read), new TypeToken<>() {}.getType());
List<String> singleIpPerCountry = contents.values().stream()
.map(Map::values)
.map(set -> set.stream().findFirst())
.filter(Optional::isPresent)
.map(Optional::get)
.map(list -> list.get(0))
.map(string -> string.split("/")[0])
.toList();
Path write = new File("src/test/resources/countries-reduced.txt").toPath();
Files.write(write, singleIpPerCountry, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}

@TestFactory
@DisplayName("Country has geocode")
Collection<DynamicTest> everyCountryHasCodeInGeocodesJson(Graphs graphs) throws URISyntaxException, IOException {
Map<String, String> geocodes = graphs.special().getGeocodes();
File testResourceFile = TestResources.getTestResourceFile("countries-reduced.txt", GeolocationTest.class);
try (Stream<String> lines = Files.lines(testResourceFile.toPath())) {
return lines
.map(underTest::getCountry)
.distinct()
.map(country -> DynamicTest.dynamicTest(country, () -> {
assertTrue(geocodes.containsKey(country.toLowerCase()),
() -> "Country '" + country + "' had no geocode associated with it.");
})).toList();
}
}
}
2 changes: 2 additions & 0 deletions Plan/common/src/test/java/extension/FullSystemExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.djrapitops.plan.delivery.DeliveryUtilities;
import com.djrapitops.plan.delivery.export.Exporter;
import com.djrapitops.plan.delivery.formatting.Formatters;
import com.djrapitops.plan.delivery.rendering.json.graphs.Graphs;
import com.djrapitops.plan.delivery.webserver.Addresses;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.ConfigSystem;
Expand Down Expand Up @@ -89,6 +90,7 @@ public FullSystemExtension() {
.put(PublicHtmlFiles.class, () -> planSystem.getDeliveryUtilities().getPublicHtmlFiles())
.put(Webserver.class, () -> planSystem.getWebServerSystem().getWebServer())
.put(Exporter.class, () -> planSystem.getExportSystem().getExporter())
.put(Graphs.class, () -> planSystem.getDeliveryUtilities().getGraphs())
.build();
}

Expand Down
Loading

0 comments on commit a884e05

Please sign in to comment.