/java/RestActionInvocationTest.java

https://github.com/nathanielksmith/javahaiku · Java · 270 lines · 186 code · 54 blank · 30 comment · 0 complexity · c7e24dbca367511d92b0efee37c52ce3 MD5 · raw file

  1. package org.apache.struts2.rest;
  2. import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.http.HttpServletResponse;
  8. import junit.framework.TestCase;
  9. import org.apache.struts2.ServletActionContext;
  10. import org.apache.struts2.dispatcher.HttpHeaderResult;
  11. import org.springframework.mock.web.MockHttpServletRequest;
  12. import org.springframework.mock.web.MockHttpServletResponse;
  13. import com.opensymphony.xwork2.ActionContext;
  14. import com.opensymphony.xwork2.DefaultUnknownHandlerManager;
  15. import com.opensymphony.xwork2.ModelDriven;
  16. import com.opensymphony.xwork2.ObjectFactory;
  17. import com.opensymphony.xwork2.config.ConfigurationException;
  18. import com.opensymphony.xwork2.config.entities.ActionConfig;
  19. import com.opensymphony.xwork2.config.entities.InterceptorMapping;
  20. import com.opensymphony.xwork2.config.entities.ResultConfig;
  21. import com.opensymphony.xwork2.mock.MockActionProxy;
  22. import com.opensymphony.xwork2.mock.MockInterceptor;
  23. import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
  24. public class RestActionInvocationTest extends TestCase {
  25. RestActionInvocation restActionInvocation;
  26. MockHttpServletRequest request;
  27. MockHttpServletResponse response;
  28. @Override
  29. protected void setUp() throws Exception {
  30. super.setUp();
  31. restActionInvocation = new RestActionInvocationTester();
  32. request = new MockHttpServletRequest();
  33. response = new MockHttpServletResponse();
  34. ServletActionContext.setRequest(request);
  35. ServletActionContext.setResponse(response);
  36. }
  37. /**
  38. * Test the correct action results: null, String, HttpHeaders, Result
  39. * @throws Exception
  40. */
  41. public void testSaveResult() throws Exception {
  42. Object methodResult = "index";
  43. ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
  44. assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
  45. setUp();
  46. methodResult = new DefaultHttpHeaders("show");
  47. assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
  48. assertEquals(methodResult, restActionInvocation.httpHeaders);
  49. setUp();
  50. methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
  51. assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
  52. assertEquals(methodResult, restActionInvocation.createResult());
  53. setUp();
  54. try {
  55. methodResult = new Object();
  56. restActionInvocation.saveResult(actionConfig, methodResult);
  57. // ko
  58. assertFalse(true);
  59. } catch (ConfigurationException c) {
  60. // ok, object not allowed
  61. }
  62. }
  63. /**
  64. * Test the target selection: exception, error messages, model and null
  65. * @throws Exception
  66. */
  67. public void testSelectTarget() throws Exception {
  68. // Exception
  69. Exception e = new Exception();
  70. restActionInvocation.getStack().set("exception", e);
  71. restActionInvocation.selectTarget();
  72. assertEquals(e, restActionInvocation.target);
  73. // Error messages
  74. setUp();
  75. String actionMessage = "Error!";
  76. RestActionSupport action = (RestActionSupport)restActionInvocation.getAction();
  77. action.addActionError(actionMessage);
  78. Map errors = new HashMap();
  79. List<String> list = new ArrayList<String>();
  80. list.add(actionMessage);
  81. errors.put("actionErrors", list);
  82. restActionInvocation.selectTarget();
  83. assertEquals(errors, restActionInvocation.target);
  84. // Model with get and no content in post, put, delete
  85. setUp();
  86. RestAction restAction = (RestAction)restActionInvocation.getAction();
  87. List<String> model = new ArrayList<String>();
  88. model.add("Item");
  89. restAction.model = model;
  90. request.setMethod("GET");
  91. restActionInvocation.selectTarget();
  92. assertEquals(model, restActionInvocation.target);
  93. request.setMethod("POST");
  94. restActionInvocation.selectTarget();
  95. assertEquals(null, restActionInvocation.target);
  96. request.setMethod("PUT");
  97. restActionInvocation.selectTarget();
  98. assertEquals(null, restActionInvocation.target);
  99. request.setMethod("DELETE");
  100. restActionInvocation.selectTarget();
  101. assertEquals(null, restActionInvocation.target);
  102. }
  103. /**
  104. * Test the not modified status code.
  105. * @throws Exception
  106. */
  107. public void testResultNotModified() throws Exception {
  108. request.addHeader("If-None-Match", "123");
  109. request.setMethod("GET");
  110. RestAction restAction = (RestAction)restActionInvocation.getAction();
  111. List<String> model = new ArrayList<String>() {
  112. @Override
  113. public int hashCode() {
  114. return 123;
  115. }
  116. };
  117. model.add("Item");
  118. restAction.model = model;
  119. restActionInvocation.processResult();
  120. assertEquals(SC_NOT_MODIFIED, response.getStatus());
  121. }
  122. /**
  123. * Test the default error result.
  124. * @throws Exception
  125. */
  126. public void testDefaultErrorResult() throws Exception {
  127. // Exception
  128. Exception e = new Exception();
  129. restActionInvocation.getStack().set("exception", e);
  130. request.setMethod("GET");
  131. RestAction restAction = (RestAction)restActionInvocation.getAction();
  132. List<String> model = new ArrayList<String>();
  133. model.add("Item");
  134. restAction.model = model;
  135. restActionInvocation.setDefaultErrorResultName("default-error");
  136. ResultConfig resultConfig = new ResultConfig.Builder("default-error",
  137. "org.apache.struts2.dispatcher.HttpHeaderResult")
  138. .addParam("status", "123").build();
  139. ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
  140. "RestAction", "org.apache.rest.RestAction")
  141. .addResultConfig(resultConfig)
  142. .build();
  143. ((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
  144. restActionInvocation.processResult();
  145. assertEquals(123, response.getStatus());
  146. }
  147. public void testNoResult() throws Exception {
  148. RestAction restAction = (RestAction)restActionInvocation.getAction();
  149. List<String> model = new ArrayList<String>();
  150. model.add("Item");
  151. restAction.model = model;
  152. request.setMethod("GET");
  153. restActionInvocation.setResultCode("index");
  154. try {
  155. restActionInvocation.processResult();
  156. // ko
  157. assertFalse(true);
  158. } catch (ConfigurationException c) {
  159. // ok, no result
  160. }
  161. }
  162. /**
  163. * Test the global execution
  164. * @throws Exception
  165. */
  166. public void testInvoke() throws Exception {
  167. // Default index method return 'success'
  168. ((MockActionProxy)restActionInvocation.getProxy()).setMethod("index");
  169. // Define result 'success'
  170. ResultConfig resultConfig = new ResultConfig.Builder("success",
  171. "org.apache.struts2.dispatcher.HttpHeaderResult")
  172. .addParam("status", "123").build();
  173. ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
  174. "RestAction", "org.apache.rest.RestAction")
  175. .addResultConfig(resultConfig)
  176. .build();
  177. ((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
  178. request.setMethod("GET");
  179. restActionInvocation.invoke();
  180. assertEquals(123, response.getStatus());
  181. }
  182. class RestActionInvocationTester extends RestActionInvocation {
  183. RestActionInvocationTester() {
  184. super(new HashMap<String,String>(), true);
  185. List<InterceptorMapping> interceptorMappings = new ArrayList<InterceptorMapping>();
  186. MockInterceptor mockInterceptor = new MockInterceptor();
  187. mockInterceptor.setFoo("interceptor");
  188. mockInterceptor.setExpectedFoo("interceptor");
  189. interceptorMappings.add(new InterceptorMapping("interceptor", mockInterceptor));
  190. interceptors = interceptorMappings.iterator();
  191. MockActionProxy actionProxy = new MockActionProxy();
  192. ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
  193. "RestAction", "org.apache.rest.RestAction").build();
  194. actionProxy.setConfig(actionConfig);
  195. proxy = actionProxy;
  196. action = new RestAction();
  197. setMimeTypeHandlerSelector(new DefaultContentTypeHandlerManager());
  198. unknownHandlerManager = new DefaultUnknownHandlerManager();
  199. try {
  200. XWorkTestCaseHelper.setUp();
  201. } catch (Exception e) {
  202. throw new RuntimeException(e);
  203. }
  204. invocationContext = ActionContext.getContext();
  205. container = ActionContext.getContext().getContainer();
  206. stack = ActionContext.getContext().getValueStack();
  207. objectFactory = container.getInstance(ObjectFactory.class);
  208. }
  209. }
  210. class RestAction extends RestActionSupport implements ModelDriven<List<String>> {
  211. List<String> model;
  212. public List<String> getModel() {
  213. return model;
  214. }
  215. }
  216. }