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.data.provider;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  import org.kuali.rice.krad.data.DataObjectWrapper;
22  import org.kuali.rice.krad.data.DataObjectService;
23  import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
24  import org.kuali.rice.krad.data.provider.impl.DataObjectWrapperBase;
25  import org.kuali.rice.krad.data.util.ReferenceLinker;
26  import org.mockito.Mock;
27  import org.mockito.invocation.InvocationOnMock;
28  import org.mockito.runners.MockitoJUnitRunner;
29  import org.mockito.stubbing.Answer;
30  import org.springframework.beans.NullValueInNestedPathException;
31  
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Map;
36  
37  import static org.junit.Assert.*;
38  import static org.mockito.Mockito.any;
39  import static org.mockito.Mockito.when;
40  
41  @RunWith(MockitoJUnitRunner.class)
42  public class DataObjectWrapperBaseTest {
43  
44      @Mock private DataObjectService dataObjectService;
45      @Mock private DataObjectMetadata metadata;
46      @Mock private ReferenceLinker referenceLinker;
47  
48      private DataObject dataObject;
49      private DataObject2 dataObject2;
50      private DataObjectWrapper<DataObject> wrap;
51  
52      @Before
53      public void setup() throws Exception {
54          this.dataObject = new DataObject("1","FieldOne",2);
55          this.dataObject2 = new DataObject2("one", "two");
56          this.dataObject.setDataObject2(this.dataObject2);
57          this.wrap = new DataObjectWrapperImpl<DataObject>(dataObject, metadata, dataObjectService, referenceLinker);
58  
59          // stub out the metadata for our DataObject type
60          when(metadata.getPrimaryKeyAttributeNames()).thenReturn(Collections.singletonList("id"));
61  
62          final DataObjectService dataObjectService = this.dataObjectService;
63          final DataObjectMetadata metadata = this.metadata;
64          // make it so that DataObjectService returns a proper wrap when asked
65          when(dataObjectService.wrap(any(DataObject.class))).thenAnswer(new Answer() {
66              @Override
67              public Object answer(InvocationOnMock invocation) {
68                  DataObject object = (DataObject)invocation.getArguments()[0];
69                  return new DataObjectWrapperImpl<DataObject>(object, metadata, dataObjectService, referenceLinker);
70              }
71          });
72      }
73  
74      static final class DataObjectWrapperImpl<T> extends DataObjectWrapperBase<T> {
75          private DataObjectWrapperImpl(T dataObject, DataObjectMetadata metadata, DataObjectService dataObjectService,
76                  ReferenceLinker referenceLinker) {
77              super(dataObject, metadata, dataObjectService, referenceLinker);
78          }
79      }
80  
81      @Test
82      public void testGetType() {
83          assertEquals(DataObject.class, wrap.getWrappedClass());
84      }
85  
86      @Test
87      public void testGetMetadata() {
88          assertEquals(metadata, wrap.getMetadata());
89      }
90  
91      @Test
92      public void testGetWrappedInstance() {
93          assertEquals(dataObject, wrap.getWrappedInstance());
94      }
95  
96      @Test
97      public void testGetPrimaryKeyValues() {
98          Map<String, Object> primaryKeyValues = wrap.getPrimaryKeyValues();
99          assertEquals(1, primaryKeyValues.size());
100         assertTrue(primaryKeyValues.containsKey("id"));
101         assertEquals("1", primaryKeyValues.get("id"));
102     }
103 
104     @Test
105     public void testEqualsByPrimaryKey() {
106         // first check that it's equal to itself
107         assertTrue(wrap.equalsByPrimaryKey(dataObject));
108 
109         // now create one with an equal primary key but different values for non-pk fields, should be euqual
110         assertTrue(wrap.equalsByPrimaryKey(new DataObject("1", "blah", 500)));
111 
112         // now create one with a different primary key, should not be equal
113         assertFalse(wrap.equalsByPrimaryKey(new DataObject("2", "FieldOne", 2)));
114 
115         // let's do some null checking
116         assertFalse(wrap.equalsByPrimaryKey(null));
117 
118         // verify what happens when primary key is null on object being compared
119         assertFalse(wrap.equalsByPrimaryKey(new DataObject(null, null, -1)));
120 
121     }
122 
123     @Test
124     public void testGetPropertyType_Nested() {
125         assertEquals(DataObject2.class, wrap.getPropertyType("dataObject2"));
126         assertEquals(String.class, wrap.getPropertyType("dataObject2.one"));
127         assertEquals(String.class, wrap.getPropertyType("dataObject2.two"));
128     }
129 
130     @Test
131     public void testGetPropertyValueNullSafe() {
132         DataObject dataObject = new DataObject("a", "b", 3);
133         DataObjectWrapper<DataObject> wrap = new DataObjectWrapperImpl<DataObject>(dataObject, metadata, dataObjectService,
134                 referenceLinker);
135         assertNull(wrap.getPropertyValue("dataObject2"));
136 
137         //wrap.setPropertyValue("dataObject2.dataObject3", new DataObject3());
138 
139         // assert that a NullValueInNestedPathException is thrown
140         try {
141             wrap.getPropertyValue("dataObject2.dataObject3");
142             fail("NullValueInNestedPathException should have been thrown");
143         } catch (NullValueInNestedPathException e) {
144             // this should be thrown!
145         }
146 
147         // now do a null-safe check
148         assertNull(wrap.getPropertyValueNullSafe("dataObject2.dataObject3"));
149 
150     }
151 
152     @Test
153     public void testGetPropertyType_Collection() {
154         // setup stuff
155         DataObject dataObject = new DataObject("a", "b", 3);
156         DataObject3 do3_1 = new DataObject3();
157         do3_1.setHello("hi");
158         do3_1.setWorld("Earth");
159         DataObject3 do3_2 = new DataObject3();
160         do3_2.setHello("howdy");
161         do3_2.setWorld("Westeros");
162         dataObject.getDataObject3s().add(do3_1);
163         dataObject.getDataObject3s().add(do3_2);
164 
165         // now check through a collection
166         DataObjectWrapper<DataObject> wrap = new DataObjectWrapperImpl<DataObject>(dataObject, metadata, dataObjectService,
167                 referenceLinker);
168         Class<?> type = wrap.getPropertyType("dataObject3s[0].hello");
169         assertEquals(String.class, type);
170         type = wrap.getPropertyType("dataObject3s[1].world");
171         assertEquals(String.class, type);
172         type = wrap.getPropertyType("dataObject3s[2].world");
173         // should be null because we have nothing at this index
174         assertNull(type);
175     }
176 
177 
178     public static final class DataObject {
179 
180         private String id;
181         private String fieldOne;
182         private Integer fieldTwo;
183         private DataObject2 dataObject2;
184 
185         private List<DataObject3> dataObject3s;
186 
187         DataObject(String id, String fieldOne, Integer fieldTwo) {
188             this.id = id;
189             this.fieldOne = fieldOne;
190             this.fieldTwo = fieldTwo;
191             this.dataObject2 = dataObject2;
192             this.dataObject3s = new ArrayList<DataObject3>();
193         }
194 
195         public String getId() {
196             return id;
197         }
198 
199         public void setId(String id) {
200             this.id = id;
201         }
202 
203         public String getFieldOne() {
204             return fieldOne;
205         }
206 
207         public void setFieldOne(String fieldOne) {
208             this.fieldOne = fieldOne;
209         }
210 
211         public Integer getFieldTwo() {
212             return fieldTwo;
213         }
214 
215         public void setFieldTwo(Integer fieldTwo) {
216             this.fieldTwo = fieldTwo;
217         }
218 
219         public DataObject2 getDataObject2() {
220             return dataObject2;
221         }
222 
223         public void setDataObject2(DataObject2 dataObject2) {
224             this.dataObject2 = dataObject2;
225         }
226 
227         public List<DataObject3> getDataObject3s() {
228             return dataObject3s;
229         }
230 
231         public void setDataObject3s(List<DataObject3> dataObject3s) {
232             this.dataObject3s = dataObject3s;
233         }
234 
235     }
236 
237     public static final class DataObject2 {
238         private String one;
239         private String two;
240         private DataObject3 dataObject3;
241 
242         public DataObject2() {}
243 
244         public DataObject2(String one, String two) {
245             this.one = one;
246             this.two = two;
247         }
248 
249         public String getOne() {
250             return one;
251         }
252 
253         public void setOne(String one) {
254             this.one = one;
255         }
256 
257         public String getTwo() {
258             return two;
259         }
260 
261         public void setTwo(String two) {
262             this.two = two;
263         }
264 
265         public DataObject3 getDataObject3() {
266             return dataObject3;
267         }
268 
269         public void setDataObject3(DataObject3 dataObject3) {
270             this.dataObject3 = dataObject3;
271         }
272     }
273 
274     public static final class DataObject3 {
275 
276         private String hello;
277         private String world;
278 
279         public String getHello() {
280             return hello;
281         }
282 
283         public void setHello(String hello) {
284             this.hello = hello;
285         }
286 
287         public String getWorld() {
288             return world;
289         }
290 
291         public void setWorld(String world) {
292             this.world = world;
293         }
294     }
295 
296 }