View Javadoc
1   /**
2    * Copyright 2005-2016 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.initialize;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.io.Serializable;
22  
23  import org.junit.After;
24  import org.junit.AfterClass;
25  import org.junit.Before;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
29  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30  import org.kuali.rice.krad.uif.UifConstants;
31  import org.kuali.rice.krad.uif.component.Component;
32  import org.kuali.rice.krad.uif.field.DataField;
33  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
34  import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
35  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
36  import org.kuali.rice.krad.uif.util.ProcessLoggingUnitTest;
37  import org.kuali.rice.krad.uif.util.UifUnitTestUtils;
38  import org.kuali.rice.krad.uif.view.View;
39  import org.kuali.rice.krad.web.form.UifFormBase;
40  import org.springframework.mock.web.MockHttpServletRequest;
41  
42  /**
43   * Unit tests for {@link InitializeDataFieldFromDictionaryTask}.
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class InitializeDataFieldFromDictionaryTest extends ProcessLoggingUnitTest {
48  
49      @BeforeClass
50      public static void setUpClass() throws Throwable {
51          UifUnitTestUtils.establishMockConfig("KRAD-InitializeDataFieldFromDictionaryTest");
52      }
53  
54      @Before
55      public void setUp() throws Throwable {
56          UifUnitTestUtils.establishMockUserSession("admin");
57      }
58  
59      @After
60      public void tearDown() throws Throwable {
61          UifUnitTestUtils.tearDownMockUserSession();
62      }
63  
64      @AfterClass
65      public static void tearDownClass() throws Throwable {
66          UifUnitTestUtils.tearDownMockConfig();
67      }
68  
69      public static class Bar implements Serializable {
70          private static final long serialVersionUID = 1275816843240407178L;
71  
72          private String text;
73  
74          public String getText() {
75              return this.text;
76          }
77  
78          public void setText(String text) {
79              this.text = text;
80          }
81      }
82  
83      public static class Foo implements Serializable {
84          private static final long serialVersionUID = -8018713299377638909L;
85  
86          private Bar descr;
87  
88          public Bar getDescr() {
89              return this.descr;
90          }
91  
92          public void setDescr(Bar descr) {
93              this.descr = descr;
94          }
95      }
96  
97      public static class TestForm extends UifFormBase {
98          private static final long serialVersionUID = 8726078766779752200L;
99  
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 }