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.actions;
017
018import static org.junit.Assert.assertTrue;
019import static org.junit.Assert.fail;
020
021import java.util.Collection;
022import java.util.Iterator;
023import java.util.List;
024
025import org.junit.Test;
026import org.kuali.rice.kew.actionrequest.ActionRequestValue;
027import org.kuali.rice.kew.actions.BlanketApproveTest.NotifySetup;
028import org.kuali.rice.kew.api.WorkflowDocument;
029import org.kuali.rice.kew.api.WorkflowDocumentFactory;
030import org.kuali.rice.kew.service.KEWServiceLocator;
031import org.kuali.rice.kew.test.KEWTestCase;
032
033public class RoleTest extends KEWTestCase {
034
035    protected void loadTestData() throws Exception {
036        loadXmlFile("ActionsConfig.xml");
037    }
038
039    @Test public void testRoleRequestGeneration() throws Exception {
040        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
041        document.route("");
042        
043        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
044        assertTrue("This user should have an approve request", document.isApprovalRequested());
045        document.approve("");
046        
047        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
048        assertTrue("This user should have an approve request", document.isApprovalRequested());
049        document.approve("");//ewestfal had force action rule
050        
051        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
052        assertTrue("This user should have an approve request", document.isApprovalRequested());
053        document.approve("");
054        
055        //this be the role delegate of jitrue
056        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("natjohns"), document.getDocumentId());
057        assertTrue("This user should have an approve request", document.isApprovalRequested());
058        document.approve("");
059        
060        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
061        document.approve("");
062        
063        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("xqi"), document.getDocumentId());
064        document.acknowledge("");
065        
066        assertTrue("Document should be final", document.isFinal());
067
068        List requests = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(document.getDocumentId());
069        List rootRequests = KEWServiceLocator.getActionRequestService().getRootRequests(requests);
070        
071        //verify our requests have been made correctly
072        for (Iterator iter = rootRequests.iterator(); iter.hasNext();) {
073                        ActionRequestValue request = (ActionRequestValue) iter.next();
074                        if (request.isRoleRequest()) {
075                                //direct children should not be role requests
076                                iterateChildrenRequests(request.getChildrenRequests(), new String[] {"U", "W"}, request);
077                        }
078                }
079    }
080        
081    private void iterateChildrenRequests(Collection childrenRequests, String[] requestTypes, ActionRequestValue parentRequest) {
082        for (Iterator iter = childrenRequests.iterator(); iter.hasNext();) {
083                        ActionRequestValue request = (ActionRequestValue) iter.next();
084                        boolean matched = false;
085                        for (int i = 0; i < requestTypes.length; i++) {
086                                if (request.getRecipientTypeCd().equals(requestTypes[i])) {
087                                        matched = true;
088                                }
089                        }
090                        if (!matched) {
091                                fail("Didn't find request of types expected Recipient Type: " + parentRequest.getRecipientTypeCd() + " RoleName: " + parentRequest.getRoleName() + " Qualified Role Name:" + parentRequest.getQualifiedRoleName() + " RuleId: " + parentRequest.getRuleBaseValuesId());
092                        }
093                        //if this is a role then it can't have a child role
094                        if (request.isRoleRequest()) {
095                                //direct children should not be role requests
096                                iterateChildrenRequests(request.getChildrenRequests(), new String[] {"U", "W"}, request);
097                        }
098                }
099    }
100}