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.web.bind;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.krad.test.TestForm;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.lifecycle.ViewPostMetadata;
23  import org.kuali.rice.krad.uif.view.ViewModel;
24  import org.springframework.mock.web.MockHttpServletRequest;
25  import org.springframework.web.context.request.RequestContextHolder;
26  import org.springframework.web.context.request.ServletWebRequest;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  /**
31   * Test cases for {@link UifViewBeanWrapper}.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class UifViewBeanWrapperTest {
36  
37      private UifViewBeanWrapper beanWrapper;
38  
39      @Before
40      public void setUp() throws Exception {
41          TestForm model = new TestForm();
42          model.setMethodToCall("field7TestMethodToCall");
43  
44          UifBeanPropertyBindingResult bindingResult = new UifBeanPropertyBindingResult(model, "model", true, 100);
45  
46          beanWrapper = new UifViewBeanWrapper(model, bindingResult);
47  
48          MockHttpServletRequest request = new MockHttpServletRequest();
49          request.setMethod("POST");
50          request.addParameter(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, "field7TestMethodToCall");
51  
52          RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
53      }
54  
55      /**
56       * Tests binding annotations are being correctly picked up for various property paths.
57       */
58      @Test
59      public void testCheckForAnnotationInPropertyPath() {
60          assertBindingAnnotationsInPath("Annotation on simple form property not picked up", "field1", Boolean.TRUE);
61  
62          assertBindingAnnotationsInPath("Annotation on simple form property not picked up", "field2", Boolean.FALSE);
63  
64          assertBindingAnnotationsInPath("Annotation picked up where not present", "field3", null);
65  
66          assertBindingAnnotationsInPath("Annotation on nested property not picked up", "dataObject.field5",
67                  Boolean.FALSE);
68  
69          assertBindingAnnotationsInPath("Annotation picked up on nested property where not present", "dataObject.field1",
70                  null);
71  
72          assertBindingAnnotationsInPath("Annotation on nested property not picked up", "dataObject.nestedObject.field1",
73                  Boolean.TRUE);
74  
75          assertBindingAnnotationsInPath("Annotation not correctly picked up for multiple annotations",
76                  "dataObject.nestedObject.field5", Boolean.FALSE);
77  
78          assertBindingAnnotationsInPath("Annotation on nested list property not picked up", "dataObject.list[0].field5",
79                  Boolean.FALSE);
80  
81          assertBindingAnnotationsInPath("Annotation on nested list property picked up where not present",
82                  "dataObject.list[0].field1", null);
83  
84          assertBindingAnnotationsInPath("Annotation on map property not picked up", "dataObject.map['key']",
85                  Boolean.TRUE);
86  
87          assertBindingAnnotationsInPath("Annotation on accessible list property not picked up",
88                  "dataObject.list2[3].field7", Boolean.TRUE);
89  
90          assertBindingAnnotationsInPath("Annotation picked up for wrong request method", "field4", null);
91  
92          assertBindingAnnotationsInPath("Annotation picked up for wrong request method", "field7", Boolean.TRUE);
93  
94          MockHttpServletRequest request = new MockHttpServletRequest();
95          request.setMethod("GET");
96  
97          RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
98  
99          assertBindingAnnotationsInPath("Annotation not picked up for correct request method", "field4", Boolean.TRUE);
100 
101         assertBindingAnnotationsInPath("Annotation not picked up for multiple request methods", "dataObject.field8",
102                 Boolean.TRUE);
103     }
104 
105     /**
106      * Helper method for testing {@link UifViewBeanWrapper#checkBindingAnnotationsInPath(java.lang.String)}.
107      *
108      * @param failureMessage message to show if assert fails
109      * @param path property path to invoke method with
110      * @param result expected annotation result
111      */
112     protected void assertBindingAnnotationsInPath(String failureMessage, String path, Boolean result) {
113         Boolean bindingAnnotationAccess = beanWrapper.checkBindingAnnotationsInPath(path);
114 
115         assertEquals(failureMessage, result, bindingAnnotationAccess);
116     }
117 
118     /**
119      * Tests binding access is correctly being granted and prevented for various cases.
120      */
121     @Test
122     public void testCheckPropertyAccess() {
123         ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
124         ((ViewModel) beanWrapper.getWrappedInstance()).setViewPostMetadata(viewPostMetadata);
125 
126         viewPostMetadata.addAccessibleBindingPath("field5");
127         viewPostMetadata.addAccessibleBindingPath("dataObject.field1");
128         viewPostMetadata.addAccessibleBindingPath("dataObject.list[3].field3");
129         viewPostMetadata.addAccessibleBindingPath("dataObject.list[3].list[2].field3");
130 
131         assertPropertyBindingAccess("Access not granted for view binding path", "field5", true);
132 
133         assertPropertyBindingAccess("Access granted for path without annotation and not in view", "field6", false);
134 
135         assertPropertyBindingAccess("Access not granted for path with accessible annotation", "field1", true);
136 
137         assertPropertyBindingAccess("Access granted for path with protected annotation", "field2", false);
138 
139         assertPropertyBindingAccess("Access not granted for nested view binding path", "dataObject.field1", true);
140 
141         assertPropertyBindingAccess("Access not granted for nested view binding path", "dataObject.list[3].field3",
142                 true);
143 
144         assertPropertyBindingAccess("Access not granted for nested view binding path",
145                 "dataObject.list[3].list[2].field3", true);
146 
147         assertPropertyBindingAccess("Access granted for path without annotation and not in view",
148                 "dataObject.list[3].list[1].field3", false);
149     }
150 
151     /**
152      * Helper method for testing {@link UifViewBeanWrapper#checkPropertyBindingAccess(java.lang.String)}.
153      *
154      * @param failureMessage message to show if assert fails
155      * @param path property path to invoke method with
156      * @param access expected access result
157      */
158     protected void assertPropertyBindingAccess(String failureMessage, String path, boolean access) {
159         boolean isPropertyAccessible = beanWrapper.checkPropertyBindingAccess(path);
160 
161         assertEquals(failureMessage, Boolean.valueOf(access), Boolean.valueOf(isPropertyAccessible));
162     }
163 }