1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.apache.ojb.broker.metadata.DescriptorRepository;
19 import org.apache.ojb.broker.metadata.MetadataManager;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.kuali.rice.core.api.CoreConstants;
25 import org.kuali.rice.core.api.config.property.ConfigContext;
26 import org.kuali.rice.core.api.criteria.QueryByCriteria;
27 import org.kuali.rice.core.api.criteria.QueryResults;
28 import org.kuali.rice.core.api.datetime.DateTimeService;
29 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
30 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
31 import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
32 import org.kuali.rice.core.framework.config.property.SimpleConfig;
33 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
34 import org.kuali.rice.core.framework.resourceloader.BeanFactoryResourceLoader;
35 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
36 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
37 import org.kuali.rice.kew.api.doctype.DocumentType;
38 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
39 import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
40 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
41 import org.kuali.rice.krad.bo.PersistableBusinessObject;
42 import org.kuali.rice.krad.dao.LookupDao;
43 import org.kuali.rice.krad.dao.MaintenanceDocumentDao;
44 import org.kuali.rice.krad.data.CompoundKey;
45 import org.kuali.rice.krad.data.DataObjectService;
46 import org.kuali.rice.krad.data.DataObjectWrapper;
47 import org.kuali.rice.krad.data.PersistenceOption;
48 import org.kuali.rice.krad.data.metadata.DataObjectCollection;
49 import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
50 import org.kuali.rice.krad.data.metadata.MetadataRepository;
51 import org.kuali.rice.krad.messages.Message;
52 import org.kuali.rice.krad.service.BusinessObjectService;
53 import org.kuali.rice.krad.service.DataDictionaryService;
54 import org.kuali.rice.krad.service.DataObjectMetaDataService;
55 import org.kuali.rice.krad.service.PersistenceService;
56 import org.kuali.rice.krad.service.PersistenceStructureService;
57 import org.kuali.rice.krad.util.KRADConstants;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mock;
60 import org.mockito.runners.MockitoJUnitRunner;
61 import org.springframework.beans.factory.support.StaticListableBeanFactory;
62 import org.springframework.core.io.Resource;
63
64 import javax.xml.namespace.QName;
65 import java.io.InputStream;
66 import java.io.Serializable;
67 import java.util.HashMap;
68 import java.util.Map;
69
70 import static org.mockito.Matchers.any;
71 import static org.mockito.Matchers.anyBoolean;
72 import static org.mockito.Matchers.anyInt;
73 import static org.mockito.Matchers.anyMap;
74 import static org.mockito.Matchers.anyObject;
75 import static org.mockito.Matchers.anyString;
76 import static org.mockito.Matchers.eq;
77 import static org.mockito.Mockito.*;
78
79
80
81
82 @RunWith(MockitoJUnitRunner.class)
83 public class LegacyDataAdapterLegacyDetectionTest {
84 private static final String TEST_ID = LegacyDataAdapterLegacyDetectionTest.class.getName();
85
86 @Mock private DataObjectService dataObjectService;
87 @Mock private MetadataRepository metadataRepository;
88 @Mock private BusinessObjectService businessObjectService;
89 @Mock private BusinessObjectMetaDataService businessObjectMetaDataService;
90 @Mock private DataObjectMetaDataService dataObjectMetaDataService;
91 @Mock private PersistenceService persistenceService;
92 @Mock private LookupDao lookupDao;
93 @Mock private LookupCriteriaGenerator lookupCriteriaGenerator;
94 @Mock private DateTimeService dateTimeService;
95 @Mock private DatabasePlatform databasePlatform;
96 @Mock private PersistenceStructureService persistenceStructureService;
97
98 @Mock private MaintenanceDocumentDao maintenanceDocumentDaoOjb;
99 @Mock private DataObjectWrapper wrap;
100
101 @InjectMocks private LegacyDataAdapterImpl lda = new LegacyDataAdapterImpl();
102 @InjectMocks private KNSLegacyDataAdapterImpl knsLegacyDataAdapter = new KNSLegacyDataAdapterImpl();
103 @InjectMocks private KRADLegacyDataAdapterImpl kradLegacyDataAdapter = new KRADLegacyDataAdapterImpl();
104
105 @Before
106 public void setup() throws Exception {
107 GlobalResourceLoader.stop();
108
109 SimpleConfig config = new SimpleConfig();
110 config.putProperty(CoreConstants.Config.APPLICATION_ID, TEST_ID);
111 ConfigContext.init(config);
112 ConfigContext.getCurrentContextConfig().removeProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK);
113
114 StaticListableBeanFactory testBf = new StaticListableBeanFactory();
115
116 testBf.addBean(CoreFrameworkServiceLocator.PARAMETER_SERVICE, mock(ParameterService.class));
117
118
119
120 testBf.addBean(KimApiServiceLocator.KIM_PERSON_SERVICE, new PersonServiceImpl());
121 testBf.addBean("kd-metadataRepository", mock(MetadataRepository.class));
122 testBf.addBean("dataDictionaryService", mock(DataDictionaryService.class));
123 testBf.addBean("knsLegacyDataAdapter", knsLegacyDataAdapter);
124 testBf.addBean("kradLegacyDataAdapter",kradLegacyDataAdapter);
125 lda.setKradLegacyDataAdapter(kradLegacyDataAdapter);
126 lda.setKnsLegacyDataAdapter(knsLegacyDataAdapter);
127
128 ResourceLoader rl = new BeanFactoryResourceLoader(new QName(TEST_ID), testBf);
129 GlobalResourceLoader.addResourceLoader(rl);
130 GlobalResourceLoader.start();
131
132
133 Resource resource = new ClasspathOrFileResourceLoader().getResource("classpath:org/kuali/rice/krad/service/impl/OJB-repository-LegacyDataAdapterLegacyDetectionTest.xml");
134 InputStream is = resource.getInputStream();
135 MetadataManager mm = MetadataManager.getInstance();
136 DescriptorRepository dr = mm.readDescriptorRepository(is);
137 is.close();
138 mm.mergeDescriptorRepository(dr);
139
140
141 when(dataObjectService.findMatching(any(Class.class), any(QueryByCriteria.class))).thenReturn(mock(QueryResults.class));
142 when(dataObjectService.wrap(any(Class.class))).thenReturn(wrap);
143 when(dataObjectService.getMetadataRepository()).thenReturn(metadataRepository);
144 when(metadataRepository.getMetadata(any(Class.class))).thenReturn(mock(DataObjectMetadata.class));
145 when(lookupCriteriaGenerator.generateCriteria(any(Class.class), anyMap(), anyBoolean())).thenReturn(
146 QueryByCriteria.Builder.create());
147 when(lookupCriteriaGenerator.createObjectCriteriaFromMap(anyObject(), anyMap())).thenReturn(QueryByCriteria.Builder.create());
148 }
149
150 protected void enableLegacyFramework() {
151 ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK, "true");
152 ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.KNS_ENABLED, "true");
153 }
154
155 protected Serializable newDataObject() {
156
157 return new String("test");
158 }
159
160 protected PersistableBusinessObject newPersistableBusinessObject() {
161
162 return new Message();
163 }
164
165 @Test
166 public void testSave() {
167 Serializable obj = newDataObject();
168 lda.save(obj);
169 verify(dataObjectService).save(obj);
170 verify(businessObjectService, never()).save(any(PersistableBusinessObject.class));
171 }
172
173 @Test
174 public void testLegacySave() {
175 enableLegacyFramework();
176 PersistableBusinessObject obj = newPersistableBusinessObject();
177 lda.save(obj);
178 verify(businessObjectService).save(obj);
179 verify(dataObjectService, never()).save(any(Serializable.class), any(PersistenceOption.class));
180 }
181
182 @Test
183 public void testLinkAndSave() {
184 Serializable obj = newDataObject();
185 lda.linkAndSave(obj);
186 verify(dataObjectService).save(obj, PersistenceOption.LINK_KEYS);
187 verify(businessObjectService, never()).linkAndSave(any(PersistableBusinessObject.class));
188 }
189
190 @Test
191 public void testLegacyLinkAndSave() {
192 enableLegacyFramework();
193 PersistableBusinessObject obj = newPersistableBusinessObject();
194 lda.linkAndSave(obj);
195 verify(businessObjectService).linkAndSave(obj);
196 verify(dataObjectService, never()).save(any(PersistableBusinessObject.class), any(PersistenceOption.class));
197 }
198
199 @Test
200 public void testFindByPrimaryKey() {
201 Map<String, Object> keys = new HashMap<String, Object>();
202
203 keys.put("a", "b");
204 lda.findByPrimaryKey(String.class, keys);
205 verify(dataObjectService).find(eq(String.class), any(CompoundKey.class));
206 verify(businessObjectService, never()).findByPrimaryKey(any(Class.class), anyMap());
207 }
208
209 @Test
210 public void testLegacyFindByPrimaryKey() {
211 enableLegacyFramework();
212 Map<String, ?> keys = new HashMap<String, Object>();
213 lda.findByPrimaryKey(Message.class, keys);
214 verify(businessObjectService).findByPrimaryKey(Message.class, keys);
215 verify(dataObjectService, never()).find(any(Class.class), any(CompoundKey.class));
216 }
217
218 @Test
219 public void testFindBySinglePrimaryKey() {
220 Object key = new String();
221 lda.findBySinglePrimaryKey(String.class, key);
222 verify(dataObjectService).find(String.class, key);
223 verify(businessObjectService, never()).findBySinglePrimaryKey(any(Class.class), any());
224 }
225
226 @Test
227 public void testLegacyFindBySinglePrimaryKey() {
228 enableLegacyFramework();
229 Object key = new String();
230 lda.findBySinglePrimaryKey(Message.class, key);
231 verify(businessObjectService).findBySinglePrimaryKey(Message.class, key);
232 verify(dataObjectService, never()).find(any(Class.class), any());
233 }
234
235 @Test
236 public void testDelete() {
237 Serializable obj = newDataObject();
238 lda.delete(obj);
239 verify(dataObjectService).delete(obj);
240 verify(businessObjectService, never()).delete(any(PersistableBusinessObject.class));
241 }
242
243
244 @Test
245 public void testLegacyDelete() {
246 enableLegacyFramework();
247 PersistableBusinessObject obj = newPersistableBusinessObject();
248 lda.delete(obj);
249 verify(businessObjectService).delete(obj);
250 verify(dataObjectService, never()).delete(any());
251 }
252
253 @Test
254 public void testDeleteMatching() {
255 Map<String, ?> keys = new HashMap<String, Object>();
256 lda.deleteMatching(String.class, keys);
257 verify(dataObjectService).deleteMatching(eq(String.class), any(QueryByCriteria.class));
258 verify(businessObjectService, never()).deleteMatching(any(Class.class), anyMap());
259 }
260
261 @Test
262 public void testLegacyDeleteMatching() {
263 enableLegacyFramework();
264 Map<String, ?> keys = new HashMap<String, Object>();
265 lda.deleteMatching(Message.class, keys);
266 verify(businessObjectService).deleteMatching(Message.class, keys);
267 verify(dataObjectService, never()).deleteMatching(any(Class.class), any(QueryByCriteria.class));
268 }
269
270 @Test
271 public void testRetrieve() {
272 Serializable obj = newDataObject();
273 try {
274 lda.retrieve(obj);
275 Assert.fail("IllegalArgumentException should have been thrown");
276 } catch (IllegalArgumentException e) {
277
278 }
279 verify(dataObjectService).wrap(any(Object.class));
280 verify(wrap).getPrimaryKeyValues();
281 verify(businessObjectService, never()).retrieve(any(PersistableBusinessObject.class));
282 }
283
284 @Test
285 public void testLegacyRetrieve() {
286 enableLegacyFramework();
287 PersistableBusinessObject obj = newPersistableBusinessObject();
288 lda.retrieve(obj);
289 verify(businessObjectService).retrieve(obj);
290 verify(dataObjectService, never()).find(any(Class.class), any(CompoundKey.class));
291 }
292
293 @Test
294 public void testFindMatching() {
295 Map<String, ?> fields = new HashMap<String, Object>();
296 lda.findMatching(String.class, fields);
297 verify(dataObjectService).findMatching(eq(String.class), any(QueryByCriteria.class));
298 verify(businessObjectService, never()).findMatching(any(Class.class), anyMap());
299 }
300
301 @Test
302 public void testLegacyFindMatching() {
303 enableLegacyFramework();
304 Map<String, ?> fields = new HashMap<String, Object>();
305 lda.findMatching(Message.class, fields);
306 verify(businessObjectService).findMatching(Message.class, fields);
307 verify(dataObjectService, never()).findMatching(any(Class.class), any(QueryByCriteria.class));
308 }
309
310 @Test
311 public void testGetPrimaryKeyFieldValuesPS() {
312 Object obj = new Object();
313 lda.getPrimaryKeyFieldValues(obj);
314 verify(dataObjectService).wrap(obj);
315 verify(wrap).getPrimaryKeyValues();
316 verify(persistenceService, never()).getPrimaryKeyFieldValues(any());
317 }
318
319 @Test
320 public void testLegacyGetPrimaryKeyFieldValuesPS() {
321 enableLegacyFramework();
322 PersistableBusinessObject obj = newPersistableBusinessObject();
323 lda.getPrimaryKeyFieldValues(obj);
324 verify(persistenceService).getPrimaryKeyFieldValues(obj);
325 verify(dataObjectService, never()).wrap(obj);
326 }
327
328 @Test
329 public void testRetrieveNonKeyFields() {
330 Object obj = new Object();
331 lda.retrieveNonKeyFields(obj);
332 verify(persistenceService, never()).retrieveNonKeyFields(any());
333 }
334
335 @Test
336 public void testLegacyRetrieveNonKeyFields() {
337 enableLegacyFramework();
338 PersistableBusinessObject obj = newPersistableBusinessObject();
339 lda.retrieveNonKeyFields(obj);
340 verify(persistenceService).retrieveNonKeyFields(obj);
341
342 }
343
344 @Test
345 public void testRetrieveReferenceObject() {
346 Object obj = new Object();
347 String name = "";
348 lda.retrieveReferenceObject(obj, name);
349 verify(persistenceService, never()).retrieveReferenceObject(any(), anyString());
350 }
351
352 @Test
353 public void testLegacyRetrieveReferenceObject() {
354 enableLegacyFramework();
355 PersistableBusinessObject obj = newPersistableBusinessObject();
356 String name = "";
357 lda.retrieveReferenceObject(obj, name);
358 verify(persistenceService).retrieveReferenceObject(obj, name);
359
360 }
361
362 @Test
363 public void testRefreshAllNonUpdatingReferences() {
364 Object obj = new Object();
365 lda.refreshAllNonUpdatingReferences(obj);
366 verify(persistenceService, never()).refreshAllNonUpdatingReferences(any(PersistableBusinessObject.class));
367 }
368
369 @Test
370 public void testLegacyRefreshAllNonUpdatingReferences() {
371 enableLegacyFramework();
372 PersistableBusinessObject obj = newPersistableBusinessObject();
373 lda.refreshAllNonUpdatingReferences(obj);
374 verify(persistenceService).refreshAllNonUpdatingReferences(obj);
375
376 }
377
378 @Test
379 public void testIsProxied() {
380 Object obj = new Object();
381 lda.isProxied(obj);
382
383 verify(persistenceService, never()).isProxied(any());
384 }
385 @Test
386 public void testLegacyIsProxied() {
387 enableLegacyFramework();
388 PersistableBusinessObject obj = newPersistableBusinessObject();
389 lda.isProxied(obj);
390 verify(persistenceService).isProxied(obj);
391
392 }
393
394 @Test
395 public void testResolveProxy() {
396 Object obj = new Object();
397 lda.resolveProxy(obj);
398
399 verify(persistenceService, never()).resolveProxy(any());
400 }
401 @Test
402 public void testLegacyResolveProxy() {
403 enableLegacyFramework();
404 PersistableBusinessObject obj = newPersistableBusinessObject();
405 lda.resolveProxy(obj);
406 verify(persistenceService).resolveProxy(obj);
407
408 }
409
410
411
412 @Test
413 public void testFindCollectionBySearchHelper() {
414 Map<String, String> fields = new HashMap<String, String>();
415 lda.findCollectionBySearchHelper(Message.class, fields, true, false, 1);
416 verify(lookupCriteriaGenerator).generateCriteria(Message.class, fields, false);
417 verify(dataObjectService).findMatching(eq(Message.class), any(QueryByCriteria.class));
418 verify(lookupDao, never()).findCollectionBySearchHelper(any(Class.class), anyMap(), anyBoolean(), anyBoolean(), anyInt());
419 }
420 @Test
421 public void testLegacyFindCollectionBySearchHelper() {
422 enableLegacyFramework();
423 Map<String, String> fields = new HashMap<String, String>();
424 lda.findCollectionBySearchHelper(Message.class, fields, true, false, 1);
425 verify(lookupDao).findCollectionBySearchHelper(Message.class, fields, true, false, 1);
426 verify(dataObjectService, never()).findMatching(any(Class.class), any(QueryByCriteria.class));
427 }
428
429 @Test
430 public void testFindObjectBySearch() {
431 Map<String, String> fields = new HashMap<String, String>();
432 lda.findObjectBySearch(Message.class, fields);
433 verify(dataObjectService).findMatching(eq(Message.class), any(QueryByCriteria.class));
434 verify(lookupDao, never()).findObjectByMap(any(Class.class), anyMap());
435 }
436 @Test
437 public void testLegacyFindObjectBySearch() {
438 enableLegacyFramework();
439 Map<String, String> fields = new HashMap<String, String>();
440 lda.findObjectBySearch(Message.class, fields);
441 verify(lookupDao).findObjectByMap(Message.class, fields);
442 verify(dataObjectService, never()).findMatching(any(Class.class), any(QueryByCriteria.class));
443 }
444
445 @Test
446 public void testListPrimaryKeyFieldNames() {
447 when(metadataRepository.contains(String.class)).thenReturn(true);
448 when(lda.isPersistable(String.class)).thenReturn(true);
449
450 lda.listPrimaryKeyFieldNames(String.class);
451 verify(metadataRepository).getMetadata(String.class);
452 verify(persistenceStructureService, never()).listPrimaryKeyFieldNames(any(Class.class));
453 }
454 @Test
455 public void testLegacyListPrimaryKeyFieldNames() {
456 enableLegacyFramework();
457 when(metadataRepository.contains(Message.class)).thenReturn(true);
458 when(lda.isPersistable(Message.class)).thenReturn(true);
459
460 lda.listPrimaryKeyFieldNames(Message.class);
461
462 verify(persistenceStructureService).listPrimaryKeyFieldNames(Message.class);
463 verify(metadataRepository, never()).getMetadata(any(Class.class));
464 }
465
466
467 @Test
468 public void testDetermineCollectionObjectType() {
469 when(metadataRepository.contains(Message.class)).thenReturn(true);
470
471 DataObjectMetadata mockMetaData = mock(DataObjectMetadata.class);
472 DataObjectCollection mockCollection = mock(DataObjectCollection.class);
473 when(mockCollection.getRelatedType()).thenReturn(any(Class.class));
474 when(mockMetaData.getCollection("collectionName")).thenReturn(mockCollection);
475 when(metadataRepository.getMetadata(Message.class)).thenReturn(mockMetaData);
476
477 when(lda.isPersistable(DocumentType.class)).thenReturn(true);
478
479 lda.determineCollectionObjectType(Message.class, "collectionName");
480
481 verify(metadataRepository).getMetadata(Message.class);
482 verify(persistenceStructureService, never()).listCollectionObjectTypes(any(Class.class));
483 }
484 @Test
485 public void testLegacyDetermineCollectionObjectType() {
486 enableLegacyFramework();
487 when(metadataRepository.contains(Message.class)).thenReturn(true);
488 when(lda.isPersistable(Message.class)).thenReturn(true);
489
490 lda.determineCollectionObjectType(Message.class, "namespaceCode");
491
492 verify(persistenceStructureService).listCollectionObjectTypes(Message.class);
493 verify(metadataRepository, never()).getMetadata(any(Class.class));
494 }
495
496 @Test
497 public void testIsLockable() {
498 lda.isLockable("foo");
499 verify(persistenceStructureService, never()).isPersistable(any(Class.class));
500 }
501 @Test
502 public void testLegacyIsLockable() {
503 enableLegacyFramework();
504 lda.isLockable(new Message());
505 verify(persistenceStructureService).isPersistable(Message.class);
506 }
507
508 @Test
509 public void testVerifyVersionNumber() {
510 Serializable obj = newDataObject();
511 lda.verifyVersionNumber(obj);
512
513 verify(persistenceStructureService, never()).isPersistable(any(Class.class));
514 }
515 @Test
516 public void testLegacyVerifyVersionNumber() {
517 enableLegacyFramework();
518 PersistableBusinessObject obj = newPersistableBusinessObject();
519 lda.verifyVersionNumber(obj);
520 verify(persistenceStructureService).isPersistable(obj.getClass());
521
522 }
523
524 @Test
525 public void testGetPrimaryKeyFieldValuesDOMDS() {
526 Object obj = new Object();
527 lda.getPrimaryKeyFieldValuesDOMDS(obj);
528 verify(dataObjectService).wrap(obj);
529 verify(wrap).getPrimaryKeyValues();
530 verify(dataObjectMetaDataService, never()).getPrimaryKeyFieldValues(any());
531 }
532
533 @Test
534 public void testLegacyGetPrimaryKeyFieldValuesDOMDS() {
535 enableLegacyFramework();
536 PersistableBusinessObject obj = newPersistableBusinessObject();
537 lda.getPrimaryKeyFieldValuesDOMDS(obj);
538 verify(dataObjectMetaDataService).getPrimaryKeyFieldValues(obj);
539 verify(dataObjectService, never()).wrap(obj);
540 }
541
542
543
544
545
546
547
548
549
550
551 @Test
552 public void testEqualsByPrimaryKeys() {
553 Object object1 = new Object();
554 Object object2 = new Object();
555 lda.equalsByPrimaryKeys(object1, object2);
556 verify(dataObjectService).wrap(object1);
557 verify(wrap).equalsByPrimaryKey(object2);
558 verifyZeroInteractions(dataObjectMetaDataService);
559 }
560
561 @Test
562 public void testEqualsByPrimaryKeysLegacy() {
563 enableLegacyFramework();
564 PersistableBusinessObject object1 = newPersistableBusinessObject();
565 PersistableBusinessObject object2 = newPersistableBusinessObject();
566 lda.equalsByPrimaryKeys(object1, object2);
567 verify(dataObjectMetaDataService).equalsByPrimaryKeys(object1, object2);
568 verifyZeroInteractions(dataObjectService);
569 }
570
571
572
573 }