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.rule;
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.PropertyDefinition;
022import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
023import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
024import org.kuali.rice.kew.test.KEWTestCase;
025import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026import org.xml.sax.InputSource;
027
028import javax.xml.xpath.XPath;
029import javax.xml.xpath.XPathConstants;
030import java.io.StringReader;
031
032import static org.junit.Assert.assertTrue;
033
034/**
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 * 
038 */
039public class PrincipalIdRoleAttributeTest extends KEWTestCase {
040
041        private static final String ATTRIBUTE_NAME = "PrincipalIdRoleAttribute";
042        private static final String PRINCIPAL_ID_PROP = "principalId";
043
044        @Test
045        public void testPrincipalIdAttribute() throws Exception {
046                loadXmlFile("PrincipalIdRoleAttributeTestConfig.xml");
047
048                WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(
049                                "ewestfal"), "PrincipalIdRoleAttributeTest");
050
051                WorkflowAttributeDefinition.Builder principalIdDef1 = WorkflowAttributeDefinition.Builder.create(
052                                "PrincipalIdRoleAttribute");
053                PropertyDefinition principalIdProp1 = PropertyDefinition.create(
054                                PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("rkirkend").getPrincipalId());
055                principalIdDef1.addPropertyDefinition(principalIdProp1);
056
057                WorkflowAttributeDefinition.Builder principalIdDef2 = WorkflowAttributeDefinition.Builder.create(
058                                "PrincipalIdRoleAttribute");
059                PropertyDefinition principalIdProp2 = PropertyDefinition.create(
060                                PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("bmcgough").getPrincipalId());
061                principalIdDef2.addPropertyDefinition(principalIdProp2);
062
063                document.addAttributeDefinition(principalIdDef1.build());
064                document.addAttributeDefinition(principalIdDef2.build());
065
066                document.route("Routing!");
067
068                // load the document as rkirkend
069
070                document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
071                                .getDocumentId());
072                assertTrue("Document should be ENROUTE", document.isEnroute());
073                assertTrue("rkirkend should have an approve request.", document
074                                .isApprovalRequested());
075
076                // load the document as bmcgough
077                document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document
078                                .getDocumentId());
079                assertTrue("bmcgough should have an approve request.", document
080                                .isApprovalRequested());
081
082                // submit an approve as bmcgough
083                document.approve("i approve");
084
085                // reload as rkirkend, verify still enroute
086                document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
087                                .getDocumentId());
088                assertTrue("Document should be ENROUTE", document.isEnroute());
089                assertTrue("rkirkend should have an approve request.", document
090                                .isApprovalRequested());
091                document.approve("i also approve");
092
093                // now the document should be FINAL
094                assertTrue("Document should be FINAL", document.isFinal());
095
096        }
097
098        @Test
099        public void testParameterizedPrincipalIdAttribute() throws Exception {
100                loadXmlFile("ParameterizedPrincipalIdRoleAttributeTestConfig.xml");
101
102                WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(
103                                "ewestfal"), "PrincipalIdRoleAttributeTest");
104
105                WorkflowAttributeDefinition.Builder principalIdDef1 = WorkflowAttributeDefinition.Builder.create(
106                                "PrincipalIdRoleAttribute");
107                PropertyDefinition principalIdProp1 = PropertyDefinition.create(
108                                PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("rkirkend").getPrincipalId());
109                principalIdDef1.addPropertyDefinition(principalIdProp1);
110
111                document.addAttributeDefinition(principalIdDef1.build());
112
113                document.route("Routing!");
114
115                // load the document as rkirkend
116
117                document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
118                                .getDocumentId());
119                assertTrue("Document should be ENROUTE", document.isEnroute());
120                assertTrue("rkirkend should have an approve request.", document
121                                .isApprovalRequested());
122
123                // now let's verify the XML
124
125                XPath xPath = XPathHelper.newXPath();
126                assertTrue("Should have found the ID.", (Boolean) xPath.evaluate(
127                                "//" + ATTRIBUTE_NAME + "/thisIdRocks", new InputSource(
128                                                new StringReader(document.getDocumentContent()
129                                                                .getFullContent())), XPathConstants.BOOLEAN));
130
131        }
132
133}