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 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 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039public class UniversityIdRoleAttributeTest extends KEWTestCase {
040    
041    private static final String UNIVERSITY_ID_PROP = "universityId";
042    
043    @Test
044    public void testUniversityIdAttribute() throws Exception {
045        loadXmlFile("UniversityIdRoleAttributeTestConfig.xml");
046        
047        // network id to university id mapping as defined in DefaultSuiteTestData.xml
048        // -----------------------------------
049        // ewestfal     ->     2001
050        // rkirkend     ->     2002
051        // bmcgough     ->     2004
052        
053        WorkflowDocument document = WorkflowDocumentFactory.createDocument("2001", "UniversityIdRoleAttributeTest");
054        
055        WorkflowAttributeDefinition.Builder universityIdDef1 = WorkflowAttributeDefinition.Builder.create("UniversityIdRoleAttribute");
056        PropertyDefinition universityIdProp1 = PropertyDefinition.create(UNIVERSITY_ID_PROP, "2002");
057        universityIdDef1.addPropertyDefinition(universityIdProp1);
058        
059        WorkflowAttributeDefinition.Builder universityIdDef2 = WorkflowAttributeDefinition.Builder.create("UniversityIdRoleAttribute");
060        PropertyDefinition universityIdProp2 = PropertyDefinition.create(UNIVERSITY_ID_PROP, "2004");
061        universityIdDef2.addPropertyDefinition(universityIdProp2);
062        
063        document.addAttributeDefinition(universityIdDef1.build());
064        document.addAttributeDefinition(universityIdDef2.build());
065        
066        document.route("Routing!");
067        
068        // load the document as rkirkend
069        
070        document = WorkflowDocumentFactory.loadDocument("2002", document.getDocumentId());
071        assertTrue("Document should be ENROUTE", document.isEnroute());
072        assertTrue("rkirkend should have an approve request.", document.isApprovalRequested());
073        
074        // load the document as bmcgough
075        document = WorkflowDocumentFactory.loadDocument("2004", document.getDocumentId());
076        assertTrue("bmcgough should have an approve request.", document.isApprovalRequested());
077        
078        // submit an approve as bmcgough
079        document.approve("i approve");
080        
081        // reload as rkirkend, verify still enroute
082        document = WorkflowDocumentFactory.loadDocument("2002", document.getDocumentId());
083        assertTrue("Document should be ENROUTE", document.isEnroute());
084        assertTrue("rkirkend should have an approve request.", document.isApprovalRequested());
085        document.approve("i also approve");
086        
087        // now the document should be FINAL
088        assertTrue("Document should be FINAL", document.isFinal());
089        
090    }
091    
092    @Test
093    public void testParameterizedUniversityIdAttribute() throws Exception {
094        loadXmlFile("ParameterizedUniversityIdRoleAttributeTestConfig.xml");
095        
096        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "UniversityIdRoleAttributeTest");
097        
098        WorkflowAttributeDefinition.Builder univIdDef1 = WorkflowAttributeDefinition.Builder.create("UniversityIdRoleAttribute");
099        PropertyDefinition univIdProp1 = PropertyDefinition.create(UNIVERSITY_ID_PROP, "2002");
100        univIdDef1.addPropertyDefinition(univIdProp1);
101                
102        document.addAttributeDefinition(univIdDef1.build());
103        
104        document.route("Routing!");
105        
106        // load the document as rkirkend
107        
108        document = WorkflowDocumentFactory.loadDocument("2002", document.getDocumentId());
109        assertTrue("Document should be ENROUTE", document.isEnroute());
110        assertTrue("rkirkend should have an approve request.", document.isApprovalRequested());
111        
112        // now let's verify the XML
113        
114        XPath xPath = XPathHelper.newXPath();
115        assertTrue("Should have found the ID.", (Boolean)xPath.evaluate("//UniversityIdRoleAttribute/thisIdRocks", new InputSource(new StringReader(document.getDocumentContent().getFullContent())), XPathConstants.BOOLEAN));
116        
117    }
118
119}