View Javadoc

1   /*
2    * Copyright 2006-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   *   RuleManagementRuleDefinitionTest is to test the methods of ruleManagementServiceImpl relating to RuleDefinitions
41   *
42   *   Each test focuses on one of the methods.
43   */
44  public class RuleManagementRuleDefinitionTest  extends RuleManagementBaseTest{
45      @Override
46      @Before
47      public void setClassDiscriminator() {
48          // set a unique discriminator for test objects of this class
49          CLASS_DISCRIMINATOR = "RMRDT";
50      }
51  
52      /**
53       *  Test testGetRuleByNameAndNamespace()
54       *
55       *  This test focuses specifically on the RuleManagementServiceImpl .getRuleByNameAndNamespace("rule name", "namespace") method
56       */
57      @Test
58      public void testGetRuleByNameAndNamespace() {
59          // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
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          // try getRuleByNameAndNamespace with null Name parameter
70          try {
71              ruleManagementService.getRuleByNameAndNamespace(null, t0.namespaceName);
72              fail("Should have thrown IllegalArgumentException: name is null or blank");
73          } catch (IllegalArgumentException e) {
74              // throws IllegalArgumentException: name is null or blank
75          }
76  
77          // try getRuleByNameAndNamespace with null Name parameter
78          try {
79              ruleManagementService.getRuleByNameAndNamespace("   ", t0.namespaceName);
80              fail("Should have thrown IllegalArgumentException: name is null or blank");
81          } catch (IllegalArgumentException e) {
82              // throws IllegalArgumentException: name is null or blank
83          }
84  
85          // try getRuleByNameAndNamespace with null namespace parameter
86          try {
87              ruleManagementService.getRuleByNameAndNamespace(ruleDefinition.getName(),null);
88              fail("should throw IllegalArgumentException: namespace is null or blank");
89          } catch (IllegalArgumentException e) {
90              // IllegalArgumentException: namespace is null or blank
91          }
92  
93          // try getRuleByNameAndNamespace with blank namespace parameter
94          try {
95              ruleManagementService.getRuleByNameAndNamespace(ruleDefinition.getName(),"    ");
96              fail("should throw IllegalArgumentException: namespace is null or blank");
97          } catch (IllegalArgumentException e) {
98              // IllegalArgumentException: namespace is null or blank
99          }
100 
101 
102     }
103 
104     /**
105      *  Test testCreateRule()
106      *
107      *  This test focuses specifically on the RuleManagementServiceImpl .createRule(RuleDefinition) method
108      */
109     @Test
110     public void testCreateRule() {
111         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
112         RuleManagementBaseTestObjectNames t1 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
113 
114         // create a Rule
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         // try to create a duplicate Rule
119         try {
120             RuleDefinition ruleSecondCreate = ruleManagementService.createRule(ruleFirstCreate);
121             fail("should have thrown RiceIllegalArgumentException");
122         } catch (RiceIllegalArgumentException e) {
123             //  throw new RiceIllegalArgumentException(ruleDefinition.getId());
124         }
125 
126         // try to create a malformed Rule
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             // throw new RiceIllegalArgumentException("propId does not match proposition.getId"
136         }
137     }
138 
139     /**
140      *  Test testUpdateRule()
141      *
142      *  This test focuses specifically on the RuleManagementServiceImpl .updateRule(RuleDefinition) method
143      */
144     @Test
145     public void testUpdateRule() {
146         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
147         RuleManagementBaseTestObjectNames t2 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
148 
149         // build a rule to test with
150         RuleDefinition.Builder ruleBuilder0 = RuleDefinition.Builder.create(buildTestRuleDefinition(t2.namespaceName,
151                 t2.object0));
152 
153         // update the rule's Name
154         ruleBuilder0.setName("updatedName");
155         ruleManagementService.updateRule(ruleBuilder0.build());
156 
157         // verify update
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         // build new rule to for test
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         // create new proposition to update rule with
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         // Update Proposition in rule
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      *  Test testDeleteRule()
184      *
185      *  This test focuses specifically on the RuleManagementServiceImpl .deleteRule("rule id") method
186      */
187     @Test
188     public void testDeleteRule() {
189         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
190         RuleManagementBaseTestObjectNames t3 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
191 
192         // create a Rule
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         // make sure proposition was cleaned up when rule was deleted
205         try {
206             ruleManagementService.deleteProposition(propositionId);
207             fail("should fail with IllegalStateException: the Proposition to delete does not exists");
208         } catch (IllegalStateException e) {
209             // IllegalStateException: the Proposition to delete does not exists
210         }
211     }
212 
213     /**
214      *  Test testFindRuleIds()
215      *
216      *  This test focuses specifically on the RuleManagementServiceImpl .findRuleIds( QueryByCriteria) method
217      */
218     @Test
219     public void testFindRuleIds() {
220         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
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      *  Test testGetRule()
248      *
249      *  This test focuses specifically on the RuleManagementServiceImpl .getRule("rule id") method
250      */
251     @Test
252     public void testGetRule() {
253         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
254         RuleManagementBaseTestObjectNames t5 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
255 
256         // create a rule to test with
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      *  Test testGetRules()
268      *
269      *  This test focuses specifically on the RuleManagementServiceImpl .getRules(List<String> ruleIds) method
270      */
271     @Test
272     public void testGetRules() {
273         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
274         RuleManagementBaseTestObjectNames t6 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
275 
276         // build two rules for testing
277         buildTestRuleDefinition(t6.namespaceName, t6.object0);
278         buildTestRuleDefinition(t6.namespaceName, t6.object1);
279 
280         // build List rule ids for the rules created
281         List<String> ruleIds = new ArrayList<String>();
282         ruleIds.add(t6.rule_0_Id);
283         ruleIds.add(t6.rule_1_Id);
284 
285         // get test rules by List of rule ids
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             // throws RiceIllegalArgumentException: ruleIds must not be null
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      * Tests whether the {@code RuleDefinition} cache is being evicted properly by checking the status the dependent
311      * objects before and after creating an {@code RuleDefinition} (and consequently emptying the cache).
312      *
313      * <p>
314      * The following object caches are affected:
315      * {@code RuleDefinition}, {@code PropositionDefinition}, {@code ActionDefinition}, {@code AgendaItemDefinition}
316      * </p>
317      */
318     @Test
319     public void testRuleCacheEvict() {
320         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
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 }