1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.krms.test;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.kuali.rice.core.api.criteria.QueryByCriteria;
22 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import static org.junit.Assert.*;
29 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
30 import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
31
32
33
34
35
36
37 public class RuleManagementReferenceObjectBindingTest extends RuleManagementBaseTest {
38 @Override
39 @Before
40 public void setClassDiscriminator() {
41
42 CLASS_DISCRIMINATOR = "RMROBT";
43 }
44
45
46
47
48
49
50 @Test
51 public void testCreateReferenceObjectBinding() {
52
53 RuleManagementBaseTestObjectNames t0 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
54
55
56 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t0.object0);
57 ReferenceObjectBinding refObjBinding = ruleManagementService.getReferenceObjectBinding(
58 refObjBindingBuilder.getId());
59 refObjBindingBuilder = ReferenceObjectBinding.Builder.create(refObjBinding);
60
61
62 assertNotNull("Created ReferenceObjectBinding not found", refObjBindingBuilder);
63 assertEquals("Invalid CollectionName of refObjBindingBuilder found", "ParkingPolicies",
64 refObjBindingBuilder.getCollectionName());
65 assertEquals("Invalid KrmsObjectId of refObjBindingBuilder found",t0.agenda_Id, refObjBindingBuilder.getKrmsObjectId());
66 assertEquals("Invalid KrmsDiscriminatorType of refObjBindingBuilder found",
67 krmsTypeRepository.getTypeByName(t0.namespaceName, "AgendaType" + t0.object0).getId(), refObjBindingBuilder.getKrmsDiscriminatorType() );
68 assertEquals("Invalid Namespace of refObjBindingBuilder found",t0.namespaceName, refObjBindingBuilder.getNamespace());
69 assertEquals("Invalid ReferenceObjectId of refObjBindingBuilder found",t0.referenceObject_0_Id, refObjBindingBuilder.getReferenceObjectId());
70 assertEquals("Invalid ReferenceDiscriminatorType of refObjBindingBuilder found",
71 t0.referenceObject_0_DiscriminatorType, refObjBindingBuilder.getReferenceDiscriminatorType());
72 assertEquals("Invalid Active value of refObjBindingBuilder found",true, refObjBindingBuilder.isActive());
73
74
75 try {
76 ruleManagementService.createReferenceObjectBinding(refObjBindingBuilder.build());
77 fail("Should have thrown IllegalStateException: the ReferenceObjectBinding to create already exists");
78 } catch (Exception e) {
79
80 }
81
82
83 refObjBindingBuilder.setId("RefObjBindIdChanged");
84 try {
85 ruleManagementService.createReferenceObjectBinding(refObjBindingBuilder.build());
86 fail("Should have thrown DataIntegrityViolationException: OJB operation; SQL []; Duplicate entry");
87 } catch (Exception e) {
88
89 }
90 }
91
92
93
94
95
96
97 @Test
98 public void testGetReferenceObjectBinding() {
99
100 RuleManagementBaseTestObjectNames t1 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
101
102 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t1.object0);
103
104 ReferenceObjectBinding refObjBinding = ruleManagementService.getReferenceObjectBinding(
105 refObjBindingBuilder.getId());
106 assertNotNull("ReferenceObjectBinding should have been returned",refObjBinding);
107 assertEquals("Incorrect value found on returned ReferenceObjectBinding",t1.referenceObject_0_Id,refObjBinding.getReferenceObjectId());
108
109
110 try {
111 refObjBinding = ruleManagementService.getReferenceObjectBinding(null);
112 fail("Should have thrown IllegalArgumentException: referenceObjectBindingId was null");
113 } catch (Exception e) {
114
115 }
116
117
118 assertNull("ReferenceObjectBinding should not have been found", ruleManagementService.getReferenceObjectBinding("junk_value"));
119 }
120
121
122
123
124
125
126 @Test
127 public void testGetReferenceObjectBindings() {
128
129 RuleManagementBaseTestObjectNames t2 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
130
131 List<String> referenceObjectBindingIds = new ArrayList<String>();
132 referenceObjectBindingIds.add(createTestReferenceObjectBinding(t2.object0).getId());
133 referenceObjectBindingIds.add(createTestReferenceObjectBinding(t2.object1).getId());
134 referenceObjectBindingIds.add(createTestReferenceObjectBinding(t2.object2).getId());
135
136 List<ReferenceObjectBinding> referenceObjectBindings = ruleManagementService.getReferenceObjectBindings(referenceObjectBindingIds);
137 int objectsFound = 0;
138 for ( ReferenceObjectBinding referenceObjectBinding : referenceObjectBindings) {
139 if ( referenceObjectBindingIds.contains( referenceObjectBinding.getId())) {
140 objectsFound++;
141 } else {
142 fail("Unexpected object returned");
143 }
144 }
145 assertEquals("Expected number of objects not returned",3,objectsFound);
146
147
148 try {
149 ruleManagementService.getReferenceObjectBindings(null);
150 fail("Should have thrown IllegalArgumentException: reference binding object ids must not be null");
151 } catch (IllegalArgumentException e) {
152
153 }
154
155 assertEquals("Incorrect number of objects returned", 0, ruleManagementService.getReferenceObjectBindings(new ArrayList<String>()).size());
156
157
158 referenceObjectBindingIds.add("junkValue");
159 referenceObjectBindings = ruleManagementService.getReferenceObjectBindings(referenceObjectBindingIds);
160 objectsFound = 0;
161 for ( ReferenceObjectBinding referenceObjectBinding : referenceObjectBindings) {
162 if ( referenceObjectBindingIds.contains( referenceObjectBinding.getId())) {
163 objectsFound++;
164 } else {
165 fail("Unexpected object returned");
166 }
167 }
168 assertEquals("Expected number of objects not returned",3,objectsFound);
169 }
170
171
172
173
174
175
176 @Test
177 public void testFindReferenceObjectBindingsByReferenceObject() {
178
179 RuleManagementBaseTestObjectNames t3 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
180 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t3.object0);
181
182
183 List<ReferenceObjectBinding> referenceObjectBindings = ruleManagementService.findReferenceObjectBindingsByReferenceObject(
184 refObjBindingBuilder.getReferenceDiscriminatorType(), refObjBindingBuilder.getReferenceObjectId());
185 assertEquals("Incorrect number of objects returned",1,referenceObjectBindings.size());
186
187
188 try {
189 ruleManagementService.findReferenceObjectBindingsByReferenceObject(null,refObjBindingBuilder.getReferenceObjectId());
190 fail("should have thrown RiceIllegalArgumentException: reference binding object discriminator type must not be null");
191 } catch (RiceIllegalArgumentException e) {
192
193 }
194
195
196 try {
197 ruleManagementService.findReferenceObjectBindingsByReferenceObject(refObjBindingBuilder.getReferenceDiscriminatorType(),null);
198 fail("should have thrown RiceIllegalArgumentException: reference object id must not be null");
199 } catch (RiceIllegalArgumentException e) {
200
201 }
202
203
204 referenceObjectBindings = ruleManagementService.findReferenceObjectBindingsByReferenceObject("junkvalue","junkvalue");
205 assertEquals("Incorrect number of objects returned",0,referenceObjectBindings.size());
206 }
207
208
209
210
211
212
213
214 @Test
215 public void testFindReferenceObjectBindingsByReferenceDiscriminatorType() {
216
217 RuleManagementBaseTestObjectNames t4 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
218
219
220 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t4.object0);
221 refObjBindingBuilder.setReferenceDiscriminatorType("ParkingAffiliationType" + t4.discriminator);
222 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder.build());
223
224 refObjBindingBuilder = createTestReferenceObjectBinding(t4.object1);
225 refObjBindingBuilder.setReferenceDiscriminatorType("ParkingAffiliationType" + t4.discriminator);
226 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder.build());
227
228 List<ReferenceObjectBinding> referenceObjectBindings = ruleManagementService.findReferenceObjectBindingsByReferenceDiscriminatorType(
229 refObjBindingBuilder.getReferenceDiscriminatorType());
230 assertEquals("Incorrect number of objects returned",2,referenceObjectBindings.size());
231
232
233 try {
234 ruleManagementService.findReferenceObjectBindingsByReferenceDiscriminatorType(" ");
235 fail("Should have thrown IllegalArgumentException: referenceDiscriminatorType is null or blank");
236 } catch (IllegalArgumentException e) {
237
238 }
239
240
241 try {
242 ruleManagementService.findReferenceObjectBindingsByReferenceDiscriminatorType(null);
243 fail("Should have thrown IllegalArgumentException: referenceDiscriminatorType is null or blank");
244 } catch (IllegalArgumentException e) {
245
246 }
247 }
248
249
250
251
252
253
254
255 @Test
256 public void testFindReferenceObjectBindingsByKrmsDiscriminatorType() {
257
258 RuleManagementBaseTestObjectNames t5 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
259
260
261 ReferenceObjectBinding.Builder refObjBindingBuilder6008 = createTestReferenceObjectBinding(t5.object0);
262
263 ReferenceObjectBinding.Builder refObjBindingBuilder6009 = createTestReferenceObjectBinding(t5.object1);
264 refObjBindingBuilder6009.setKrmsDiscriminatorType(refObjBindingBuilder6008.getKrmsDiscriminatorType());
265 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder6009.build());
266
267 List<ReferenceObjectBinding> referenceObjectBindings = ruleManagementService.findReferenceObjectBindingsByKrmsDiscriminatorType(refObjBindingBuilder6008.getKrmsDiscriminatorType());
268 assertEquals("Incorrect number of objects returned",2,referenceObjectBindings.size());
269
270
271 try {
272 ruleManagementService.findReferenceObjectBindingsByKrmsDiscriminatorType(" ");
273 fail("Should have thrown IllegalArgumentException: krmsDiscriminatorType is null or blank");
274 } catch (IllegalArgumentException e) {
275
276 }
277
278
279 try {
280 ruleManagementService.findReferenceObjectBindingsByKrmsDiscriminatorType(null);
281 fail("Should have thrown IllegalArgumentException: krmsDiscriminatorType is null or blank");
282 } catch (IllegalArgumentException e) {
283
284 }
285 }
286
287
288
289
290
291
292
293 @Test
294 public void testFindReferenceObjectBindingsByKrmsObject() {
295
296 RuleManagementBaseTestObjectNames t6 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
297
298
299 ReferenceObjectBinding.Builder refObjBindingBuilder0 = createTestReferenceObjectBinding(t6.object0);
300
301 ReferenceObjectBinding.Builder refObjBindingBuilder1 = createTestReferenceObjectBinding(t6.object1);
302 refObjBindingBuilder1.setKrmsObjectId(refObjBindingBuilder0.getKrmsObjectId());
303 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder1.build());
304
305 List<ReferenceObjectBinding> referenceObjectBindings = ruleManagementService.findReferenceObjectBindingsByKrmsObject(
306 refObjBindingBuilder0.getKrmsObjectId());
307 assertEquals("Incorrect number of objects returned",2,referenceObjectBindings.size());
308
309
310 try {
311 ruleManagementService.findReferenceObjectBindingsByKrmsObject(" ");
312 fail("Should have thrown IllegalArgumentException: krmsObjectId is null or blank");
313 } catch (IllegalArgumentException e) {
314
315 }
316
317
318 try {
319 ruleManagementService.findReferenceObjectBindingsByKrmsObject(null);
320 fail("Should have thrown IllegalArgumentException: krmsObjectId is null or blank");
321 } catch (IllegalArgumentException e) {
322
323 }
324 }
325
326
327
328
329
330
331 @Test
332 public void testUpdateReferenceObjectBinding() {
333
334 RuleManagementBaseTestObjectNames t7 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t7");
335
336 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t7.object0);
337
338 ReferenceObjectBinding refObjBinding = ruleManagementService.getReferenceObjectBinding(refObjBindingBuilder.getId());
339 refObjBindingBuilder = ReferenceObjectBinding.Builder.create(refObjBinding);
340
341
342 assertNotNull("Created ReferenceObjectBinding not found", refObjBindingBuilder);
343 assertEquals("Invalid CollectionName of refObjBindingBuilder found","ParkingPolicies", refObjBindingBuilder.getCollectionName());
344 assertEquals("Invalid KrmsObjectId of refObjBindingBuilder found",t7.agenda_Id, refObjBindingBuilder.getKrmsObjectId());
345 assertEquals("Invalid KrmsDiscriminatorType of refObjBindingBuilder found",
346 krmsTypeRepository.getTypeByName(t7.namespaceName, "AgendaType"+t7.object0).getId(),
347 refObjBindingBuilder.getKrmsDiscriminatorType() );
348 assertEquals("Invalid Namespace of refObjBindingBuilder found",t7.namespaceName, refObjBindingBuilder.getNamespace());
349 assertEquals("Invalid ReferenceObjectId of refObjBindingBuilder found",t7.referenceObject_0_Id, refObjBindingBuilder.getReferenceObjectId());
350 assertEquals("Invalid ReferenceDiscriminatorType of refObjBindingBuilder found","ParkingAffiliationType",
351 refObjBindingBuilder.getReferenceDiscriminatorType());
352 assertEquals("Invalid Active value of refObjBindingBuilder found",true, refObjBindingBuilder.isActive());
353
354
355 refObjBindingBuilder.setCollectionName("ParkingPolicies6Changed");
356 refObjBindingBuilder.setKrmsObjectId("AgendaId6Changed");
357 refObjBindingBuilder.setKrmsDiscriminatorType("KDTtype6Changed");
358 refObjBindingBuilder.setNamespace("Namespace6Changed");
359 refObjBindingBuilder.setReferenceObjectId("PA6Changed");
360 refObjBindingBuilder.setReferenceDiscriminatorType("ParkingAffiliationTypeChanged");
361 refObjBindingBuilder.setActive(false);
362 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder.build());
363
364
365 refObjBinding = ruleManagementService.getReferenceObjectBinding(refObjBindingBuilder.getId());
366 refObjBindingBuilder = ReferenceObjectBinding.Builder.create(refObjBinding);
367 assertNotNull("Created ReferenceObjectBinding not found", refObjBindingBuilder);
368 assertEquals("Invalid CollectionName of refObjBindingBuilder found", "ParkingPolicies6Changed",
369 refObjBindingBuilder.getCollectionName());
370 assertEquals("Invalid KrmsObjectId of refObjBindingBuilder found","AgendaId6Changed", refObjBindingBuilder.getKrmsObjectId());
371 assertEquals("Invalid KrmsDiscriminatorType of refObjBindingBuilder found","KDTtype6Changed", refObjBindingBuilder.getKrmsDiscriminatorType() );
372 assertEquals("Invalid Namespace of refObjBindingBuilder found","Namespace6Changed", refObjBindingBuilder.getNamespace());
373 assertEquals("Invalid ReferenceObjectId of refObjBindingBuilder found","PA6Changed", refObjBindingBuilder.getReferenceObjectId());
374 assertEquals("Invalid ReferenceDiscriminatorType of refObjBindingBuilder found","ParkingAffiliationTypeChanged", refObjBindingBuilder.getReferenceDiscriminatorType());
375 assertEquals("Invalid Active value of refObjBindingBuilder found",false, refObjBindingBuilder.isActive());
376
377
378 refObjBindingBuilder.setId("junkValue");
379 try {
380 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder.build());
381 fail("Should have thrown IllegalStateException: the ReferenceObjectBinding to update does not exists");
382 } catch (IllegalStateException e) {
383
384 }
385 }
386
387
388
389
390
391
392
393 @Test
394 public void testDeleteReferenceObjectBinding() {
395
396 RuleManagementBaseTestObjectNames t8 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t8");
397
398 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t8.object0);
399 ReferenceObjectBinding refObjBinding = ruleManagementService.getReferenceObjectBinding(refObjBindingBuilder.getId());
400 refObjBindingBuilder = ReferenceObjectBinding.Builder.create(refObjBinding);
401 assertNotNull("Created ReferenceObjectBinding not found", refObjBindingBuilder);
402
403
404 ruleManagementService.deleteReferenceObjectBinding(refObjBinding.getId());
405 assertNull("Deleted ReferenceObjectBinding found", ruleManagementService.getReferenceObjectBinding(
406 refObjBindingBuilder.getId()));
407
408
409 try {
410 ruleManagementService.deleteReferenceObjectBinding(refObjBinding.getId());
411 fail("should have thrown IllegalStateException: the ReferenceObjectBinding to delete does not exists");
412 } catch (IllegalStateException e) {
413
414 }
415
416
417 try {
418 ruleManagementService.deleteReferenceObjectBinding(null);
419 fail("should have thrown IllegalArgumentException: referenceObjectBindingId was null");
420 } catch (IllegalArgumentException e) {
421
422 }
423 }
424
425
426
427
428
429
430
431 @Test
432 public void testFindReferenceObjectBindingIds() {
433
434 RuleManagementBaseTestObjectNames t9 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t9");
435
436
437 List<String> refObjBindingBuilderIds = new ArrayList<String>();
438 ReferenceObjectBinding.Builder refObjBindingBuilder = createTestReferenceObjectBinding(t9.object0);
439 refObjBindingBuilderIds.add(refObjBindingBuilder.getId());
440 refObjBindingBuilder = createTestReferenceObjectBinding(t9.object1);
441 refObjBindingBuilderIds.add(refObjBindingBuilder.getId());
442 refObjBindingBuilder = createTestReferenceObjectBinding(t9.object2);
443 refObjBindingBuilderIds.add(refObjBindingBuilder.getId());
444 refObjBindingBuilder.setActive(false);
445 ruleManagementService.updateReferenceObjectBinding(refObjBindingBuilder.build());
446
447
448 QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
449 query.setPredicates(in("id", refObjBindingBuilderIds.toArray(new String[]{})));
450
451 List<String> referenceObjectBindingIds = ruleManagementService.findReferenceObjectBindingIds(query.build());
452 for (String referenceObjectBindingId : referenceObjectBindingIds ) {
453 assertTrue(refObjBindingBuilderIds.contains(referenceObjectBindingId));
454 }
455
456 assertEquals("incorrect number of ReferenceObjectBindingIds found", 3, referenceObjectBindingIds.size());
457
458
459 query = QueryByCriteria.Builder.create();
460 query.setPredicates( equal("active","Y"), in("id", refObjBindingBuilderIds.toArray(new String[]{})));
461
462 referenceObjectBindingIds = ruleManagementService.findReferenceObjectBindingIds(query.build());
463 for (String referenceObjectBindingId : referenceObjectBindingIds ) {
464 assertTrue(refObjBindingBuilderIds.contains(referenceObjectBindingId));
465 }
466
467 assertEquals("incorrect number of ReferenceObjectBindingIds found", 2, referenceObjectBindingIds.size());
468 }
469 }