View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.lifecycle;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertSame;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.junit.After;
33  import org.junit.AfterClass;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.kuali.rice.krad.sampleapp_2_4_M2.demo.uif.form.KradSampleAppForm;
38  import org.kuali.rice.krad.sampleapp_2_4_M2.labs.KradLabsForm;
39  import org.kuali.rice.krad.sampleapp_2_4_M2.labs.kitchensink.UifComponentsTestForm;
40  import org.kuali.rice.krad.sampleapp_2_4_M2.labs.transaction.TransactionForm;
41  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
42  import org.kuali.rice.krad.uif.UifConstants;
43  import org.kuali.rice.krad.uif.UifParameters;
44  import org.kuali.rice.krad.uif.component.Component;
45  import org.kuali.rice.krad.uif.container.Group;
46  import org.kuali.rice.krad.uif.service.ViewService;
47  import org.kuali.rice.krad.uif.util.ComponentFactory;
48  import org.kuali.rice.krad.uif.util.ComponentUtils;
49  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
50  import org.kuali.rice.krad.uif.util.ProcessLogger;
51  import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest;
52  import org.kuali.rice.krad.uif.util.UifUnitTestUtils;
53  import org.kuali.rice.krad.uif.util.ViewCleaner;
54  import org.kuali.rice.krad.uif.view.View;
55  import org.kuali.rice.krad.web.bind.UifServletRequestDataBinder;
56  import org.kuali.rice.krad.web.controller.UifControllerHelper;
57  import org.kuali.rice.krad.web.controller.helper.DataTablesPagingHelper;
58  import org.kuali.rice.krad.web.form.UifFormBase;
59  import org.kuali.rice.krad.web.login.DummyLoginForm;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  import org.springframework.mock.web.MockHttpServletRequest;
63  import org.springframework.mock.web.MockHttpServletResponse;
64  
65  /**
66   * Unit tests for proving correct operation of the ViewHelperService.
67   * 
68   * @author Kuali Rice Team (rice.collab@kuali.org)
69   */
70  public class ViewLifecycleTest extends ProcessLoggingUnitTest {
71      
72      private static final Logger LOG = LoggerFactory.getLogger(ViewLifecycleTest.class);
73  
74      @BeforeClass
75      public static void setUpClass() throws Throwable {
76          UifUnitTestUtils.establishMockConfig("KRAD-ViewLifecycleTest");
77      }
78  
79      @Before
80      public void setUp() throws Throwable {
81          UifUnitTestUtils.establishMockUserSession("admin");
82      }
83  
84      @After
85      public void tearDown() throws Throwable {
86          UifUnitTestUtils.tearDownMockUserSession();
87      }
88  
89      @AfterClass
90      public static void tearDownClass() throws Throwable {
91          UifUnitTestUtils.tearDownMockConfig();
92      }
93  
94      @Test
95      public void testSanity() throws Throwable {
96          MockHttpServletRequest request = new MockHttpServletRequest();
97          MockHttpServletResponse response = new MockHttpServletResponse();
98          DummyLoginForm loginForm = new DummyLoginForm();
99          request.setParameter(UifParameters.VIEW_ID, "DummyLoginView");
100         new UifServletRequestDataBinder(loginForm).bind(request);
101         UifControllerHelper.prepareViewForRendering(request, response, loginForm);
102         View dummyLogin = loginForm.getView();
103         assertEquals(UifConstants.ViewStatus.RENDERED, dummyLogin.getViewStatus());
104         assertEquals("LoginPage", dummyLogin.getCurrentPage().getId());
105         assertEquals("Rice-UserName",
106                 ObjectPropertyUtils.getPropertyValue(dummyLogin, "currentPage.items[0].items[1].items[1].items[1].items[3].id"));
107         
108         ViewCleaner.cleanView(dummyLogin);
109     }
110 
111     private View testFormView(UifFormBase form, String viewName) throws Throwable {
112         MockHttpServletRequest request = new MockHttpServletRequest();
113         MockHttpServletResponse response = new MockHttpServletResponse();
114         request.setParameter(UifParameters.VIEW_ID, viewName);
115         new UifServletRequestDataBinder(form).bind(request);
116         UifControllerHelper.prepareViewForRendering(request, response, form);
117         View view = form.getView();
118         assertEquals(UifConstants.ViewStatus.RENDERED, view.getViewStatus());
119         ViewCleaner.cleanView(view);
120         return view;
121     }
122     
123     @Test
124     public void testKitchenSinkView() throws Throwable {
125         testFormView(new UifComponentsTestForm(), "UifCompView");
126     }
127     
128     @Test
129     public void testTransactionView() throws Throwable {
130         testFormView(new TransactionForm(), "TransactionView");
131     }
132     
133     @Test
134     public void testTransactionViewOnly() throws Throwable {
135         ViewService viewService = KRADServiceLocatorWeb.getViewService();
136         viewService.getViewById("TransactionView");
137     }
138     
139     @Test
140     public void testComponentsForLifecycle() throws Throwable {
141         ViewService viewService = KRADServiceLocatorWeb.getViewService();
142         View view = viewService.getViewById("TransactionView");
143         
144         List<Component> l1 = view.getComponentsForLifecycle();
145         Map<String, Component> m2 = ComponentUtils.getComponentsForLifecycle(view, false);
146         LOG.debug(l1.toString());
147         LOG.debug(m2.toString());
148         assertTrue(l1.size() >= m2.size());
149 
150         Iterator<Entry<String, Component>> i2 = m2.entrySet().iterator();
151         while (i2.hasNext()) {
152             Entry<String, Component> e2 = i2.next();
153             assertTrue(e2.getKey(), l1.contains(e2.getValue()));
154             l1.remove(e2.getValue());
155         }
156         for (int i=0;i<l1.size();i++) {
157             assertNull(l1.get(i) == null ? null : l1.get(i).toString(), l1.get(i));
158         }
159     }
160     
161     @Test
162     public void testTransactionInitPhase() throws Throwable {
163         ViewService viewService = KRADServiceLocatorWeb.getViewService();
164         final View transactionView = viewService.getViewById("TransactionView");
165         final UifFormBase tform = new UifFormBase();
166         ViewLifecycle.encapsulateLifecycle(transactionView, tform, null, null, new Runnable() {
167             @Override
168             public void run() {
169                 View view = ViewLifecycle.getView();
170                 assertSame(transactionView, view);
171                 
172                 assertEquals("TransactionView", view.getId());
173                 
174                 ProcessLogger.trace("begin-init");
175                 ViewLifecycle.getHelper().populateViewFromRequestParameters(Collections.<String, String> emptyMap());
176                 
177                 ProcessLogger.trace("populate-request");
178                 tform.setViewRequestParameters(view.getViewRequestParameters());
179                 
180                 ViewLifecycle.getHelper().performCustomViewInitialization(tform);
181 
182                 ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
183                 processor.performPhase(LifecyclePhaseFactory.initialize(view, tform));
184                 
185                 ProcessLogger.trace("end-init");
186             }});
187     }
188 
189     @Test
190     public void testComponentLibrary() throws Throwable {
191         KradSampleAppForm form = new KradSampleAppForm();
192         testFormView(form, "ComponentLibraryHome");
193     }
194 
195     @Test
196     public void testPerformanceMediumAll() throws Throwable {
197         KradLabsForm form = new KradLabsForm();
198         View view = testFormView(form, "Lab-PerformanceMedium");
199 
200         form.setPostedView(view);
201         form.setView(null);
202 
203         assertEquals("uu3q2a9", view.getItems().get(0).getItems().get(1).getId());
204         String tableId = "uu3q2a9";
205 
206         MockHttpServletRequest request = new MockHttpServletRequest();
207         request.setParameter("methodToCall", "tableJsonRetrieval");
208         request.setParameter("tableId", tableId);
209         request.setParameter("ajaxReturnType", "update-none");
210         request.setParameter("ajaxRequest", "true");
211         request.setParameter("sEcho", "1");
212         request.setParameter("iColumns", "11");
213         request.setParameter("sColumns", "");
214         request.setParameter("iDisplayStart", "0");
215         request.setParameter("iDisplayLength", "10");
216         request.setParameter("mDataProp_0", "function");
217         request.setParameter("mDataProp_1", "function");
218         request.setParameter("mDataProp_2", "function");
219         request.setParameter("mDataProp_3", "function");
220         request.setParameter("mDataProp_4", "function");
221         request.setParameter("mDataProp_5", "function");
222         request.setParameter("mDataProp_6", "function");
223         request.setParameter("mDataProp_7", "function");
224         request.setParameter("mDataProp_8", "function");
225         request.setParameter("mDataProp_9", "function");
226         request.setParameter("mDataProp_10", "function");
227         request.setParameter("sSearch", "");
228         request.setParameter("bRegex", "false");
229         request.setParameter("sSearch_0", "");
230         request.setParameter("bRegex_0", "false");
231         request.setParameter("bSearchable_0", "true");
232         request.setParameter("sSearch_1", "");
233         request.setParameter("bRegex_1", "false");
234         request.setParameter("bSearchable_1", "true");
235         request.setParameter("sSearch_2", "");
236         request.setParameter("bRegex_2", "false");
237         request.setParameter("bSearchable_2", "true");
238         request.setParameter("sSearch_3", "");
239         request.setParameter("bRegex_3", "false");
240         request.setParameter("bSearchable_3", "true");
241         request.setParameter("sSearch_4", "");
242         request.setParameter("bRegex_4", "false");
243         request.setParameter("bSearchable_4", "true");
244         request.setParameter("sSearch_5", "");
245         request.setParameter("bRegex_5", "false");
246         request.setParameter("bSearchable_5", "true");
247         request.setParameter("sSearch_6", "");
248         request.setParameter("bRegex_6", "false");
249         request.setParameter("bSearchable_6", "true");
250         request.setParameter("sSearch_7", "");
251         request.setParameter("bRegex_7", "false");
252         request.setParameter("bSearchable_7", "true");
253         request.setParameter("sSearch_8", "");
254         request.setParameter("bRegex_8", "false");
255         request.setParameter("bSearchable_8", "true");
256         request.setParameter("sSearch_9", "");
257         request.setParameter("bRegex_9", "false");
258         request.setParameter("bSearchable_9", "true");
259         request.setParameter("sSearch_10", "");
260         request.setParameter("bRegex_10", "false");
261         request.setParameter("bSearchable_10", "true");
262         request.setParameter("iSortCol_0", "0");
263         request.setParameter("sSortDir_0", "asc");
264         request.setParameter("iSortingCols", "1");
265         request.setParameter("bSortable_0", "false");
266         request.setParameter("bSortable_1", "false");
267         request.setParameter("bSortable_2", "true");
268         request.setParameter("bSortable_3", "true");
269         request.setParameter("bSortable_4", "true");
270         request.setParameter("bSortable_5", "true");
271         request.setParameter("bSortable_6", "true");
272         request.setParameter("bSortable_7", "true");
273         request.setParameter("bSortable_8", "false");
274         request.setParameter("bSortable_9", "true");
275         request.setParameter("bSortable_10", "false");
276 
277         DataTablesPagingHelper.DataTablesInputs dataTablesInputs =
278                 new DataTablesPagingHelper.DataTablesInputs(request);
279         DataTablesPagingHelper pagingHelper = new DataTablesPagingHelper();
280         HttpServletResponse response = new MockHttpServletResponse();
281         pagingHelper.processPagingRequest(tableId, form, request, response, dataTablesInputs);
282     }
283 
284 
285     @SuppressWarnings("unchecked")
286     @Test
287     public void testMutability() throws Throwable {
288         ViewService viewService = KRADServiceLocatorWeb.getViewService();
289         View loginView = viewService.getViewById("DummyLoginView");
290         Group group = ComponentFactory.getGroupWithDisclosureGridLayout();
291         group.setId("foo");
292         group.setHeaderText("bar");
293         group.setItems(new ArrayList<Component>());
294         ((List<Group>) loginView.getItems()).add(group);
295         assertSame(group, loginView.getItems().get(loginView.getItems().size()-1));
296     }
297 
298 }