001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kew.clientapp;
017
018import org.junit.Test;
019import org.kuali.rice.kew.api.WorkflowDocument;
020import org.kuali.rice.kew.api.WorkflowDocumentFactory;
021import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
022import org.kuali.rice.kew.test.KEWTestCase;
023
024import java.util.List;
025import java.util.Set;
026
027import static org.junit.Assert.*;
028
029/**
030 * Place to test WorkflowDocument.
031 *
032 */
033public class WorkflowDocumentTest extends KEWTestCase {
034
035    protected void loadTestData() throws Exception {
036        loadXmlFile("ClientAppConfig.xml");
037    }
038
039    @Test public void testDirtyContent() {
040        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
041        document.setApplicationContent("application content");
042        document.setAttributeContent("attribute content");
043        document.setSearchableContent("searchable content");
044        assertEquals("application content", document.getApplicationContent());
045        assertEquals("application content", document.getDocumentContent().getApplicationContent());
046        assertEquals("attribute content", document.getAttributeContent());
047        assertEquals("attribute content", document.getDocumentContent().getAttributeContent());
048        assertEquals("searchable content", document.getDocumentContent().getSearchableContent());
049    }
050
051    @Test public void testLoadNonExistentDocument() throws Exception {
052        try {
053                WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), "123456789");
054                fail("load of non-existent document should have thrown IllegalArgumentException");
055        } catch (IllegalArgumentException e) {}
056    }
057
058    @Test public void testWorkflowDocument() throws Exception {
059        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
060        document.route("");
061
062        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
063        document.approve("");
064
065        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
066        document.approve("");
067
068        boolean containsInitiated = false;
069        boolean containsTemplate1 = false;
070        boolean containsTemplate2 = false;
071        for (RouteNodeInstance routeNodeInstance : document.getRouteNodeInstances()) {
072            if (routeNodeInstance.getName().equals("Initiated")) {
073                containsInitiated = true;
074            } else if (routeNodeInstance.getName().equals("Template1")) {
075                containsTemplate1 = true;
076            } else if (routeNodeInstance.getName().equals("Template2")) {
077                containsTemplate2 = true;
078            }
079        }
080
081        assertTrue("Should have gone through initiated node", containsInitiated);
082        assertTrue("Should have gone through template1 node", containsTemplate1);
083        assertTrue("Should have gone through template2 node", containsTemplate2);
084    }
085
086    /**
087     * Test that the document is being updated appropriately after a return to previous call
088     *
089     * @throws Exception
090     */
091    @Test public void testReturnToPreviousCorrectlyUpdatingDocumentStatus() throws Exception {
092
093        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
094        document.route("");
095
096        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
097        document.returnToPreviousNode("", "Initiated");
098
099        assertFalse("ewestfal should no longer have approval status", document.isApprovalRequested());
100        assertFalse("ewestfal should no long have blanket approve status", document.isBlanketApproveCapable());
101
102        //just for good measure
103        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
104        assertTrue("rkirkend should now have an approve request", document.isApprovalRequested());
105    }
106
107    @Test public void testGetPreviousRouteNodeNames() throws Exception {
108
109        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
110        document.route("");
111
112        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
113        document.approve("");
114
115        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
116        List<String> previousNodeNames = document.getPreviousNodeNames();
117        assertEquals("Should have 2 previous Node Names", 2, previousNodeNames.size());
118        assertEquals("Last node name should be the first visisted", "Initiated", previousNodeNames.get(0));
119        assertEquals("First node name should be last node visited", "Template1", previousNodeNames.get(1));
120        Set<String> currentNodes = document.getNodeNames();
121        assertEquals("Should have 1 current node name", 1, currentNodes.size());
122        assertEquals("Current node name incorrect", "Template2", currentNodes.iterator().next());
123        document.returnToPreviousNode("", "Template1");
124        previousNodeNames = document.getPreviousNodeNames();
125        assertEquals("Should have 1 previous Node Name", 1, previousNodeNames.size());
126        assertEquals("Previous Node name incorrect", "Initiated", previousNodeNames.get(0));
127
128    }
129
130    @Test public void testIsRouteCapable() throws Exception {
131
132        WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "UnitTestDocument");
133
134        verifyIsRouteCapable(false, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
135        verifyIsRouteCapable(false, "2001", doc.getDocumentId());
136
137        verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
138        verifyIsRouteCapable(true, "2002", doc.getDocumentId());
139
140        doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "NonInitiatorCanRouteDocument");
141
142        verifyIsRouteCapable(true, getPrincipalIdForName("ewestfal"), doc.getDocumentId());
143        verifyIsRouteCapable(true, "2001", doc.getDocumentId());
144
145        verifyIsRouteCapable(true, getPrincipalIdForName("rkirkend"), doc.getDocumentId());
146        verifyIsRouteCapable(true, "2002", doc.getDocumentId());
147    }
148
149    private void verifyIsRouteCapable(boolean routeCapable, String userId, String docId) throws Exception {
150        WorkflowDocument doc = WorkflowDocumentFactory.loadDocument(userId, docId);
151        assertEquals(routeCapable, doc.isRouteCapable());
152    }
153
154}