package com.jmh.foundation.webapp.action;

import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.junit.Before;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.ServletActionContext;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.jmh.foundation.model.User;
import com.jmh.foundation.service.MockSecurityManager;

/**
 * User: dusty
 * Date: Jan 16, 2009
 */
@ContextConfiguration(loader=StrutsContextLoader.class , locations = {"classpath:/applicationContext-resources.xml","classpath:/applicationContext-service.xml","classpath:/applicationContext-CrowdClient.xml","classpath:/applicationContext.xml","/WEB-INF/security.xml" })
@TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
@Transactional
public class BaseStrutsTestCaseSpring extends AbstractTransactionalJUnit4SpringContextTests{
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;
    private Dispatcher dispatcher;
    protected ActionProxy proxy;
    WebApplicationContext context;

    @Before
    public void onSetUpBeforeTransaction() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        context = (WebApplicationContext)applicationContext;        
        HashMap params = new HashMap();
//        params.put("actionPackages", "com.jmh.issues.webapp.action");
        dispatcher = new Dispatcher(context.getServletContext(), params);
        dispatcher.init();
        Dispatcher.setInstance(dispatcher);

    }

    protected <T> T createAction(String namespace, String name)
            throws Exception {
        Map extraContext = dispatcher.createContextMap(request, response, null, context.getServletContext());
        proxy = dispatcher.getContainer().getInstance(ActionProxyFactory.class).
                createActionProxy(
                        namespace, name, null,extraContext, true, false);
        proxy.getInvocation().getInvocationContext().
                setSession(new HashMap());
        //if you are using Convention plugin set this to false.  There is a bug somewhere and I haven't had time to trace it down.
		proxy.setExecuteResult(true);
        ServletActionContext.setContext(
                proxy.getInvocation().getInvocationContext());
        ServletActionContext.setServletContext(context.getServletContext());
        return (T) proxy.getAction();
    }

    public void setUpMockUserManager(RootAction action, User user) {
        MockSecurityManager userManager = (MockSecurityManager) context.getBean("mockUserManager");
        if (user != null)
            userManager.setUser(user);
        action.setSecurityManager(userManager);
    }


}
