View Javadoc
1   /**
2    * Copyright 2005-2014 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.assertSame;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.junit.After;
26  import org.junit.AfterClass;
27  import org.junit.Before;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.kuali.rice.core.api.config.property.ConfigContext;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.test.KRADTestCase;
33  import org.kuali.rice.krad.uif.UifConstants;
34  import org.kuali.rice.krad.uif.UifParameters;
35  import org.kuali.rice.krad.uif.component.Component;
36  import org.kuali.rice.krad.uif.container.Group;
37  import org.kuali.rice.krad.uif.service.ViewService;
38  import org.kuali.rice.krad.uif.util.ComponentFactory;
39  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
40  import org.kuali.rice.krad.uif.util.ProcessLogger;
41  import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest;
42  import org.kuali.rice.krad.uif.util.UifUnitTestUtils;
43  import org.kuali.rice.krad.uif.view.View;
44  import org.kuali.rice.krad.util.KRADConstants;
45  import org.kuali.rice.krad.web.bind.UifServletRequestDataBinder;
46  import org.kuali.rice.krad.web.form.UifFormBase;
47  import org.kuali.rice.krad.web.login.DummyLoginForm;
48  import org.springframework.mock.web.MockHttpServletRequest;
49  import org.springframework.web.context.request.RequestContextHolder;
50  import org.springframework.web.context.request.ServletWebRequest;
51  import org.springframework.web.servlet.ModelAndView;
52  
53  /**
54   * Integration tests for proving correct operation of the ViewHelperService.
55   *
56   * @author Kuali Rice Team (rice.collab@kuali.org)
57   */
58  public class ViewLifecycleTest extends KRADTestCase {
59  
60      @BeforeClass
61      public static void setUpClass() throws Throwable {
62          MockHttpServletRequest request = new MockHttpServletRequest();
63          request.setMethod("POST");
64  
65          RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
66      }
67  
68      @Override
69      @Before
70      public void setUp() throws Exception {
71          super.setUp();
72  
73          UifUnitTestUtils.establishMockUserSession("admin");
74      }
75  
76      @Override
77      @After
78      public void tearDown()throws Exception {
79          UifUnitTestUtils.tearDownMockUserSession();
80  
81          super.tearDown();
82      }
83  
84      @Test
85      public void testSanity() throws Throwable {
86          DummyLoginForm form = new DummyLoginForm();
87  
88          testFormView(form, "DummyLoginView");
89  
90          View view = form.getView();
91  
92          assertEquals("LoginPage", view.getCurrentPage().getId());
93          assertEquals("Rice-UserName", ObjectPropertyUtils.getPropertyValue(view,
94                  "currentPage.items[0].items[1].items[1].items[1].items[3].id"));
95      }
96  
97      @SuppressWarnings("unchecked")
98      @Test
99      public void testMutability() throws Throwable {
100         ViewService viewService = KRADServiceLocatorWeb.getViewService();
101         View view = viewService.getViewById("DummyLoginView");
102 
103         Group group = ComponentFactory.getGroupWithDisclosureGridLayout();
104         group.setId("foo");
105         group.setHeaderText("bar");
106         group.setItems(new ArrayList<Component>());
107 
108         ((List<Group>) view.getItems()).add(group);
109         assertSame(group, view.getItems().get(view.getItems().size() - 1));
110     }
111 
112     @Test
113     public void testPagedView() throws Throwable {
114         UifFormBase form = new UifFormBase();
115 
116         testFormView(form, "TestPagedView");
117 
118         form.setFormKey(null);
119         form.setPageId("TestPagedView-Page2");
120 
121         testFormView(form, "TestPagedView");
122 
123         form.setFormKey(null);
124         form.setPageId("TestPagedView-Page3");
125 
126         testFormView(form, "TestPagedView");
127     }
128 
129     @Test
130     public void testInitializationPhase() throws Throwable {
131         ViewService viewService = KRADServiceLocatorWeb.getViewService();
132         final View view = viewService.getViewById("TestPagedView");
133         final UifFormBase form = new UifFormBase();
134 
135         ViewLifecycle.encapsulateLifecycle(view, form, null, new Runnable() {
136             @Override
137             public void run() {
138                 View currentView = ViewLifecycle.getView();
139 
140                 assertSame(view, currentView);
141                 assertEquals("TestPagedView", currentView.getId());
142 
143                 ProcessLogger.trace("begin-init");
144                 ViewLifecycle.getHelper().populateViewFromRequestParameters(Collections.<String, String> emptyMap());
145 
146                 ProcessLogger.trace("populate-request");
147                 form.setViewRequestParameters(currentView.getViewRequestParameters());
148 
149                 ViewLifecycle.getHelper().performCustomViewInitialization(form);
150 
151                 ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
152                         UifConstants.ViewPhases.INITIALIZE, view, null, "", null);
153                 ViewLifecycle.getProcessor().performPhase(phase);
154 
155                 ProcessLogger.trace("end-init");
156             }
157         });
158     }
159 
160     private UifFormBase testFormView(UifFormBase form, String viewName) throws Throwable {
161         ViewService viewService = KRADServiceLocatorWeb.getViewService();
162         View view = viewService.getViewById(viewName);
163         form.setView(view);
164         assertEquals(UifConstants.ViewStatus.CREATED, view.getViewStatus());
165 
166         MockHttpServletRequest request = new MockHttpServletRequest();
167         request.setParameter(UifParameters.VIEW_ID, viewName);
168         new UifServletRequestDataBinder(form).bind(request);
169 
170         ModelAndView modelAndView = new ModelAndView();
171         modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
172 
173         KRADServiceLocatorWeb.getModelAndViewService().prepareView(request, modelAndView);
174 
175         view = form.getView();
176         assertEquals(UifConstants.ViewStatus.FINAL, view.getViewStatus());
177 
178         return form;
179     }
180 
181 }