@Test public void testUpdateLocation() throws Exception { LocationRepository repository = container.getInjector().getInstance(LocationRepository.class); String id = repository.create(new Location("foo", 0, 0)).getId(); Map<String, Object> location = new HashMap<String, Object>(); location.put("id", id); location.put("name", "test-location"); location.put("longitude", -122.4d); location.put("latitude", 48.5d); ObjectMapper om = new ObjectMapper(); HttpPut updateRequest = new HttpPut("/location/" + id); updateRequest.setEntity( new ByteArrayEntity(om.writeValueAsBytes(location), ContentType.APPLICATION_JSON)); HttpResponse httpResponse = container.execute(updateRequest); assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getStatusLine().getStatusCode()); Map<String, ?> response = om.readValue( EntityUtils.toString(httpResponse.getEntity()), new TypeReference<Map<String, ?>>() {}); assertEquals("test-location", response.get("name")); assertEquals(-122.4d, response.get("longitude")); assertEquals(48.5d, response.get("latitude")); }
@Test public void testDeleteLocation() throws Exception { LocationRepository repository = container.getInjector().getInstance(LocationRepository.class); String id = repository.create(new Location("foo", 0, 0)).getId(); HttpResponse httpResponse = container.execute(new HttpDelete("/location/" + id)); assertEquals(HttpURLConnection.HTTP_NO_CONTENT, httpResponse.getStatusLine().getStatusCode()); }
@Test public void testGetLocation() throws Exception { LocationRepository repository = container.getInjector().getInstance(LocationRepository.class); String id = repository.create(new Location("foo", 0, 0)).getId(); HttpResponse httpResponse = container.execute(new HttpGet("/location/" + id)); assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getStatusLine().getStatusCode()); HttpEntity entity = httpResponse.getEntity(); assertEquals(ContentType.APPLICATION_JSON.getMimeType(), ContentType.get(entity).getMimeType()); String content = EntityUtils.toString(entity); ObjectMapper om = new ObjectMapper(); Map<String, ?> response = om.readValue(content, new TypeReference<Map<String, ?>>() {}); assertEquals(id, response.get("id")); }