View Javadoc
1   /*
2    * Copyright 2006-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  
17  package org.kuali.rice.krad.web.bind;
18  
19  import org.junit.Test;
20  import org.kuali.rice.krad.data.DataObjectService;
21  import org.kuali.rice.krad.data.KradDataServiceLocator;
22  import org.kuali.rice.krad.maintenance.MaintainableImpl;
23  import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
24  import org.kuali.rice.krad.test.KRADTestCase;
25  import org.kuali.rice.krad.test.conference.ConferenceSession;
26  import org.kuali.rice.krad.test.conference.ConferenceSessionForm;
27  import org.kuali.rice.krad.test.conference.SessionCoordinator;
28  import org.kuali.rice.krad.uif.UifParameters;
29  import org.kuali.rice.krad.uif.lifecycle.ViewPostMetadata;
30  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
31  import org.kuali.rice.krad.web.form.UifFormManager;
32  import org.springframework.mock.web.MockHttpServletRequest;
33  import org.springframework.web.context.request.RequestContextHolder;
34  import org.springframework.web.context.request.ServletWebRequest;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import java.util.Set;
38  import java.util.UUID;
39  
40  import static org.junit.Assert.*;
41  import static org.junit.Assert.assertEquals;
42  
43  /**
44   * Integration test for the {@link org.kuali.rice.krad.web.bind.UifServletRequestDataBinder}.
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  public class UifServletRequestDataBinderIntegrationTest extends KRADTestCase {
49  
50      /**
51       * Tests auto linking of the ConferenceSession on a SessionCoordinator
52       * "document.newMaintainableObject.dataObject.altCoordinator1Id" is passed on the request. This should use
53       * "foreign-key-based" linking to fetch the SessionCoordinator
54       */
55      @Test
56      public void testAutoLinking_UsingForeignKey() {
57          MaintenanceDocumentForm form = buildMaintenanceDocumentForm();
58          ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
59          form.setViewPostMetadata(viewPostMetadata);
60  
61          MaintenanceDocumentBase maintDoc = (MaintenanceDocumentBase)form.getDocument();
62  
63          UifServletRequestDataBinder binder = new UifServletRequestDataBinder(form, "form");
64  
65          // auto linking should be on by default
66          assertTrue(binder.isChangeTracking());
67          assertTrue(binder.isAutoLinking());
68  
69          // go ahead and create a SessionCoordinator
70          SessionCoordinator sc = new SessionCoordinator();
71          sc.setName("admin");
72          sc = getDataObjectService().save(sc);
73          assertNotNull(sc.getId());
74  
75          // before we bind, let's make sure that our account manager is null
76          ConferenceSession session = (ConferenceSession)maintDoc.getNewMaintainableObject().getDataObject();
77          assertNull(session.getAltCoordinator1());
78  
79          MockHttpServletRequest request = setupMockRequest();
80  
81          String basePath = "document.newMaintainableObject.dataObject.";
82          String altCoordIdPath = basePath + "altCoordinator1Id";
83  
84          addAccessibleRequestParameter(altCoordIdPath, sc.getId().toString(), request,
85                  viewPostMetadata);
86  
87          // let's bind this sucker
88          binder.bind(request);
89  
90          // now our session should have an alt coordinator and an alt coordinator id
91          assertAltSessionCoordinator(sc, session);
92      }
93  
94      /**
95       * Tests auto linking of the SessionCoordinator on an account whenever
96       * "document.newMaintainableObject.dataObject.accoumentManager.amId" is passed on the request. This should use
97       * "identity-based" linking to fetch the SessionCoordinator
98       */
99      @Test
100     public void testAutoLinking_UsingIdentity() {
101         MaintenanceDocumentForm form = buildMaintenanceDocumentForm();
102         ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
103         form.setViewPostMetadata(viewPostMetadata);
104 
105         MaintenanceDocumentBase maintDoc = (MaintenanceDocumentBase)form.getDocument();
106 
107         UifServletRequestDataBinder binder = new UifServletRequestDataBinder(form, "form");
108         // auto linking should be on by default
109         assertTrue(binder.isChangeTracking());
110         assertTrue(binder.isAutoLinking());
111 
112         // go ahead and create a SessionCoordinator
113         SessionCoordinator sc = new SessionCoordinator();
114         sc.setName("admin");
115         sc = getDataObjectService().save(sc);
116         assertNotNull(sc.getId());
117 
118         // before we bind, let's make sure that our account manager is null
119         ConferenceSession session = (ConferenceSession)maintDoc.getNewMaintainableObject().getDataObject();
120         assertNull(session.getAltCoordinator1());
121 
122         MockHttpServletRequest request = setupMockRequest();
123 
124         String basePath = "document.newMaintainableObject.dataObject.";
125         String altCoordIdPath = basePath + "altCoordinator1.id";
126 
127         addAccessibleRequestParameter(altCoordIdPath, sc.getId().toString(), request, viewPostMetadata);
128 
129         // let's bind this sucker
130         binder.bind(request);
131 
132         // now our session should have an "alt 1" coordinator and coordinator id
133         assertAltSessionCoordinator(sc, session);
134     }
135 
136     /**
137      * Test auto-linking on a list target object.
138      */
139     @Test
140     public void testAutoLinking_ListTarget() {
141         ConferenceSessionForm form = new ConferenceSessionForm();
142 
143         ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
144         form.setViewPostMetadata(viewPostMetadata);
145 
146         UifServletRequestDataBinder binder = new UifServletRequestDataBinder(form, "form");
147 
148         SessionCoordinator sc = new SessionCoordinator();
149         sc.setName("admin");
150         sc = getDataObjectService().save(sc);
151         assertNotNull(sc.getId());
152 
153         MockHttpServletRequest request = setupMockRequest();
154 
155         addAccessibleRequestParameter("conferenceSessionList[0].altCoordinator1Id", sc.getId().toString(), request,
156                 viewPostMetadata);
157         addAccessibleRequestParameter("conferenceSessionList[1].altCoordinator1Id", sc.getId().toString(), request,
158                 viewPostMetadata);
159 
160         binder.bind(request);
161 
162         assertAltSessionCoordinator(sc, form.getConferenceSessionList().get(0));
163         assertAltSessionCoordinator(sc, form.getConferenceSessionList().get(1));
164     }
165 
166     /**
167      * Test auto-linking on a map target object.
168      */
169     @Test
170     public void testAutoLinking_MapTarget() {
171         ConferenceSessionForm form = new ConferenceSessionForm();
172 
173         ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
174         form.setViewPostMetadata(viewPostMetadata);
175 
176         UifServletRequestDataBinder binder = new UifServletRequestDataBinder(form, "form");
177 
178         SessionCoordinator sc = new SessionCoordinator();
179         sc.setName("admin");
180         sc = getDataObjectService().save(sc);
181         assertNotNull(sc.getId());
182 
183         MockHttpServletRequest request = setupMockRequest();
184 
185         addAccessibleRequestParameter("conferenceSessionMap['foo1'].altCoordinator1Id", sc.getId().toString(), request,
186                 viewPostMetadata);
187         addAccessibleRequestParameter("conferenceSessionMap['foo2'].altCoordinator1Id", sc.getId().toString(), request,
188                 viewPostMetadata);
189 
190         binder.bind(request);
191 
192         assertAltSessionCoordinator(sc, form.getConferenceSessionMap().get("foo1"));
193         assertAltSessionCoordinator(sc, form.getConferenceSessionMap().get("foo2"));
194     }
195 
196     @Test
197     public void testModifiedPropertyTracking() {
198         MaintenanceDocumentForm form = buildMaintenanceDocumentForm();
199         ViewPostMetadata viewPostMetadata = new ViewPostMetadata();
200         form.setViewPostMetadata(viewPostMetadata);
201 
202         MaintenanceDocumentBase maintDoc = (MaintenanceDocumentBase)form.getDocument();
203 
204         UifServletRequestDataBinder binder = new UifServletRequestDataBinder(form, "form");
205         // turn off auto linking for this test
206         binder.setAutoLinking(false);
207 
208         MockHttpServletRequest request = new MockHttpServletRequest();
209         RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
210 
211         UifFormManager formManager = new UifFormManager();
212         request.getSession().setAttribute(UifParameters.FORM_MANAGER, formManager);
213         String basePath = "document.newMaintainableObject.dataObject.";
214         String idPath = basePath + "id";
215         String titlePath = basePath + "sessionTitle";
216         String altCoordinator1IdPath = basePath + "altCoordinator1Id";
217         String altCoordinator1_idPath = basePath + "altCoordinator1.id";
218 
219         addAccessibleRequestParameter(idPath, "12345", request, viewPostMetadata);
220         addAccessibleRequestParameter(titlePath, "My New ConferenceSession", request, viewPostMetadata);
221         addAccessibleRequestParameter(altCoordinator1IdPath, null, request, viewPostMetadata);
222         addAccessibleRequestParameter(altCoordinator1_idPath, "1", request, viewPostMetadata);
223 
224         // now let's run the binding and make sure it bound correctly
225         binder.bind(request);
226 
227         // verify that it performed proper binding
228         ConferenceSession session = (ConferenceSession)maintDoc.getNewMaintainableObject().getDataObject();
229         assertEquals("12345", session.getId());
230         assertEquals("My New ConferenceSession", session.getSessionTitle());
231         assertNull(session.getAltCoordinator1Id());
232         assertNotNull(session.getAltCoordinator1());
233         assertEquals(Long.valueOf(1), session.getAltCoordinator1().getId());
234         assertTrue(session.getAltCoordinator1().getAltCoordinatedSessions1().isEmpty());
235 
236         // finally, let's ensure it tracked the appropriate modified properties
237         UifBeanPropertyBindingResult result = (UifBeanPropertyBindingResult)binder.getInternalBindingResult();
238         Set<String> modifiedPaths = result.getModifiedPaths();
239 
240         // should contain these paths
241         assertTrue(modifiedPaths.contains(idPath));
242         assertTrue(modifiedPaths.contains(titlePath));
243         assertTrue(modifiedPaths.contains(altCoordinator1_idPath));
244 
245         // should not contain the altCoordinator1Id path since we didn't actually change it's value
246         assertFalse(modifiedPaths.contains(altCoordinator1IdPath));
247 
248         // total number of modified paths should be 3
249         assertEquals(3, modifiedPaths.size());
250 
251         // Now let's rebind the same requests, there should be no modified paths this time
252 
253         // create a new binder first
254         binder = new UifServletRequestDataBinder(form, "form");
255         binder.bind(request);
256         result = (UifBeanPropertyBindingResult)binder.getInternalBindingResult();
257         modifiedPaths = result.getModifiedPaths();
258         assertEquals(0, modifiedPaths.size());
259 
260         // now let's make a minor change to the amId
261         binder = new UifServletRequestDataBinder(form, "form");
262         request.setParameter(altCoordinator1IdPath, "2");
263         binder.bind(request);
264 
265         // check the account, then the modified paths
266         assertEquals(Long.valueOf(2), session.getAltCoordinator1Id());
267         result = (UifBeanPropertyBindingResult)binder.getInternalBindingResult();
268         modifiedPaths = result.getModifiedPaths();
269         assertEquals(1, modifiedPaths.size());
270         assertTrue(modifiedPaths.contains(altCoordinator1IdPath));
271     }
272 
273     private MaintenanceDocumentForm buildMaintenanceDocumentForm() {
274         MaintenanceDocumentForm form = new MaintenanceDocumentForm();
275         MaintenanceDocumentBase document = new MaintenanceDocumentBase();
276         ConferenceSession session = new ConferenceSession();
277 
278         MaintainableImpl oldMaintainable = new MaintainableImpl();
279         oldMaintainable.setDataObject(session);
280         MaintainableImpl newMaintainable = new MaintainableImpl();
281         newMaintainable.setDataObject(session);
282 
283         document.setOldMaintainableObject(oldMaintainable);
284         document.setNewMaintainableObject(newMaintainable);
285 
286         // let's create a doc id here that is a random value so it shouldn't conflict with anything
287         document.setDocumentNumber(UUID.randomUUID().toString());
288         form.setDocument(document);
289 
290         return form;
291     }
292 
293     protected void addAccessibleRequestParameter(String parameterKey, String parameterValue,
294             MockHttpServletRequest request, ViewPostMetadata viewPostMetadata) {
295         request.addParameter(parameterKey, parameterValue);
296         viewPostMetadata.addAccessibleBindingPath(parameterKey);
297     }
298 
299     protected MockHttpServletRequest setupMockRequest() {
300         MockHttpServletRequest request = new MockHttpServletRequest();
301         RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
302 
303         UifFormManager formManager = new UifFormManager();
304         request.getSession().setAttribute(UifParameters.FORM_MANAGER, formManager);
305 
306         return request;
307     }
308 
309     protected void assertAltSessionCoordinator(SessionCoordinator sc, ConferenceSession conferenceSession){
310         assertEquals(sc.getId(), conferenceSession.getAltCoordinator1Id());
311         assertNotNull(conferenceSession.getAltCoordinator1());
312         assertEquals(sc.getId(), conferenceSession.getAltCoordinator1().getId());
313         assertEquals("admin", conferenceSession.getAltCoordinator1().getName());
314     }
315 
316     private DataObjectService getDataObjectService() {
317         return KradDataServiceLocator.getDataObjectService();
318     }
319 }