View Javadoc

1   /*
2    * Copyright 2007-2009 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.role;
17  
18  import java.util.List;
19  
20  import org.junit.Test;
21  import org.kuali.rice.kew.engine.RouteContext;
22  import org.kuali.rice.kew.routeheader.DocumentContent;
23  import org.kuali.rice.kew.routeheader.StandardDocumentContent;
24  import org.kuali.rice.kew.rule.bo.RuleAttribute;
25  import org.kuali.rice.kim.bo.types.dto.AttributeSet;
26  import org.kuali.rice.test.BaseRiceTestCase;
27  
28  /**
29   * Tests the XPathQualifierResolver.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public class XPathQualifierResolverTest extends BaseRiceTestCase {
35  
36  	private static final String SIMPLE_CONFIG_1 = "<resolverConfig>" +
37  		"<baseXPathExpression>/xmlData/accountNumbers</baseXPathExpression>" +
38  		"<qualifier name=\"accountNumber\">" +
39  		"<xPathExpression>./accountNumber</xPathExpression>" +
40  		"</qualifier>" +
41  		"</resolverConfig>";
42  	
43  	private static final String SIMPLE_DOC_XML_1 = "<xmlData>" +
44  		"<accountNumbers>" +
45  		"<accountNumber>12345</accountNumber>" +
46  		"<accountNumber>54321</accountNumber>" +
47  		"<accountNumber>102030</accountNumber>" +
48  		"<accountNumber>302010</accountNumber>" +
49  		"</accountNumbers>" +
50  		"</xmlData>";
51  	
52  	private static final String SIMPLE_CONFIG_2 = "<resolverConfig>" +
53  	"<qualifier name=\"accountNumber\">" +
54  	"<xPathExpression>//accountNumber</xPathExpression>" +
55  	"</qualifier>" +
56  	"</resolverConfig>";
57  	
58  	private static final String COMPOUND_CONFIG_1 = "<resolverConfig>" +
59  		"<baseXPathExpression>/xmlData/chartOrg</baseXPathExpression>" +
60  		"<qualifier name=\"chart\">" +
61  		"<xPathExpression>./chart</xPathExpression>" +
62  		"</qualifier>" +
63  		"<qualifier name=\"org\">" +
64  		"<xPathExpression>./org</xPathExpression>" +
65  		"</qualifier>" +
66  		"</resolverConfig>";
67  
68  	private static final String COMPOUND_CONFIG_2 = "<resolverConfig>" +
69  		"<qualifier name=\"chart\">" +
70  		"<xPathExpression>//chart</xPathExpression>" +
71  		"</qualifier>" +
72  		"<qualifier name=\"org\">" +
73  		"<xPathExpression>//org</xPathExpression>" +
74  		"</qualifier>" +
75  		"</resolverConfig>";
76  
77  	
78  	private static final String COMPOUND_DOC_XML_1 =  "<xmlData>" +
79  	 	"<chartOrg>" +
80  	 	"<chart>BL</chart>" +
81  	 	"<org>BUS</org>" +
82  	 	"</chartOrg>" +
83  	 	"<chartOrg>" +
84  	 	"<chart>IN</chart>" +
85  	 	"<org>MED</org>" +
86  	 	"</chartOrg>" +
87  	 	"</xmlData>";
88  	
89  	private static final String COMPOUND_DOC_XML_2 =  "<xmlData>" +
90  	 	"<chartOrg>" +
91  	 	"<chart>BL</chart>" +
92  	 	"<org>BUS</org>" +
93  	 	"<chart>IN</chart>" +
94  	 	"<org>MED</org>" +
95  	 	"</chartOrg>" +
96  	 	"</xmlData>";
97  	
98  	private static final String COMPOUND_DOC_XML_3 =  "<xmlData>" +
99   		"<chartOrg>" +
100  		"<chart>BL</chart>" +
101  		"<org>BUS</org>" +
102  		"</chartOrg>" +
103  		"</xmlData>";
104 	
105 	@Test
106 	public void testResolve_simpleAttributeSet() throws Exception {
107 		XPathQualifierResolver resolver = new XPathQualifierResolver();
108 		
109 		RuleAttribute ruleAttribute = new RuleAttribute();
110 		ruleAttribute.setXmlConfigData(SIMPLE_CONFIG_1);
111 		resolver.setRuleAttribute(ruleAttribute);
112 		
113 		RouteContext context = new RouteContext();
114 		DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
115 		context.setDocumentContent(docContent);
116 		
117 		List<AttributeSet> attributeSets = resolver.resolve(context);
118 		verifyAccountAttributeSets(attributeSets);
119 	}
120 	
121 	@Test
122 	public void testResolve_simpleAttributeSet_noBaseXPath() throws Exception {
123 		XPathQualifierResolver resolver = new XPathQualifierResolver();
124 		
125 		RuleAttribute ruleAttribute = new RuleAttribute();
126 		ruleAttribute.setXmlConfigData(SIMPLE_CONFIG_2);
127 		resolver.setRuleAttribute(ruleAttribute);
128 		
129 		RouteContext context = new RouteContext();
130 		DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
131 		context.setDocumentContent(docContent);
132 		
133 		List<AttributeSet> attributeSets = resolver.resolve(context);
134 		verifyAccountAttributeSets(attributeSets);
135 		
136 	}
137 	
138 	private void verifyAccountAttributeSets(List<AttributeSet> attributeSets) {
139 		assertEquals("Incorrect number of attribute sets.", 4, attributeSets.size());
140 		
141 		String acctNumKey = "accountNumber";
142 		AttributeSet attributeSet1 = attributeSets.get(0);
143 		assertEquals(1, attributeSet1.size());
144 		assertEquals("12345", attributeSet1.get(acctNumKey));
145 		AttributeSet attributeSet2 = attributeSets.get(1);
146 		assertEquals(1, attributeSet2.size());
147 		assertEquals("54321", attributeSet2.get(acctNumKey));
148 		AttributeSet attributeSet3 = attributeSets.get(2);
149 		assertEquals(1, attributeSet3.size());
150 		assertEquals("102030", attributeSet3.get(acctNumKey));
151 		AttributeSet attributeSet4 = attributeSets.get(3);
152 		assertEquals(1, attributeSet4.size());
153 		assertEquals("302010", attributeSet4.get(acctNumKey));
154 	}
155 	
156 	@Test
157 	public void testResolve_compoundAttributeSet() throws Exception {
158 		XPathQualifierResolver resolver = new XPathQualifierResolver();
159 		
160 		RuleAttribute ruleAttribute = new RuleAttribute();
161 		ruleAttribute.setXmlConfigData(COMPOUND_CONFIG_1);
162 		resolver.setRuleAttribute(ruleAttribute);
163 		
164 		RouteContext context = new RouteContext();
165 		DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_1);
166 		context.setDocumentContent(docContent);
167 		
168 		List<AttributeSet> attributeSets = resolver.resolve(context);
169 		assertEquals("Incorrect number of attribute sets", 2, attributeSets.size());
170 		
171 		String chartKey = "chart";
172 		String orgKey = "org";
173 		
174 		AttributeSet attributeSet1 = attributeSets.get(0);
175 		assertEquals(2, attributeSet1.size());
176 		assertEquals("BL", attributeSet1.get(chartKey));
177 		assertEquals("BUS", attributeSet1.get(orgKey));
178 		
179 		AttributeSet attributeSet2 = attributeSets.get(1);
180 		assertEquals(2, attributeSet2.size());
181 		assertEquals("IN", attributeSet2.get(chartKey));
182 		assertEquals("MED", attributeSet2.get(orgKey));
183 	}
184 	
185 	@Test
186 	public void testResolve_compoundAttributeSet_badXml() throws Exception {
187 		XPathQualifierResolver resolver = new XPathQualifierResolver();
188 		
189 		RuleAttribute ruleAttribute = new RuleAttribute();
190 		ruleAttribute.setXmlConfigData(COMPOUND_CONFIG_1);
191 		resolver.setRuleAttribute(ruleAttribute);
192 		
193 		RouteContext context = new RouteContext();
194 		DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
195 		context.setDocumentContent(docContent);
196 		
197 		// should fail with this document content
198 		
199 		try {
200 			resolver.resolve(context);
201 			fail("Invalid XML was encountered, resolver should have thrown an exception");
202 		} catch (Exception e) {
203 			e.printStackTrace();
204 		}
205 		
206 		// however, should succeed with this
207 		
208 		context = new RouteContext();
209 		docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
210 		context.setDocumentContent(docContent);
211 		
212 		List<AttributeSet> attributeSets = resolver.resolve(context);
213 		assertEquals(1, attributeSets.size());
214 		assertEquals("BL", attributeSets.get(0).get("chart"));
215 		assertEquals("BUS", attributeSets.get(0).get("org"));
216 	}
217 	
218 	@Test
219 	public void testResolve_compoundAttributeSet_noBaseXPath() throws Exception {
220 		XPathQualifierResolver resolver = new XPathQualifierResolver();
221 		
222 		RuleAttribute ruleAttribute = new RuleAttribute();
223 		ruleAttribute.setXmlConfigData(COMPOUND_CONFIG_2);
224 		resolver.setRuleAttribute(ruleAttribute);
225 		
226 		RouteContext context = new RouteContext();
227 		DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
228 		context.setDocumentContent(docContent);
229 		
230 		// should fail with this document content
231 		
232 		try {
233 			resolver.resolve(context);
234 			fail("Invalid XML was encountered, resolver should have thrown an exception");
235 		} catch (Exception e) {
236 			e.printStackTrace();
237 		}
238 		
239 		// however, should succeed with this
240 		
241 		context = new RouteContext();
242 		docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
243 		context.setDocumentContent(docContent);
244 		
245 		List<AttributeSet> attributeSets = resolver.resolve(context);
246 		assertEquals(1, attributeSets.size());
247 		assertEquals("BL", attributeSets.get(0).get("chart"));
248 		assertEquals("BUS", attributeSets.get(0).get("org"));
249 		
250 	}
251 	
252 }