Skip to content

Commit

Permalink
Updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Aug 2, 2017
1 parent a64da53 commit ee5575d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

package org.apache.struts2.rest;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.rest.handler.ContentTypeHandler;

Expand All @@ -41,6 +44,8 @@
*/
public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManager {

private static final Logger LOG = LogManager.getLogger(DefaultContentTypeHandlerManager.class);

/** ContentTypeHandlers keyed by the extension */
Map<String, ContentTypeHandler> handlersByExtension = new HashMap<String, ContentTypeHandler>();
/** ContentTypeHandlers keyed by the content-type */
Expand Down Expand Up @@ -115,7 +120,7 @@ public ContentTypeHandler getHandlerForRequest(HttpServletRequest request) {

/**
* Gets the handler for the response by looking at the extension of the request
* @param req The request
* @param request The request
* @return The appropriate handler
*
* WW-4588: modified to get a handler for the response side and auto generate the response type
Expand Down Expand Up @@ -153,20 +158,28 @@ private ContentTypeHandler findHandler(final String type) {
return handler;
}

@Override
public String handleResult(ActionConfig actionConfig, Object methodResult, Object target) throws IOException {
LOG.warn("This method is deprecated!");
return readResultCode(methodResult);
}

/**
* Handles the result using handlers to generate content type-specific content
*
* @param actionConfig The action config for the current request
* @param invocation The action invocation for the current request
* @param methodResult The object returned from the action method
* @param target The object to return, usually the action object
* @return The new result code to process
* @throws IOException If unable to write to the response
*/
public String handleResult(ActionConfig actionConfig, Object methodResult, Object target) throws IOException {
public String handleResult(ActionInvocation invocation, Object methodResult, Object target) throws IOException {
String resultCode = readResultCode(methodResult);
Integer statusCode = readStatusCode(methodResult);
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse();
ActionConfig actionConfig = invocation.getProxy().getConfig();

if(statusCode != null) {
res.setStatus(statusCode);
}
Expand All @@ -178,7 +191,7 @@ public String handleResult(ActionConfig actionConfig, Object methodResult, Objec
resultCode = extCode;
} else {
StringWriter writer = new StringWriter();
resultCode = handler.fromObject(target, resultCode, writer);
resultCode = handler.fromObject(invocation, target, resultCode, writer);
String text = writer.toString();
if (text.length() > 0) {
byte[] data = text.getBytes("UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.mock.MockActionProxy;
import junit.framework.TestCase;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.rest.handler.AbstractContentTypeHandler;
import org.apache.struts2.rest.handler.ContentTypeHandler;
import org.apache.struts2.rest.handler.FormUrlEncodedHandler;
import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -49,6 +53,7 @@ public class ContentTypeHandlerManagerTest extends TestCase {
private DefaultContentTypeHandlerManager mgr;
private MockHttpServletResponse mockResponse;
private MockHttpServletRequest mockRequest;
private MockActionInvocation invocation;

@Override
public void setUp() {
Expand All @@ -59,6 +64,9 @@ public void setUp() {
ActionContext.setContext(new ActionContext(new HashMap()));
ServletActionContext.setRequest(mockRequest);
ServletActionContext.setResponse(mockResponse);

invocation = new MockActionInvocation();
invocation.setProxy(new MockActionProxy());
}

@Override
Expand All @@ -71,9 +79,9 @@ public void tearDown() {
public void testHandleResultOK() throws IOException {

String obj = "mystring";
ContentTypeHandler handler = new ContentTypeHandler() {
public void toObject(Reader in, Object target) {}
public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
ContentTypeHandler handler = new AbstractContentTypeHandler() {
public void toObject(ActionInvocation invocation, Reader in, Object target) {}
public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException {
stream.write(obj.toString());
return resultCode;
}
Expand All @@ -82,7 +90,11 @@ public String fromObject(Object obj, String resultCode, Writer stream) throws IO
};
mgr.handlersByExtension.put("xml", handler);
mgr.setDefaultExtension("xml");
mgr.handleResult(new ActionConfig.Builder("", "", "").build(), new DefaultHttpHeaders().withStatus(SC_OK), obj);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "").build();
MockActionProxy proxy = new MockActionProxy();
proxy.setConfig(actionConfig);
invocation.setProxy(proxy);
mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_OK), obj);

assertEquals(obj.getBytes().length, mockResponse.getContentLength());
}
Expand All @@ -92,7 +104,7 @@ public void testHandleResultNotModified() throws IOException {
Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
mockHandlerXml.matchAndReturn("getExtension", "xml");
mgr.handlersByExtension.put("xml", (ContentTypeHandler) mockHandlerXml.proxy());
mgr.handleResult(null, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object());
mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object());

assertEquals(0, mockResponse.getContentLength());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.apache.struts2.rest;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Scope;
import org.apache.struts2.rest.handler.AbstractContentTypeHandler;
import org.apache.struts2.rest.handler.ContentTypeHandler;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -113,13 +115,13 @@ class DummyContainer implements Container {
private ContentTypeHandler handler;

DummyContainer(final String contentType, final String extension) {
handler = new ContentTypeHandler() {
handler = new AbstractContentTypeHandler() {

public void toObject(Reader in, Object target) throws IOException {
public void toObject(ActionInvocation invocation, Reader in, Object target) throws IOException {

}

public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void testCustomValidationFailureStatusCode() throws Exception {
Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getConfig", null);
mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package org.apache.struts2.rest.handler;

import com.opensymphony.xwork2.mock.MockActionInvocation;
import junit.framework.TestCase;

import java.io.IOException;
Expand All @@ -37,7 +38,7 @@ public void testFromObject() throws IOException {

StringWriter writer = new StringWriter();
JacksonLibHandler handler = new JacksonLibHandler();
handler.fromObject(contact, "success", writer);
handler.fromObject(new MockActionInvocation(), contact, "success", writer);
String data = writer.toString();
assertTrue(data.startsWith("{"));
assertTrue(data.contains("\"age\":44"));
Expand All @@ -50,7 +51,7 @@ public void testFromObjectArray() throws IOException {

StringWriter writer = new StringWriter();
JacksonLibHandler handler = new JacksonLibHandler();
handler.fromObject(Arrays.asList(contact), "success", writer);
handler.fromObject(new MockActionInvocation(), Arrays.asList(contact), "success", writer);

String data = writer.toString();
assertTrue(data.startsWith("[{"));
Expand All @@ -65,7 +66,7 @@ public void testToObject() throws IOException {
Contact target = new Contact();
StringReader reader = new StringReader("{\"age\":44,\"important\":true,\"name\":\"bob\"}");
JacksonLibHandler handler = new JacksonLibHandler();
handler.toObject(reader, target);
handler.toObject(new MockActionInvocation(), reader, target);
assertEquals(contact, target);
}

Expand All @@ -78,7 +79,7 @@ public void testToObjectList() throws IOException {
List<Contact> target = new ArrayList<Contact>();
StringReader reader = new StringReader("[{\"age\":44,\"important\":true,\"name\":\"bob\"},{\"age\":33,\"important\":false,\"name\":\"john\"}]");
JacksonLibHandler handler = new JacksonLibHandler();
handler.toObject(reader, target);
handler.toObject(new MockActionInvocation(), reader, target);
assertEquals(source.size(), target.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.StringWriter;
import java.util.Arrays;

import com.opensymphony.xwork2.mock.MockActionInvocation;
import junit.framework.TestCase;

public class JsonLibHandlerTest extends TestCase {
Expand All @@ -35,7 +36,7 @@ public void testFromObject() throws IOException {

StringWriter writer = new StringWriter();
JsonLibHandler handler = new JsonLibHandler();
handler.fromObject(contact, "success", writer);
handler.fromObject(new MockActionInvocation(), contact, "success", writer);
String data = writer.toString();
assertTrue(data.startsWith("{"));
assertTrue(data.contains("\"age\":44"));
Expand All @@ -48,7 +49,7 @@ public void testFromObjectArray() throws IOException {

StringWriter writer = new StringWriter();
JsonLibHandler handler = new JsonLibHandler();
handler.fromObject(Arrays.asList(contact), "success", writer);
handler.fromObject(new MockActionInvocation(), Arrays.asList(contact), "success", writer);

String data = writer.toString();
assertTrue(data.startsWith("[{"));
Expand All @@ -63,7 +64,7 @@ public void testToObject() throws IOException {
Contact target = new Contact();
StringReader reader = new StringReader("{\"age\":44,\"important\":true,\"name\":\"bob\"}");
JsonLibHandler handler = new JsonLibHandler();
handler.toObject(reader, target);
handler.toObject(new MockActionInvocation(), reader, target);

assertEquals(contact, target);
}
Expand Down

0 comments on commit ee5575d

Please sign in to comment.