001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.actionlist.web;
018:
019: import java.net.URL;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.junit.Test;
026: import org.kuali.workflow.test.WorkflowTestCase;
027:
028: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
029: import com.gargoylesoftware.htmlunit.WebClient;
030: import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
031: import com.gargoylesoftware.htmlunit.html.HtmlForm;
032: import com.gargoylesoftware.htmlunit.html.HtmlOption;
033: import com.gargoylesoftware.htmlunit.html.HtmlPage;
034: import com.gargoylesoftware.htmlunit.html.HtmlSelect;
035: import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
036:
037: import edu.iu.uis.eden.EdenConstants;
038: import edu.iu.uis.eden.KEWServiceLocator;
039: import edu.iu.uis.eden.actionitem.ActionItem;
040: import edu.iu.uis.eden.clientapp.WorkflowDocument;
041: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
042: import edu.iu.uis.eden.preferences.Preferences;
043: import edu.iu.uis.eden.user.AuthenticationUserId;
044: import edu.iu.uis.eden.user.WorkflowUser;
045:
046: /**
047: * Tests the web GUI for the ActionList.
048: *
049: * @author ewestfal
050: */
051: public class ActionListActionTest extends WorkflowTestCase {
052:
053: private static final String URL_PREFIX = "http://localhost:9912/en-test/";
054:
055: private WebClient webClient;
056: private WorkflowUser quickstartUser;
057:
058: protected void loadTestData() throws Exception {
059: loadXmlFile("ActionListWebConfig.xml");
060: }
061:
062: @Override
063: protected void setUpTransaction() throws Exception {
064: super .setUpTransaction();
065: webClient = new WebClient();
066:
067: // Set the user preference refresh rate to 0 to prevent a <META HTTP-EQUIV="Refresh" .../> tag from being rendered.
068: // If it is rendered than HtmlUnit will immediately redirect, causing an error to be thrown.
069: this .quickstartUser = KEWServiceLocator
070: .getUserService()
071: .getWorkflowUser(new AuthenticationUserId("quickstart"));
072: Preferences preferences = KEWServiceLocator
073: .getPreferencesService().getPreferences(quickstartUser);
074: preferences.setRefreshRate("0");
075: KEWServiceLocator.getPreferencesService().savePreferences(
076: quickstartUser, preferences);
077: }
078:
079: /**
080: * Tests the mass action list.
081: */
082: @Test
083: public void testMassActionList() throws Exception {
084: URL url = new URL(URL_PREFIX + "ActionList.do");
085: HtmlPage page = (HtmlPage) webClient.getPage(url);
086:
087: // On the first access, we should end up on the backdoor and login as quickstart
088: HtmlForm form = (HtmlForm) page.getForms().get(0);
089: HtmlTextInput textInput = (HtmlTextInput) form
090: .getInputByName("__login_user");
091: assertEquals("quickstart", textInput.getDefaultValue());
092: page = (HtmlPage) form.submit();
093:
094: // we should be on the Action List now, check that theres a form here
095: assertEquals("Should be one form.", 1, page.getForms().size());
096:
097: // route 10 documents to quickstart and add FYIs which should result in Mass Actions
098: int numDocs = 10;
099: for (int i = 0; i < numDocs; i++) {
100: WorkflowDocument document = new WorkflowDocument(
101: new NetworkIdVO("ewestfal"), "MassActionListTest");
102: document.appSpecificRouteDocumentToUser(
103: EdenConstants.ACTION_REQUEST_FYI_REQ, "",
104: new NetworkIdVO("quickstart"), "", true);
105: document.routeDocument("");
106: assertTrue("Document should be FINAL.", document
107: .stateIsFinal());
108: }
109:
110: // check that the quickstart user has 10 action items
111: Collection actionList = KEWServiceLocator
112: .getActionListService().findByWorkflowUser(
113: quickstartUser);
114: assertEquals("Should have 10 items.", 10, actionList.size());
115:
116: // now refresh the Action List
117: page = (HtmlPage) webClient.getPage(url);
118:
119: form = (HtmlForm) page.getForms().get(0);
120: for (int i = 0; i < numDocs; i++) {
121: String actionTakenCodeFieldName = "actions[" + i
122: + "].actionTakenCd";
123: HtmlSelect select = form
124: .getSelectByName(actionTakenCodeFieldName);
125: List options = select.getOptions();
126: assertEquals("Should have two options", 2, select
127: .getOptionSize());
128: boolean hasNone = false;
129: boolean hasFYI = false;
130: for (Iterator iter = options.iterator(); iter.hasNext();) {
131: HtmlOption option = (HtmlOption) iter.next();
132: if (option.getValueAttribute().equals("NONE")) {
133: hasNone = true;
134: } else if (option.getValueAttribute().equals(
135: EdenConstants.ACTION_REQUEST_FYI_REQ)) {
136: hasFYI = true;
137: }
138: }
139: assertTrue("Should have had NONE option", hasNone);
140: assertTrue("Should have had FYI option", hasFYI);
141: select.setSelectedAttribute(
142: EdenConstants.ACTION_REQUEST_FYI_REQ, true);
143: }
144:
145: // get a reference to the "takeMassActions" link and click it
146: HtmlAnchor anchor = (HtmlAnchor) page
147: .getHtmlElementById("takeMassActions");
148: anchor.click();
149:
150: // after taking mass actions, user should have no requests left in their action list
151: actionList = KEWServiceLocator.getActionListService()
152: .findByWorkflowUser(quickstartUser);
153: assertEquals("Should have 0 items.", 0, actionList.size());
154:
155: // now let's route some and intersperse them with documents that won't have the Mass Action option,
156: // the odd index docs are approvals, the even index ones are fyis which will have mass action available
157: List<Doc> massActionable = new ArrayList<Doc>();
158: List<Doc> nonMassActionable = new ArrayList<Doc>();
159: numDocs = 10;
160: for (int i = 0; i < numDocs; i++) {
161: boolean isMassActionable = (i % 2 == 0);
162: String actionRequested = (isMassActionable ? EdenConstants.ACTION_REQUEST_FYI_REQ
163: : EdenConstants.ACTION_REQUEST_APPROVE_REQ);
164: WorkflowDocument document = new WorkflowDocument(
165: new NetworkIdVO("ewestfal"), "MassActionListTest");
166: document.appSpecificRouteDocumentToUser(actionRequested,
167: "", new NetworkIdVO("quickstart"), "", true);
168: document.routeDocument("");
169: if (isMassActionable) {
170: massActionable.add(new Doc(i, document
171: .getRouteHeaderId()));
172: } else {
173: nonMassActionable.add(new Doc(i, document
174: .getRouteHeaderId()));
175: }
176: }
177:
178: // check that the quickstart user has 10 action items
179: actionList = KEWServiceLocator.getActionListService()
180: .findByWorkflowUser(quickstartUser);
181: assertEquals("Should have 10 items.", 10, actionList.size());
182:
183: // refresh the Action List
184: page = (HtmlPage) webClient.getPage(url);
185: assertEquals("Should be one form on the page.", 1, page
186: .getForms().size());
187: form = (HtmlForm) page.getForms().get(0);
188:
189: for (int i = 0; i < numDocs; i++) {
190: boolean isMassActionable = (i % 2 == 0);
191: String actionTakenCodeFieldName = "actions[" + i
192: + "].actionTakenCd";
193: if (isMassActionable) {
194: HtmlSelect select = form
195: .getSelectByName(actionTakenCodeFieldName);
196: List options = select.getOptions();
197: assertEquals("Should have two options", 2, select
198: .getOptionSize());
199: boolean hasNone = false;
200: boolean hasFYI = false;
201: for (Iterator iter = options.iterator(); iter.hasNext();) {
202: HtmlOption option = (HtmlOption) iter.next();
203: if (option.getValueAttribute().equals("NONE")) {
204: hasNone = true;
205: } else if (option.getValueAttribute().equals("F")) {
206: hasFYI = true;
207: }
208: }
209: assertTrue("Should have had NONE option", hasNone);
210: assertTrue("Should have had FYI option", hasFYI);
211: select.setSelectedAttribute("F", true);
212:
213: } else {
214: try {
215: form.getSelectByName(actionTakenCodeFieldName);
216: fail("ElementNotFoundException should have been thrown");
217: } catch (ElementNotFoundException e) {
218: }
219: }
220: }
221:
222: // take the actions
223: anchor = (HtmlAnchor) page
224: .getHtmlElementById("takeMassActions");
225: anchor.click();
226:
227: // since only half the actions were "mass actionable", we should have 5 now
228: actionList = KEWServiceLocator.getActionListService()
229: .findByWorkflowUser(quickstartUser);
230: assertEquals("Should have 5 items.", 5, actionList.size());
231: // check that the documents remaining are the ones which are approve requests and not the mass actionable fyi requests
232: for (Iterator iterator = actionList.iterator(); iterator
233: .hasNext();) {
234: ActionItem actionItem = (ActionItem) iterator.next();
235: assertEquals(EdenConstants.ACTION_REQUEST_APPROVE_REQ,
236: actionItem.getActionRequestCd());
237: boolean foundDoc = false;
238: for (Doc doc : nonMassActionable) {
239: if (doc.docId.equals(actionItem.getRouteHeaderId())) {
240: foundDoc = true;
241: break;
242: }
243: }
244: if (!foundDoc) {
245: fail("The given remaining document should have been non-Mass Actionable: "
246: + actionItem.getRouteHeaderId());
247: }
248: }
249:
250: }
251:
252: private static class Doc {
253: public int index;
254: public Long docId;
255:
256: public Doc(int index, Long docId) {
257: this.index = index;
258: this.docId = docId;
259: }
260:
261: }
262:
263: }
|