1   package org.marketchangers.prayer.portlet;
2   
3   import java.io.IOException;
4   import java.util.Collections;
5   
6   import javax.portlet.ActionRequest;
7   import javax.portlet.ActionResponse;
8   import javax.portlet.Portlet;
9   import javax.portlet.PortletConfig;
10  import javax.portlet.PortletContext;
11  import javax.portlet.PortletException;
12  import javax.portlet.PortletRequestDispatcher;
13  import javax.portlet.PortletSession;
14  import javax.portlet.RenderRequest;
15  import javax.portlet.RenderResponse;
16  
17  import junit.framework.TestCase;
18  
19  import org.marketchangers.PortalUser;
20  import org.marketchangers.PortalUserManager;
21  import org.marketchangers.ServiceLocator;
22  import org.marketchangers.prayer.PrayerRequestManager;
23  import org.marketchangers.prayer.PrayerRequestQuery;
24  
25  import com.mockobjects.constraint.Constraint;
26  import com.mockobjects.dynamic.C;
27  import com.mockobjects.dynamic.Mock;
28  
29  /***
30   * @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
31   * @author <a href="mailto:jniu@wc-group.com">Jianshuo Niu</a>
32   */
33  public class PrayerRequestSearchPortletTest extends TestCase {
34  	private Portlet portlet;
35  
36  	private Mock actionRequest;
37  	private Mock actionResponse;
38  	private Mock config;
39  	private Mock context;
40  	private Mock dispatcher;
41  	private Mock locator;
42  	private Mock renderRequest;
43  	private Mock renderResponse;
44  	private Mock requestManager;
45  	private Mock session;
46  	private Mock userManager;
47  
48  	// --------------------------------------------- SetUp and TearDown Methods
49  	public void setUp() {
50  		actionRequest = new Mock(ActionRequest.class);
51  		actionResponse = new Mock(ActionResponse.class);
52  		config = new Mock(PortletConfig.class);
53  		context = new Mock(PortletContext.class);
54  		dispatcher = new Mock(PortletRequestDispatcher.class);
55  		locator = new Mock(ServiceLocator.class);
56  		renderRequest = new Mock(RenderRequest.class);
57  		renderResponse = new Mock(RenderResponse.class);
58  		requestManager = new Mock(PrayerRequestManager.class);
59  		session = new Mock(PortletSession.class);
60  		userManager = new Mock(PortalUserManager.class);
61  
62  		portlet = new PrayerRequestSearchPortlet();
63  	}
64  
65  	public void dtearDown() {
66  		actionRequest.verify();
67  		actionResponse.verify();
68  		config.verify();
69  		context.verify();
70  		dispatcher.verify();
71  		locator.verify();
72  		renderRequest.verify();
73  		renderResponse.verify();
74  		requestManager.verify();
75  		session.verify();
76  		userManager.verify();
77  	}
78  
79  	// ----------------------------------------------------------- Test Methods
80  	// TODO get test back in sync with code
81  	public void doNotTestRenderLooksUpValuesAndDelegatesToJsp()
82  		throws Exception {
83  		config.expectAndReturn(
84  			"getPortletContext",
85  			(PortletContext) context.proxy());
86  
87  		context.matchAndReturn(
88  			"getAttribute",
89  			ServiceLocator.KEY,
90  			(ServiceLocator) locator.proxy());
91  
92  		locator.expectAndReturn(
93  			"get",
94  			PrayerRequestManager.class,
95  			(PrayerRequestManager) requestManager.proxy());
96  
97  		renderRequest.expectAndReturn("getRemoteUser", "JOE BOB");
98  
99  		// render parameters for prayerRequestSearch.jsp (search box)
100 		requestManager.expectAndReturn(
101 			"getRequestorsForIntercessor",
102 			"JOE BOB",
103 			Collections.singletonList("THELMA LOU"));
104 
105 		PortalUser thelma = new PortalUser();
106 
107 		locator.expectAndReturn(
108 			"get",
109 			PortalUserManager.class,
110 			(PortalUserManager) userManager.proxy());
111 
112 		userManager.expectAndReturn("lookupUserById", "THELMA LOU", thelma);
113 
114 		expectSetAttribute(
115 			"prayerRequestors",
116 			C.eq(Collections.singletonMap("THELMA LOU", thelma)));
117 
118 		// render parameters for prayerList.jsp (request headers)
119 		expectSetAttribute("userRole", C.eq("intercessor"));
120 
121 		renderRequest.expectAndReturn(
122 			"getPortletSession",
123 			(PortletSession) session.proxy());
124 
125 		session.expectAndReturn(
126 			"getAttribute",
127 			"prayerRequests",
128 			Collections.EMPTY_LIST);
129 		expectSetAttribute("prayerRequests", C.eq(Collections.EMPTY_LIST));
130 
131 		// render parameters for singleRequest.jsp (full view)
132 		renderRequest.expectAndReturn("getParameter", "prayerId", "42");
133 
134 		requestManager.expectAndReturn(
135 			"getPrayerRequestById",
136 			new Integer(42),
137 			null);
138 
139 		expectSetAttribute("prayerRequest", C.IS_NULL);
140 
141 		requestManager.expectAndReturn(
142 			"getPrayerCommentsForId",
143 			new Integer(42),
144 			Collections.EMPTY_LIST);
145 
146 		// dispatch
147 		context.expectAndReturn(
148 			"getRequestDispatcher",
149 			"/prayerRequestSearch.jsp",
150 			(PortletRequestDispatcher) dispatcher.proxy());
151 
152 		dispatcher.expect(
153 			"include",
154 			C.args(
155 				C.eq((RenderRequest) renderRequest.proxy()),
156 				C.eq((RenderResponse) renderResponse.proxy())));
157 
158 		portlet.init((PortletConfig) config.proxy());
159 
160 		portlet.render(
161 			(RenderRequest) renderRequest.proxy(),
162 			(RenderResponse) renderResponse.proxy());
163 	}
164 
165 	public void testProcessActionSearchesIfUserActionIsSearch()
166 		throws PortletException, IOException {
167 
168 		// init
169 		config.expectAndReturn(
170 			"getPortletContext",
171 			(PortletContext) context.proxy());
172 
173 		portlet.init((PortletConfig) config.proxy());
174 
175 		// process action
176 		actionRequest.expectAndReturn("getParameter", "userAction", "search");
177 		actionRequest.expectAndReturn("getParameterMap", Collections.EMPTY_MAP);
178 		actionRequest.expectAndReturn("getRemoteUser", "JOE BOB");
179 
180 		actionRequest.expect("getParameter", "comment");
181 		actionRequest.expect("getParameter", "prayerId");
182 
183 		context.expectAndReturn(
184 			"getAttribute",
185 			ServiceLocator.KEY,
186 			(ServiceLocator) locator.proxy());
187 
188 		locator.expectAndReturn(
189 			"get",
190 			PrayerRequestManager.class,
191 			(PrayerRequestManager) requestManager.proxy());
192 
193 		requestManager.expectAndReturn(
194 			"search",
195 			C.args(
196 				C.eq("JOE BOB"),
197 				C.eq(new PrayerRequestQuery(Collections.EMPTY_MAP))),
198 			Collections.EMPTY_LIST);
199 
200 		actionRequest.expectAndReturn(
201 			"getPortletSession",
202 			(PortletSession) session.proxy());
203 
204 		session.expect(
205 			"setAttribute",
206 			C.args(C.eq("prayerRequests"), C.eq(Collections.EMPTY_LIST)));
207 		actionResponse.expect(
208 			"setRenderParameter",
209 			C.args(C.eq("validSearch"), C.eq("true")));
210 		portlet.processAction(
211 			(ActionRequest) actionRequest.proxy(),
212 			(ActionResponse) actionResponse.proxy());
213 	}
214 
215 	// TODO implement test
216 	public void doNotTestProcessActionAddsCommentIfUserActionIsAddComment()
217 		throws PortletException, IOException {
218 
219 		// init
220 		config.expectAndReturn(
221 			"getPortletContext",
222 			(PortletContext) context.proxy());
223 
224 		portlet.init((PortletConfig) config.proxy());
225 
226 		// processAction
227 		actionRequest.expectAndReturn(
228 			"getParameter",
229 			"userAction",
230 			"addComment");
231 
232 		fail("Test not implemented.");
233 
234 		portlet.processAction(
235 			(ActionRequest) actionRequest.proxy(),
236 			(ActionResponse) actionResponse.proxy());
237 	}
238 
239 	private void expectSetAttribute(String name, Constraint constraint) {
240 		renderRequest.expect("setAttribute", C.args(C.eq(name), constraint));
241 	}
242 }