diff --git a/README.md b/README.md index 190765b..a6e6e67 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,36 @@ -# columbus +# Columbus Сервис получения местоположения по IP +При старте поднимает ин мемори базу данных от maxmind.com которая читает из файла *.mmdb. +База maxmind принимает только два вида запросов, дай мне страну по IP, дай мне город по IP. +Ответы содержат id объектов, из одного пространства значейний для разных типов объектов +(Континет, Страна, Подразделение первого уровня, Подразделение второго уровня, Город), +имена и коды обектов на разных языках, и доп информацию. + + +### Найденые проблемы: +* Запрос вида дай мне город, не всегда находит город, и может вернуть только континет или страну. +(например для ip: 89.218.51.9) +* Для некоторых типов обектов(например, подразделений первого уровня - области в РФ) нет своего geoname_id в базе, +хотя подразделение есть и фигирирует в описаниях других объектов. Тестовые запросы в бд +
+select DISTINCT subdivision_1_iso_code, subdivision_1_name
+FROM city_locations_en
+WHERE country_name = 'Russia'
+--return 83 records
+
+select DISTINCT subdivision_1_iso_code, subdivision_1_name
+FROM city_locations_en
+WHERE country_name = 'Russia' and subdivision_2_iso_code is null and city_name is null;
+--return 41 records
+
+ + + + + + + + + -при старте поднимает ин мемори базу данных которая читает из файла и \ No newline at end of file diff --git a/src/test/java/com/rbkmoney/columbus/CityLocationsDaoTest.java b/src/test/java/com/rbkmoney/columbus/CityLocationsDaoTest.java deleted file mode 100644 index 8ecd75a..0000000 --- a/src/test/java/com/rbkmoney/columbus/CityLocationsDaoTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.rbkmoney.columbus; - -import com.rbkmoney.columbus.dao.CityLocationsDao; -import com.rbkmoney.columbus.model.CityLocation; -import com.rbkmoney.columbus.model.Lang; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest -public class CityLocationsDaoTest { - @Autowired - CityLocationsDao cityLocationsDao; - - @Test - public void getLocationByGeoIds(){ - final int kamiziak = 553248; - final int moscow = 524901; - final Integer[] ids = {kamiziak,moscow} ; - List list = cityLocationsDao.getByGeoIds(Set(ids), Lang.RU); - - assertEquals(2, list.size()); - assertTrue(list.get(0).getGeonameId() == ids[0] || list.get(0).getGeonameId() == ids[1]); - assertTrue(list.get(1).getGeonameId() == ids[0] || list.get(1).getGeonameId() == ids[1]); - - assertEquals("Камызяк",getById(list, kamiziak).getCityName()); - assertEquals("Москва",getById(list, moscow).getCityName()); - } - - public static CityLocation getById(List list, int id){ - for(CityLocation cl: list){ - if(cl.getGeonameId() == id){ - return cl; - } - } - - return null; - } - public static Set Set(Integer[] ids){ - return new HashSet(Arrays.asList(ids)); - } - -} diff --git a/src/test/java/com/rbkmoney/columbus/GeoServiceTest.java b/src/test/java/com/rbkmoney/columbus/GeoServiceTest.java index 418aac9..0186d89 100644 --- a/src/test/java/com/rbkmoney/columbus/GeoServiceTest.java +++ b/src/test/java/com/rbkmoney/columbus/GeoServiceTest.java @@ -1,49 +1,80 @@ package com.rbkmoney.columbus; import com.rbkmoney.columbus.dao.CityLocationsDao; +import com.rbkmoney.columbus.dao.GeoIpDao; +import com.rbkmoney.columbus.model.CityLocation; +import com.rbkmoney.columbus.model.Lang; +import com.rbkmoney.columbus.model.LocationInfo; import com.rbkmoney.columbus.service.GeoService; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest -@Ignore //need location names db with data (for example "city_locations_ru") public class GeoServiceTest { + public static final Map IP_TO_CITY = new HashMap<>(); + static { + IP_TO_CITY.put("94.159.54.234", "Moscow"); + IP_TO_CITY.put("212.71.235.130", "London"); + } @Autowired CityLocationsDao cityLocationsDao; @Autowired - GeoService geoEnrichmentService; + GeoIpDao geoIpDao; + + @Autowired + GeoService service; @Test - public void startManyThreads(){ - ExecutorService executorService = Executors.newFixedThreadPool(1000); - for(int i=0; i<1000; i++ ){ - executorService.submit(new MyRannable()); + public void testWrongIp() { + LocationInfo undefinedLocation = geoIpDao.getLocationInfoByIp("null"); + assertEquals("Неизвестно", undefinedLocation.getCity().getNames().get(Lang.RU)); + } + + @Test + public void getLocationByIp(){ + for(String ip: IP_TO_CITY.keySet()){ + LocationInfo locationInfo = service.getLocationByIp(ip); + assertEquals(locationInfo.getCity().getNames().get(Lang.ENG), IP_TO_CITY.get(ip)); } } - class MyRannable implements Runnable{ + @Test + public void getLocationByGeoIds(){ + final int kamiziak = 553248; + final int moscow = 524901; + final Integer[] ids = {kamiziak,moscow} ; + List list = cityLocationsDao.getByGeoIds(Set(ids), Lang.RU); - @Override - public void run() { - System.out.println("Thread started:" + Thread.currentThread().getName() ); - try { - Thread.currentThread().sleep(1000000L); - } catch (InterruptedException e) { - e.printStackTrace(); + assertEquals(2, list.size()); + assertTrue(list.get(0).getGeonameId() == ids[0] || list.get(0).getGeonameId() == ids[1]); + assertTrue(list.get(1).getGeonameId() == ids[0] || list.get(1).getGeonameId() == ids[1]); + + assertEquals("Камызяк",getById(list, kamiziak).getCityName()); + assertEquals("Москва",getById(list, moscow).getCityName()); + } + + private static CityLocation getById(List list, int id){ + for(CityLocation cl: list){ + if(cl.getGeonameId() == id){ + return cl; } - System.out.println("Thread stopped:" + Thread.currentThread().getName() ); - } + + return null; + } + private static Set Set(Integer[] ids){ + return new HashSet(Arrays.asList(ids)); } } diff --git a/src/test/java/com/rbkmoney/columbus/MaxMindDbTest.java b/src/test/java/com/rbkmoney/columbus/MaxMindDbTest.java deleted file mode 100644 index 10d47d0..0000000 --- a/src/test/java/com/rbkmoney/columbus/MaxMindDbTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.rbkmoney.columbus; - -import com.rbkmoney.columbus.dao.GeoIpDao; -import com.rbkmoney.columbus.model.Lang; -import com.rbkmoney.columbus.model.LocationInfo; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import static org.junit.Assert.assertEquals; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest -@Ignore //to run test need correct .mmdb file path in config -public class MaxMindDbTest { - - @Autowired - GeoIpDao geoIpDao; - - @Test - public void testMoscow() { - LocationInfo locationInfoByIp = geoIpDao.getLocationInfoByIp("94.159.54.234"); - assertEquals("Москва", locationInfoByIp.getCity().getNames().get(Lang.RU)); - } - - @Test - public void testWrongIp() { - LocationInfo undefinedLocation = geoIpDao.getLocationInfoByIp("null"); - assertEquals("Неизвестно", undefinedLocation.getCity().getNames().get(Lang.RU)); - } -}