001/** 002 * Copyright 2005-2015 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.initialize; 017 018import static org.junit.Assert.assertFalse; 019import static org.junit.Assert.assertTrue; 020 021import java.io.Serializable; 022 023import org.junit.After; 024import org.junit.AfterClass; 025import org.junit.Before; 026import org.junit.BeforeClass; 027import org.junit.Test; 028import org.kuali.rice.krad.datadictionary.AttributeDefinition; 029import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 030import org.kuali.rice.krad.uif.UifConstants; 031import org.kuali.rice.krad.uif.component.Component; 032import org.kuali.rice.krad.uif.field.DataField; 033import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle; 034import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase; 035import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; 036import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest; 037import org.kuali.rice.krad.uif.util.UifUnitTestUtils; 038import org.kuali.rice.krad.uif.view.View; 039import org.kuali.rice.krad.web.form.UifFormBase; 040import org.springframework.mock.web.MockHttpServletRequest; 041 042/** 043 * Unit tests for {@link InitializeDataFieldFromDictionaryTask}. 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 */ 047public class InitializeDataFieldFromDictionaryTest extends ProcessLoggingUnitTest { 048 049 @BeforeClass 050 public static void setUpClass() throws Throwable { 051 UifUnitTestUtils.establishMockConfig("KRAD-InitializeDataFieldFromDictionaryTest"); 052 } 053 054 @Before 055 public void setUp() throws Throwable { 056 UifUnitTestUtils.establishMockUserSession("admin"); 057 } 058 059 @After 060 public void tearDown() throws Throwable { 061 UifUnitTestUtils.tearDownMockUserSession(); 062 } 063 064 @AfterClass 065 public static void tearDownClass() throws Throwable { 066 UifUnitTestUtils.tearDownMockConfig(); 067 } 068 069 public static class Bar implements Serializable { 070 private static final long serialVersionUID = 1275816843240407178L; 071 072 private String text; 073 074 public String getText() { 075 return this.text; 076 } 077 078 public void setText(String text) { 079 this.text = text; 080 } 081 } 082 083 public static class Foo implements Serializable { 084 private static final long serialVersionUID = -8018713299377638909L; 085 086 private Bar descr; 087 088 public Bar getDescr() { 089 return this.descr; 090 } 091 092 public void setDescr(Bar descr) { 093 this.descr = descr; 094 } 095 } 096 097 public static class TestForm extends UifFormBase { 098 private static final long serialVersionUID = 8726078766779752200L; 099 100 private Foo foo; 101 private Bar bar; 102 103 public Foo getFoo() { 104 return this.foo; 105 } 106 107 public void setFoo(Foo foo) { 108 this.foo = foo; 109 } 110 111 public Bar getBar() { 112 return this.bar; 113 } 114 115 public void setBar(Bar bar) { 116 this.bar = bar; 117 } 118 } 119 120 public static class TestAttributeProcess implements Runnable { 121 122 @Override 123 public void run() { 124 View view = ViewLifecycle.getView(); 125 126 String parentPath = "currentPage.items[0]"; 127 Component parent = ObjectPropertyUtils.getPropertyValue(view, parentPath); 128 129 String foopath = "items[0]"; 130 DataField foofield = ObjectPropertyUtils.getPropertyValue(parent, foopath); 131 ViewLifecyclePhase foophase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase( 132 UifConstants.ViewPhases.INITIALIZE, foofield, parent, foopath, null); 133 InitializeDataFieldFromDictionaryTask footask = new InitializeDataFieldFromDictionaryTask(); 134 footask.setElementState(foophase); 135 136 String barpath = "items[1]"; 137 DataField barfield = ObjectPropertyUtils.getPropertyValue(parent, barpath); 138 ViewLifecyclePhase barphase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase( 139 UifConstants.ViewPhases.INITIALIZE, barfield, parent, barpath, null); 140 InitializeDataFieldFromDictionaryTask bartask = new InitializeDataFieldFromDictionaryTask(); 141 bartask.setElementState(barphase); 142 143 AttributeDefinition fooattribute = footask.findNestedDictionaryAttribute(foofield.getPropertyName()); 144 assertTrue(fooattribute.isRequired()); 145 146 AttributeDefinition barattribute = bartask.findNestedDictionaryAttribute(barfield.getPropertyName()); 147 assertFalse(barattribute.isRequired()); 148 } 149 } 150 151 @Test 152 public void testAttribute() throws Throwable { 153 ViewLifecycle 154 .encapsulateLifecycle(KRADServiceLocatorWeb.getViewService().getViewById("TestView"), 155 new TestForm(), new MockHttpServletRequest(), 156 new TestAttributeProcess()); 157 } 158 159}