package org.geo.spatialsearch.rest.integration;

import static junit.framework.Assert.assertEquals;

import junit.framework.Assert;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import com.sun.jersey.api.client.ClientResponse;

public class CensusResourceTest extends RestfulTest {

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @Test
    public void testFindStateByCoordinates() throws JSONException {
        ClientResponse response = this.webResource.path("census").path("state2010")
                .queryParam("latitude", "42.919")
                .queryParam("longitude", "-75.2517")
                .queryParam("format", "json").get(ClientResponse.class);
        assertEquals(200, response.getStatus()); // 200 = OK

        String json = response.getEntity(String.class);
        JSONObject results = new JSONObject(json);
        JSONObject jo = (JSONObject) results.get("Results");
        JSONArray states = (JSONArray) jo.get("state");
        Assert.assertFalse(states.isNull(0));
        JSONObject state = (JSONObject) states.get(0);

        assertEquals("36", state.get("geoid"));
        assertEquals("New York", state.get("name"));
        assertEquals("NY", state.get("stusps"));
    }

}

@@ -420,20 +420,17 @@
 Test %7B%0A%0A
-    
+%09
 @Overrid
@@ -435,20 +435,17 @@
 ide%0A
-    
+%09
 @Before%0A
     
@@ -440,20 +440,17 @@
 @Before%0A
-    
+%09
 public v
@@ -480,24 +480,18 @@
 ption %7B%0A
-        
+%09%09
 super.se
@@ -501,29 +501,20 @@
 ();%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 @Test%0A
-    
+%09
 publ
@@ -573,24 +573,18 @@
 ption %7B%0A
-        
+%09%09
 ClientRe
@@ -632,16 +632,21 @@
 census%22)
+%0A%09%09%09%09
 .path(%22s
@@ -655,33 +655,16 @@
 te2010%22)
-%0A                
 .queryPa
@@ -689,32 +689,20 @@
 2.919%22)%0A
-                
+%09%09%09%09
 .queryPa
@@ -734,24 +734,12 @@
 7%22)%0A
-                
+%09%09%09%09
 .que
@@ -791,24 +791,18 @@
 class);%0A
-        
+%09%09
 assertEq
@@ -847,24 +847,18 @@
 0 = OK%0A%0A
-        
+%09%09
 String j
@@ -897,24 +897,18 @@
 class);%0A
-        
+%09%09
 JSONObje
@@ -942,24 +942,18 @@
 (json);%0A
-        
+%09%09
 JSONObje
@@ -997,24 +997,18 @@
 ults%22);%0A
-        
+%09%09
 JSONArra
@@ -1047,24 +1047,18 @@
 tate%22);%0A
-        
+%09%09
 Assert.a
@@ -1087,24 +1087,18 @@
 ll(0));%0A
-        
+%09%09
 JSONObje
@@ -1137,24 +1137,18 @@
 et(0);%0A%0A
-        
+%09%09
 assertEq
@@ -1175,32 +1175,26 @@
 (%22geoid%22));%0A
-        
+%09%09
 assertEquals
@@ -1230,62 +1230,838 @@
 ));%0A
-        assertEquals(%22NY%22, state.get(%22stusps%22));%0A    %7D%0A
+%09%09assertEquals(%22NY%22, state.get(%22stusps%22));%0A%09%7D%0A%0A%09@Test%0A%09public void testFindBlockByCoordinates() throws JSONException %7B%0A%09%09ClientResponse response = this.webResource.path(%22census%22)%0A%09%09%09%09.path(%22block2010%22).queryParam(%22latitude%22, %2242.649%22)%0A%09%09%09%09.queryParam(%22longitude%22, %22 -73.781%22)%0A%09%09%09%09.queryParam(%22format%22, %22json%22).get(ClientResponse.class);%0A%09%09assertEquals(200, response.getStatus()); // 200 = OK%0A%0A%09%09String json = response.getEntity(String.class);%0A%09%09JSONObject results = new JSONObject(json);%0A%09%09JSONObject jo = (JSONObject) results.get(%22Results%22);%0A%09%09JSONArray blocks = (JSONArray) jo.get(%22block%22);%0A%09%09Assert.assertFalse(blocks.isNull(0));%0A%09%09JSONObject block = (JSONObject) blocks.get(0);%0A%0A%09%09assertEquals(%22360010021002003%22, block.get(%22geoid%22));%0A%09%09assertEquals(%2236%22, block.get(%22statefp%22));%0A%09%09assertEquals(%22001%22, block.get(%22countyfp%22));%0A%0A%09%7D
 %0A%7D%0A

Add find block by coordinate integration test