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.kew.rule.service.impl;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.junit.Test;
21  import org.kuali.rice.kew.api.KewApiConstants;
22  import org.kuali.rice.kew.api.rule.RuleTemplate;
23  import org.kuali.rice.kew.rule.RuleBaseValues;
24  import org.kuali.rice.kew.rule.RuleDelegationBo;
25  import org.kuali.rice.kew.rule.RuleExpressionDef;
26  import org.kuali.rice.kew.rule.RuleExtensionBo;
27  import org.kuali.rice.kew.rule.RuleExtensionValue;
28  import org.kuali.rice.kew.rule.RuleResponsibilityBo;
29  import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
30  import org.kuali.rice.kew.rule.bo.RuleAttribute;
31  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
32  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
33  import org.kuali.rice.kew.service.KEWServiceLocator;
34  import org.kuali.rice.kew.test.KEWTestCase;
35  import org.kuali.rice.krad.data.PersistenceOption;
36  import org.kuali.rice.krad.service.KRADServiceLocator;
37  import org.kuali.rice.krad.test.KRADTestCase;
38  import org.kuali.rice.test.BaselineTestCase;
39  
40  import java.util.List;
41  
42  import static org.junit.Assert.assertTrue;
43  
44  /**
45   * Tests to confirm JPA mapping for the Kew module Rule objects
46   */
47  public class KewRuleDataJpaTest extends KEWTestCase{
48      @Test
49      public void testRuleBaseValuesPersistAndFetch() throws Exception{
50          RuleBaseValues ruleBaseValues = setupRuleBaseValues();
51          String ruleBaseValuesId = ruleBaseValues.getId();
52          assertTrue("RuleBaseValues persisted correctly",ruleBaseValues != null &&
53                  StringUtils.isNotBlank(ruleBaseValuesId));
54          ruleBaseValues = KRADServiceLocator.getDataObjectService().find(RuleBaseValues.class,ruleBaseValuesId);
55          assertTrue("RuleBaseValues refetched correctly",ruleBaseValues != null &&
56                  StringUtils.equals(ruleBaseValuesId,ruleBaseValues.getId()));
57          assertTrue("RuleExtension persisted correctly",
58                  ruleBaseValues.getRuleExtensions() != null && ruleBaseValues.getRuleExtensions().size() == 1);
59          RuleExtensionBo ruleExtensionBo = ruleBaseValues.getRuleExtensions().get(0);
60          assertTrue("RuleExtensionValue persisted correctly", ruleExtensionBo.getExtensionValues() != null
61                  && ruleExtensionBo.getExtensionValues().size() == 1);
62          assertTrue("RuleResponsibilities persisted correctly", ruleBaseValues.getRuleResponsibilities() != null
63                  && ruleBaseValues.getRuleResponsibilities().size() == 1);
64          assertTrue("RuleTemplate persisted correctly", ruleBaseValues.getRuleTemplate() != null
65                  && StringUtils.isNotBlank(ruleBaseValues.getRuleTemplate().getId()));
66          assertTrue("RuleExpressionDef persisted correctly", ruleBaseValues.getRuleExpressionDef() != null
67                  && StringUtils.isNotBlank(ruleBaseValues.getRuleExpressionDef().getExpression()));
68          assertTrue("Rule Template Option persisted correctly",
69                  ruleBaseValues.getRuleTemplate().getRuleTemplateOptions() != null
70               && ruleBaseValues.getRuleTemplate().getRuleTemplateOptions().size() == 1);
71          assertTrue("Rule Template Attribute persisted correctly",
72                  ruleBaseValues.getRuleTemplate().getRuleTemplateAttributes() != null &&
73                  ruleBaseValues.getRuleTemplate().getRuleTemplateAttributes().size() == 1);
74      }
75  
76      @Test
77      public void testRuleAttributeServiceFindByRuleAttribute() throws Exception{
78          RuleAttribute ruleAttribute = setupRuleAttribute();
79  
80          List<RuleAttribute> ruleAttributeList = KEWServiceLocator.getRuleAttributeService().
81                                      findByRuleAttribute(ruleAttribute);
82  
83          assertTrue("Rule attribute find by rule attribute fetched correctly",ruleAttributeList != null
84                  && ruleAttributeList.size() == 1);
85      }
86  
87      @Test
88      public void testRuleAttributeServiceGetAllRuleAttributes() throws Exception{
89          List<RuleAttribute> ruleAttributeList = KEWServiceLocator.getRuleAttributeService().findAll();
90          for(RuleAttribute ruleAttribute : ruleAttributeList){
91              KRADServiceLocator.getDataObjectService().delete(ruleAttribute);
92          }
93  
94          setupRuleAttribute();
95          setupRuleAttributeSimilar();
96  
97          ruleAttributeList = KEWServiceLocator.getRuleAttributeService().findAll();
98          assertTrue("Rule attribute fetched all correctly",ruleAttributeList != null
99                  && ruleAttributeList.size() == 2);
100     }
101 
102     @Test
103          public void tesRuleAttributeServiceFindByName() throws Exception{
104         RuleAttribute ruleAttribute = setupRuleAttribute();
105 
106         ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(ruleAttribute.getName());
107         assertTrue("RuleAttribute find by name fetched correctly",ruleAttribute != null);
108     }
109 
110     @Test
111     public void tesRuleAttributeServiceFindByClassName() throws Exception{
112         RuleAttribute ruleAttribute = setupRuleAttribute();
113         setupRuleAttributeSimilar();
114 
115         List<RuleAttribute> ruleAttributeList = KEWServiceLocator.getRuleAttributeService().findByClassName(ruleAttribute.getResourceDescriptor());
116         assertTrue("Rule attribute find by class name fetched correctly",ruleAttributeList != null
117                 && ruleAttributeList.size() == 2);
118     }
119 
120     @Test
121     public void testRuleTemplateServiceFindByRuleTemplateName() throws Exception{
122         RuleTemplateBo ruleTemplateBo = setupRuleTemplateBo("test");
123         String name = ruleTemplateBo.getName();
124         ruleTemplateBo = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateBo.getName());
125         assertTrue("RuleTemplate fetched based on name", ruleTemplateBo != null
126                     && StringUtils.equals(name,ruleTemplateBo.getName()));
127     }
128 
129     @Test
130     public void testRuleTemplateServiceDeleteRuleTemplateOption() throws Exception{
131         RuleTemplateBo ruleTemplateBo = setupRuleTemplateBo("test");
132         String optionId = ruleTemplateBo.getRuleTemplateOptions().get(0).getId();
133         KEWServiceLocator.getRuleTemplateService().deleteRuleTemplateOption(optionId);
134         RuleTemplateOptionBo ruleTemplateOptionBo = KRADServiceLocator.getDataObjectService().
135                         find(RuleTemplateOptionBo.class, optionId);
136         assertTrue("Rule Template Option is null",ruleTemplateOptionBo == null);
137     }
138 
139     @Test
140     public void testRuleDelegationServiceFindAllCurrentRuleDelegations() throws Exception{
141         RuleBaseValues ruleBaseValues = setupRuleBaseValues();
142         RuleDelegationBo ruleDelegationBo = setupRuleDelegationBo(ruleBaseValues);
143         ruleDelegationBo.setDelegationRule(ruleBaseValues);
144         KRADServiceLocator.getDataObjectService().save(ruleDelegationBo);
145         List<RuleDelegationBo> ruleDelegationBos = KEWServiceLocator.getRuleDelegationService().
146                     findAllCurrentRuleDelegations();
147         assertTrue("Rule delegation bo found",ruleDelegationBos != null);
148     }
149 
150     @Test
151     public void testRuleDelegationServiceFindByDelegateRuleId() throws Exception{
152         RuleBaseValues ruleBaseValues = setupRuleBaseValues();
153         RuleDelegationBo ruleDelegationBo = setupRuleDelegationBo(ruleBaseValues);
154         List<RuleDelegationBo> ruleDelegationBos = KEWServiceLocator.getRuleDelegationService().
155                         findByDelegateRuleId(ruleDelegationBo.getDelegateRuleId());
156         assertTrue("Rule Delegation Bo fetched by rule id",ruleDelegationBos != null && ruleDelegationBos.size() == 1);
157     }
158 
159     @Test
160     public void testRuleTemplateServiceFindAll() throws Exception{
161         List<RuleTemplateBo> ruleTemplateBos = KEWServiceLocator.getRuleTemplateService().findAll();
162         for(RuleTemplateBo ruleTemplateBo : ruleTemplateBos){
163             KRADServiceLocator.getDataObjectService().delete(ruleTemplateBo);
164         }
165 
166         setupRuleTemplateBo("test");
167         setupRuleTemplateBo("otherTest");
168 
169         ruleTemplateBos = KEWServiceLocator.getRuleTemplateService().findAll();
170         assertTrue("Rule Template Bo fetched all", ruleTemplateBos != null && ruleTemplateBos.size() == 2);
171     }
172 
173     @Test
174     public void testRuleDelegationServiceFindByResponsibilityIdWithCurrentRule() throws Exception{
175         RuleBaseValues ruleBaseValues = setupRuleBaseValues();
176         RuleDelegationBo ruleDelegationBo = setupRuleDelegationBo(ruleBaseValues);
177         ruleDelegationBo.setResponsibilityId(ruleBaseValues.getId());
178         ruleDelegationBo.setDelegateRuleId(ruleBaseValues.getId());
179         ruleDelegationBo = KRADServiceLocator.getDataObjectService().save(ruleDelegationBo);
180 
181         List<RuleDelegationBo> ruleDelegationBos = KEWServiceLocator.getRuleDelegationService().findByResponsibilityId(
182                     ruleDelegationBo.getResponsibilityId());
183         assertTrue("Rule Delegation Bo fetched ", ruleDelegationBos != null && ruleDelegationBos.size() == 1);
184     }
185 
186     private RuleDelegationBo setupRuleDelegationBo(RuleBaseValues ruleBaseValues){
187         RuleDelegationBo ruleDelegationBo = new RuleDelegationBo();
188         ruleDelegationBo.setDelegationTypeCode("P");
189         ruleDelegationBo.setGroupReviewerName("Testing");
190         ruleDelegationBo.setPersonReviewer("blah");
191         ruleDelegationBo.setDelegationRuleBaseValues(ruleBaseValues);
192         ruleDelegationBo.setDelegateRuleId(ruleBaseValues.getId());
193         ruleDelegationBo.setResponsibilityId("1234");
194 
195         return KRADServiceLocator.getDataObjectService().save(ruleDelegationBo);
196     }
197 
198     private RuleTemplateBo setupRuleTemplateBo(String name){
199         RuleTemplateBo ruleTemplate = new RuleTemplateBo();
200         ruleTemplate.setName(name);
201         ruleTemplate.setReturnUrl("testing");
202         ruleTemplate.setDescription("description");
203 
204         RuleTemplateOptionBo ruleTemplateOptionBo = new RuleTemplateOptionBo();
205         ruleTemplateOptionBo.setCode("P");
206         ruleTemplateOptionBo.setValue("VAL");
207         ruleTemplateOptionBo.setRuleTemplate(ruleTemplate);
208         ruleTemplate.getRuleTemplateOptions().add(ruleTemplateOptionBo);
209 
210         return KRADServiceLocator.getDataObjectService().save(ruleTemplate, PersistenceOption.FLUSH);
211     }
212 
213 
214 
215     private RuleAttribute setupRuleAttribute(){
216         RuleAttribute ruleAttribute = new RuleAttribute();
217         ruleAttribute.setApplicationId("TST");
218         ruleAttribute.setDescription("Testing");
219         ruleAttribute.setLabel("New Label");
220         ruleAttribute.setResourceDescriptor("ResourceDescriptor");
221         ruleAttribute.setType("newType");
222         ruleAttribute.setName("Attr");
223 
224         return KRADServiceLocator.getDataObjectService().save(ruleAttribute, PersistenceOption.FLUSH);
225     }
226 
227     private RuleAttribute setupRuleAttributeSimilar(){
228         RuleAttribute ruleAttribute = new RuleAttribute();
229         ruleAttribute.setApplicationId("TST2");
230         ruleAttribute.setDescription("Testingfdsa");
231         ruleAttribute.setLabel("New Labefdsal");
232         ruleAttribute.setResourceDescriptor("ResourceDescriptor");
233         ruleAttribute.setType("newType");
234         ruleAttribute.setName("Attr2");
235 
236         return KRADServiceLocator.getDataObjectService().save(ruleAttribute, PersistenceOption.FLUSH);
237     }
238 
239 
240 
241 
242     private RuleBaseValues setupRuleBaseValues() {
243         final RuleBaseValues rbv = new RuleBaseValues();
244         rbv.setActive(Boolean.TRUE);
245         rbv.setCurrentInd(Boolean.TRUE);
246         rbv.setDescription("A test rule");
247         rbv.setDocTypeName("TestDocumentType");
248         rbv.setForceAction(Boolean.FALSE);
249 
250 
251 
252         RuleResponsibilityBo ruleResponsibilityBo = new RuleResponsibilityBo();
253         ruleResponsibilityBo.setResponsibilityId("1234");
254         ruleResponsibilityBo.setRuleBaseValues(rbv);
255         ruleResponsibilityBo.setRuleResponsibilityName("user2");
256         ruleResponsibilityBo.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
257         rbv.getRuleResponsibilities().add(ruleResponsibilityBo);
258 
259         RuleTemplateBo ruleTemplate = new RuleTemplateBo();
260         ruleTemplate.setName("test");
261         ruleTemplate.setDescription("description");
262         rbv.setRuleTemplate(ruleTemplate);
263 
264         RuleTemplateOptionBo ruleTemplateOptionBo = new RuleTemplateOptionBo();
265         ruleTemplateOptionBo.setCode("TST");
266         ruleTemplateOptionBo.setValue("VAL");
267         ruleTemplateOptionBo.setRuleTemplate(ruleTemplate);
268         ruleTemplate.getRuleTemplateOptions().add(ruleTemplateOptionBo);
269 
270         RuleTemplateAttributeBo ruleTemplateAttributeBo = new RuleTemplateAttributeBo();
271         ruleTemplateAttributeBo.setActive(true);
272         ruleTemplateAttributeBo.setDefaultValue("testAttr");
273         ruleTemplateAttributeBo.setDisplayOrder(1);
274         ruleTemplateAttributeBo.setRequired(true);
275         ruleTemplateAttributeBo.setRuleTemplate(ruleTemplate);
276 
277         RuleAttribute ruleAttribute = setupRuleAttribute();
278         ruleTemplateAttributeBo.setRuleAttribute(ruleAttribute);
279 
280         ruleTemplate.getRuleTemplateAttributes().add(ruleTemplateAttributeBo);
281 
282 
283         RuleExpressionDef ruleExpressionDef = new RuleExpressionDef();
284         ruleExpressionDef.setExpression("test");
285         ruleExpressionDef.setType("TST");
286 
287         rbv.setRuleExpressionDef(ruleExpressionDef);
288 
289         RuleExtensionBo ext = new RuleExtensionBo();
290         RuleExtensionValue val = new RuleExtensionValue();
291         val.setKey("emptyvalue");
292         val.setValue("testing");
293         val.setExtension(ext);
294         ext.getExtensionValues().add(val);
295         ext.setRuleBaseValues(rbv);
296         ext.setRuleTemplateAttribute(ruleTemplateAttributeBo);
297         rbv.getRuleExtensions().add(ext);
298 
299         return KRADServiceLocator.getDataObjectService().save(rbv);
300     }
301 
302 
303 
304 }