|
3 | 3 | import java.io.File; |
4 | 4 | import java.io.IOException; |
5 | 5 | import java.net.InetAddress; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
6 | 9 |
|
| 10 | +import com.google.common.collect.ImmutableMap; |
7 | 11 | import com.maxmind.geoip2.DatabaseReader; |
8 | 12 | import com.maxmind.geoip2.model.CountryResponse; |
| 13 | +import com.maxmind.geoip2.record.Continent; |
9 | 14 | import com.maxmind.geoip2.record.Country; |
10 | 15 | import fr.xephi.authme.settings.Settings; |
11 | 16 | import fr.xephi.authme.settings.properties.ProtectionSettings; |
@@ -58,12 +63,7 @@ public void shouldGetCountry() throws Exception { |
58 | 63 | // given |
59 | 64 | InetAddress ip = InetAddress.getByName("123.45.67.89"); |
60 | 65 | String countryCode = "XX"; |
61 | | - |
62 | | - Country country = mock(Country.class); |
63 | | - given(country.getIsoCode()).willReturn(countryCode); |
64 | | - |
65 | | - CountryResponse response = mock(CountryResponse.class); |
66 | | - given(response.getCountry()).willReturn(country); |
| 66 | + CountryResponse response = createCountryResponse(countryCode, "Unknown"); |
67 | 67 | given(lookupService.country(ip)).willReturn(response); |
68 | 68 | given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true); |
69 | 69 |
|
@@ -93,12 +93,7 @@ public void shouldLookUpCountryName() throws Exception { |
93 | 93 | // given |
94 | 94 | InetAddress ip = InetAddress.getByName("24.45.167.89"); |
95 | 95 | String countryName = "Ecuador"; |
96 | | - |
97 | | - Country country = mock(Country.class); |
98 | | - given(country.getName()).willReturn(countryName); |
99 | | - |
100 | | - CountryResponse response = mock(CountryResponse.class); |
101 | | - given(response.getCountry()).willReturn(country); |
| 96 | + CountryResponse response = createCountryResponse("EC", countryName); |
102 | 97 | given(lookupService.country(ip)).willReturn(response); |
103 | 98 | given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true); |
104 | 99 |
|
@@ -136,4 +131,13 @@ public void shouldNotLookUpCountryNameIfDisabled() throws Exception { |
136 | 131 | assertThat(result, equalTo("N/A")); |
137 | 132 | verifyNoInteractions(lookupService); |
138 | 133 | } |
| 134 | + |
| 135 | + private static CountryResponse createCountryResponse(String countryCode, String countryName) { |
| 136 | + List<String> locales = Collections.singletonList("en"); |
| 137 | + Continent continent = new Continent(locales, "XX", 1L, Collections.emptyMap()); |
| 138 | + |
| 139 | + Map<String, String> countryNames = ImmutableMap.of("en", countryName); |
| 140 | + Country country = new Country(locales, 100, 3L, false, countryCode, countryNames); |
| 141 | + return new CountryResponse(continent, country, null, country, null, null); |
| 142 | + } |
139 | 143 | } |
0 commit comments