/*
* Copyright 2009 Johan Maasing.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package com.google.code.pj2r.resources;
import com.google.code.pj2r.Marshaller;
import com.google.code.pj2r.beanutils.BeanUtilsMarshaller;
import com.google.code.pj2r.resources.mocks.ConflictResource;
import com.google.code.pj2r.resources.mocks.MockNullListResouce;
import com.google.code.pj2r.resources.mocks.MockResource;
import com.google.code.pj2r.resources.mocks.MockResourceFactory;
import com.google.code.pj2r.resources.mocks.UnsafeIDMockResource;
import com.google.code.pj2r.resources.mocks.request.MockHeadersHttpServletRequest;
import com.google.code.pj2r.resources.mocks.response.WritableHttpServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author joma7188
*/
public class ResourceDispatcherDelegateTest {
public ResourceDispatcherDelegateTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
/**
* Test of doGet method, of class ResourceDispatcherDelegate.
*/
@Test
public void testDoGetList() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock", "GET");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doGet(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("application/json; charset=UTF-8", response.getContentType());
assertTrue(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
assertEquals(MockResource.TEST_ARRAY, response.getContent());
}
@Test
public void testDoGetListWithNullResource() throws Exception {
MockNullListResouce resource = new MockNullListResouce();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock", "GET");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doGet(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("application/json; charset=UTF-8", response.getContentType());
assertTrue(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
assertEquals("[]", response.getContent());
}
@Test
public void testDoGetObject() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "GET");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doGet(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("application/json; charset=UTF-8", response.getContentType());
assertFalse(resource.isList());
assertTrue(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
assertEquals(MockResource.TEST_OBJECT, response.getContent());
}
/**
* Test of doPost method, of class ResourceDispatcherDelegate.
*/
@Test
public void testDoPost() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock", "POST");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPost(request, response);
assertEquals("Status is created", HttpServletResponse.SC_CREATED, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertTrue(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
String locationHeader = response.getHeader("Location");
assertNotNull("Location header is set", locationHeader);
assertEquals("Location header is correct", "http://localhost/1", locationHeader);
}
@Test
public void testDoPostWithUnsafeID() throws Exception {
UnsafeIDMockResource resource = new UnsafeIDMockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock", "POST");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPost(request, response);
assertEquals("Status is created", HttpServletResponse.SC_CREATED, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertTrue(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
String locationHeader = response.getHeader("Location");
assertNotNull("Location header is set", locationHeader);
assertFalse("Location header is not unencoded",
"http://localhost/ three spaces before and then some really wierd characters in the ID: <>/:".equals(locationHeader));
assertTrue("Location header is encoded",
"http://localhost/+++three+spaces+before+and+then+some+really+wierd+characters+in+the+ID%3A+%C3%85%C3%84%C3%96%3C%3E%2F%3A".equals(locationHeader));
}
/**
* Test of doDelete method, of class ResourceDispatcherDelegate.
*/
@Test
public void testDoDelete() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "DELETE");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doDelete(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertTrue(resource.isDelete());
assertFalse(resource.isUpdate());
assertEquals("1", resource.getDeleteId());
}
/**
* Test of doPut method, of class ResourceDispatcherDelegate.
*/
@Test
public void testDoPut() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "PUT");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPut(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertTrue(resource.isUpdate());
assertEquals("1", resource.getUpdateId());
}
@Test
public void testDoPutWithConflict() throws Exception {
ConflictResource resource = new ConflictResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "PUT");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPut(request, response);
assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertTrue(resource.isUpdate());
assertEquals("1", resource.getUpdateId());
}
@Test
public void testDoPutWithAcceptAllHeader() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "PUT");
request.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPut(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertTrue(resource.isUpdate());
assertEquals("1", resource.getUpdateId());
}
@Test
public void testDoPutWithAcceptJSONHeader() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "PUT");
request.putHeader("Accept", "application/json");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPut(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertTrue(resource.isUpdate());
assertEquals("1", resource.getUpdateId());
}
@Test
public void testDoPutWithUnAcceptableHeader() throws Exception {
MockResource resource = new MockResource();
MockResourceFactory factory = new MockResourceFactory(resource);
Marshaller marshaller = new BeanUtilsMarshaller();
MockHeadersHttpServletRequest request = new MockHeadersHttpServletRequest("/mock/1", "PUT");
request.putHeader("Accept", "text/html");
WritableHttpServletResponse response = new WritableHttpServletResponse();
ResourceDispatcherDelegate instance = new ResourceDispatcherDelegate(factory, marshaller);
instance.doPut(request, response);
assertEquals(HttpServletResponse.SC_NOT_ACCEPTABLE, response.getStatus());
// No methods called on resource
assertFalse(resource.isList());
assertFalse(resource.isGet());
assertFalse(resource.isCreate());
assertFalse(resource.isDelete());
assertFalse(resource.isUpdate());
}
}
|