001/** 002 * Copyright 2005-2014 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 */ 016package org.kuali.rice.krad.uif.lifecycle; 017 018import static org.junit.Assert.assertEquals; 019import static org.junit.Assert.assertSame; 020 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024 025import org.junit.After; 026import org.junit.AfterClass; 027import org.junit.Before; 028import org.junit.BeforeClass; 029import org.junit.Test; 030import org.kuali.rice.core.api.config.property.ConfigContext; 031import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 032import org.kuali.rice.krad.test.KRADTestCase; 033import org.kuali.rice.krad.uif.UifConstants; 034import org.kuali.rice.krad.uif.UifParameters; 035import org.kuali.rice.krad.uif.component.Component; 036import org.kuali.rice.krad.uif.container.Group; 037import org.kuali.rice.krad.uif.service.ViewService; 038import org.kuali.rice.krad.uif.util.ComponentFactory; 039import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; 040import org.kuali.rice.krad.uif.util.ProcessLogger; 041import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest; 042import org.kuali.rice.krad.uif.util.UifUnitTestUtils; 043import org.kuali.rice.krad.uif.view.View; 044import org.kuali.rice.krad.util.KRADConstants; 045import org.kuali.rice.krad.web.bind.UifServletRequestDataBinder; 046import org.kuali.rice.krad.web.form.UifFormBase; 047import org.kuali.rice.krad.web.login.DummyLoginForm; 048import org.springframework.mock.web.MockHttpServletRequest; 049import org.springframework.web.context.request.RequestContextHolder; 050import org.springframework.web.context.request.ServletWebRequest; 051import org.springframework.web.servlet.ModelAndView; 052 053/** 054 * Integration tests for proving correct operation of the ViewHelperService. 055 * 056 * @author Kuali Rice Team (rice.collab@kuali.org) 057 */ 058public class ViewLifecycleTest extends KRADTestCase { 059 060 @BeforeClass 061 public static void setUpClass() throws Throwable { 062 MockHttpServletRequest request = new MockHttpServletRequest(); 063 request.setMethod("POST"); 064 065 RequestContextHolder.setRequestAttributes(new ServletWebRequest(request)); 066 } 067 068 @Override 069 @Before 070 public void setUp() throws Exception { 071 super.setUp(); 072 073 UifUnitTestUtils.establishMockUserSession("admin"); 074 } 075 076 @Override 077 @After 078 public void tearDown()throws Exception { 079 UifUnitTestUtils.tearDownMockUserSession(); 080 081 super.tearDown(); 082 } 083 084 @Test 085 public void testSanity() throws Throwable { 086 DummyLoginForm form = new DummyLoginForm(); 087 088 testFormView(form, "DummyLoginView"); 089 090 View view = form.getView(); 091 092 assertEquals("LoginPage", view.getCurrentPage().getId()); 093 assertEquals("Rice-UserName", ObjectPropertyUtils.getPropertyValue(view, 094 "currentPage.items[0].items[1].items[1].items[1].items[3].id")); 095 } 096 097 @SuppressWarnings("unchecked") 098 @Test 099 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}