001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.workflow;
021:
022: import java.security.Principal;
023: import java.util.Collection;
024: import java.util.Properties;
025:
026: import junit.framework.TestCase;
027:
028: import com.ecyrd.jspwiki.TestEngine;
029: import com.ecyrd.jspwiki.WikiException;
030: import com.ecyrd.jspwiki.WikiSession;
031: import com.ecyrd.jspwiki.auth.GroupPrincipal;
032: import com.ecyrd.jspwiki.auth.WikiPrincipal;
033:
034: public class DecisionQueueTest extends TestCase {
035:
036: TestEngine m_engine;
037:
038: DecisionQueue m_queue;
039:
040: Workflow w;
041:
042: Decision d1;
043:
044: Decision d2;
045:
046: Decision d3;
047:
048: WikiSession janneSession;
049:
050: WikiSession adminSession;
051:
052: protected void setUp() throws Exception {
053: super .setUp();
054: Properties props = new Properties();
055: props.load(TestEngine.findTestProperties());
056: m_engine = new TestEngine(props);
057: m_queue = m_engine.getWorkflowManager().getDecisionQueue();
058: adminSession = m_engine.adminSession();
059: janneSession = m_engine.janneSession();
060: w = new Workflow("workflow.key", new WikiPrincipal("Owner1"));
061: w.setWorkflowManager(m_engine.getWorkflowManager());
062: d1 = new SimpleDecision(w, "decision1.key", new GroupPrincipal(
063: "Admin"));
064: d2 = new SimpleDecision(w, "decision2.key", new WikiPrincipal(
065: "Owner2"));
066: d3 = new SimpleDecision(w, "decision3.key", janneSession
067: .getUserPrincipal());
068: m_queue.add(d1);
069: m_queue.add(d2);
070: m_queue.add(d3);
071: }
072:
073: public void testAdd() {
074: Decision[] decisions = m_queue.decisions();
075: assertEquals(d1, decisions[0]);
076: assertEquals(d2, decisions[1]);
077: assertEquals(d3, decisions[2]);
078: }
079:
080: public void testRemove() {
081: Decision[] decisions = m_queue.decisions();
082: assertEquals(3, decisions.length);
083:
084: m_queue.remove(d2);
085: decisions = m_queue.decisions();
086: assertEquals(2, decisions.length);
087: assertEquals(d1, decisions[0]);
088: assertEquals(d3, decisions[1]);
089:
090: m_queue.remove(d1);
091: decisions = m_queue.decisions();
092: assertEquals(1, decisions.length);
093: assertEquals(d3, decisions[0]);
094:
095: m_queue.remove(d3);
096: decisions = m_queue.decisions();
097: assertEquals(0, decisions.length);
098: }
099:
100: public void testComplete() throws WikiException {
101: assertEquals(3, m_queue.decisions().length);
102:
103: // Execute the competion for decision 1 (approve/deny)
104: m_queue.decide(d1, Outcome.DECISION_APPROVE);
105:
106: // Decision should be marked completed, and removed from queue
107: assertTrue(d1.isCompleted());
108: assertEquals(2, m_queue.decisions().length);
109:
110: // Execute the competion for decision 2 (approve/deny/hold)
111: m_queue.decide(d2, Outcome.DECISION_DENY);
112:
113: // Decision should be marked completed, and removed from queue
114: assertTrue(d2.isCompleted());
115: assertEquals(1, m_queue.decisions().length);
116: }
117:
118: public void testReassign() throws WikiException {
119: // Janne owns 1 decision (d3)
120: assertEquals(janneSession.getUserPrincipal(), d3.getActor());
121: assertEquals(1, m_queue.getActorDecisions(janneSession).size());
122:
123: // Reassign the decision
124: m_queue.reassign(d3, new WikiPrincipal("NewOwner"));
125:
126: // d3 should have a different owner now, and it won't show up in
127: // Janne's decision list
128: assertEquals(new WikiPrincipal("NewOwner"), d3.getActor());
129: assertEquals(0, m_queue.getActorDecisions(janneSession).size());
130: }
131:
132: public void testDecisions() {
133: Decision[] decisions = m_queue.decisions();
134: assertEquals(3, decisions.length);
135: assertEquals(d1, decisions[0]);
136: assertEquals(d2, decisions[1]);
137: assertEquals(d3, decisions[2]);
138: }
139:
140: public void testActorDecisions() {
141: Collection decisions = m_queue.getActorDecisions(adminSession);
142: assertEquals(1, decisions.size());
143:
144: decisions = m_queue.getActorDecisions(janneSession);
145: assertEquals(1, decisions.size());
146: }
147:
148: public void testDecisionWorkflow() throws WikiException {
149: Principal janne = janneSession.getUserPrincipal();
150:
151: // Clean out the queue first
152: m_queue.remove(d1);
153: m_queue.remove(d2);
154: m_queue.remove(d3);
155:
156: // Create a workflow with 3 steps, with a Decision for Janne in the middle
157: w = new Workflow("workflow.key", new WikiPrincipal("Owner1"));
158: w.setWorkflowManager(m_engine.getWorkflowManager());
159: Step startTask = new TaskTest.NormalTask(w);
160: Step endTask = new TaskTest.NormalTask(w);
161: Decision decision = new SimpleDecision(w,
162: "decision.Actor1Decision", janne);
163: startTask.addSuccessor(Outcome.STEP_COMPLETE, decision);
164: decision.addSuccessor(Outcome.DECISION_APPROVE, endTask);
165: w.setFirstStep(startTask);
166:
167: // Start the workflow, and verify that the Decision is the current Step
168: w.start();
169: assertEquals(decision, w.getCurrentStep());
170:
171: // Verify that it's also in Janne's DecisionQueue
172: Collection decisions = m_queue.getActorDecisions(janneSession);
173: assertEquals(1, decisions.size());
174: Decision d = (Decision) decisions.iterator().next();
175: assertEquals(decision, d);
176:
177: // Make Decision, and verify that it's gone from the queue
178: m_queue.decide(decision, Outcome.DECISION_APPROVE);
179: decisions = m_queue.getActorDecisions(janneSession);
180: assertEquals(0, decisions.size());
181: }
182:
183: }
|