View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.rule;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.api.WorkflowDocument;
20  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
21  import org.kuali.rice.kew.api.document.PropertyDefinition;
22  import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
23  import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
24  import org.kuali.rice.kew.test.KEWTestCase;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.xml.sax.InputSource;
27  
28  import javax.xml.xpath.XPath;
29  import javax.xml.xpath.XPathConstants;
30  import java.io.StringReader;
31  
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   * 
38   */
39  public class PrincipalIdRoleAttributeTest extends KEWTestCase {
40  
41  	private static final String ATTRIBUTE_NAME = "PrincipalIdRoleAttribute";
42  	private static final String PRINCIPAL_ID_PROP = "principalId";
43  
44  	@Test
45  	public void testPrincipalIdAttribute() throws Exception {
46  		loadXmlFile("PrincipalIdRoleAttributeTestConfig.xml");
47  
48  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(
49  				"ewestfal"), "PrincipalIdRoleAttributeTest");
50  
51  		WorkflowAttributeDefinition.Builder principalIdDef1 = WorkflowAttributeDefinition.Builder.create(
52  				"PrincipalIdRoleAttribute");
53  		PropertyDefinition principalIdProp1 = PropertyDefinition.create(
54  				PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("rkirkend").getPrincipalId());
55  		principalIdDef1.addPropertyDefinition(principalIdProp1);
56  
57  		WorkflowAttributeDefinition.Builder principalIdDef2 = WorkflowAttributeDefinition.Builder.create(
58  				"PrincipalIdRoleAttribute");
59  		PropertyDefinition principalIdProp2 = PropertyDefinition.create(
60  				PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("bmcgough").getPrincipalId());
61  		principalIdDef2.addPropertyDefinition(principalIdProp2);
62  
63  		document.addAttributeDefinition(principalIdDef1.build());
64  		document.addAttributeDefinition(principalIdDef2.build());
65  
66  		document.route("Routing!");
67  
68  		// load the document as rkirkend
69  
70  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
71  				.getDocumentId());
72  		assertTrue("Document should be ENROUTE", document.isEnroute());
73  		assertTrue("rkirkend should have an approve request.", document
74  				.isApprovalRequested());
75  
76  		// load the document as bmcgough
77  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document
78  				.getDocumentId());
79  		assertTrue("bmcgough should have an approve request.", document
80  				.isApprovalRequested());
81  
82  		// submit an approve as bmcgough
83  		document.approve("i approve");
84  
85  		// reload as rkirkend, verify still enroute
86  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
87  				.getDocumentId());
88  		assertTrue("Document should be ENROUTE", document.isEnroute());
89  		assertTrue("rkirkend should have an approve request.", document
90  				.isApprovalRequested());
91  		document.approve("i also approve");
92  
93  		// now the document should be FINAL
94  		assertTrue("Document should be FINAL", document.isFinal());
95  
96  	}
97  
98  	@Test
99  	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 }