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