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