package brooklyn.location.geo;

import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
import brooklyn.location.basic.SshMachineLocation;

public class HostGeoLookupIntegrationTest {

    public static final Logger log = LoggerFactory.getLogger(HostGeoLookupIntegrationTest.class);
    
    @Test(groups = "Integration")
    public void testLocalhostGetsLocation() throws Exception {
        SshMachineLocation l = new LocalhostMachineProvisioningLocation().obtain();
        HostGeoInfo geo = HostGeoInfo.fromLocation(l);
        log.info("localhost is in "+geo);
        Assert.assertNotNull(geo, "couldn't load data; must be online and with credit with the HostGeoLookup impl (e.g. GeoBytes)");
        Assert.assertTrue(-90 <= geo.latitude && geo.latitude <= 90); 
    }

    @Test(groups = "Integration")
    public void testGeobytesLookup() throws Exception {
        HostGeoInfo geo = new GeoBytesHostGeoLookup().getHostGeoInfo(InetAddress.getByName("geobytes.com"));
        Assert.assertEquals(geo.displayName, "Baltimore (US)");
        Assert.assertEquals(geo.latitude, 39.2894, 0.1);
        Assert.assertEquals(geo.longitude, -76.6384, 0.1);
    }

    @Test(groups = "Integration")
    public void testUtraceLookup() throws Exception {
        HostGeoInfo geo = new UtraceHostGeoLookup().getHostGeoInfo(InetAddress.getByName("utrace.de"));
        Assert.assertTrue(geo.displayName.contains("(DE)"));
        Assert.assertEquals(geo.latitude, 51, 2);
        Assert.assertEquals(geo.longitude, 9, 5);
    }

    @Test(groups = "Integration")
    public void testMaxmindLookup() throws Exception {
        HostGeoInfo geo = new MaxMindHostGeoLookup().getHostGeoInfo(InetAddress.getByName("maxmind.com"));
        log.info("maxmind.com at "+geo);
        Assert.assertEquals(geo.displayName, "Washington, DC (US)");
        Assert.assertEquals(geo.latitude, 38.90, 0.1);
        Assert.assertEquals(geo.longitude, -77.02, 0.1);
    }

}

@@ -175,16 +175,56 @@
 .Test;%0A%0A
+import com.google.common.base.Objects;%0A%0A
 import b
@@ -1956,32 +1956,142 @@
 %22+geo);%0A        
+%0A        // used to be Washington; now Dalas - in case this is temporary failover will accept either!%0A//      
 Assert.assertEqu
@@ -2131,32 +2131,32 @@
  DC (US)%22);%0A
- 
+//
       
- 
 Assert.asser
@@ -2182,34 +2182,34 @@
 e, 38.90, 0.1);%0A
-  
+//
       Assert.ass
@@ -2251,13 +2251,442 @@
 1);%0A    
+   
+ %0A        Assert.assertTrue(Objects.equal(geo.displayName, %22Washington, DC (US)%22) %7C%7C Objects.equal(geo.displayName, %22Dallas, TX (US)%22), %22name=%22+geo.displayName);%0A        Assert.assertTrue(Math.abs(geo.latitude - 38.90) %3C= 0.1 %7C%7C Math.abs(geo.latitude - 32.78) %3C= 0.1, %22lat=%22+geo.latitude);%0A        Assert.assertTrue(Math.abs(geo.longitude - -77.02) %3C= 0.1 %7C%7C Math.abs(geo.longitude - -96.82) %3C= 0.1, %22lon=%22+geo.longitude);%0A    
 %7D
-%0A
 %0A%7D%0A

Fix HostGeoLookupIntegrationTest