package org.intalio.tempo.portlet;

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

import javax.portlet.PortletResponse;
import javax.servlet.http.HttpSession;

import junit.framework.TestCase;

import org.apache.pluto.wrappers.PortletRequestWrapper;
import org.intalio.tempo.uiframework.Configuration;
import org.intalio.tempo.uiframework.UIFWApplicationState;
import org.intalio.tempo.uiframework.actions.TasksCollector;
import org.intalio.tempo.uiframework.forms.FormManager;
import org.intalio.tempo.uiframework.forms.FormManagerBroker;
import org.intalio.tempo.web.User;
import org.jmock.Expectations;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;

import com.googlecode.instinct.expect.ExpectThat;
import com.googlecode.instinct.expect.ExpectThatImpl;
import com.googlecode.instinct.integrate.junit4.InstinctRunner;
import com.googlecode.instinct.marker.annotate.Mock;
import com.googlecode.instinct.marker.annotate.Specification;
import com.googlecode.instinct.marker.annotate.Stub;
import com.googlecode.instinct.marker.annotate.Subject;

@RunWith(InstinctRunner.class)
public class TasksActionTest extends TestCase {
	   protected transient Logger _log = LoggerFactory.getLogger(getClass());
	    final static ExpectThat expect = new ExpectThatImpl();
	    
	    @Subject TasksAction ta;
	    @Subject TasksCollector tc;
	    @Mock PortletRequestWrapper req; 
	    @Mock PortletResponse res;
	    @Mock HttpSession s;
//	    @Mock ApplicationState st;
	    @Mock UIFWApplicationState st;
	    @Mock BindException be;
	    @Mock Map<String, Object> model;
	    @Mock User user;
	    @Stub HashMap<String, Object> m;
	    @Mock FormManager formManager;
	    
	    @Specification
	    public void testExecute() throws Exception{
	    	
	    	FormManagerBroker.getInstance().setFormManager(formManager);
	    	
	    	//model = new HashMap<String, Object>();
	    	ta = new TasksAction();
	    	
	    	
	    	
	    	final String token = "token1";
	    	final String user1 = "user1";
	    	tc = new TasksCollector(req, user1, token);
	    	
	    	m = new HashMap<String, Object>();
	    	m.put("errors", ta.getErrors());
	    	m.put("participantToken", token);
	    	m.put("currentUser", user1);
	    	m.put("refreshTime", 5);
	    	expect.that(new Expectations(){{
	    		one(req).getParameter("update");will(returnValue("true"));
	    		atLeast(1).of(be).getModel();will(returnValue(model));	    		
	    		atLeast(1).of(model).put("errors", ta.getErrors());
	    		atLeast(1).of(req).getSession();will(returnValue(s));
	    		atLeast(1).of(s).getAttribute("APPLICATION_STATE");will(returnValue(st));
	            atLeast(1).of(st).getCurrentUser();will(returnValue(user));
	            atLeast(1).of(user).getToken();will(returnValue(token));
	            atLeast(1).of(user).getName();will(returnValue(user1));
	            atLeast(1).of(model).put("activityTasks", tc.get_activityTasks());
	            atLeast(1).of(model).put("notifications", tc.get_notifications());
	            atLeast(1).of(model).put("initTasks", tc.get_initTasks());	            
//	            atLeast(1).of(req).getScheme();will(returnValue("schema"));
//	            atLeast(1).of(req).getServerName();will(returnValue("www.intalio.com"));
//	            atLeast(1).of(req).getServerPort();will(returnValue(80));
	            atLeast(1).of(model).put("participantToken", token);
	            atLeast(1).of(model).put("currentUser", user1);
	            atLeast(1).of(model).put("refreshTime", 5);
	            atLeast(1).of(model).size();will(returnValue(7));
	            atLeast(1).of(model).entrySet();will(returnValue(m.entrySet()));
	            one(req).getParameter("update");will(returnValue("false"));
	            
	    	}});
	    	Configuration.getInstance().setServiceEndpoint("http://www.intalio.org");
	    	ta.setRequest(req);
	    	ta.setResponse(res);
	    	ta.setBindErrors(be);
	    	assertNotNull(ta.execute());
	    	assertNotNull(ta.execute());
	    	assertNotNull(ta.getErrorView());
	    	
	    }

}

@@ -3158,244 +3158,8 @@
    %0A
-//%09            atLeast(1).of(req).getScheme();will(returnValue(%22schema%22));%0A//%09            atLeast(1).of(req).getServerName();will(returnValue(%22www.intalio.com%22));%0A//%09            atLeast(1).of(req).getServerPort();will(returnValue(80));%0A
 %09   
@@ -3738,16 +3738,355 @@
 rs(be);%0A
+%09    %09ta.setCommand(%22command%22);%0A%09    %09ta.setRequiredRoles(new String%5B%5D%7B%22role1%22%7D);%0A%09    %09assertTrue(ta.getBindErrors().equals(be));%0A%09    %09assertTrue(ta.getRequest().equals(req));%0A%09    %09assertTrue(ta.getResponse().equals(res));%0A%09    %09assertTrue(ta.getCommand().equals(%22command%22));%0A%09    %09assertTrue(ta.getRequiredRoles()%5B0%5D.equals(%22role1%22));%0A
 %09    %09as
@@ -4092,39 +4092,43 @@
 ssertNotNull(ta.
-e
+doE
 xecut
-e
+ion
 ());%0A%09    %09asser
@@ -4143,15 +4143,19 @@
 (ta.
-e
+doE
 xecut
-e
+ion
 ());

enhance ut for portlet/TasksAction