View Javadoc

1   /**
2    * Copyright 2005-2011 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   * This is a description of what this class does - ewestfal don't forget to fill
36   * this in.
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   * 
40   */
41  public class PrincipalIdRoleAttributeTest extends KEWTestCase {
42  
43  	private static final String ATTRIBUTE_NAME = "PrincipalIdRoleAttribute";
44  	private static final String PRINCIPAL_ID_PROP = "principalId";
45  
46  	@Test
47  	public void testPrincipalIdAttribute() throws Exception {
48  		loadXmlFile("PrincipalIdRoleAttributeTestConfig.xml");
49  
50  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(
51  				"ewestfal"), "PrincipalIdRoleAttributeTest");
52  
53  		WorkflowAttributeDefinition.Builder principalIdDef1 = WorkflowAttributeDefinition.Builder.create(
54  				"PrincipalIdRoleAttribute");
55  		PropertyDefinition principalIdProp1 = PropertyDefinition.create(
56  				PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("rkirkend").getPrincipalId());
57  		principalIdDef1.addPropertyDefinition(principalIdProp1);
58  
59  		WorkflowAttributeDefinition.Builder principalIdDef2 = WorkflowAttributeDefinition.Builder.create(
60  				"PrincipalIdRoleAttribute");
61  		PropertyDefinition principalIdProp2 = PropertyDefinition.create(
62  				PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("bmcgough").getPrincipalId());
63  		principalIdDef2.addPropertyDefinition(principalIdProp2);
64  
65  		document.addAttributeDefinition(principalIdDef1.build());
66  		document.addAttributeDefinition(principalIdDef2.build());
67  
68  		document.route("Routing!");
69  
70  		// load the document as rkirkend
71  
72  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
73  				.getDocumentId());
74  		assertTrue("Document should be ENROUTE", document.isEnroute());
75  		assertTrue("rkirkend should have an approve request.", document
76  				.isApprovalRequested());
77  
78  		// load the document as bmcgough
79  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document
80  				.getDocumentId());
81  		assertTrue("bmcgough should have an approve request.", document
82  				.isApprovalRequested());
83  
84  		// submit an approve as bmcgough
85  		document.approve("i approve");
86  
87  		// reload as rkirkend, verify still enroute
88  		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
89  				.getDocumentId());
90  		assertTrue("Document should be ENROUTE", document.isEnroute());
91  		assertTrue("rkirkend should have an approve request.", document
92  				.isApprovalRequested());
93  		document.approve("i also approve");
94  
95  		// now the document should be FINAL
96  		assertTrue("Document should be FINAL", document.isFinal());
97  
98  	}
99  
100 	@Test
101 	public void testParameterizedPrincipalIdAttribute() throws Exception {
102 		loadXmlFile("ParameterizedPrincipalIdRoleAttributeTestConfig.xml");
103 
104 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(
105 				"ewestfal"), "PrincipalIdRoleAttributeTest");
106 
107 		WorkflowAttributeDefinition.Builder principalIdDef1 = WorkflowAttributeDefinition.Builder.create(
108 				"PrincipalIdRoleAttribute");
109 		PropertyDefinition principalIdProp1 = PropertyDefinition.create(
110 				PRINCIPAL_ID_PROP, KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("rkirkend").getPrincipalId());
111 		principalIdDef1.addPropertyDefinition(principalIdProp1);
112 
113 		document.addAttributeDefinition(principalIdDef1.build());
114 
115 		document.route("Routing!");
116 
117 		// load the document as rkirkend
118 
119 		document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document
120 				.getDocumentId());
121 		assertTrue("Document should be ENROUTE", document.isEnroute());
122 		assertTrue("rkirkend should have an approve request.", document
123 				.isApprovalRequested());
124 
125 		// now let's verify the XML
126 
127 		XPath xPath = XPathHelper.newXPath();
128 		assertTrue("Should have found the ID.", (Boolean) xPath.evaluate(
129 				"//" + ATTRIBUTE_NAME + "/thisIdRocks", new InputSource(
130 						new StringReader(document.getDocumentContent()
131 								.getFullContent())), XPathConstants.BOOLEAN));
132 
133 	}
134 
135 }