1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48 public class UifServletRequestDataBinderIntegrationTest extends KRADTestCase {
49
50
51
52
53
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
66 assertTrue(binder.isChangeTracking());
67 assertTrue(binder.isAutoLinking());
68
69
70 SessionCoordinator sc = new SessionCoordinator();
71 sc.setName("admin");
72 sc = getDataObjectService().save(sc);
73 assertNotNull(sc.getId());
74
75
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
88 binder.bind(request);
89
90
91 assertAltSessionCoordinator(sc, session);
92 }
93
94
95
96
97
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
109 assertTrue(binder.isChangeTracking());
110 assertTrue(binder.isAutoLinking());
111
112
113 SessionCoordinator sc = new SessionCoordinator();
114 sc.setName("admin");
115 sc = getDataObjectService().save(sc);
116 assertNotNull(sc.getId());
117
118
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
130 binder.bind(request);
131
132
133 assertAltSessionCoordinator(sc, session);
134 }
135
136
137
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
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
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
225 binder.bind(request);
226
227
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
237 UifBeanPropertyBindingResult result = (UifBeanPropertyBindingResult)binder.getInternalBindingResult();
238 Set<String> modifiedPaths = result.getModifiedPaths();
239
240
241 assertTrue(modifiedPaths.contains(idPath));
242 assertTrue(modifiedPaths.contains(titlePath));
243 assertTrue(modifiedPaths.contains(altCoordinator1_idPath));
244
245
246 assertFalse(modifiedPaths.contains(altCoordinator1IdPath));
247
248
249 assertEquals(3, modifiedPaths.size());
250
251
252
253
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
261 binder = new UifServletRequestDataBinder(form, "form");
262 request.setParameter(altCoordinator1IdPath, "2");
263 binder.bind(request);
264
265
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
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 }