View Javadoc
1   /*
2    * Copyright 2005-2014 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.rule.RuleDefinition;
26  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
27  
28  import java.util.Arrays;
29  import java.util.List;
30  
31  import static org.junit.Assert.*;
32  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
33  
34  /**
35   *   RuleManagementActionDefinitionTest is to test the methods of ruleManagementServiceImpl relating to krms Actions
36   *
37   *   Each test focuses on one of the methods.
38   */
39      public class RuleManagementActionDefinitionTest extends RuleManagementBaseTest {
40  
41      @Override
42      @Before
43      public void setClassDiscriminator() {
44          // set a unique discriminator for test objects of this class
45          CLASS_DISCRIMINATOR = "RMADT";
46      }
47  
48      /**
49       *  Test testCreateAction()
50       *
51       *  This test focuses specifically on the RuleManagementServiceImpl .createAction(ActionDefinition) method
52       */
53      @Test
54      public void testCreateAction() {
55          // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
56          RuleManagementBaseTestObjectNames t0 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
57  
58          KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(t0.namespaceName);
59          RuleDefinition ruleDefintion = buildTestRuleDefinition(t0.namespaceName, t0.discriminator);
60  
61          ActionDefinition actionDefinition = ActionDefinition.Builder.create(t0.action0_Id, t0.action0_Name,
62                  t0.namespaceName,krmsTypeDefinition.getId(),ruleDefintion.getId(),1).build();
63  
64          assertNull("action should not be in database", ruleManagementService.getAction(t0.action0_Id));
65  
66          // primary statement for test
67          actionDefinition =  ruleManagementService.createAction(actionDefinition);
68  
69          ActionDefinition returnActionDefinition = ruleManagementService.getAction(actionDefinition.getId());
70  
71          assertNotNull("created action not found", (Object) returnActionDefinition);
72          assertEquals("create action error:", t0.action0_Id, returnActionDefinition.getId());
73      }
74  
75      /**
76       *  Test testUpdateAction()
77       *
78       *  This test focuses specifically on the RuleManagementServiceImpl .testUpdateAction(ActionDefinition) method
79       */
80      @Test
81      public void testUpdateAction() {
82          // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
83          RuleManagementBaseTestObjectNames t1 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
84  
85          KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(t1.namespaceName);
86          RuleDefinition ruleDefinition = buildTestRuleDefinition(t1.namespaceName, t1.object0);
87  
88          ActionDefinition actionDefinition = ActionDefinition.Builder.create(t1.action0_Id,t1.action0_Name,
89                  t1.namespaceName,krmsTypeDefinition.getId(),ruleDefinition.getId(),1).build();
90  
91          assertNull("action should not be in database", ruleManagementService.getAction(t1.action0_Id));
92  
93          actionDefinition =  ruleManagementService.createAction(actionDefinition);
94  
95          ActionDefinition returnActionDefinition = ruleManagementService.getAction(actionDefinition.getId());
96          ActionDefinition.Builder builder = ActionDefinition.Builder.create(returnActionDefinition);
97          builder.setDescription("ChangedDescr");
98  
99          // primary statement for test
100         ruleManagementService.updateAction(builder.build());
101 
102         returnActionDefinition = ruleManagementService.getAction(actionDefinition.getId());
103 
104         assertNotNull("action not found", returnActionDefinition);
105         assertEquals("update action error:","ChangedDescr", returnActionDefinition.getDescription());
106     }
107 
108     /**
109      *  Test testDeleteAction()
110      *
111      *  This test focuses specifically on the RuleManagementServiceImpl .testDeleteAction(ActionDefinition) method
112      */
113     @Test
114     public void testDeleteAction() {
115         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
116         RuleManagementBaseTestObjectNames t2 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
117 
118         KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(t2.namespaceName);
119         RuleDefinition ruleDefintion = buildTestRuleDefinition(t2.namespaceName, t2.object0);
120 
121         ActionDefinition actionDefinition = ActionDefinition.Builder.create(t2.action0_Id,t2.action0_Name,
122                 t2.namespaceName,krmsTypeDefinition.getId(),ruleDefintion.getId(),1).build();
123 
124         assertNull("action should not be in database", ruleManagementService.getAction(t2.action0_Id));
125 
126         actionDefinition =  ruleManagementService.createAction(actionDefinition);
127         actionDefinition = ruleManagementService.getAction(actionDefinition.getId());
128         assertNotNull("action not found", ruleManagementService.getAction(actionDefinition.getId()));
129 
130         try {
131             // primary statement for test
132             ruleManagementService.deleteAction(t2.action0_Id);
133             fail("should fail deleteAction not implemented");
134         }   catch (RiceIllegalArgumentException e) {
135             // RiceIllegalArgumentException ("not implemented yet because not supported by the bo service");
136         }
137 
138         actionDefinition = ruleManagementService.getAction(actionDefinition.getId());
139         assertNotNull("action not found", (Object) actionDefinition);
140     }
141 
142     /**
143      *  Test testGetAction()
144      *
145      *  This test focuses specifically on the RuleManagementServiceImpl .testGetAction(Action_Id) method
146      */
147     @Test
148     public void testGetAction() {
149         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
150         RuleManagementBaseTestObjectNames t3 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
151 
152         KrmsTypeDefinition krmsTypeDefinition = createKrmsActionTypeDefinition(t3.namespaceName);
153         RuleDefinition ruleDefintion = buildTestRuleDefinition(t3.namespaceName, t3.object0);
154 
155         ActionDefinition actionDefinition = ActionDefinition.Builder.create(t3.action0_Id,t3.action0_Name,
156                 t3.namespaceName,krmsTypeDefinition.getId(),ruleDefintion.getId(),1).build();
157 
158         assertNull("action should not be in database", ruleManagementService.getAction(t3.action0_Id));
159         actionDefinition =  ruleManagementService.createAction(actionDefinition);
160 
161         // primary statement being tested
162         ActionDefinition returnActionDefinition = ruleManagementService.getAction(actionDefinition.getId());
163 
164         assertNotNull("action not found", (Object) returnActionDefinition);
165         assertEquals("getAction error:", t3.action0_Id, returnActionDefinition.getId());
166 
167     }
168 
169     /**
170      *  Test testGetActions()
171      *
172      *  This test focuses specifically on the RuleManagementServiceImpl .testGetActions(List<Action_Id>) method
173      */
174     @Test
175     public void testGetActions() {
176         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
177         RuleManagementBaseTestObjectNames t4 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
178 
179         RuleDefinition ruleDefinition0 = buildTestRuleDefinition(t4.namespaceName, t4.object0);
180         RuleDefinition ruleDefinition1 = buildTestRuleDefinition(t4.namespaceName, t4.object1);
181         RuleDefinition ruleDefinition2 = buildTestRuleDefinition(t4.namespaceName, t4.object2);
182         RuleDefinition ruleDefinition3 = buildTestRuleDefinition(t4.namespaceName, t4.object3);
183 
184         buildTestActionDefinition(t4.action0_Id, t4.action0_Name, t4.action0_Descr, 1, ruleDefinition0.getId(),
185                 t4.namespaceName);
186         buildTestActionDefinition(t4.action1_Id, t4.action1_Name, t4.action1_Descr, 1, ruleDefinition1.getId(),
187                 t4.namespaceName);
188         buildTestActionDefinition(t4.action2_Id, t4.action2_Name, t4.action2_Descr, 1, ruleDefinition2.getId(),
189                 t4.namespaceName);
190         buildTestActionDefinition(t4.action3_Id, t4.action3_Name, t4.action3_Descr, 1, ruleDefinition3.getId(),
191                 t4.namespaceName);
192         List<String> actionIds = Arrays.asList(t4.action0_Id, t4.action1_Id, t4.action2_Id, t4.action3_Id);
193 
194         // primary statement being tested
195         List<ActionDefinition> returnActionDefinitions = ruleManagementService.getActions(actionIds);
196 
197         assertEquals("incorrect number of actions returned",4,returnActionDefinitions.size());
198 
199         // count the returned actions, returnActionDefinitions.size() may reflect nulls for not found
200         int actionsFound = 0;
201         for( ActionDefinition actionDefinition : returnActionDefinitions ) {
202             if(actionIds.contains(actionDefinition.getId())) {
203                 actionsFound++;
204             }
205         }
206 
207         assertEquals("incorrect number of actions returned",4,actionsFound);
208         assertEquals("action not found",t4.action0_Descr, ruleManagementService.getAction(t4.action0_Id).getDescription());
209         assertEquals("action not found",t4.action1_Descr, ruleManagementService.getAction(t4.action1_Id).getDescription());
210         assertEquals("action not found",t4.action2_Descr, ruleManagementService.getAction(t4.action2_Id).getDescription());
211         assertEquals("action not found",t4.action3_Descr, ruleManagementService.getAction(t4.action3_Id).getDescription());
212     }
213 
214     /**
215      *  Test testFindActionIds()
216      *
217      *  This test focuses specifically on the RuleManagementServiceImpl .testFindActionIds(QueryByCriteria) method
218      */
219     @Test
220     public void testFindActionIds() {
221         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
222         RuleManagementBaseTestObjectNames t5 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
223         RuleDefinition ruleDefinition = buildTestRuleDefinition(t5.namespaceName, t5.object0);
224         buildTestActionDefinition(t5.action0_Id, t5.action0_Name, t5.action0_Descr, 1, ruleDefinition.getId(),
225                 t5.namespaceName);
226 
227         QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
228         builder.setPredicates(equal("name", t5.action0_Name));
229 
230         List<String> actionIds = ruleManagementService.findActionIds(builder.build());
231 
232         if(!actionIds.contains(t5.action0_Id)){
233             fail("actionId not found");
234         }
235     }
236 
237     /**
238      * Tests whether the {@code ActionDefinition} cache is being evicted properly by checking the status the
239      * dependent objects before and after creating an {@code ActionDefinition} (and consequently emptying the cache).
240      *
241      * <p>
242      * The following object caches are affected:
243      * {@code ActionDefinition}, {@code RuleDefinition}
244      * </p>
245      */
246     @Test
247     public void testActionCacheEvict() {
248         // get a set of unique object names for use by this test (discriminator passed can be any unique value within this class)
249         RuleManagementBaseTestObjectNames t6 =  new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
250 
251         verifyEmptyAction(t6);
252 
253         RuleDefinition ruleDefinition = buildTestRuleDefinition(t6.namespaceName, t6.object0);
254         buildTestActionDefinition(t6.action_Id, t6.action_Name, t6.action_Descr, 1, ruleDefinition.getId(), t6.namespaceName);
255 
256         verifyFullAction(t6);
257     }
258 
259     private void verifyEmptyAction(RuleManagementBaseTestObjectNames t) {
260         ActionDefinition action = ruleManagementService.getAction(t.action_Id);
261         assertNull("Action is not null", action);
262 
263         RuleDefinition rule = ruleManagementService.getRule(t.rule_Id);
264         assertFalse("Action in Rule found", rule != null);
265     }
266 
267     private void verifyFullAction(RuleManagementBaseTestObjectNames t) {
268         ActionDefinition action = ruleManagementService.getAction(t.action_Id);
269         assertNotNull("Action is null", action);
270 
271         boolean foundRule = false;
272         RuleDefinition rule = ruleManagementService.getRule(t.rule_Id);
273         if (rule != null) {
274             for (ActionDefinition ruleAction : rule.getActions()) {
275                 if (StringUtils.equals(t.rule_Id, ruleAction.getRuleId())) {
276                     foundRule = true;
277                     break;
278                 }
279             }
280         }
281         assertTrue("Action in Rule not found", foundRule);
282     }
283 }