001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.actions;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertNotNull;
020    import static org.junit.Assert.assertTrue;
021    
022    import org.junit.Test;
023    import org.kuali.rice.kew.api.WorkflowDocument;
024    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
025    import org.kuali.rice.kew.api.action.ActionRequest;
026    import org.kuali.rice.kew.api.action.ActionTaken;
027    import org.kuali.rice.kew.api.exception.WorkflowException;
028    import org.kuali.rice.kew.engine.node.Branch;
029    import org.kuali.rice.kew.engine.node.BranchState;
030    import org.kuali.rice.kew.test.KEWTestCase;
031    
032    /**
033     * Test case that tests setting and getting variables, both programmatically
034     * and via the "SetVar" node; stolen directly from ApproveActionTest.testPreapprovals
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    public class VariablesTest extends KEWTestCase {
038    
039            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VariablesTest.class);
040    
041        protected void loadTestData() throws Exception {
042            loadXmlFile("ActionsConfig.xml");
043        }
044    
045        private void dumpInfoAboutDoc(WorkflowDocument doc) throws WorkflowException {
046            LOG.info("\tDoc: class=" + doc.getDocumentTypeName() + " title=" + doc.getTitle() + " status=" + doc.getStatus());
047            LOG.info("\tActionRequests:");
048            for (ActionRequest ar: doc.getRootActionRequests()) {
049                LOG.info("\t\tId: " + ar.getId() + " PrincipalId: " + ar.getPrincipalId() + " ActionRequested: " + ar.getActionRequested() + " ActionTaken: " + (ar.getActionTaken() != null ? ar.getActionTaken().getActionTaken() : null) + " NodeName: " + ar.getNodeName() + " Status:" + ar.getStatus());
050            }
051            LOG.info("\tActionTakens:");
052            for (ActionTaken at: doc.getActionsTaken()) {
053                LOG.info("\t\tId: " + at.getId() + " PrincipalId: " + at.getPrincipalId() + " ActionTaken: " + at.getActionTaken());
054            }
055            LOG.info("\tNodeNames:");
056            for (String name: doc.getNodeNames()) {
057                LOG.info("\t\t" + name);
058            }
059        }
060    
061        public void dumpBranch(Branch b) {
062            LOG.info("Branch: " + b.getBranchId() + " " + b.getName());
063            for (BranchState bs: b.getBranchState()) {
064                LOG.info(bs.getBranchStateId() + " " + bs.getKey() + " " + bs.getValue());
065            }
066        }
067    
068        @Test public void testVariables() throws Exception {
069            WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "VariablesTest");
070            doc.route("");
071    
072            //rock some preapprovals and other actions...
073            doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), doc.getDocumentId());
074            dumpInfoAboutDoc(doc);
075            doc.setVariable("myexcellentvariable", "righton");
076            doc.approve("");
077            assertEquals("startedVariableValue", doc.getVariableValue("started"));
078            assertEquals("startedVariableValue", doc.getVariableValue("copiedVar"));
079    
080            doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), doc.getDocumentId());
081            assertEquals("righton", doc.getVariableValue("myexcellentvariable"));
082            doc.setVariable("vartwo", "two");
083            doc.setVariable("myexcellentvariable", "ichangedit");
084            doc.acknowledge("");
085    
086            doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user3"), doc.getDocumentId());
087            assertEquals("ichangedit", doc.getVariableValue("myexcellentvariable"));
088            assertEquals("two", doc.getVariableValue("vartwo"));
089            doc.setVariable("another", "another");
090            doc.setVariable("vartwo", null);
091            doc.complete("");
092    
093            //approve as the person the doc is routed to so we can move the documen on and hopefully to final
094            doc = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), doc.getDocumentId());
095            assertEquals("ichangedit", doc.getVariableValue("myexcellentvariable"));
096            assertEquals(null, doc.getVariableValue("vartwo"));
097            assertEquals("another", doc.getVariableValue("another"));
098            doc.approve("");
099    
100            assertEquals("endedVariableValue", doc.getVariableValue("ended"));
101            assertNotNull(doc.getVariableValue("google"));
102            LOG.info(doc.getVariableValue("google"));
103            assertEquals("documentContentendedVariableValue", doc.getVariableValue("xpath"));
104            LOG.info(doc.getVariableValue("xpath"));
105    
106            assertEquals("aNewStartedVariableValue", doc.getVariableValue("started"));
107    
108            assertTrue("the document should be final", doc.isFinal());
109        }
110    }