alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (RestActionInvocationTest.java)

This example Struts source code file (RestActionInvocationTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

actionconfig, arraylist, arraylist, exception, exception, get, hashmap, http, item, list, list, override, request, response, restaction, restaction, restactioninvocationtester, servlet, util

The Struts RestActionInvocationTest.java source code

package org.apache.struts2.rest;

import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import junit.framework.TestCase;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.HttpHeaderResult;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.DefaultUnknownHandlerManager;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.InterceptorMapping;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.mock.MockInterceptor;
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;

public class RestActionInvocationTest extends TestCase {

	RestActionInvocation restActionInvocation;
	MockHttpServletRequest request;
	MockHttpServletResponse response;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		
		restActionInvocation = new RestActionInvocationTester();
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		ServletActionContext.setRequest(request);
		ServletActionContext.setResponse(response);

	}
	
	/**
	 * Test the correct action results: null, String, HttpHeaders, Result
	 * @throws Exception
	 */
	public void testSaveResult() throws Exception {

		Object methodResult = "index";
		ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
		assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
    	
		setUp();
    	methodResult = new DefaultHttpHeaders("show");
    	assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
    	assertEquals(methodResult, restActionInvocation.httpHeaders);
    	
		setUp();
    	methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
    	assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
    	assertEquals(methodResult, restActionInvocation.createResult());

		setUp();
    	try {
    		methodResult = new Object();
    		restActionInvocation.saveResult(actionConfig, methodResult);

    		// ko
    		assertFalse(true);
    		
    	} catch (ConfigurationException c) {
    		// ok, object not allowed
    	}
	}
	
	/**
	 * Test the target selection: exception, error messages, model and null
	 * @throws Exception
	 */
	public void testSelectTarget() throws Exception {
		
		// Exception
		Exception e = new Exception();
		restActionInvocation.getStack().set("exception", e);
		restActionInvocation.selectTarget();
		assertEquals(e, restActionInvocation.target);

		// Error messages
		setUp();
		String actionMessage = "Error!";
		RestActionSupport action = (RestActionSupport)restActionInvocation.getAction();
		action.addActionError(actionMessage);
		Map errors = new HashMap();
		List<String> list = new ArrayList();
		list.add(actionMessage);
    	errors.put("actionErrors", list);
    	restActionInvocation.selectTarget();
		assertEquals(errors, restActionInvocation.target);
		
    	// Model with get and no content in post, put, delete
    	setUp();
		RestAction restAction = (RestAction)restActionInvocation.getAction();
		List<String> model = new ArrayList();
		model.add("Item");
		restAction.model = model;
		request.setMethod("GET");
		restActionInvocation.selectTarget();
		assertEquals(model, restActionInvocation.target);
		request.setMethod("POST");
		restActionInvocation.selectTarget();
		assertEquals(null, restActionInvocation.target);
		request.setMethod("PUT");
		restActionInvocation.selectTarget();
		assertEquals(null, restActionInvocation.target);
		request.setMethod("DELETE");
		restActionInvocation.selectTarget();
		assertEquals(null, restActionInvocation.target);
		
	}

	/**
	 * Test the not modified status code.
	 * @throws Exception
	 */
	public void testResultNotModified() throws Exception {

		request.addHeader("If-None-Match", "123");
		request.setMethod("GET");

		RestAction restAction = (RestAction)restActionInvocation.getAction();
		List<String> model = new ArrayList() {
			@Override
			public int hashCode() {
				return 123;
			}
		};
		model.add("Item");
		restAction.model = model;
		
		restActionInvocation.processResult();
		assertEquals(SC_NOT_MODIFIED, response.getStatus());
        
    }
	
	/**
	 * Test the default error result.
	 * @throws Exception
	 */
	public void testDefaultErrorResult() throws Exception {
		
		// Exception
		Exception e = new Exception();
		restActionInvocation.getStack().set("exception", e);
		request.setMethod("GET");

		RestAction restAction = (RestAction)restActionInvocation.getAction();
		List<String> model = new ArrayList();
		model.add("Item");
		restAction.model = model;
		
		restActionInvocation.setDefaultErrorResultName("default-error");
		ResultConfig resultConfig = new ResultConfig.Builder("default-error", 
			"org.apache.struts2.dispatcher.HttpHeaderResult")
	    	.addParam("status", "123").build();
	    ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", 
				"RestAction", "org.apache.rest.RestAction")
	    	.addResultConfig(resultConfig)
	    	.build();
	    ((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
		
		restActionInvocation.processResult();
		assertEquals(123, response.getStatus());
		
	}
	
	public void testNoResult() throws Exception {
		
		RestAction restAction = (RestAction)restActionInvocation.getAction();
		List<String> model = new ArrayList();
		model.add("Item");
		restAction.model = model;
		request.setMethod("GET");
		restActionInvocation.setResultCode("index");
		
		try {
			restActionInvocation.processResult();

    		// ko
    		assertFalse(true);
    		
    	} catch (ConfigurationException c) {
    		// ok, no result
    	}

	}
	
	/**
	 * Test the global execution
	 * @throws Exception
	 */
	public void testInvoke() throws Exception {
        
	    // Default index method return 'success'
	    ((MockActionProxy)restActionInvocation.getProxy()).setMethod("index");

	    // Define result 'success'
		ResultConfig resultConfig = new ResultConfig.Builder("success", 
			"org.apache.struts2.dispatcher.HttpHeaderResult")
	    	.addParam("status", "123").build();
	    ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", 
				"RestAction", "org.apache.rest.RestAction")
	    	.addResultConfig(resultConfig)
	    	.build();
	    ((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);

		request.setMethod("GET");
		
        restActionInvocation.invoke();

        assertEquals(123, response.getStatus());
    }


    class RestActionInvocationTester extends RestActionInvocation {
    	RestActionInvocationTester() {
            super(new HashMap<String,String>(), true);
            List<InterceptorMapping> interceptorMappings = new ArrayList();
            MockInterceptor mockInterceptor = new MockInterceptor();
            mockInterceptor.setFoo("interceptor");
            mockInterceptor.setExpectedFoo("interceptor");
            interceptorMappings.add(new InterceptorMapping("interceptor", mockInterceptor));
            interceptors = interceptorMappings.iterator();
            MockActionProxy actionProxy = new MockActionProxy();
            ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", 
    				"RestAction", "org.apache.rest.RestAction").build();
            actionProxy.setConfig(actionConfig);
            proxy = actionProxy;
            action = new RestAction();
            setMimeTypeHandlerSelector(new DefaultContentTypeHandlerManager());
            unknownHandlerManager = new DefaultUnknownHandlerManager();
			try {
				XWorkTestCaseHelper.setUp();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			invocationContext = ActionContext.getContext();
			container = ActionContext.getContext().getContainer();
			stack = ActionContext.getContext().getValueStack();
			objectFactory = container.getInstance(ObjectFactory.class);
			
        }
    	
    }

    class RestAction extends RestActionSupport implements ModelDriven<List {

    	List<String> model;
		
    	public List<String> getModel() {
			return model;
		}
    	
    }
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts RestActionInvocationTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.