View Javadoc

1   /*
2    * Copyright 2007-2009 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  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   * This class tests the code that handles the default values for the rule templates.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class RuleTemplateDefaultsTest extends KEWTestCase {
44  
45  	/**
46  	 * Creates a KualiMaintenanceForm with the given rule template inside of its RuleBaseValues instance.
47  	 * 
48  	 * @param rtName The rule template to use.
49  	 */
50  	private void createNewKualiMaintenanceForm(String rtName) {
51  		// Initialize the required variables.
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  		// Setup the rule base and the maintainables.
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  		// Setup the maintenance document and the maintenance form.
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  	 * A convenience method for creating a set of expected key label pairs.
73  	 * 
74  	 * @param hasAcknowledge Indicates that a KeyLabelPair for "acknowledge" options should exist.
75  	 * @param hasComplete Indicates that a KeyLabelPair for "complete" options should exist.
76  	 * @param hasApprove Indicates that a KeyLabelPair for "approve" options should exist.
77  	 * @param hasFyi Indicates that a KeyLabelPair for "fyi" options should exist.
78  	 * @return A Set containing the desired expected KeyLabelPair keys.
79  	 */
80  	private Set<String> createExpectedKeysSet(boolean hasAcknowledge, boolean hasComplete, boolean hasApprove, boolean hasFyi) {
81  		final Set<String> expectedKeys = new HashSet<String>();
82  		// Insert the desired expected options into the set.
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  	 * A convenience method for placing the keys from a KeyLabelPair list into a set.
92  	 * 
93  	 * @param kValues The KeyLabelPairs to process.
94  	 * @return A Set containing the keys of each KeyLabelPair.
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 	 * Tests to ensure that the "TestRuleTemplate" in DefaultTestData.xml has the four action request options defined as "true",
106 	 * either explicitly or by default.
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 	 * Tests to ensure that the proper key values are returned based upon the class type of the currently-set Kuali form.
117 	 * 
118 	 * @throws Exception
119 	 */
120 	@Test public void testCorrectKeyValuesReturnedBasedOnKualiFormInstance() throws Exception {
121 		// First, check that the proper values are returned when the Kuali form is *not* a KualiMaintenanceForm.
122 		GlobalVariables.setKualiForm(new KualiForm());
123 		assertRuleTemplateHasExpectedKeyLabelPairs(
124 				createExpectedKeysSet(true, true, true, true),
125 				createSetOfKeyLabelPairKeys((new RuleMaintenanceActionRequestCodeValuesFinder()).getKeyValues()));
126 		// Next, check that the proper values are returned when the Kuali form is a KualiMaintenanceForm containing a given rule template.
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 	 * Tests to ensure that the rule template in RT_ValidRuleTemplateWithFullDefaults.xml has the expected action request options.
136 	 * 
137 	 * @throws Exception
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 		// Test each rule template from the given file.
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 	 * A convenience method for performing KeyLabelPair existence/nonexistence tests.
158 	 * 
159 	 * @param expectedKeys The expected KeyLabelPair keys.
160 	 * @param actualKeys The actual KeyLabelPair keys.
161 	 * @throws Exception
162 	 */
163 	private void assertRuleTemplateHasExpectedKeyLabelPairs(Set<String> expectedKeys, Set<String> actualKeys) throws Exception {
164 		// Check to see if all required keys are in the set.
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 		// If any keys are still in the list, then fail the test because we expected their equivalent rule template options to
171 		// have a non-true value.
172 		if (!actualKeys.isEmpty()) {
173 			// Construct the error message.
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 			// Fail the test.
182 			fail(errMsg.toString());
183 		}
184 	}
185 	
186 	/**
187 	 * A convenience method for verifying that a rule template contains the expected default action.
188 	 * 
189 	 * @param expectedDefActions The default actions expected by each responsibility (person, then group, then role).
190 	 * @throws Exception
191 	 */
192 	private void assertRuleTemplateHasExpectedDefaultActions(String[] expectedDefActions) throws Exception {
193 		// Acquire the Maintainable and the responsibility constants.
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 		// Check each responsibility's default action.
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 }