package com.ctrip.ops.sysdev;

import com.ctrip.ops.sysdev.filters.GeoIP2;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.UnsupportedEncodingException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class TestGeoIP2 {
    @Test
    public void testGeoIP2() throws Exception {
        File f = new File("/tmp/GeoLite2-City.mmdb");
        if (!f.exists() || f.isDirectory()) {
            throw new Exception("please put /tmp/GeoLite2-City.mmdb and then test");
        }

        HashMap config = new HashMap() {
            {
                put("source", "clientip");
                put("database", "/tmp/GeoLite2-City.mmdb");
            }
        };

        GeoIP2 geoip2Filter = new GeoIP2(config);

        Map event = new HashMap();
        event.put("clientip", "61.240.136.69");
        event = geoip2Filter.process(event);
        Assert.assertEquals(((Map) event.get("geoip")).get("country_name"), "China");
        Assert.assertEquals(((Map) event.get("geoip")).get("country_code"), "CN");
        Assert.assertEquals(((Map) event.get("geoip")).get("country_isocode"), "CN");
        Assert.assertNull(((Map) event.get("geoip")).get("subdivision_name"));
        Assert.assertNull(((Map) event.get("geoip")).get("city_name"));

        event = new HashMap();
        event.put("clientip", "115.239.211.112");
        event = geoip2Filter.process(event);
        Assert.assertEquals(((Map) event.get("geoip")).get("country_name"), "China");
        Assert.assertEquals(((Map) event.get("geoip")).get("country_code"), "CN");
        Assert.assertEquals(((Map) event.get("geoip")).get("country_isocode"), "CN");
        Assert.assertEquals(((Map) event.get("geoip")).get("city_name"), "Hangzhou");
        Assert.assertEquals(((Map) event.get("geoip")).get("subdivision_name"), "Zhejiang");
        Assert.assertEquals(((Map) event.get("geoip")).get("longitude"), 120.1614);
        Assert.assertEquals(((Map) event.get("geoip")).get("latitude"), 30.2936);

        event = new HashMap();
        event.put("clientip", "10.10.10.10");
        event = geoip2Filter.process(event);
        Assert.assertNull(event.get("geoip"));
        Assert.assertEquals(((ArrayList) event.get("tags")).get(0), "geoipfail");

        config = new HashMap() {
            {
                put("source", "clientip");
                put("database", "/tmp/GeoLite2-City.mmdb");
                put("country_code", false);
                put("latitude", false);
                put("longitude", false);
            }
        };
        geoip2Filter = new GeoIP2(config);
        event = new HashMap();
        event.put("clientip", "115.239.211.112");
        event = geoip2Filter.process(event);

        Assert.assertEquals(((Map) event.get("geoip")).get("country_name"), "China");
        Assert.assertEquals(((Map) event.get("geoip")).get("country_isocode"), "CN");
        Assert.assertEquals(((Map) event.get("geoip")).get("city_name"), "Hangzhou");
        Assert.assertEquals(((Map) event.get("geoip")).get("subdivision_name"), "Zhejiang");
        Assert.assertNull(((Map) event.get("geoip")).get("country_code"));
        Assert.assertNull(((Map) event.get("geoip")).get("latitude"));
        Assert.assertNull(((Map) event.get("geoip")).get("longitude"));
        Assert.assertEquals(((double[]) ((Map) event.get("geoip")).get("location"))[0], 120.1614);
        Assert.assertEquals(((double[]) ((Map) event.get("geoip")).get("location"))[1], 30.2936);
    }
}

@@ -3540,16 +3540,856 @@
 0.2936);
+%0A%0A        f = new File(%22/tmp/GeoLite2-Country.mmdb%22);%0A        if (f.exists() && !f.isDirectory()) %7B%0A            config = new HashMap() %7B%0A                %7B%0A                    put(%22source%22, %22clientip%22);%0A                    put(%22database%22, %22/tmp/GeoLite2-Country.mmdb%22);%0A                %7D%0A            %7D;%0A            geoip2Filter = new GeoIP2(config);%0A%0A            event = new HashMap();%0A            event.put(%22clientip%22, %22115.239.211.112%22);%0A            event = geoip2Filter.process(event);%0A            Assert.assertEquals(((Map) event.get(%22geoip%22)).get(%22country_name%22), %22China%22);%0A            Assert.assertEquals(((Map) event.get(%22geoip%22)).get(%22country_code%22), %22CN%22);%0A            Assert.assertEquals(((Map) event.get(%22geoip%22)).get(%22country_isocode%22), %22CN%22);%0A            Assert.assertNull(((Map) event.get(%22geoip%22)).get(%22city_name%22));%0A        %7D
 %0A    %7D%0A%7D

add testcase for country db