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.apache.commons.lang.StringUtils;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.kuali.rice.core.api.criteria.QueryByCriteria;
23 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24 import org.kuali.rice.krms.api.repository.action.ActionDefinition;
25 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
26 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
27 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Set;
33
34 import static org.junit.Assert.*;
35 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
36 import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
37
38
39
40
41
42
43
44 public class RuleManagementRuleDefinitionTest extends RuleManagementBaseTest{
45 @Override
46 @Before
47 public void setClassDiscriminator() {
48
49 CLASS_DISCRIMINATOR = "RMRDT";
50 }
51
52
53
54
55
56
57 @Test
58 public void testGetRuleByNameAndNamespace() {
59
60 RuleManagementBaseTestObjectNames t0 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
61
62 RuleDefinition ruleDefinition = buildTestRuleDefinition(t0.namespaceName, t0.object0);
63
64 RuleDefinition returnRuleDefinition = ruleManagementService.getRuleByNameAndNamespace(
65 ruleDefinition.getName(), ruleDefinition.getNamespace());
66
67 assertEquals("rule not found", ruleDefinition.getId(), returnRuleDefinition.getId());
68
69
70 try {
71 ruleManagementService.getRuleByNameAndNamespace(null, t0.namespaceName);
72 fail("Should have thrown IllegalArgumentException: name is null or blank");
73 } catch (IllegalArgumentException e) {
74
75 }
76
77
78 try {
79 ruleManagementService.getRuleByNameAndNamespace(" ", t0.namespaceName);
80 fail("Should have thrown IllegalArgumentException: name is null or blank");
81 } catch (IllegalArgumentException e) {
82
83 }
84
85
86 try {
87 ruleManagementService.getRuleByNameAndNamespace(ruleDefinition.getName(),null);
88 fail("should throw IllegalArgumentException: namespace is null or blank");
89 } catch (IllegalArgumentException e) {
90
91 }
92
93
94 try {
95 ruleManagementService.getRuleByNameAndNamespace(ruleDefinition.getName()," ");
96 fail("should throw IllegalArgumentException: namespace is null or blank");
97 } catch (IllegalArgumentException e) {
98
99 }
100
101
102 }
103
104
105
106
107
108
109 @Test
110 public void testCreateRule() {
111
112 RuleManagementBaseTestObjectNames t1 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
113
114
115 RuleDefinition ruleFirstCreate = buildTestRuleDefinition(t1.namespaceName, t1.object0);
116 assertTrue("created Rule not found", ruleManagementService.getRule(ruleFirstCreate.getId()).getId().contains(t1.rule_0_Id));
117
118
119 try {
120 RuleDefinition ruleSecondCreate = ruleManagementService.createRule(ruleFirstCreate);
121 fail("should have thrown RiceIllegalArgumentException");
122 } catch (RiceIllegalArgumentException e) {
123
124 }
125
126
127 RuleDefinition malformedRule = buildTestRuleDefinition(t1.namespaceName, t1.object1);
128 RuleDefinition.Builder builder = RuleDefinition.Builder.create(malformedRule);
129 builder.setPropId("invalidValue");
130 malformedRule = builder.build();
131 try {
132 ruleManagementService.createRule(malformedRule);
133 fail("should have thrown RiceIllegalArgumentException");
134 } catch (RiceIllegalArgumentException e) {
135
136 }
137 }
138
139
140
141
142
143
144 @Test
145 public void testUpdateRule() {
146
147 RuleManagementBaseTestObjectNames t2 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
148
149
150 RuleDefinition.Builder ruleBuilder0 = RuleDefinition.Builder.create(buildTestRuleDefinition(t2.namespaceName,
151 t2.object0));
152
153
154 ruleBuilder0.setName("updatedName");
155 ruleManagementService.updateRule(ruleBuilder0.build());
156
157
158 RuleDefinition rule0 = ruleManagementService.getRule(t2.rule_0_Id);
159 assertNotEquals("Rule Name Not Updated", t2.rule_0_Name, rule0.getName());
160 assertEquals("Rule Name Not Updated", "updatedName", rule0.getName());
161
162
163 RuleDefinition.Builder ruleBuilder1 = RuleDefinition.Builder.create(buildTestRuleDefinition(t2.namespaceName,
164 t2.object1));
165 assertEquals("Expected Proposition not found in Rule",t2.proposition_1_Descr,ruleBuilder1.getProposition().getDescription());
166
167
168 String newPropId = "PropNewId";
169 PropositionDefinition prop = createTestSimpleProposition(t2.namespaceName, newPropId, "TSI_"+newPropId,
170 "ABC", "=", "java.lang.String", t2.rule_0_Id, "TSI_" + newPropId + "_Descr");
171 PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(prop);
172 ruleBuilder1.setPropId(newPropId);
173 ruleBuilder1.setProposition(propBuilder);
174
175
176 ruleManagementService.updateRule(ruleBuilder1.build());
177
178 rule0 = ruleManagementService.getRule(ruleBuilder1.getId());
179 assertEquals("Expected Proposition not found in Rule","PropNewId_simple_proposition",rule0.getProposition().getDescription());
180 }
181
182
183
184
185
186
187 @Test
188 public void testDeleteRule() {
189
190 RuleManagementBaseTestObjectNames t3 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
191
192
193 RuleDefinition rule = buildTestRuleDefinition(t3.namespaceName, t3.object0);
194 assertTrue("created Rule not found", ruleManagementService.getRule(rule.getId()).getId().contains(t3.rule_0_Id));
195 String propositionId = rule.getPropId();
196 assertEquals("Proposition for Rule not found", t3.proposition_0_Descr,
197 ruleManagementService.getProposition(propositionId).getDescription());
198
199
200 ruleManagementService.deleteRule(rule.getId());
201
202 assertNull("Rule was not deleted", ruleManagementService.getRule(rule.getId()));
203
204
205 try {
206 ruleManagementService.deleteProposition(propositionId);
207 fail("should fail with IllegalStateException: the Proposition to delete does not exists");
208 } catch (IllegalStateException e) {
209
210 }
211 }
212
213
214
215
216
217
218 @Test
219 public void testFindRuleIds() {
220
221 RuleManagementBaseTestObjectNames t4 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
222
223 RuleDefinition rule0 = buildTestRuleDefinition(t4.namespaceName, t4.object0);
224 RuleDefinition rule1 = buildTestRuleDefinition(t4.namespaceName, t4.object1);
225 RuleDefinition rule2 = buildTestRuleDefinition(t4.namespaceName, t4.object2);
226 RuleDefinition rule3 = buildTestRuleDefinition(t4.namespaceName, t4.object3);
227 String ruleNameSpace = rule0.getNamespace();
228 List<String> ruleNames = new ArrayList<String>();
229 ruleNames.add(rule0.getName());
230 ruleNames.add(rule1.getName());
231 ruleNames.add(rule2.getName());
232 ruleNames.add(rule3.getName());
233
234 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
235
236 builder.setPredicates(equal("namespace", ruleNameSpace), in("name", ruleNames.toArray(new String[]{})));
237
238 List<String> ruleIds = ruleManagementService.findRuleIds(builder.build());
239 assertEquals("Wrong number of RuleIds returned", 4, ruleIds.size());
240
241 if(!ruleIds.contains(rule0.getId())){
242 fail("RuleId not found in results");
243 }
244 }
245
246
247
248
249
250
251 @Test
252 public void testGetRule() {
253
254 RuleManagementBaseTestObjectNames t5 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
255
256
257 RuleDefinition ruleDefinition = buildTestRuleDefinition(t5.namespaceName, t5.object0);
258
259 assertNotNull(ruleManagementService.getRule(ruleDefinition.getId()));
260
261 assertNull("Should have returned null", ruleManagementService.getRule(null));
262 assertNull("Should have returned null", ruleManagementService.getRule(" "));
263 assertNull("Should have returned null", ruleManagementService.getRule("badValueId"));
264 }
265
266
267
268
269
270
271 @Test
272 public void testGetRules() {
273
274 RuleManagementBaseTestObjectNames t6 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
275
276
277 buildTestRuleDefinition(t6.namespaceName, t6.object0);
278 buildTestRuleDefinition(t6.namespaceName, t6.object1);
279
280
281 List<String> ruleIds = new ArrayList<String>();
282 ruleIds.add(t6.rule_0_Id);
283 ruleIds.add(t6.rule_1_Id);
284
285
286 List<RuleDefinition> ruleDefinitions = ruleManagementService.getRules(ruleIds);
287 assertEquals("Two RuleDefintions should have been returned",2,ruleDefinitions.size());
288
289 for(RuleDefinition ruleDefinition : ruleDefinitions) {
290 if (!ruleIds.contains(ruleDefinition.getId())) {
291 fail("Invalid RuleDefinition returned");
292 }
293 }
294
295 try {
296 ruleManagementService.getRules(null);
297 fail("Should have failed with RiceIllegalArgumentException: ruleIds must not be null");
298 } catch (RiceIllegalArgumentException e) {
299
300 }
301
302 assertEquals("No RuleDefinitions should have been returned",0,
303 ruleManagementService.getRules(new ArrayList<String>()).size());
304
305 ruleIds = Arrays.asList("badValueId");
306 assertEquals("No RuleDefinitions should have been returned",0, ruleManagementService.getRules(ruleIds).size());
307 }
308
309
310
311
312
313
314
315
316
317
318 @Test
319 public void testRuleCacheEvict() {
320
321 RuleManagementBaseTestObjectNames t7 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t7");
322
323 verifyEmptyRule(t7);
324
325 String ruleId = buildTestRuleDefinition(t7.namespaceName, t7.object0).getId();
326 buildTestActionDefinition(t7.action_Id, t7.action_Name, t7.action_Descr, 1, ruleId, t7.namespaceName);
327 String agendaId = createTestAgenda(t7.object0).getId();
328 buildTestAgendaItemDefinition(t7.agendaItem_Id, agendaId, ruleId);
329
330 verifyFullRule(t7);
331 }
332
333 private void verifyEmptyRule(RuleManagementBaseTestObjectNames t) {
334 RuleDefinition rule = ruleManagementService.getRule(t.rule_Id);
335 assertNull("Rule is not null", rule);
336
337 Set<PropositionDefinition> propositions = ruleManagementService.getPropositionsByRule(t.rule_Id);
338 assertFalse("Rule in Proposition found", propositions != null && !propositions.isEmpty());
339
340 ActionDefinition action = ruleManagementService.getAction(t.action_Id);
341 assertFalse("Rule in Action found", action != null);
342
343 AgendaItemDefinition agendaItem = ruleManagementService.getAgendaItem(t.agendaItem_Id);
344 assertFalse("Rule in AgendaItem found", agendaItem != null);
345 }
346
347 private void verifyFullRule(RuleManagementBaseTestObjectNames t) {
348 RuleDefinition rule = ruleManagementService.getRule(t.rule_Id);
349 assertNotNull("Rule is null", rule);
350
351 boolean foundRule = false;
352 Set<PropositionDefinition> propositions = ruleManagementService.getPropositionsByRule(t.rule_Id);
353 if (propositions != null) {
354 for (PropositionDefinition proposition : propositions) {
355 if (StringUtils.equals(t.rule_Id, proposition.getRuleId())) {
356 foundRule = true;
357 break;
358 }
359 }
360 }
361 assertTrue("Rule in Proposition not found", foundRule);
362
363 ActionDefinition action = ruleManagementService.getAction(t.action_Id);
364 assertTrue("Rule in Action not found", action != null);
365 assertTrue("Rule in Action not found", StringUtils.equals(t.rule_Id, action.getRuleId()));
366
367 AgendaItemDefinition agendaItem = ruleManagementService.getAgendaItem(t.agendaItem_Id);
368 assertTrue("Rule in AgendaItem not found", agendaItem != null);
369 assertTrue("Rule in AgendaItem not found", StringUtils.equals(t.rule_Id, agendaItem.getRuleId()));
370 }
371 }