1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.jpa;
17
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.persistence.EntityManagerFactory;
22 import javax.persistence.Persistence;
23
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.kuali.rice.core.api.data.DataType;
29 import org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl;
30 import org.kuali.rice.krad.data.jpa.testbo.CollectionDataObject;
31 import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
32 import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectExtension;
33 import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectTwoPkFields;
34 import org.kuali.rice.krad.data.jpa.testbo.TestNonPersistableObject;
35 import org.kuali.rice.krad.data.metadata.DataObjectAttribute;
36 import org.kuali.rice.krad.data.metadata.DataObjectAttributeRelationship;
37 import org.kuali.rice.krad.data.metadata.DataObjectCollection;
38 import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
39 import org.kuali.rice.krad.data.metadata.DataObjectRelationship;
40 import org.kuali.rice.krad.data.metadata.MetadataChild;
41
42 import static org.junit.Assert.*;
43
44 public class JpaMetadataProviderTest {
45 static EclipseLinkJpaMetadataProviderImpl metadataProvider;
46
47
48 @BeforeClass
49 public static void setUpBeforeClass() throws Exception {
50 metadataProvider = new EclipseLinkJpaMetadataProviderImpl();
51 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
52 metadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
53 }
54
55 @Test
56 public void testGetMetadataForType() throws Exception {
57 DataObjectMetadata metadata = metadataProvider
58 .getMetadataForType(TestDataObject.class);
59 assertNotNull("Returned metadata should not be null", metadata);
60 metadata = metadataProvider.getMetadataForType(Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject"));
61 assertNotNull("Returned metadata should not be null", metadata);
62 }
63
64 @Test
65 public void testGetAllMetadata() throws Exception {
66 Map<Class<?>, DataObjectMetadata> metadata = metadataProvider.provideMetadata();
67 assertNotNull("Returned metadata should not be null", metadata);
68 assertFalse("metadata map should not have been empty", metadata.isEmpty());
69 assertTrue("Should have had an entry for TestDataObject", metadata.containsKey(TestDataObject.class));
70 assertTrue("Should have had an entry for TestDataObject (when class name specified)", metadata.containsKey(
71 Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject")));
72 }
73
74 @Test
75 public void testGetMetadataForClass_ClassData() {
76 DataObjectMetadata metadata = metadataProvider
77 .getMetadataForType(TestDataObject.class);
78 System.err.println(metadata);
79 assertEquals("Incorrect Data Object Type", TestDataObject.class, metadata.getType());
80 assertEquals("Incorrect Type Label", "Test Data Object", metadata.getLabel());
81 assertEquals("Table name not set as the backing object name", "KRTST_TEST_TABLE_T",
82 metadata.getBackingObjectName());
83 }
84
85 @Test
86 public void testGetMetadataForClass_VersionAttribute() {
87 DataObjectMetadata metadata = metadataProvider
88 .getMetadataForType(TestDataObject.class);
89 assertEquals("Incorrect Version Setting on TestDataObject", Boolean.FALSE,
90 metadata.isSupportsOptimisticLocking());
91
92 metadata = metadataProvider.getMetadataForType(TestDataObjectTwoPkFields.class);
93 assertEquals("Incorrect Version Setting on TestDataObjectTwoPkFields", Boolean.TRUE,
94 metadata.isSupportsOptimisticLocking());
95 }
96
97 @Test
98 public void testGetMetadataForClass_Attributes() {
99 DataObjectMetadata metadata = metadataProvider
100 .getMetadataForType(TestDataObject.class);
101 List<DataObjectAttribute> attributes = metadata.getAttributes();
102 System.err.println(attributes);
103 assertNotNull("Returned attributes should not have been null", attributes);
104 assertFalse("Returned attributes should not have been empty", attributes.isEmpty());
105 DataObjectAttribute firstAttribute = metadata.getAttribute("primaryKeyProperty");
106 assertEquals("property name incorrect", "primaryKeyProperty", firstAttribute.getName());
107 assertEquals("Property label incorrect", "Primary Key Property", firstAttribute.getLabel());
108 assertEquals("Column name not set as the backing object name", "PK_PROP", firstAttribute.getBackingObjectName());
109 }
110
111 @Test
112 public void testGetMetadataForClass_Attribute_Types() {
113 DataObjectMetadata metadata = metadataProvider
114 .getMetadataForType(TestDataObject.class);
115 DataObjectAttribute attribute = metadata.getAttribute("stringProperty");
116
117 assertEquals("property DataType incorrect", DataType.STRING, attribute.getDataType());
118
119 attribute = metadata.getAttribute("dateProperty");
120
121 assertEquals("property DataType incorrect", DataType.DATE, attribute.getDataType());
122
123 attribute = metadata.getAttribute("currencyProperty");
124
125 assertEquals("property DataType incorrect", DataType.CURRENCY, attribute.getDataType());
126 }
127
128 @Test
129 public void testGetMetadataForClass_Attribute_Type_Unknown() {
130 DataObjectMetadata metadata = metadataProvider
131 .getMetadataForType(TestDataObject.class);
132 DataObjectAttribute attribute = metadata.getAttribute("nonStandardDataType");
133
134 assertEquals("nonStandardDataType property DataType incorrect", DataType.STRING, attribute.getDataType());
135 }
136
137 @Test
138 public void testGetMetadataForClass_Attribute_Non_OJB_Property() {
139 DataObjectMetadata metadata = metadataProvider
140 .getMetadataForType(TestDataObject.class);
141 DataObjectAttribute attribute = metadata.getAttribute("nonPersistedProperty");
142
143 Assert.assertNull("nonPersistedProperty should not exist", attribute);
144 }
145
146 @Test
147 public void testGetMetadataForCollection() {
148 DataObjectMetadata metadata = metadataProvider
149 .getMetadataForType(TestDataObject.class);
150 List<DataObjectCollection> collections = metadata.getCollections();
151
152
153 assertNotNull("Collections object should not be null", collections);
154 assertEquals("Collections size incorrect", 4, collections.size());
155
156 DataObjectCollection collection = collections.get(0);
157 assertEquals("property name incorrect", "collectionProperty", collection.getName());
158 assertEquals("collection backing object incorrect", "KRTST_TEST_COLL_T", collection.getBackingObjectName());
159 assertEquals("collection label incorrect", "Collection Property", collection.getLabel());
160 assertEquals("collection item label incorrect", "Collection Data Object", collection.getElementLabel());
161
162 collection = metadata.getCollection("collectionPropertyTwo");
163 assertNotNull("Collection object for collectionPropertyTwo should not be null", collection);
164 assertEquals("property name incorrect", "collectionPropertyTwo", collection.getName());
165
166 assertNotNull("attribute relationships must not be null", collection.getAttributeRelationships());
167 assertEquals("attribute relationships size incorrect", 1, collection.getAttributeRelationships().size());
168 DataObjectAttributeRelationship relationship = collection.getAttributeRelationships().get(0);
169 assertEquals("parent attribute name mismatch", "stringProperty", relationship.getParentAttributeName());
170 assertEquals("child attribute name mismatch", "primaryKeyPropertyUsingDifferentName",
171 relationship.getChildAttributeName());
172
173 assertNotNull("collection default sort list must not be null", collection.getDefaultOrdering());
174 assertEquals("collection default sort size incorrect", 1, collection.getDefaultOrdering().size());
175 }
176
177 @Test
178 public void testGetMetadataForCollection_Indirect() {
179 DataObjectMetadata metadata = metadataProvider
180 .getMetadataForType(TestDataObject.class);
181 DataObjectCollection collection = metadata.getCollection("indirectCollection");
182 assertNotNull("Collection object for indirectCollection should not be null", collection);
183 assertTrue("Should be labeled as indirect", collection.isIndirectCollection());
184 assertTrue("attribute relationship list should be empty: collection.getAttributeRelationships()",
185 collection.getAttributeRelationships().isEmpty());
186 }
187
188 @Test
189 public void testGetMetadataForRelationship_referencedObject_main() {
190 DataObjectMetadata metadata = metadataProvider
191 .getMetadataForType(TestDataObject.class);
192 List<DataObjectRelationship> relationships = metadata.getRelationships();
193 System.err.println(relationships);
194 assertNotNull("Relationships object should not be null", relationships);
195 assertEquals("Relationships size incorrect", 4, relationships.size());
196 DataObjectRelationship relationship = metadata.getRelationship("referencedObject");
197 assertNotNull("retrieval by property name failed", relationship);
198 assertEquals("property name incorrect", "referencedObject", relationship.getName());
199 assertEquals("collection backing object incorrect", "KRTST_TEST_REF_OBJ_T", relationship.getBackingObjectName());
200 assertEquals("collection label incorrect", "Referenced Object", relationship.getLabel());
201 }
202
203 @Test
204 public void testGetMetadataForRelationship_referencedObject_properties() {
205 DataObjectMetadata metadata = metadataProvider
206 .getMetadataForType(TestDataObject.class);
207 DataObjectRelationship relationship = metadata.getRelationship("referencedObject");
208 assertNotNull("retrieval by property name failed", relationship);
209
210 assertTrue("relationship should have been proxied", relationship.isLoadedDynamicallyUponUse());
211 assertFalse("loaded with parent should be false", relationship.isLoadedAtParentLoadTime());
212 assertFalse("saved with parent should be false", relationship.isSavedWithParent());
213 assertFalse("deleted with parent should be false", relationship.isDeletedWithParent());
214 assertTrue("read-only should be true", relationship.isReadOnly());
215 }
216
217 @Test
218 public void testGetMetadataForRelationship_referencedObject_attributes() {
219 DataObjectMetadata metadata = metadataProvider
220 .getMetadataForType(TestDataObject.class);
221 DataObjectRelationship relationship = metadata.getRelationship("referencedObject");
222 assertNotNull("retrieval by property name failed", relationship);
223
224 assertNotNull("attribute relationships must not be null", relationship.getAttributeRelationships());
225 assertEquals("attribute relationships size incorrect", 1, relationship.getAttributeRelationships().size());
226 DataObjectAttributeRelationship linkingAttributes = relationship.getAttributeRelationships().get(0);
227 assertEquals("parent attribute name mismatch", "stringProperty", linkingAttributes.getParentAttributeName());
228 assertEquals("child attribute name mismatch", "stringProperty", linkingAttributes.getChildAttributeName());
229 }
230
231 @Test
232 public void testGetMetadataForRelationship_anotherReferencedObject_main() {
233 DataObjectMetadata metadata = metadataProvider
234 .getMetadataForType(TestDataObject.class);
235 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
236 assertNotNull("retrieval by property name failed", relationship);
237
238 System.err.println("anotherReferencedObject: " + relationship);
239
240 assertEquals("property name incorrect", "anotherReferencedObject", relationship.getName());
241 assertEquals("collection backing object incorrect", "KRTST_TEST_ANOTHER_REF_OBJ_T",
242 relationship.getBackingObjectName());
243 assertEquals("collection label incorrect", "Another Referenced Object", relationship.getLabel());
244 }
245
246 @Test
247 public void testGetMetadataForRelationship_anotherReferencedObject_properties() {
248 DataObjectMetadata metadata = metadataProvider
249 .getMetadataForType(TestDataObject.class);
250 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
251 assertNotNull("retrieval by property name failed", relationship);
252
253 System.err.println("anotherReferencedObject: " + relationship);
254
255 assertFalse("relationship should not have been proxied", relationship.isLoadedDynamicallyUponUse());
256 assertTrue("loaded with parent should be true", relationship.isLoadedAtParentLoadTime());
257 assertFalse("saved with parent should be false", relationship.isSavedWithParent());
258 assertFalse("deleted with parent should be false", relationship.isDeletedWithParent());
259 assertTrue("read-only should be true", relationship.isReadOnly());
260 }
261
262 @Test
263 public void testGetMetadataForRelationship_anotherReferencedObject_attributes() {
264 DataObjectMetadata metadata = metadataProvider
265 .getMetadataForType(TestDataObject.class);
266 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
267 assertNotNull("retrieval by property name failed", relationship);
268
269 System.err.println("anotherReferencedObject: " + relationship);
270
271 assertNotNull("attribute relationships must not be null", relationship.getAttributeRelationships());
272 assertEquals("attribute relationships size incorrect", 2, relationship.getAttributeRelationships().size());
273 DataObjectAttributeRelationship linkingAttribute = relationship.getAttributeRelationships().get(0);
274 assertEquals("first parent attribute name mismatch", "stringProperty",
275 linkingAttribute.getParentAttributeName());
276 assertEquals("first child attribute name mismatch", "stringProperty", linkingAttribute.getChildAttributeName());
277
278 linkingAttribute = relationship.getAttributeRelationships().get(1);
279 assertEquals("second parent attribute name mismatch", "dateProperty", linkingAttribute.getParentAttributeName());
280 assertEquals("second child attribute name mismatch", "dateProperty", linkingAttribute.getChildAttributeName());
281 }
282
283 @Test
284 public void testGetMetadataForRelationship_yetAnotherReferencedObject_main() {
285 DataObjectMetadata metadata = metadataProvider
286 .getMetadataForType(TestDataObject.class);
287 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
288 assertNotNull("retrieval by property name failed", relationship);
289
290 System.err.println("anotherReferencedObject: " + relationship);
291
292 assertEquals("property name incorrect", "anotherReferencedObject", relationship.getName());
293 assertEquals("collection backing object incorrect", "KRTST_TEST_ANOTHER_REF_OBJ_T",
294 relationship.getBackingObjectName());
295 assertEquals("collection label incorrect", "Another Referenced Object", relationship.getLabel());
296 }
297
298 @Test
299 public void testGetMetadataForRelationship_yetAnotherReferencedObject_properties() {
300 DataObjectMetadata metadata = metadataProvider
301 .getMetadataForType(TestDataObject.class);
302 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
303 assertNotNull("retrieval by property name failed", relationship);
304
305 System.err.println("anotherReferencedObject: " + relationship);
306
307 assertFalse("relationship should not have been proxied", relationship.isLoadedDynamicallyUponUse());
308 assertTrue("loaded with parent should be true", relationship.isLoadedAtParentLoadTime());
309 assertFalse("saved with parent should be false", relationship.isSavedWithParent());
310 assertFalse("deleted with parent should be false", relationship.isDeletedWithParent());
311 assertTrue("read-only should be true", relationship.isReadOnly());
312 }
313
314 @Test
315 public void testGetMetadataForRelationship_yetAnotherReferencedObject_attributes() {
316 DataObjectMetadata metadata = metadataProvider
317 .getMetadataForType(TestDataObject.class);
318 DataObjectRelationship relationship = metadata.getRelationship("anotherReferencedObject");
319 assertNotNull("retrieval by property name failed", relationship);
320
321 System.err.println("anotherReferencedObject: " + relationship);
322
323 assertNotNull("attribute relationships must not be null", relationship.getAttributeRelationships());
324 assertEquals("attribute relationships size incorrect", 2, relationship.getAttributeRelationships().size());
325 DataObjectAttributeRelationship linkingAttribute = relationship.getAttributeRelationships().get(0);
326 assertEquals("first parent attribute name mismatch", "stringProperty",
327 linkingAttribute.getParentAttributeName());
328 assertEquals("first child attribute name mismatch", "stringProperty", linkingAttribute.getChildAttributeName());
329
330 linkingAttribute = relationship.getAttributeRelationships().get(1);
331 assertEquals("second parent attribute name mismatch", "dateProperty", linkingAttribute.getParentAttributeName());
332 assertEquals("second child attribute name mismatch", "dateProperty", linkingAttribute.getChildAttributeName());
333 }
334
335 @Test
336 public void testGetMetadataForRelationship_extension() {
337 DataObjectMetadata metadata = metadataProvider
338 .getMetadataForType(TestDataObject.class);
339 DataObjectRelationship relationship = metadata.getRelationship("extension");
340 assertNotNull("retrieval by property name failed", relationship);
341 assertTrue("Should have no attribute relationships.", relationship.getAttributeRelationships().isEmpty());
342
343 assertTrue("should be loaded with parent", relationship.isLoadedAtParentLoadTime());
344 assertFalse("should NOT be proxied", relationship.isLoadedDynamicallyUponUse());
345 assertFalse("should NOT be read-only", relationship.isReadOnly());
346 assertTrue("should be saved with parent", relationship.isSavedWithParent());
347 assertTrue("should be deleted with parent", relationship.isDeletedWithParent());
348 assertEquals("Should be related to TestDataObjectExtension", TestDataObjectExtension.class, relationship.getRelatedType());
349
350 MetadataChild inverse = relationship.getInverseRelationship();
351 assertNotNull("extension relationship should have an inverse relationship", inverse);
352 assertTrue("Inverse should be a relationship and not a collection.", inverse instanceof DataObjectRelationship);
353
354 }
355
356 @Test
357 public void testGetMetadataForRelationship_byLastAttribute_stringProperty() {
358 DataObjectMetadata metadata = metadataProvider
359 .getMetadataForType(TestDataObject.class);
360
361 DataObjectRelationship relationship = metadata.getRelationshipByLastAttributeInRelationship("stringProperty");
362 assertNotNull("retrieval by last attribute name (stringProperty) failed", relationship);
363 assertEquals("retrieval by last attribute name (stringProperty) returned wrong relationship",
364 "referencedObject", relationship.getName());
365 }
366
367 @Test
368 public void testGetMetadataForRelationship_byLastAttribute_dateProperty() {
369 DataObjectMetadata metadata = metadataProvider
370 .getMetadataForType(TestDataObject.class);
371
372 DataObjectRelationship relationship = metadata.getRelationshipByLastAttributeInRelationship("dateProperty");
373 assertNotNull("retrieval by last attribute name (dateProperty) failed", relationship);
374 assertEquals("retrieval by last attribute name (dateProperty) returned wrong relationship",
375 "anotherReferencedObject", relationship.getName());
376 }
377
378 @Test
379 public void testGetMetadataForRelationship_byLastAttribute_primaryKeyProperty() {
380 DataObjectMetadata metadata = metadataProvider
381 .getMetadataForType(TestDataObject.class);
382
383 DataObjectRelationship relationship = metadata
384 .getRelationshipByLastAttributeInRelationship("primaryKeyProperty");
385 assertNotNull("retrieval by last attribute name (primaryKeyProperty) failed", relationship);
386 assertEquals("retrieval by last attribute name (primaryKeyProperty) returned wrong relationship",
387 "yetAnotherReferencedObject", relationship.getName());
388 }
389
390 @Test
391 public void testGetMetadataForRelationship_byInvolvedAttribute() {
392 DataObjectMetadata metadata = metadataProvider
393 .getMetadataForType(TestDataObject.class);
394
395 List<DataObjectRelationship> rels = metadata.getRelationshipsInvolvingAttribute("stringProperty");
396 assertNotNull("retrieval by attribute name (stringProperty) failed", rels);
397 assertEquals("retrieval by attribute name (stringProperty): wrong number returned", 2, rels.size());
398
399 rels = metadata.getRelationshipsInvolvingAttribute("dateProperty");
400 assertNotNull("retrieval by attribute name (dateProperty) failed", rels);
401 assertEquals("retrieval by attribute name (dateProperty): wrong number returned", 1, rels.size());
402 }
403
404 @Test
405 public void testGetMetadataForClass_PKFields() {
406 DataObjectMetadata metadata = metadataProvider
407 .getMetadataForType(TestDataObject.class);
408 assertNotNull("PK field list should not have been null", metadata.getPrimaryKeyAttributeNames());
409 assertEquals("PK field list length incorrect", 1, metadata.getPrimaryKeyAttributeNames().size());
410 assertEquals("PK field wrong", "primaryKeyProperty", metadata.getPrimaryKeyAttributeNames().get(0));
411 assertEquals("Primary Display Field Wrong", "primaryKeyProperty", metadata.getPrimaryDisplayAttributeName());
412 }
413
414 @Test
415 public void testGetMetadataForClass_PKFields_TwoFieldIdClass() {
416 DataObjectMetadata metadata = metadataProvider.getMetadataForType(TestDataObjectTwoPkFields.class);
417 assertNotNull("PK field list should not have been null", metadata.getPrimaryKeyAttributeNames());
418 System.err.println("PK Properties: " + metadata.getPrimaryKeyAttributeNames());
419 assertEquals("PK field list length incorrect", 2, metadata.getPrimaryKeyAttributeNames().size());
420 assertEquals("PK field 1 wrong", "primaryKeyProperty", metadata.getPrimaryKeyAttributeNames().get(0));
421 assertEquals("PK field 2 wrong", "primaryKeyPropertyTwo", metadata.getPrimaryKeyAttributeNames().get(1));
422 assertEquals("Primary Display Field Wrong", "primaryKeyPropertyTwo", metadata.getPrimaryDisplayAttributeName());
423 }
424
425 @Test
426 public void testGetMetadataForClass_PKFields_TwoFieldNoIdClass() {
427 DataObjectMetadata metadata = metadataProvider.getMetadataForType(CollectionDataObject.class);
428 assertNotNull("PK field list should not have been null", metadata.getPrimaryKeyAttributeNames());
429 System.err.println("PK Properties: " + metadata.getPrimaryKeyAttributeNames());
430 assertEquals("PK field list length incorrect", 2, metadata.getPrimaryKeyAttributeNames().size());
431 assertEquals("PK field 1 wrong", "primaryKeyProperty", metadata.getPrimaryKeyAttributeNames().get(0));
432 assertEquals("PK field 2 wrong", "collectionKeyProperty", metadata.getPrimaryKeyAttributeNames().get(1));
433 assertEquals("Primary Display Field Wrong", "collectionKeyProperty", metadata.getPrimaryDisplayAttributeName());
434 }
435
436 @Test
437 public void testIsClassPersistable_ValidType() {
438 assertTrue("TestDataObject should have been persistable", metadataProvider.handles(TestDataObject.class));
439 }
440
441 @Test
442 public void testIsClassPersistable_InvalidType() {
443 assertFalse("TestNonPersistableObject should not have been persistable", metadataProvider.handles(
444 TestNonPersistableObject.class));
445 }
446
447 @Test
448 public void testAttributeSecurityIfEncrypted() {
449 DataObjectMetadata metadata = metadataProvider
450 .getMetadataForType(TestDataObject.class);
451 DataObjectAttribute attribute = metadata.getAttribute("encryptedProperty");
452
453 assertNotNull("encryptedProperty Missing", attribute);
454 assertTrue("sensitive property not set on encryptedProperty", attribute.isSensitive());
455 }
456 }