View Javadoc

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