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 kmForm.setDocument(maintDoc);
69 KNSGlobalVariables.setKualiForm(kmForm);
70 }
71
72
73
74
75
76
77
78
79
80
81 private Set<String> createExpectedKeysSet(boolean hasAcknowledge, boolean hasComplete, boolean hasApprove, boolean hasFyi) {
82 final Set<String> expectedKeys = new HashSet<String>();
83
84 if (hasAcknowledge) { expectedKeys.add(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ); }
85 if (hasComplete) { expectedKeys.add(KewApiConstants.ACTION_REQUEST_COMPLETE_REQ); }
86 if (hasApprove) { expectedKeys.add(KewApiConstants.ACTION_REQUEST_APPROVE_REQ); }
87 if (hasFyi) { expectedKeys.add(KewApiConstants.ACTION_REQUEST_FYI_REQ); }
88 return expectedKeys;
89 }
90
91
92
93
94
95
96
97 private Set<String> createSetOfKeyValueKeys(List<KeyValue> klpList) {
98 final Set<String> actualKeys = new HashSet<String>();
99 for (Iterator<KeyValue> iterator = klpList.iterator(); iterator.hasNext();) {
100 actualKeys.add((String) iterator.next().getKey());
101 }
102 return actualKeys;
103 }
104
105
106
107
108
109 @Test public void testAllTrueOptionsInTestRuleTemplate() throws Exception {
110 createNewKualiMaintenanceForm("TestRuleTemplate");
111 assertRuleTemplateHasExpectedKeyValues(
112 createExpectedKeysSet(true, true, true, true),
113 createSetOfKeyValueKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
114 }
115
116
117
118
119
120
121 @Test public void testCorrectKeyValuesReturnedBasedOnKualiFormInstance() throws Exception {
122
123 KNSGlobalVariables.setKualiForm(new KualiForm());
124 assertRuleTemplateHasExpectedKeyValues(
125 createExpectedKeysSet(true, true, true, true),
126 createSetOfKeyValueKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
127
128 loadXmlFile("RT_ValidRuleTemplatesWithVaryingDefaults.xml");
129 createNewKualiMaintenanceForm("Test_Rule_Template2");
130 assertRuleTemplateHasExpectedKeyValues(
131 createExpectedKeysSet(false, false, false, true),
132 createSetOfKeyValueKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
133 }
134
135
136
137
138
139
140 @Test public void testOptionsInRT_ValidRuleTemplatesWithVaryingDefaults() throws Exception {
141 loadXmlFile("RT_ValidRuleTemplatesWithVaryingDefaults.xml");
142 final String[] ruleTemplates = {"RuleTemplate_With_Valid_Defaults", "RuleTemplate_With_More_Valid_Defaults"};
143 final boolean[][] kSetBools = { {false, false, true, false}, {true, true, false, false} };
144 final String[][] defaultActions = {
145 {KewApiConstants.ACTION_REQUEST_APPROVE_REQ,KewApiConstants.ACTION_REQUEST_APPROVE_REQ,KewApiConstants.ACTION_REQUEST_APPROVE_REQ},
146 {KewApiConstants.ACTION_REQUEST_COMPLETE_REQ,KewApiConstants.ACTION_REQUEST_COMPLETE_REQ,KewApiConstants.ACTION_REQUEST_COMPLETE_REQ}};
147
148 for (int i = 0; i < ruleTemplates.length; i++) {
149 createNewKualiMaintenanceForm(ruleTemplates[i]);
150 assertRuleTemplateHasExpectedKeyValues(
151 createExpectedKeysSet(kSetBools[i][0], kSetBools[i][1], kSetBools[i][2], kSetBools[i][3]),
152 createSetOfKeyValueKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
153 assertRuleTemplateHasExpectedDefaultActions(defaultActions[i]);
154 }
155 }
156
157
158
159
160
161
162
163
164 private void assertRuleTemplateHasExpectedKeyValues(Set<String> expectedKeys, Set<String> actualKeys) throws Exception {
165
166 for (Iterator<String> iterator = expectedKeys.iterator(); iterator.hasNext();) {
167 final String expKey = iterator.next();
168 assertTrue("The key label pair with a key of '" + expKey + "' should have been true.", actualKeys.contains(expKey));
169 actualKeys.remove(expKey);
170 }
171
172
173 if (!actualKeys.isEmpty()) {
174
175 final String pluralStr = (actualKeys.size() != 1) ? "s" : "";
176 final StringBuilder errMsg = new StringBuilder();
177 errMsg.append("The key label pair").append(pluralStr).append(" with the key").append(pluralStr).append(" of ");
178 for (Iterator<String> iterator = actualKeys.iterator(); iterator.hasNext();) {
179 errMsg.append("'").append(iterator.next()).append(iterator.hasNext() ? "', " : "' ");
180 }
181 errMsg.append("should have been false.");
182
183 fail(errMsg.toString());
184 }
185 }
186
187
188
189
190
191
192
193 private void assertRuleTemplateHasExpectedDefaultActions(String[] expectedDefActions) throws Exception {
194
195 final RoutingRuleMaintainable rrMaint = (RoutingRuleMaintainable) ((MaintenanceDocument) ((KualiMaintenanceForm)
196 KNSGlobalVariables.getKualiForm()).getDocument()).getNewMaintainableObject();
197 final String[] respSectionConsts = { KEWPropertyConstants.PERSON_RESP_SECTION, KEWPropertyConstants.GROUP_RESP_SECTION,
198 KEWPropertyConstants.ROLE_RESP_SECTION };
199
200 for (int i = 0; i < respSectionConsts.length; i++) {
201 final String actualDefAction =
202 ((RuleResponsibilityBo) rrMaint.initNewCollectionLine(respSectionConsts[i])).getActionRequestedCd();
203 assertEquals("The rule template does not have the expected default approve action.", expectedDefActions[i], actualDefAction);
204 }
205 }
206 }