1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule.service.impl;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.persistence.PersistenceException;
29
30 import org.junit.Test;
31 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
32 import org.kuali.rice.kew.rule.RuleBaseValues;
33 import org.kuali.rice.kew.rule.RuleExtensionBo;
34 import org.kuali.rice.kew.rule.RuleExtensionValue;
35 import org.kuali.rice.kew.service.KEWServiceLocator;
36 import org.kuali.rice.kew.test.KEWTestCase;
37 import org.kuali.rice.test.BaselineTestCase;
38 import org.springframework.dao.DataIntegrityViolationException;
39
40 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
41 public class RuleServiceTest extends KEWTestCase {
42
43 protected void loadTestData() throws Exception {
44 loadXmlFile("org/kuali/rice/kew/rule/RouteTemplateConfig.xml");
45 }
46
47
48
49
50
51
52
53 @Test
54 public void testEmptyRuleExtension() throws Exception {
55 final RuleBaseValues rbv = new RuleBaseValues();
56 rbv.setActive(Boolean.TRUE);
57 rbv.setCurrentInd(Boolean.TRUE);
58 rbv.setDescription("A test rule");
59 rbv.setDocTypeName("TestDocumentType");
60 rbv.setForceAction(Boolean.FALSE);
61
62 RuleExtensionBo ext = new RuleExtensionBo();
63 RuleExtensionValue val = new RuleExtensionValue();
64 val.setKey("emptyvalue");
65 val.setValue("");
66 ext.getExtensionValues().add(val);
67 rbv.getRuleExtensions().add(ext);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 final boolean isKewJpaEnabled = OrmUtils.isJpaEnabled("rice.kew");
85 try {
86 KEWServiceLocator.getRuleService().save2(rbv);
87 fail("exception did not happen");
88 } catch (RuntimeException e) {
89 boolean fail = !isKewJpaEnabled ? e instanceof PersistenceException : e instanceof DataIntegrityViolationException;
90 if (fail) {
91 fail("Did not throw exception as expected. If rule service behavior has changed, update this test.");
92 }
93 }
94
95
96 }
97
98
99
100
101
102 @Test
103 public void testRetrievalOfRulesWithoutResponsibilities() throws Exception {
104 loadXmlFile("org/kuali/rice/kew/rule/RulesWithoutResponsibilities.xml");
105 final String NULL_ID = null;
106 final String[] expectedRuleNames = {"NoResponsibilitiesRule1", "NoResponsibilitiesRule2", "NoResponsibilitiesRule3"};
107 final String[] expectedRuleDocTypes = {"RiceDocument.RuleDocument", "RiceDocument.child1", "RiceDocument.child1child"};
108 final String[] expectedRuleDescriptions = {"A rule with no responsibilities", "Another rule without responsibilities", "A third rule lacking responsibilities"};
109 final String[] personResponsibilities = {"rkirkend", "rkirkend", "user1"};
110 final String[] groupResponsibilities = {"TestWorkgroup", "NonSIT", "TestWorkgroup"};
111 int actualResponsibilitylessRuleCount = 0;
112 List<?> ruleList = null;
113
114
115 ruleList = KEWServiceLocator.getRuleService().search(null, NULL_ID, null, null, null, null, null, null, null, "");
116 assertNotNull("The returned rule list should not be null", ruleList);
117 for (Iterator<?> ruleIter = ruleList.iterator(); ruleIter.hasNext();) {
118 RuleBaseValues rBaseValues = (RuleBaseValues) ruleIter.next();
119 if (rBaseValues.getRuleResponsibilities() == null || rBaseValues.getRuleResponsibilities().isEmpty()) {
120 actualResponsibilitylessRuleCount++;
121 }
122 }
123 assertEquals("Wrong number of responsibility-less rules found", expectedRuleNames.length, actualResponsibilitylessRuleCount);
124
125
126 for (int i = 0; i < expectedRuleNames.length; i++) {
127 ruleList = KEWServiceLocator.getRuleService().search(expectedRuleDocTypes[i], NULL_ID, null, expectedRuleDescriptions[i], null, null, null, null, null, "");
128 assertNotNull("The returned rule list should not be null when searching for rule '" + expectedRuleNames[i] + "'", ruleList);
129 assertEquals("Exactly one rule should have been retrieved when searching for rule '" + expectedRuleNames[i] + "'", 1, ruleList.size());
130 RuleBaseValues rBaseValues = (RuleBaseValues) ruleList.get(0);
131 assertEquals("The retrieved rule has the wrong name", expectedRuleNames[i], rBaseValues.getName());
132 assertEquals("Rule '" + expectedRuleNames[i] + "' has the wrong doc type name", expectedRuleDocTypes[i], rBaseValues.getDocTypeName());
133 assertEquals("Rule '" + expectedRuleNames[i] + "' has the wrong description", expectedRuleDescriptions[i], rBaseValues.getDescription());
134 assertTrue("Rule '" + expectedRuleNames[i] + "' should not have any responsibilities",
135 rBaseValues.getRuleResponsibilities() == null || rBaseValues.getRuleResponsibilities().isEmpty());
136 }
137
138
139 for (int i = 0; i < expectedRuleNames.length; i++) {
140 ruleList = KEWServiceLocator.getRuleService().search(expectedRuleDocTypes[i], NULL_ID, null, null, null,
141 KEWServiceLocator.getIdentityHelperService().getPrincipalByPrincipalName(personResponsibilities[i]).getPrincipalId(), null, null, null, "user");
142 assertNotNull("The returned rule list should not be null for doc type '" + expectedRuleDocTypes[i] + "'", ruleList);
143 assertFalse("The returned rule list should not be empty for doc type '" + expectedRuleDocTypes[i] + "'", ruleList.isEmpty());
144 for (Iterator<?> ruleIter = ruleList.iterator(); ruleIter.hasNext();) {
145 RuleBaseValues rBaseValues = (RuleBaseValues) ruleIter.next();
146 assertTrue((new StringBuilder()).append("Found a rule without responsibilities for doc type '").append(
147 expectedRuleDocTypes[i]).append("' and principal '").append(personResponsibilities[i]).append("'").toString(),
148 rBaseValues.getRuleResponsibilities() != null && !rBaseValues.getRuleResponsibilities().isEmpty());
149 }
150 }
151
152
153 for (int i = 0; i < expectedRuleNames.length; i++) {
154 ruleList = KEWServiceLocator.getRuleService().search(expectedRuleDocTypes[i], NULL_ID, null, null,
155 KEWServiceLocator.getIdentityHelperService().getGroupByName("KR-WKFLW", groupResponsibilities[i]).getId(), null, null, null, null, "");
156 assertNotNull("The returned rule list should not be null for doc type '" + expectedRuleDocTypes[i] + "'", ruleList);
157 assertFalse("The returned rule list should not be empty for doc type '" + expectedRuleDocTypes[i] + "'", ruleList.isEmpty());
158 for (Iterator<?> ruleIter = ruleList.iterator(); ruleIter.hasNext();) {
159 RuleBaseValues rBaseValues = (RuleBaseValues) ruleIter.next();
160 assertTrue((new StringBuilder()).append("Found a rule without responsibilities for doc type '").append(
161 expectedRuleDocTypes[i]).append("' and group '").append(groupResponsibilities[i]).append("' with namespace 'KR-WKFLW'").toString(),
162 rBaseValues.getRuleResponsibilities() != null && !rBaseValues.getRuleResponsibilities().isEmpty());
163 }
164 }
165 }
166 }