<commit_before>/*
 * Copyright 2005-2007 The Kuali Foundation.
 * 
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.iu.uis.eden.actions;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.kuali.workflow.test.KEWTestCase;

import edu.iu.uis.eden.DocumentRouteLevelChange;
import edu.iu.uis.eden.EdenConstants;
import edu.iu.uis.eden.KEWServiceLocator;
import edu.iu.uis.eden.actionrequests.ActionRequestValue;
import edu.iu.uis.eden.clientapp.WorkflowDocument;
import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
import edu.iu.uis.eden.doctype.DocumentType;
import edu.iu.uis.eden.doctype.dao.DocumentTypeDAO;
import edu.iu.uis.eden.engine.node.RouteNodeInstance;
import edu.iu.uis.eden.postprocessor.DefaultPostProcessor;
import edu.iu.uis.eden.postprocessor.ProcessDocReport;
import edu.iu.uis.eden.test.TestUtilities;
import edu.iu.uis.eden.util.Utilities;

public class ReturnToPreviousNodeActionTest extends KEWTestCase {
    
	@Test public void testReturnToPreviousSequential() throws Exception {
        
        WorkflowDocument document = new WorkflowDocument(new NetworkIdVO("ewestfal"), SequentialSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
        
        // approve the document to the third node (workflow document 2)
        document = new WorkflowDocument(new NetworkIdVO("bmcgough"), document.getRouteHeaderId());
        assertTrue("bmcgough should have approve.", document.isApprovalRequested());
        document.approve("");
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        assertTrue("rkirkend should have approve.", document.isApprovalRequested());
        document.approve("");
        
        // we should now be at workflow document 2 node with request to pmckown
        assertEquals("Should be at WorkflowDocument2.", SequentialSetup.WORKFLOW_DOCUMENT_2_NODE, document.getNodeNames()[0]);
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        assertTrue("Document should be enroute.", document.stateIsEnroute());
        assertTrue("pmckown should have approve.", document.isApprovalRequested());
        
        // now return the document to the AdHoc node
        document.returnToPreviousNode("", SequentialSetup.ADHOC_NODE);
        
        // there should now be 1 requests, an APPROVE to the initiator, since pmckown took the "return" action, he will not get
        // an FYI generated to him
        List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        boolean isApproveToEwestfal = false;
        for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) iterator.next();
            if (request.getWorkflowUser().getAuthenticationUserId().getId().equals("ewestfal")) {
                assertEquals("Should be approve request.", EdenConstants.ACTION_REQUEST_APPROVE_REQ, request.getActionRequested());
                isApproveToEwestfal = true;
            }
        }
        assertTrue(isApproveToEwestfal);
        assertEquals("Should be 1 requests.", 1, actionRequests.size());

        // Route a new document, and test the notification requests
        document = new WorkflowDocument(new NetworkIdVO("ewestfal"), SequentialSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
        // there should now be 2 requests, one to rkirkend and one to bmcgough
        actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        assertEquals("There should be 2 requests.", 2, actionRequests.size());
        
        // now return to the current node we are on, effectively refreshing it, initiate this action as rkirkend so that bmcgough gets an FYI
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        RouteNodeInstance preReturnNodeInstance = (RouteNodeInstance)KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(document.getRouteHeaderId()).iterator().next();
        document.returnToPreviousNode("", SequentialSetup.WORKFLOW_DOCUMENT_NODE);
        preReturnNodeInstance = KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(preReturnNodeInstance.getRouteNodeInstanceId());
        RouteNodeInstance postReturnNodeInstance = (RouteNodeInstance)KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(document.getRouteHeaderId()).iterator().next();
        
        // check the nodes
        assertFalse("Node instances should be different.", preReturnNodeInstance.getRouteNodeInstanceId().equals(postReturnNodeInstance.getRouteNodeInstanceId()));
        assertEquals("Route nodes should be equal.", preReturnNodeInstance.getRouteNode().getRouteNodeId(), postReturnNodeInstance.getRouteNode().getRouteNodeId());
        // check the relationship between the nodes
        assertEquals("Should have 1 next node.", 1, preReturnNodeInstance.getNextNodeInstances().size());
        assertEquals("Should have 1 previous node.", 1, postReturnNodeInstance.getPreviousNodeInstances().size());
        assertEquals("Should have 0 next node.", 0, postReturnNodeInstance.getNextNodeInstances().size());
        assertEquals("pre node's next node should be the post node.", postReturnNodeInstance.getRouteNodeInstanceId(), ((RouteNodeInstance)preReturnNodeInstance.getNextNodeInstances().iterator().next()).getRouteNodeInstanceId());
        assertEquals("post node's previous node should be the pre node.", preReturnNodeInstance.getRouteNodeInstanceId(), ((RouteNodeInstance)postReturnNodeInstance.getPreviousNodeInstances().iterator().next()).getRouteNodeInstanceId());
        
        // there should now be 3 requests, a new approve to rkirkend and bmcgough and an FYI
        actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        assertEquals("There should be 3 requests.", 3, actionRequests.size());
        boolean isApproveToRkirkend = false;
        boolean isApproveToBmcgough = false;
        boolean isFyiToBmcgough = false;
        for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) iterator.next();
            String netId = request.getWorkflowUser().getAuthenticationUserId().getId(); 
            if (netId.equals("rkirkend")) {
                assertEquals("Should be approve request.", EdenConstants.ACTION_REQUEST_APPROVE_REQ, request.getActionRequested());
                isApproveToRkirkend = true;
            } else if (netId.equals("bmcgough")) {
                if (request.getActionRequested().equals(EdenConstants.ACTION_REQUEST_APPROVE_REQ)) {
                    isApproveToBmcgough = true;
                } else if (request.getActionRequested().equals(EdenConstants.ACTION_REQUEST_FYI_REQ)) {
                    isFyiToBmcgough = true;
                }
                
            }
        }
        assertTrue(isApproveToRkirkend);
        assertTrue(isApproveToBmcgough);
        assertTrue(isFyiToBmcgough);
               
    }
    /**
     * our routing all ignore previous:
     * 
     *          M
     *          S
     *  B1      B2      B3      
     *  B1.1    B2.1    
     *          J
     *          M.1
     *          M.2
     *          M.3
     * 
     * M-WorkflowDocument-bmcgough(A)/rkrirked(A)
     * 
     * S-Split
     * 
     * B1- WorkflowDocument2-B1 -pmckown(A)
     * B1.1- WorkflowDocument3-B1 -jitrue(A)
     * 
     * B2- WorkflowDocument3-B2 -jitrue(A)
     * B2.1- WorkflowDocument2-B2 -pmckown(A)
     * 
     * B3- WorkflowDocument4-B3 -jthomas(A)
     * 
     * J-Join
     * 
     * M.1- WorkflowDocumentFinal -xqi(A)
     * M.2- Acknowledge1 -temay(K)
     * M.3- Acknowledge2 -jhopf(K)
     */
    @Test public void testReturnToPreviousParallel() throws Exception {
    	// set up our own post processor
    	DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(ParallelSetup.DOCUMENT_TYPE_NAME);
    	docType.setPostProcessorName(ReturnToPreviousPostProcessor.class.getName());
    	//side step the normal validation of the service
    	((DocumentTypeDAO)KEWServiceLocator.getService("enDocumentTypeDAO")).save(docType);
    	
    	// route a document
        WorkflowDocument document = new WorkflowDocument(new NetworkIdVO("ewestfal"), ParallelSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
                
        //M branch
        document = new WorkflowDocument(new NetworkIdVO("bmcgough"), document.getRouteHeaderId());
        document.approve("");
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        document.approve("");
        
        assertAtNodes(document.getRouteHeaderId(), new String[] { ParallelSetup.WORKFLOW_DOCUMENT_2_B1_NODE, ParallelSetup.WORKFLOW_DOCUMENT_3_B2_NODE, ParallelSetup.WORKFLOW_DOCUMENT_4_B3_NODE});
        assertInBranches(document.getRouteHeaderId(), new String[] { "B1", "B2", "B3" });
        assertAllBranchesSameParent(document.getRouteHeaderId(), "PRIMARY");
        
        // assert that the proper parties have the document
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        document = new WorkflowDocument(new NetworkIdVO("jitrue"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        document = new WorkflowDocument(new NetworkIdVO("jthomas"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        
        // Approve as pmckown on B1 to transition to next node in B1, this is where we'll return from
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        document.approve("");
   
        // now assert that the document is in the proper state
        assertAtNodes(document.getRouteHeaderId(), new String[] { ParallelSetup.WORKFLOW_DOCUMENT_3_B1_NODE, ParallelSetup.WORKFLOW_DOCUMENT_3_B2_NODE, ParallelSetup.WORKFLOW_DOCUMENT_4_B3_NODE});<commit_msg>Removed a direct dao call from the ReturnToPreviousNodeActionTest which might have been causing Atomikos to be unhappy.<commit_after>/*
 * Copyright 2005-2007 The Kuali Foundation.
 * 
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.iu.uis.eden.actions;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.kuali.workflow.test.KEWTestCase;

import edu.iu.uis.eden.DocumentRouteLevelChange;
import edu.iu.uis.eden.EdenConstants;
import edu.iu.uis.eden.KEWServiceLocator;
import edu.iu.uis.eden.actionrequests.ActionRequestValue;
import edu.iu.uis.eden.clientapp.WorkflowDocument;
import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
import edu.iu.uis.eden.doctype.DocumentType;
import edu.iu.uis.eden.engine.node.RouteNodeInstance;
import edu.iu.uis.eden.postprocessor.DefaultPostProcessor;
import edu.iu.uis.eden.postprocessor.ProcessDocReport;
import edu.iu.uis.eden.test.TestUtilities;
import edu.iu.uis.eden.util.Utilities;

public class ReturnToPreviousNodeActionTest extends KEWTestCase {
    
	@Test public void testReturnToPreviousSequential() throws Exception {
        
        WorkflowDocument document = new WorkflowDocument(new NetworkIdVO("ewestfal"), SequentialSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
        
        // approve the document to the third node (workflow document 2)
        document = new WorkflowDocument(new NetworkIdVO("bmcgough"), document.getRouteHeaderId());
        assertTrue("bmcgough should have approve.", document.isApprovalRequested());
        document.approve("");
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        assertTrue("rkirkend should have approve.", document.isApprovalRequested());
        document.approve("");
        
        // we should now be at workflow document 2 node with request to pmckown
        assertEquals("Should be at WorkflowDocument2.", SequentialSetup.WORKFLOW_DOCUMENT_2_NODE, document.getNodeNames()[0]);
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        assertTrue("Document should be enroute.", document.stateIsEnroute());
        assertTrue("pmckown should have approve.", document.isApprovalRequested());
        
        // now return the document to the AdHoc node
        document.returnToPreviousNode("", SequentialSetup.ADHOC_NODE);
        
        // there should now be 1 requests, an APPROVE to the initiator, since pmckown took the "return" action, he will not get
        // an FYI generated to him
        List actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        boolean isApproveToEwestfal = false;
        for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) iterator.next();
            if (request.getWorkflowUser().getAuthenticationUserId().getId().equals("ewestfal")) {
                assertEquals("Should be approve request.", EdenConstants.ACTION_REQUEST_APPROVE_REQ, request.getActionRequested());
                isApproveToEwestfal = true;
            }
        }
        assertTrue(isApproveToEwestfal);
        assertEquals("Should be 1 requests.", 1, actionRequests.size());

        // Route a new document, and test the notification requests
        document = new WorkflowDocument(new NetworkIdVO("ewestfal"), SequentialSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
        // there should now be 2 requests, one to rkirkend and one to bmcgough
        actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        assertEquals("There should be 2 requests.", 2, actionRequests.size());
        
        // now return to the current node we are on, effectively refreshing it, initiate this action as rkirkend so that bmcgough gets an FYI
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        RouteNodeInstance preReturnNodeInstance = (RouteNodeInstance)KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(document.getRouteHeaderId()).iterator().next();
        document.returnToPreviousNode("", SequentialSetup.WORKFLOW_DOCUMENT_NODE);
        preReturnNodeInstance = KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(preReturnNodeInstance.getRouteNodeInstanceId());
        RouteNodeInstance postReturnNodeInstance = (RouteNodeInstance)KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(document.getRouteHeaderId()).iterator().next();
        
        // check the nodes
        assertFalse("Node instances should be different.", preReturnNodeInstance.getRouteNodeInstanceId().equals(postReturnNodeInstance.getRouteNodeInstanceId()));
        assertEquals("Route nodes should be equal.", preReturnNodeInstance.getRouteNode().getRouteNodeId(), postReturnNodeInstance.getRouteNode().getRouteNodeId());
        // check the relationship between the nodes
        assertEquals("Should have 1 next node.", 1, preReturnNodeInstance.getNextNodeInstances().size());
        assertEquals("Should have 1 previous node.", 1, postReturnNodeInstance.getPreviousNodeInstances().size());
        assertEquals("Should have 0 next node.", 0, postReturnNodeInstance.getNextNodeInstances().size());
        assertEquals("pre node's next node should be the post node.", postReturnNodeInstance.getRouteNodeInstanceId(), ((RouteNodeInstance)preReturnNodeInstance.getNextNodeInstances().iterator().next()).getRouteNodeInstanceId());
        assertEquals("post node's previous node should be the pre node.", preReturnNodeInstance.getRouteNodeInstanceId(), ((RouteNodeInstance)postReturnNodeInstance.getPreviousNodeInstances().iterator().next()).getRouteNodeInstanceId());
        
        // there should now be 3 requests, a new approve to rkirkend and bmcgough and an FYI
        actionRequests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByRouteHeaderId(document.getRouteHeaderId());
        assertEquals("There should be 3 requests.", 3, actionRequests.size());
        boolean isApproveToRkirkend = false;
        boolean isApproveToBmcgough = false;
        boolean isFyiToBmcgough = false;
        for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) iterator.next();
            String netId = request.getWorkflowUser().getAuthenticationUserId().getId(); 
            if (netId.equals("rkirkend")) {
                assertEquals("Should be approve request.", EdenConstants.ACTION_REQUEST_APPROVE_REQ, request.getActionRequested());
                isApproveToRkirkend = true;
            } else if (netId.equals("bmcgough")) {
                if (request.getActionRequested().equals(EdenConstants.ACTION_REQUEST_APPROVE_REQ)) {
                    isApproveToBmcgough = true;
                } else if (request.getActionRequested().equals(EdenConstants.ACTION_REQUEST_FYI_REQ)) {
                    isFyiToBmcgough = true;
                }
                
            }
        }
        assertTrue(isApproveToRkirkend);
        assertTrue(isApproveToBmcgough);
        assertTrue(isFyiToBmcgough);
               
    }
    /**
     * our routing all ignore previous:
     * 
     *          M
     *          S
     *  B1      B2      B3      
     *  B1.1    B2.1    
     *          J
     *          M.1
     *          M.2
     *          M.3
     * 
     * M-WorkflowDocument-bmcgough(A)/rkrirked(A)
     * 
     * S-Split
     * 
     * B1- WorkflowDocument2-B1 -pmckown(A)
     * B1.1- WorkflowDocument3-B1 -jitrue(A)
     * 
     * B2- WorkflowDocument3-B2 -jitrue(A)
     * B2.1- WorkflowDocument2-B2 -pmckown(A)
     * 
     * B3- WorkflowDocument4-B3 -jthomas(A)
     * 
     * J-Join
     * 
     * M.1- WorkflowDocumentFinal -xqi(A)
     * M.2- Acknowledge1 -temay(K)
     * M.3- Acknowledge2 -jhopf(K)
     */
    @Test public void testReturnToPreviousParallel() throws Exception {
    	// set up our own post processor
    	DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(ParallelSetup.DOCUMENT_TYPE_NAME);
    	docType.setPostProcessorName(ReturnToPreviousPostProcessor.class.getName());
    	//side step the normal validation of the service
    	//((DocumentTypeDAO)KEWServiceLocator.getService("enDocumentTypeDAO")).save(docType);
    	KEWServiceLocator.getDocumentTypeService().save(docType);
    	
    	// route a document
        WorkflowDocument document = new WorkflowDocument(new NetworkIdVO("ewestfal"), ParallelSetup.DOCUMENT_TYPE_NAME);
        document.routeDocument("");
                
        //M branch
        document = new WorkflowDocument(new NetworkIdVO("bmcgough"), document.getRouteHeaderId());
        document.approve("");
        document = new WorkflowDocument(new NetworkIdVO("rkirkend"), document.getRouteHeaderId());
        document.approve("");
        
        assertAtNodes(document.getRouteHeaderId(), new String[] { ParallelSetup.WORKFLOW_DOCUMENT_2_B1_NODE, ParallelSetup.WORKFLOW_DOCUMENT_3_B2_NODE, ParallelSetup.WORKFLOW_DOCUMENT_4_B3_NODE});
        assertInBranches(document.getRouteHeaderId(), new String[] { "B1", "B2", "B3" });
        assertAllBranchesSameParent(document.getRouteHeaderId(), "PRIMARY");
        
        // assert that the proper parties have the document
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        document = new WorkflowDocument(new NetworkIdVO("jitrue"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        document = new WorkflowDocument(new NetworkIdVO("jthomas"), document.getRouteHeaderId());
        assertTrue(document.isApprovalRequested());
        
        // Approve as pmckown on B1 to transition to next node in B1, this is where we'll return from
        document = new WorkflowDocument(new NetworkIdVO("pmckown"), document.getRouteHeaderId());
        document.approve("");
   
        // now assert that the document is in the proper state
        assertAtNodes(document.getRouteHeaderId(), new String[] { ParallelSetup.WORKFLOW_DOCUMENT_3_B1_NODE, ParallelSetup.WORKFLOW_DOCUMENT_3_B2_NODE, ParallelSetup.WORKFLOW_DOCUMENT_4_B3_NODE});
{'fraction_non_alphanumeric': 0.09899321215434377, 'fraction_numerical': 0.007288961778506674, 'mean_word_length': 3.5057471264367814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6348818', 'n_tokens_mistral': 6111, 'n_tokens_neox': 6001, 'n_words': 1334}