[Test] Fixed GeolocationCacheTest

This commit is contained in:
Rsl1122 2018-10-13 10:46:11 +03:00
parent de7eb5ddef
commit 50cc9a4304

View File

@ -1,7 +1,9 @@
package com.djrapitops.plan.system.cache; package com.djrapitops.plan.system.cache;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.file.PlanFiles; import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plugin.logging.console.TestPluginLogger; import com.djrapitops.plugin.logging.console.TestPluginLogger;
import org.junit.Before; import org.junit.Before;
@ -12,81 +14,90 @@ import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.mocks.SystemMockUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static junit.framework.TestCase.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when;
/** /**
* Tests for {@link GeolocationCache}.
*
* @author Fuzzlemann * @author Fuzzlemann
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(MockitoJUnitRunner.Silent.class)
public class GeolocationCacheTest { public class GeolocationCacheTest {
private final Map<String, String> ipsToCountries = new HashMap<>(); private static final Map<String, String> TEST_DATA = new HashMap<>();
private static File IP_STORE;
@Mock @Mock
private PlanFiles files; public PlanFiles files;
@Mock @Mock
private PlanConfig config; public PlanConfig config;
private GeolocationCache geolocationCache;
@ClassRule @ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(); public static TemporaryFolder temporaryFolder = new TemporaryFolder();
private GeolocationCache underTest;
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws IOException {
SystemMockUtil.setUp(temporaryFolder.getRoot()) IP_STORE = temporaryFolder.newFile("GeoIP.dat");
.enableConfigSystem() // TemporaryFolder creates the file, which prevents cache from downloading the GeoIP database from the internet.
.enableCacheSystem(); // This is why the file needs to be removed first.
Files.delete(IP_STORE.toPath());
TEST_DATA.put("8.8.8.8", "United States");
TEST_DATA.put("8.8.4.4", "United States");
TEST_DATA.put("4.4.2.2", "United States");
TEST_DATA.put("208.67.222.222", "United States");
TEST_DATA.put("208.67.220.220", "United States");
TEST_DATA.put("205.210.42.205", "Canada");
TEST_DATA.put("64.68.200.200", "Canada");
TEST_DATA.put("0.0.0.0", "Not Known");
TEST_DATA.put("127.0.0.1", "Local Machine");
} }
@Before @Before
public void setUp() throws IOException { public void setUp() throws EnableException {
doReturn(temporaryFolder.newFile("GeoIP.dat")).when(files.getFileFromPluginFolder("GeoIP.dat")); when(config.isTrue(Settings.DATA_GEOLOCATIONS)).thenReturn(true);
geolocationCache = new GeolocationCache(new Locale(), files, config, new TestPluginLogger()); when(files.getFileFromPluginFolder("GeoIP.dat")).thenReturn(IP_STORE);
ipsToCountries.put("8.8.8.8", "United States"); assertTrue(config.isTrue(Settings.DATA_GEOLOCATIONS));
ipsToCountries.put("8.8.4.4", "United States");
ipsToCountries.put("4.4.2.2", "United States"); underTest = new GeolocationCache(new Locale(), files, config, new TestPluginLogger());
ipsToCountries.put("208.67.222.222", "United States"); underTest.enable();
ipsToCountries.put("208.67.220.220", "United States");
ipsToCountries.put("205.210.42.205", "Canada");
ipsToCountries.put("64.68.200.200", "Canada");
ipsToCountries.put("0.0.0.0", "Not Known");
ipsToCountries.put("127.0.0.1", "Local Machine");
} }
@Test @Test
public void testCountryGetting() { public void countryIsFetched() {
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) { for (Map.Entry<String, String> entry : TEST_DATA.entrySet()) {
String ip = entry.getKey(); String ip = entry.getKey();
String expCountry = entry.getValue(); String expCountry = entry.getValue();
String country = geolocationCache.getCountry(ip); String country = underTest.getCountry(ip);
assertEquals(country, expCountry); assertEquals(expCountry, country);
} }
} }
@Test @Test
public void testCaching() { public void callsToCachedIPsReturnCachedEntries() {
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) { for (Map.Entry<String, String> entry : TEST_DATA.entrySet()) {
String ip = entry.getKey(); String ip = entry.getKey();
String expIp = entry.getValue(); String expIp = entry.getValue();
assertFalse(geolocationCache.isCached(ip)); assertFalse(underTest.isCached(ip));
String countrySecondCall = geolocationCache.getCountry(ip); String countrySecondCall = underTest.getCountry(ip);
assertTrue(geolocationCache.isCached(ip)); assertTrue(underTest.isCached(ip));
String countryThirdCall = geolocationCache.getCountry(ip); String countryThirdCall = underTest.getCountry(ip);
assertEquals(countrySecondCall, countryThirdCall); assertSame(countrySecondCall, countryThirdCall);
assertEquals(countryThirdCall, expIp); assertEquals(expIp, countryThirdCall);
} }
} }
} }