1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.junit.Test;
24 import org.kuali.rice.core.util.KeyLabelPair;
25 import org.kuali.rice.kew.actionrequest.bo.RuleMaintenanceActionRequestCodeValuesFinder;
26 import org.kuali.rice.kew.document.RoutingRuleMaintainable;
27 import org.kuali.rice.kew.service.KEWServiceLocator;
28 import org.kuali.rice.kew.test.KEWTestCase;
29 import org.kuali.rice.kew.util.KEWConstants;
30 import org.kuali.rice.kew.util.KEWPropertyConstants;
31 import org.kuali.rice.kns.document.MaintenanceDocument;
32 import org.kuali.rice.kns.document.MaintenanceDocumentBase;
33 import org.kuali.rice.kns.maintenance.Maintainable;
34 import org.kuali.rice.kns.util.GlobalVariables;
35 import org.kuali.rice.kns.web.struts.form.KualiForm;
36 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
37
38
39
40
41
42
43 public class RuleTemplateDefaultsTest extends KEWTestCase {
44
45
46
47
48
49
50 private void createNewKualiMaintenanceForm(String rtName) {
51
52 final KualiMaintenanceForm kmForm = new KualiMaintenanceForm();
53 final MaintenanceDocument maintDoc = new MaintenanceDocumentBase();
54 final Maintainable oldMaint = new RoutingRuleMaintainable();
55 final Maintainable newMaint = new RoutingRuleMaintainable();
56 final RuleBaseValues rbValues = new RuleBaseValues();
57
58 rbValues.setRuleTemplate(KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(rtName));
59 oldMaint.setBusinessObject(rbValues);
60 oldMaint.setBoClass(rbValues.getClass());
61 newMaint.setBusinessObject(rbValues);
62 newMaint.setBoClass(rbValues.getClass());
63
64 maintDoc.setOldMaintainableObject(oldMaint);
65 maintDoc.setNewMaintainableObject(newMaint);
66 maintDoc.getDocumentHeader().setDocumentDescription("This is a rule template test");
67 kmForm.setDocument(maintDoc);
68 GlobalVariables.setKualiForm(kmForm);
69 }
70
71
72
73
74
75
76
77
78
79
80 private Set<String> createExpectedKeysSet(boolean hasAcknowledge, boolean hasComplete, boolean hasApprove, boolean hasFyi) {
81 final Set<String> expectedKeys = new HashSet<String>();
82
83 if (hasAcknowledge) { expectedKeys.add(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ); }
84 if (hasComplete) { expectedKeys.add(KEWConstants.ACTION_REQUEST_COMPLETE_REQ); }
85 if (hasApprove) { expectedKeys.add(KEWConstants.ACTION_REQUEST_APPROVE_REQ); }
86 if (hasFyi) { expectedKeys.add(KEWConstants.ACTION_REQUEST_FYI_REQ); }
87 return expectedKeys;
88 }
89
90
91
92
93
94
95
96 private Set<String> createSetOfKeyLabelPairKeys(List<KeyLabelPair> klpList) {
97 final Set<String> actualKeys = new HashSet<String>();
98 for (Iterator<KeyLabelPair> iterator = klpList.iterator(); iterator.hasNext();) {
99 actualKeys.add((String) iterator.next().key);
100 }
101 return actualKeys;
102 }
103
104
105
106
107
108 @Test public void testAllTrueOptionsInTestRuleTemplate() throws Exception {
109 createNewKualiMaintenanceForm("TestRuleTemplate");
110 assertRuleTemplateHasExpectedKeyLabelPairs(
111 createExpectedKeysSet(true, true, true, true),
112 createSetOfKeyLabelPairKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
113 }
114
115
116
117
118
119
120 @Test public void testCorrectKeyValuesReturnedBasedOnKualiFormInstance() throws Exception {
121
122 GlobalVariables.setKualiForm(new KualiForm());
123 assertRuleTemplateHasExpectedKeyLabelPairs(
124 createExpectedKeysSet(true, true, true, true),
125 createSetOfKeyLabelPairKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
126
127 loadXmlFile("RT_ValidRuleTemplatesWithVaryingDefaults.xml");
128 createNewKualiMaintenanceForm("Test_Rule_Template2");
129 assertRuleTemplateHasExpectedKeyLabelPairs(
130 createExpectedKeysSet(false, false, false, true),
131 createSetOfKeyLabelPairKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
132 }
133
134
135
136
137
138
139 @Test public void testOptionsInRT_ValidRuleTemplatesWithVaryingDefaults() throws Exception {
140 loadXmlFile("RT_ValidRuleTemplatesWithVaryingDefaults.xml");
141 final String[] ruleTemplates = {"RuleTemplate_With_Valid_Defaults", "RuleTemplate_With_More_Valid_Defaults"};
142 final boolean[][] kSetBools = { {false, false, true, false}, {true, true, false, false} };
143 final String[][] defaultActions = {
144 {KEWConstants.ACTION_REQUEST_APPROVE_REQ,KEWConstants.ACTION_REQUEST_APPROVE_REQ,KEWConstants.ACTION_REQUEST_APPROVE_REQ},
145 {KEWConstants.ACTION_REQUEST_COMPLETE_REQ,KEWConstants.ACTION_REQUEST_COMPLETE_REQ,KEWConstants.ACTION_REQUEST_COMPLETE_REQ}};
146
147 for (int i = 0; i < ruleTemplates.length; i++) {
148 createNewKualiMaintenanceForm(ruleTemplates[i]);
149 assertRuleTemplateHasExpectedKeyLabelPairs(
150 createExpectedKeysSet(kSetBools[i][0], kSetBools[i][1], kSetBools[i][2], kSetBools[i][3]),
151 createSetOfKeyLabelPairKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
152 assertRuleTemplateHasExpectedDefaultActions(defaultActions[i]);
153 }
154 }
155
156
157
158
159
160
161
162
163 private void assertRuleTemplateHasExpectedKeyLabelPairs(Set<String> expectedKeys, Set<String> actualKeys) throws Exception {
164
165 for (Iterator<String> iterator = expectedKeys.iterator(); iterator.hasNext();) {
166 final String expKey = iterator.next();
167 assertTrue("The key label pair with a key of '" + expKey + "' should have been true.", actualKeys.contains(expKey));
168 actualKeys.remove(expKey);
169 }
170
171
172 if (!actualKeys.isEmpty()) {
173
174 final String pluralStr = (actualKeys.size() != 1) ? "s" : "";
175 final StringBuilder errMsg = new StringBuilder();
176 errMsg.append("The key label pair").append(pluralStr).append(" with the key").append(pluralStr).append(" of ");
177 for (Iterator<String> iterator = actualKeys.iterator(); iterator.hasNext();) {
178 errMsg.append("'").append(iterator.next()).append(iterator.hasNext() ? "', " : "' ");
179 }
180 errMsg.append("should have been false.");
181
182 fail(errMsg.toString());
183 }
184 }
185
186
187
188
189
190
191
192 private void assertRuleTemplateHasExpectedDefaultActions(String[] expectedDefActions) throws Exception {
193
194 final RoutingRuleMaintainable rrMaint = (RoutingRuleMaintainable) ((MaintenanceDocument) ((KualiMaintenanceForm)
195 GlobalVariables.getKualiForm()).getDocument()).getNewMaintainableObject();
196 final String[] respSectionConsts = { KEWPropertyConstants.PERSON_RESP_SECTION, KEWPropertyConstants.GROUP_RESP_SECTION,
197 KEWPropertyConstants.ROLE_RESP_SECTION };
198
199 for (int i = 0; i < respSectionConsts.length; i++) {
200 final String actualDefAction =
201 ((RuleResponsibility) rrMaint.initNewCollectionLine(respSectionConsts[i])).getActionRequestedCd();
202 assertEquals("The rule template does not have the expected default approve action.", expectedDefActions[i], actualDefAction);
203 }
204 }
205 }