|
17 | 17 | package com.djrapitops.plan.gathering.geolocation; |
18 | 18 |
|
19 | 19 | import com.djrapitops.plan.PlanSystem; |
| 20 | +import com.djrapitops.plan.delivery.rendering.json.graphs.Graphs; |
20 | 21 | import com.djrapitops.plan.processing.Processing; |
21 | 22 | import com.djrapitops.plan.settings.ConfigSystem; |
22 | 23 | import com.djrapitops.plan.settings.config.PlanConfig; |
23 | 24 | import com.djrapitops.plan.settings.config.paths.DataGatheringSettings; |
24 | 25 | import com.djrapitops.plan.settings.locale.Locale; |
25 | 26 | import com.djrapitops.plan.storage.file.PlanFiles; |
| 27 | +import com.google.gson.Gson; |
| 28 | +import com.google.gson.reflect.TypeToken; |
26 | 29 | import extension.FullSystemExtension; |
27 | 30 | import net.playeranalytics.plugin.server.PluginLogger; |
28 | | -import org.junit.jupiter.api.AfterEach; |
29 | | -import org.junit.jupiter.api.BeforeAll; |
30 | | -import org.junit.jupiter.api.BeforeEach; |
31 | | -import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.*; |
32 | 32 | import org.junit.jupiter.api.extension.ExtendWith; |
33 | 33 | import org.mockito.junit.jupiter.MockitoExtension; |
34 | 34 | import utilities.TestErrorLogger; |
35 | 35 | import utilities.TestPluginLogger; |
| 36 | +import utilities.TestResources; |
36 | 37 | import utilities.mocks.TestProcessing; |
37 | 38 |
|
| 39 | +import java.io.File; |
38 | 40 | import java.io.IOException; |
| 41 | +import java.io.StringReader; |
| 42 | +import java.net.URISyntaxException; |
39 | 43 | import java.nio.file.Files; |
40 | | -import java.util.HashMap; |
41 | | -import java.util.Map; |
| 44 | +import java.nio.file.Path; |
| 45 | +import java.nio.file.StandardOpenOption; |
| 46 | +import java.util.*; |
| 47 | +import java.util.stream.Stream; |
42 | 48 |
|
43 | 49 | import static org.junit.jupiter.api.Assertions.*; |
44 | 50 |
|
@@ -118,4 +124,38 @@ void callsToCachedIPsReturnCachedEntries() { |
118 | 124 | assertEquals(expIp, countryThirdCall); |
119 | 125 | } |
120 | 126 | } |
| 127 | + |
| 128 | + // Test utility for reading https://cable.ayra.ch/ip/data/countries.json for getting first IP of each country |
| 129 | + // Have to manually remove 3 first ones and the IPv6 addresses at the end. |
| 130 | + public static void main(String[] args) throws URISyntaxException, IOException { |
| 131 | + File testResourceFile = TestResources.getTestResourceFile("countries.json", GeolocationTest.class); |
| 132 | + String read = Files.readString(testResourceFile.toPath()); |
| 133 | + Map<String, Map<String, List<String>>> contents = new Gson().fromJson(new StringReader(read), new TypeToken<>() {}.getType()); |
| 134 | + List<String> singleIpPerCountry = contents.values().stream() |
| 135 | + .map(Map::values) |
| 136 | + .map(set -> set.stream().findFirst()) |
| 137 | + .filter(Optional::isPresent) |
| 138 | + .map(Optional::get) |
| 139 | + .map(list -> list.get(0)) |
| 140 | + .map(string -> string.split("/")[0]) |
| 141 | + .toList(); |
| 142 | + Path write = new File("src/test/resources/countries-reduced.txt").toPath(); |
| 143 | + Files.write(write, singleIpPerCountry, StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 144 | + } |
| 145 | + |
| 146 | + @TestFactory |
| 147 | + @DisplayName("Country has geocode") |
| 148 | + Collection<DynamicTest> everyCountryHasCodeInGeocodesJson(Graphs graphs) throws URISyntaxException, IOException { |
| 149 | + Map<String, String> geocodes = graphs.special().getGeocodes(); |
| 150 | + File testResourceFile = TestResources.getTestResourceFile("countries-reduced.txt", GeolocationTest.class); |
| 151 | + try (Stream<String> lines = Files.lines(testResourceFile.toPath())) { |
| 152 | + return lines |
| 153 | + .map(underTest::getCountry) |
| 154 | + .distinct() |
| 155 | + .map(country -> DynamicTest.dynamicTest(country, () -> { |
| 156 | + assertTrue(geocodes.containsKey(country.toLowerCase()), |
| 157 | + () -> "Country '" + country + "' had no geocode associated with it."); |
| 158 | + })).toList(); |
| 159 | + } |
| 160 | + } |
121 | 161 | } |
0 commit comments