View Javadoc

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