View Javadoc
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   * An integration test which tests various aspects of the default DataObjectWrapper implementation.
20   *
21   * @author Kuali Rice Team (rice.collab@kuali.org)
22   */
23  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB) // set it to CLEAR_DB so we can have more control over the transactions within this test
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          // now refresh the "referencedObject" relationship, "stringProperty" represents the internal key here
34          assertNull(testDataObject.getReferencedObject());
35          assertNull(testDataObject.getStringProperty());
36  
37          // right now we have no foreign keys so this should essentially do nothing
38          DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
39          wrapper.fetchRelationship("referencedObject");
40          // should still be null
41          assertNull(testDataObject.getReferencedObject());
42          assertNull(testDataObject.getStringProperty());
43  
44          // next, let's save a value for referenced object
45          ReferencedDataObject referencedDataObject = new ReferencedDataObject();
46          referencedDataObject.setStringProperty("cba");
47          referencedDataObject.setSomeOtherStringProperty("efg");
48          referencedDataObject = getDataObjectService().save(referencedDataObject);
49  
50          // if we set the string property on the TestDataObject and refresh, it should fetch the missing
51          // ReferencedDataObject for us
52          testDataObject.setStringProperty("cba");
53          assertNull(testDataObject.getReferencedObject());
54          wrapper.fetchRelationship("referencedObject");
55          // now referenced object should not be null any longer
56          assertNotNull(testDataObject.getReferencedObject());
57          assertEquals("efg", testDataObject.getReferencedObject().getSomeOtherStringProperty());
58  
59  
60  
61  
62          // now, let's update the referenced data object value and make sure the refresh works
63          // first, let's refetch the referenced object and update it
64          referencedDataObject = getDataObjectService().find(ReferencedDataObject.class,"cba");
65          assertEquals("efg", referencedDataObject.getSomeOtherStringProperty());
66          referencedDataObject.setSomeOtherStringProperty("efg2");
67          getDataObjectService().save(referencedDataObject);
68  
69          // it's updated now, so let's verify the original testDataObject is out of date
70          assertEquals("efg", testDataObject.getReferencedObject().getSomeOtherStringProperty());
71          wrapper = getDataObjectService().wrap(testDataObject);
72          wrapper.fetchRelationship("referencedObject");
73          // now it should be updated!
74          assertEquals("efg2", testDataObject.getReferencedObject().getSomeOtherStringProperty());
75  
76          // create a new referenced object to ensure we can fetch and update to a new foreign key as well
77          ReferencedDataObject referencedDataObject2 = new ReferencedDataObject();
78          referencedDataObject2.setStringProperty("cba2");
79          referencedDataObject2.setSomeOtherStringProperty("efg3");
80          getDataObjectService().save(referencedDataObject2);
81  
82          // now, let's repoint the FK and try a fetch
83          testDataObject.setStringProperty("cba2");
84          wrapper.fetchRelationship("referencedObject");
85          assertEquals("cba2", testDataObject.getReferencedObject().getStringProperty());
86          assertEquals("efg3", testDataObject.getReferencedObject().getSomeOtherStringProperty());
87  
88          // lastly, let's try setting our FK to null and ensure our fetched relationship is set to null
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         // now refresh the "anotherReferencedObject" relationship, "stringProperty" and "dateProperty" represent the internal key here
104         assertNull(testDataObject.getAnotherReferencedObject());
105         assertNull(testDataObject.getStringProperty());
106         assertNull(testDataObject.getDateProperty());
107 
108         // right now we have no foreign keys so this should essentially do nothing
109         DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
110         wrapper.fetchRelationship("anotherReferencedObject");
111         // should still be null
112         assertNull(testDataObject.getAnotherReferencedObject());
113         assertNull(testDataObject.getStringProperty());
114         assertNull(testDataObject.getDateProperty());
115 
116         Date date = newDateWithTimeAtStartOfDay();
117 
118         // next, let's save a value for referenced object
119         AnotherReferencedDataObject referencedDataObject = new AnotherReferencedDataObject();
120         referencedDataObject.setStringProperty("cba");
121         referencedDataObject.setDateProperty(date);
122         referencedDataObject.setSomeOtherStringProperty("efg");
123         referencedDataObject = getDataObjectService().save(referencedDataObject);
124 
125         // if we set the internal keys on the TestDataObject and refresh, it should fetch the missing
126         // AnotherReferencedDataObject for us
127         testDataObject.setStringProperty("cba");
128         testDataObject.setDateProperty(date);
129         assertNull(testDataObject.getAnotherReferencedObject());
130         wrapper.fetchRelationship("anotherReferencedObject");
131         // now referenced object should not be null any longer
132         assertNotNull(testDataObject.getAnotherReferencedObject());
133         assertEquals("efg", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
134 
135         // now, let's update the referenced data object value and make sure the refresh works
136         // first, let's refetch the referenced object and update it
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         // it's updated now, so let's verify the original testDataObject is out of date
146         assertEquals("efg", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
147         wrapper = getDataObjectService().wrap(testDataObject);
148         wrapper.fetchRelationship("anotherReferencedObject");
149         // now it should be updated!
150         assertEquals("efg2", testDataObject.getAnotherReferencedObject().getSomeOtherStringProperty());
151 
152         // create a new referenced object to ensure we can fetch and update to a new foreign key as well
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         // now, let's repoint the FK and try a fetch
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         // lastly, let's try setting our FK to null and ensure our fetched relationship is set to null
169         // first, a partial set
170         testDataObject.setStringProperty(null);
171         wrapper.fetchRelationship("anotherReferencedObject");
172         assertNull(testDataObject.getAnotherReferencedObject());
173         // set it back so we can repopulate and then try when setting both FK fields to null
174         testDataObject.setStringProperty("cba2");
175         wrapper.fetchRelationship("anotherReferencedObject");
176         assertNotNull(testDataObject.getAnotherReferencedObject());
177         // set both FK's to null
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         // now refresh the "yetAnotherReferencedObject" relationship, the pk represents the internal key here
197         assertNull(testDataObject.getAnotherReferencedObject());
198 
199         // right now we have no foreign keys so this should essentially do nothing
200         DataObjectWrapper<?> wrapper = getDataObjectService().wrap(testDataObject);
201         wrapper.fetchRelationship("yetAnotherReferencedObject");
202         // should still be null
203         assertNull(testDataObject.getAnotherReferencedObject());
204 
205         // next, let's save a value for referenced object
206         YetAnotherReferencedDataObject referencedDataObject = new YetAnotherReferencedDataObject();
207         referencedDataObject.setId("abc");
208         referencedDataObject.setSomeOtherStringProperty("efg");
209         referencedDataObject = getDataObjectService().save(referencedDataObject);
210 
211         // now, if we refresh it should fetch the missing AnotherReferencedDataObject for us
212         assertNull(testDataObject.getYetAnotherReferencedObject());
213         wrapper.fetchRelationship("yetAnotherReferencedObject");
214         // now referenced object should not be null any longer
215         assertNotNull(testDataObject.getYetAnotherReferencedObject());
216         assertEquals("efg", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
217 
218         // now that it's saved, let's update the referenced data object value and make sure the refresh works
219         // first, let's refetch the referenced object and update it
220         referencedDataObject = getDataObjectService().find(YetAnotherReferencedDataObject.class, "abc");
221         assertEquals("efg", referencedDataObject.getSomeOtherStringProperty());
222         referencedDataObject.setSomeOtherStringProperty("efg2");
223         referencedDataObject = getDataObjectService().save(referencedDataObject);
224 
225         // it's updated now, so let's verify the original testDataObject is out of date
226         assertEquals("efg", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
227         wrapper = getDataObjectService().wrap(testDataObject);
228         wrapper.fetchRelationship("yetAnotherReferencedObject");
229         // now it should be updated!
230         assertEquals("efg2", testDataObject.getYetAnotherReferencedObject().getSomeOtherStringProperty());
231         // pk should still be the same
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         // use an invalid foreign key, the refresh should essentially do nothing
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 }