001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.view;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertSame;
020    
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.concurrent.Callable;
025    
026    import org.junit.After;
027    import org.junit.AfterClass;
028    import org.junit.Before;
029    import org.junit.BeforeClass;
030    import org.junit.Test;
031    import org.kuali.rice.krad.sampleapp_2_4_M2.labs.KradLabsForm;
032    import org.kuali.rice.krad.sampleapp_2_4_M2.labs.kitchensink.UifComponentsTestForm;
033    import org.kuali.rice.krad.sampleapp_2_4_M2.labs.transaction.TransactionForm;
034    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
035    import org.kuali.rice.krad.uif.UifConstants;
036    import org.kuali.rice.krad.uif.UifParameters;
037    import org.kuali.rice.krad.uif.component.Component;
038    import org.kuali.rice.krad.uif.container.Group;
039    import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
040    import org.kuali.rice.krad.uif.service.ViewService;
041    import org.kuali.rice.krad.uif.util.ComponentFactory;
042    import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
043    import org.kuali.rice.krad.uif.util.ProcessLogger;
044    import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest;
045    import org.kuali.rice.krad.uif.util.UifUnitTestUtils;
046    import org.kuali.rice.krad.uif.util.ViewCleaner;
047    import org.kuali.rice.krad.web.bind.UifServletRequestDataBinder;
048    import org.kuali.rice.krad.web.controller.UifControllerHelper;
049    import org.kuali.rice.krad.web.controller.helper.DataTablesPagingHelper;
050    import org.kuali.rice.krad.web.form.UifFormBase;
051    import org.kuali.rice.krad.web.login.DummyLoginForm;
052    import org.springframework.mock.web.MockHttpServletRequest;
053    import org.springframework.mock.web.MockHttpServletResponse;
054    
055    /**
056     * Unit tests for proving correct operation of the ViewHelperService.
057     * 
058     * @author Kuali Rice Team (rice.collab@kuali.org)
059     */
060    public class ViewHelperServiceTest extends ProcessLoggingUnitTest {
061    
062        @BeforeClass
063        public static void setUpClass() throws Throwable {
064            UifUnitTestUtils.establishMockConfig("KRAD-ViewHelperServiceTest");
065        }
066    
067        @Before
068        public void setUp() throws Throwable {
069            UifUnitTestUtils.establishMockUserSession("admin");
070        }
071    
072        @After
073        public void tearDown() throws Throwable {
074            UifUnitTestUtils.tearDownMockUserSession();
075        }
076    
077        @AfterClass
078        public static void tearDownClass() throws Throwable {
079            UifUnitTestUtils.tearDownMockConfig();
080        }
081    
082        @Test
083        public void testSanity() throws Throwable {
084            MockHttpServletRequest request = new MockHttpServletRequest();
085            MockHttpServletResponse response = new MockHttpServletResponse();
086            DummyLoginForm loginForm = new DummyLoginForm();
087            request.setParameter(UifParameters.VIEW_ID, "DummyLoginView");
088            new UifServletRequestDataBinder(loginForm).bind(request);
089            UifControllerHelper.prepareViewForRendering(request, response, loginForm);
090            View dummyLogin = loginForm.getView();
091            assertEquals(UifConstants.ViewStatus.RENDERED, dummyLogin.getViewStatus());
092            assertEquals("LoginPage", dummyLogin.getCurrentPage().getId());
093            assertEquals("Rice-UserName",
094                    ObjectPropertyUtils.getPropertyValue(dummyLogin, "currentPage.items[0].items[1].items[1].items[1].items[3].id"));
095            
096            ViewCleaner.cleanView(dummyLogin);
097        }
098    
099        @Test
100        public void testKitchenSinkView() throws Throwable {
101            ViewService viewService = KRADServiceLocatorWeb.getViewService();
102            View uifCompView = viewService.getViewById("UifCompView");
103            UifComponentsTestForm uifcompform = new UifComponentsTestForm();
104            // TODO:
105            //        ViewLifecycle.buildView(uifCompView, uifcompform, Collections.<String, String> emptyMap());
106        }
107        
108        @Test
109        public void testTransactionView() throws Throwable {
110            MockHttpServletRequest request = new MockHttpServletRequest();
111            MockHttpServletResponse response = new MockHttpServletResponse();
112            TransactionForm tform = new TransactionForm();
113            request.setParameter(UifParameters.VIEW_ID, "TransactionView");
114            new UifServletRequestDataBinder(tform).bind(request);
115            UifControllerHelper.prepareViewForRendering(request, response, tform);
116            View transactionView = tform.getView();
117            assertEquals(UifConstants.ViewStatus.RENDERED, transactionView.getViewStatus());
118            ViewCleaner.cleanView(transactionView);
119        }
120        
121        @Test
122        public void testTransactionViewOnly() throws Throwable {
123            ViewService viewService = KRADServiceLocatorWeb.getViewService();
124            viewService.getViewById("TransactionView");
125        }
126        
127        @Test
128        public void testTransactionInitPhase() throws Throwable {
129            ViewService viewService = KRADServiceLocatorWeb.getViewService();
130            final View transactionView = viewService.getViewById("TransactionView");
131            final UifFormBase tform = new UifFormBase();
132            ViewLifecycle.encapsulateLifecycle(transactionView, tform, null, null, new Runnable() {
133                @Override
134                public void run() {
135                    ViewLifecycle viewLifecycle = ViewLifecycle.getActiveLifecycle();
136                    assertSame(transactionView, viewLifecycle.getOriginalView());
137                    
138                    View view = ViewLifecycle.getView();
139                    //                assertNotSame(transactionView, view);
140                    assertEquals("TransactionView", view.getId());
141                    
142                    ProcessLogger.trace("begin-init");
143                    viewLifecycle.populateViewFromRequestParameters(Collections.<String, String> emptyMap());
144                    
145                    ProcessLogger.trace("populate-request");
146                    tform.setViewRequestParameters(view.getViewRequestParameters());
147                    
148                    ProcessLogger.trace("set-request");
149                    viewLifecycle.performInitialization();
150                    
151                    ProcessLogger.trace("perform-init");
152                    view.index();
153                    
154                    ProcessLogger.trace("end-init");
155                }});
156        }
157    
158        @Test
159        public void testPerformanceMediumAll() throws Throwable {
160            MockHttpServletRequest request = new MockHttpServletRequest();
161            MockHttpServletResponse response = new MockHttpServletResponse();
162            KradLabsForm pform = new KradLabsForm();
163            request.setParameter(UifParameters.VIEW_ID, "Lab-PerformanceMedium");
164            new UifServletRequestDataBinder(pform).bind(request);
165            UifControllerHelper.prepareViewForRendering(request, response, pform);
166            View performanceView = pform.getView();
167            assertEquals(UifConstants.ViewStatus.RENDERED, performanceView.getViewStatus());
168            ViewCleaner.cleanView(performanceView);
169            pform.setPostedView(performanceView);
170            pform.setView(null);
171    
172            // TODO: set table ID to non-dynamic
173            //        String tableId = "u108";
174            //        
175            //        request = new MockHttpServletRequest();
176            //        request.setParameter("methodToCall", "tableJsonRetrieval");
177            //        request.setParameter("tableId", tableId);
178            //        request.setParameter("ajaxReturnType", "update-none");
179            //        request.setParameter("ajaxRequest", "true");
180            //        request.setParameter("sEcho", "1");
181            //        request.setParameter("iColumns", "11");
182            //        request.setParameter("sColumns", "");
183            //        request.setParameter("iDisplayStart", "0");
184            //        request.setParameter("iDisplayLength", "10");
185            //        request.setParameter("mDataProp_0", "function");
186            //        request.setParameter("mDataProp_1", "function");
187            //        request.setParameter("mDataProp_2", "function");
188            //        request.setParameter("mDataProp_3", "function");
189            //        request.setParameter("mDataProp_4", "function");
190            //        request.setParameter("mDataProp_5", "function");
191            //        request.setParameter("mDataProp_6", "function");
192            //        request.setParameter("mDataProp_7", "function");
193            //        request.setParameter("mDataProp_8", "function");
194            //        request.setParameter("mDataProp_9", "function");
195            //        request.setParameter("mDataProp_10", "function");
196            //        request.setParameter("sSearch", "");
197            //        request.setParameter("bRegex", "false");
198            //        request.setParameter("sSearch_0", "");
199            //        request.setParameter("bRegex_0", "false");
200            //        request.setParameter("bSearchable_0", "true");
201            //        request.setParameter("sSearch_1", "");
202            //        request.setParameter("bRegex_1", "false");
203            //        request.setParameter("bSearchable_1", "true");
204            //        request.setParameter("sSearch_2", "");
205            //        request.setParameter("bRegex_2", "false");
206            //        request.setParameter("bSearchable_2", "true");
207            //        request.setParameter("sSearch_3", "");
208            //        request.setParameter("bRegex_3", "false");
209            //        request.setParameter("bSearchable_3", "true");
210            //        request.setParameter("sSearch_4", "");
211            //        request.setParameter("bRegex_4", "false");
212            //        request.setParameter("bSearchable_4", "true");
213            //        request.setParameter("sSearch_5", "");
214            //        request.setParameter("bRegex_5", "false");
215            //        request.setParameter("bSearchable_5", "true");
216            //        request.setParameter("sSearch_6", "");
217            //        request.setParameter("bRegex_6", "false");
218            //        request.setParameter("bSearchable_6", "true");
219            //        request.setParameter("sSearch_7", "");
220            //        request.setParameter("bRegex_7", "false");
221            //        request.setParameter("bSearchable_7", "true");
222            //        request.setParameter("sSearch_8", "");
223            //        request.setParameter("bRegex_8", "false");
224            //        request.setParameter("bSearchable_8", "true");
225            //        request.setParameter("sSearch_9", "");
226            //        request.setParameter("bRegex_9", "false");
227            //        request.setParameter("bSearchable_9", "true");
228            //        request.setParameter("sSearch_10", "");
229            //        request.setParameter("bRegex_10", "false");
230            //        request.setParameter("bSearchable_10", "true");
231            //        request.setParameter("iSortCol_0", "0");
232            //        request.setParameter("sSortDir_0", "asc");
233            //        request.setParameter("iSortingCols", "1");
234            //        request.setParameter("bSortable_0", "false");
235            //        request.setParameter("bSortable_1", "false");
236            //        request.setParameter("bSortable_2", "true");
237            //        request.setParameter("bSortable_3", "true");
238            //        request.setParameter("bSortable_4", "true");
239            //        request.setParameter("bSortable_5", "true");
240            //        request.setParameter("bSortable_6", "true");
241            //        request.setParameter("bSortable_7", "true");
242            //        request.setParameter("bSortable_8", "false");
243            //        request.setParameter("bSortable_9", "true");
244            //        request.setParameter("bSortable_10", "false");
245            //
246            //        DataTablesPagingHelper.DataTablesInputs dataTablesInputs =
247            //                new DataTablesPagingHelper.DataTablesInputs(request);
248            //        DataTablesPagingHelper pagingHelper = new DataTablesPagingHelper();
249            //        pagingHelper.processPagingRequest(tableId, pform, request, response, dataTablesInputs);
250        }
251    
252    
253        @SuppressWarnings("unchecked")
254        @Test
255        public void testMutability() throws Throwable {
256            ViewService viewService = KRADServiceLocatorWeb.getViewService();
257            final View loginView = viewService.getViewById("DummyLoginView");
258            Group group = ViewLifecycle.encapsulateInitialization(new Callable<Group>(){
259                @Override
260                public Group call() throws Exception {
261                    Group group = ComponentFactory.getGroupWithDisclosureGridLayout();
262                    group.setId("foo");
263                    group.setHeaderText("bar");
264                    group.setItems(new ArrayList<Component>());
265                    ((List<Group>) loginView.getItems()).add(group);
266                    return group;
267                }});
268            assertSame(group, loginView.getItems().get(loginView.getItems().size()-1));
269        }
270    
271    }