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