1 package org.kuali.rice.krad.data;
2
3 import org.joda.time.DateTime;
4 import org.junit.Test;
5 import org.kuali.rice.krad.data.jpa.testbo.AnotherReferencedDataObject;
6 import org.kuali.rice.krad.data.jpa.testbo.ReferencedDataObject;
7 import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
8 import org.kuali.rice.krad.data.jpa.testbo.YetAnotherReferencedDataObject;
9 import org.kuali.rice.krad.test.KRADTestCase;
10 import org.kuali.rice.test.BaselineTestCase;
11
12 import java.util.Date;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15
16 import static org.junit.Assert.*;
17
18
19
20
21
22
23 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
24 public class DataObjectWrapperIntegrationTest extends KRADTestCase {
25
26 @Test
27 public void testRefreshRelationship_MultipleForeignKeys() {
28 TestDataObject testDataObject = new TestDataObject();
29 testDataObject.setPrimaryKeyProperty("abc");
30 testDataObject = getDataObjectService().save(testDataObject);
31 assertEquals("abc", testDataObject.getPrimaryKeyProperty());
32
33
34 assertNull(testDataObject.getReferencedObject());
35 assertNull(testDataObject.getStringProperty());
36
37
38 DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
39 wrapper.fetchRelationship("referencedObject");
40
41 assertNull(testDataObject.getReferencedObject());
42 assertNull(testDataObject.getStringProperty());
43
44
45 ReferencedDataObject referencedDataObject = new ReferencedDataObject();
46 referencedDataObject.setStringProperty("cba");
47 referencedDataObject.setSomeOtherStringProperty("efg");
48 referencedDataObject = getDataObjectService().save(referencedDataObject);
49
50
51
52 testDataObject.setStringProperty("cba");
53 assertNull(testDataObject.getReferencedObject());
54 wrapper.fetchRelationship("referencedObject");
55
56 assertNotNull(testDataObject.getReferencedObject());
57 assertEquals("efg", testDataObject.getReferencedObject().getSomeOtherStringProperty());
58
59
60
61
62
63
64 referencedDataObject = getDataObjectService().find(ReferencedDataObject.class,"cba");
65 assertEquals("efg", referencedDataObject.getSomeOtherStringProperty());
66 referencedDataObject.setSomeOtherStringProperty("efg2");
67 getDataObjectService().save(referencedDataObject);
68
69
70 assertEquals("efg", testDataObject.getReferencedObject().getSomeOtherStringProperty());
71 wrapper = getDataObjectService().wrap(testDataObject);
72 wrapper.fetchRelationship("referencedObject");
73
74 assertEquals("efg2", testDataObject.getReferencedObject().getSomeOtherStringProperty());
75
76
77 ReferencedDataObject referencedDataObject2 = new ReferencedDataObject();
78 referencedDataObject2.setStringProperty("cba2");
79 referencedDataObject2.setSomeOtherStringProperty("efg3");
80 getDataObjectService().save(referencedDataObject2);
81
82
83 testDataObject.setStringProperty("cba2");
84 wrapper.fetchRelationship("referencedObject");
85 assertEquals("cba2", testDataObject.getReferencedObject().getStringProperty());
86 assertEquals("efg3", testDataObject.getReferencedObject().getSomeOtherStringProperty());
87
88
89 testDataObject.setStringProperty(null);
90 assertNotNull(testDataObject.getReferencedObject());
91 wrapper.fetchRelationship("referencedObject");
92 assertNull(testDataObject.getReferencedObject());
93
94 }
95
96 @Test
97 public void testRefreshRelationship_CompoundForeignKeys() {
98 TestDataObject testDataObject = new TestDataObject();
99 testDataObject.setPrimaryKeyProperty("abc");
100 testDataObject = getDataObjectService().save(testDataObject);
101 assertEquals("abc", testDataObject.getPrimaryKeyProperty());
102
103
104 assertNull(testDataObject.getAnotherReferencedObject());
105 assertNull(testDataObject.getStringProperty());
106 assertNull(testDataObject.getDateProperty());
107
108
109 DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
110 wrapper.fetchRelationship("anotherReferencedObject");
111
112 assertNull(testDataObject.getAnotherReferencedObject());
113 assertNull(testDataObject.getStringProperty());
114 assertNull(testDataObject.getDateProperty());
115
116 Date date = newDateWithTimeAtStartOfDay();
117
118
119 AnotherReferencedDataObject referencedDataObject = new AnotherReferencedDataObject();
120 referencedDataObject.setStringProperty("cba");
121 referencedDataObject.setDateProperty(date);
122 referencedDataObject.setSomeOtherStringProperty("efg");
123 referencedDataObject = getDataObjectService().save(referencedDataObject);
124
125
126
127 testDataObject.setStringProperty("cba");
128 testDataObject.setDateProperty(date);
129 assertNull(testDataObject.getAnotherReferencedObject());
130 wrapper.fetchRelationship("anotherReferencedObject");
131
132 assertNotNull(testDataObject.getAnotherReferencedObject());
133 assertEquals("efg", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
134
135
136
137 Map<String, Object> compoundKey = new LinkedHashMap<String, Object>();
138 compoundKey.put("stringProperty", "cba");
139 compoundKey.put("dateProperty", date);
140 referencedDataObject = getDataObjectService().find(AnotherReferencedDataObject.class, new CompoundKey(compoundKey));
141 assertEquals("efg", referencedDataObject.getSomeOtherStringProperty());
142 referencedDataObject.setSomeOtherStringProperty("efg2");
143 getDataObjectService().save(referencedDataObject);
144
145
146 assertEquals("efg", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
147 wrapper = getDataObjectService().wrap(testDataObject);
148 wrapper.fetchRelationship("anotherReferencedObject");
149
150 assertEquals("efg2", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
151
152
153 AnotherReferencedDataObject referencedDataObject2 = new AnotherReferencedDataObject();
154 referencedDataObject2.setStringProperty("cba2");
155 Date date2 = newDateWithTimeAtStartOfDay();
156 referencedDataObject2.setDateProperty(date2);
157 referencedDataObject2.setSomeOtherStringProperty("efg3");
158 getDataObjectService().save(referencedDataObject2);
159
160
161 testDataObject.setStringProperty("cba2");
162 testDataObject.setDateProperty(date2);
163 wrapper.fetchRelationship("anotherReferencedObject");
164 assertEquals("cba2", testDataObject.getAnotherReferencedObject().getStringProperty());
165 assertEquals(date2, testDataObject.getAnotherReferencedObject().getDateProperty());
166 assertEquals("efg3", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
167
168
169
170 testDataObject.setStringProperty(null);
171 wrapper.fetchRelationship("anotherReferencedObject");
172 assertNull(testDataObject.getAnotherReferencedObject());
173
174 testDataObject.setStringProperty("cba2");
175 wrapper.fetchRelationship("anotherReferencedObject");
176 assertNotNull(testDataObject.getAnotherReferencedObject());
177
178 testDataObject.setStringProperty(null);
179 testDataObject.setDateProperty(null);
180 wrapper.fetchRelationship("anotherReferencedObject");
181 assertNull(testDataObject.getAnotherReferencedObject());
182
183 }
184
185 private Date newDateWithTimeAtStartOfDay() {
186 return DateTime.now().withTimeAtStartOfDay().toDate();
187 }
188
189 @Test
190 public void testRefreshRelationship_OneToOne_SingleForeignKey() {
191 TestDataObject testDataObject = new TestDataObject();
192 testDataObject.setPrimaryKeyProperty("abc");
193 testDataObject = getDataObjectService().save(testDataObject);
194 assertEquals("abc", testDataObject.getPrimaryKeyProperty());
195
196
197 assertNull(testDataObject.getAnotherReferencedObject());
198
199
200 DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
201 wrapper.fetchRelationship("yetAnotherReferencedObject");
202
203 assertNull(testDataObject.getAnotherReferencedObject());
204
205
206 YetAnotherReferencedDataObject referencedDataObject = new YetAnotherReferencedDataObject();
207 referencedDataObject.setId("abc");
208 referencedDataObject.setSomeOtherStringProperty("efg");
209 referencedDataObject = getDataObjectService().save(referencedDataObject);
210
211
212 assertNull(testDataObject.getYetAnotherReferencedObject());
213 wrapper.fetchRelationship("yetAnotherReferencedObject");
214
215 assertNotNull(testDataObject.getYetAnotherReferencedObject());
216 assertEquals("efg", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
217
218
219
220 referencedDataObject = getDataObjectService().find(YetAnotherReferencedDataObject.class, "abc");
221 assertEquals("efg", referencedDataObject.getSomeOtherStringProperty());
222 referencedDataObject.setSomeOtherStringProperty("efg2");
223 referencedDataObject = getDataObjectService().save(referencedDataObject);
224
225
226 assertEquals("efg", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
227 wrapper = getDataObjectService().wrap(testDataObject);
228 wrapper.fetchRelationship("yetAnotherReferencedObject");
229
230 assertEquals("efg2", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
231
232 assertEquals("abc", testDataObject.getPrimaryKeyProperty());
233 }
234
235 @Test
236 public void testRefreshRelationship_ErrorCases() {
237 TestDataObject testDataObject = new TestDataObject();
238 testDataObject.setPrimaryKeyProperty("abc");
239 testDataObject = getDataObjectService().save(testDataObject);
240 assertEquals("abc", testDataObject.getPrimaryKeyProperty());
241
242 DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
243
244 try {
245 wrapper.fetchRelationship("badReferenceName");
246 fail("IllegalArgumentException should have been thrown");
247 } catch (IllegalArgumentException e) {}
248
249 try {
250 wrapper.fetchRelationship("");
251 fail("IllegalArgumentException should have been thrown");
252 } catch (IllegalArgumentException e) {}
253
254
255 testDataObject.setStringProperty("thisDontPointToNuthin!");
256 assertEquals("thisDontPointToNuthin!", testDataObject.getStringProperty());
257 assertNull(testDataObject.getReferencedObject());
258
259 }
260
261
262 private DataObjectService getDataObjectService() {
263 return KradDataServiceLocator.getDataObjectService();
264 }
265
266 }