1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.role;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.junit.Test;
20 import org.kuali.rice.kew.api.extension.ExtensionDefinition;
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.kew.api.KewApiConstants;
26 import org.kuali.rice.test.BaseRiceTestCase;
27
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.fail;
36
37
38
39
40
41
42
43 public class XPathQualifierResolverTest extends BaseRiceTestCase {
44
45 private static final String SIMPLE_CONFIG_1 = "<resolverConfig>" +
46 "<baseXPathExpression>/xmlData/accountNumbers</baseXPathExpression>" +
47 "<attributes name=\"accountNumber\">" +
48 "<xPathExpression>./accountNumber</xPathExpression>" +
49 "</attributes>" +
50 "</resolverConfig>";
51
52 private static final String SIMPLE_DOC_XML_1 = "<xmlData>" +
53 "<accountNumbers>" +
54 "<accountNumber>12345</accountNumber>" +
55 "<accountNumber>54321</accountNumber>" +
56 "<accountNumber>102030</accountNumber>" +
57 "<accountNumber>302010</accountNumber>" +
58 "</accountNumbers>" +
59 "</xmlData>";
60
61 private static final String SIMPLE_CONFIG_2 = "<resolverConfig>" +
62 "<attributes name=\"accountNumber\">" +
63 "<xPathExpression>//accountNumber</xPathExpression>" +
64 "</attributes>" +
65 "</resolverConfig>";
66
67 private static final String COMPOUND_CONFIG_1 = "<resolverConfig>" +
68 "<baseXPathExpression>/xmlData/chartOrg</baseXPathExpression>" +
69 "<attributes name=\"chart\">" +
70 "<xPathExpression>./chart</xPathExpression>" +
71 "</attributes>" +
72 "<attributes name=\"org\">" +
73 "<xPathExpression>./org</xPathExpression>" +
74 "</attributes>" +
75 "</resolverConfig>";
76
77 private static final String COMPOUND_CONFIG_2 = "<resolverConfig>" +
78 "<attributes name=\"chart\">" +
79 "<xPathExpression>//chart</xPathExpression>" +
80 "</attributes>" +
81 "<attributes name=\"org\">" +
82 "<xPathExpression>//org</xPathExpression>" +
83 "</attributes>" +
84 "</resolverConfig>";
85
86
87 private static final String COMPOUND_DOC_XML_1 = "<xmlData>" +
88 "<chartOrg>" +
89 "<chart>BL</chart>" +
90 "<org>BUS</org>" +
91 "</chartOrg>" +
92 "<chartOrg>" +
93 "<chart>IN</chart>" +
94 "<org>MED</org>" +
95 "</chartOrg>" +
96 "</xmlData>";
97
98 private static final String COMPOUND_DOC_XML_2 = "<xmlData>" +
99 "<chartOrg>" +
100 "<chart>BL</chart>" +
101 "<org>BUS</org>" +
102 "<chart>IN</chart>" +
103 "<org>MED</org>" +
104 "</chartOrg>" +
105 "</xmlData>";
106
107 private static final String COMPOUND_DOC_XML_3 = "<xmlData>" +
108 "<chartOrg>" +
109 "<chart>BL</chart>" +
110 "<org>BUS</org>" +
111 "</chartOrg>" +
112 "</xmlData>";
113
114 @Test
115 public void testResolve_simpleMap() throws Exception {
116
117
118
119
120
121
122 Set<String> configStrings = new HashSet<String>();
123 configStrings.add(SIMPLE_CONFIG_1);
124 configStrings.add(StringUtils.replace(SIMPLE_CONFIG_1, "attributes", "qualifier"));
125
126 for (String currentTestConfig : configStrings){
127 XPathQualifierResolver resolver = new XPathQualifierResolver();
128 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
129 extensionDefinition.setConfiguration(Collections.singletonMap(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, currentTestConfig));
130
131 resolver.setExtensionDefinition(extensionDefinition.build());
132
133 RouteContext context = new RouteContext();
134 DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
135 context.setDocumentContent(docContent);
136
137 List<Map<String, String>> maps = resolver.resolve(context);
138 verifyAccountmaps(maps);
139 }
140
141 }
142
143 @Test
144 public void testResolve_simpleMap_noBaseXPath() throws Exception {
145
146
147
148
149
150
151 Set<String> configStrings = new HashSet<String>();
152 configStrings.add(SIMPLE_CONFIG_2);
153 configStrings.add(StringUtils.replace(SIMPLE_CONFIG_2, "attributes", "qualifier"));
154
155 for (String currentTestConfig : configStrings){
156 XPathQualifierResolver resolver = new XPathQualifierResolver();
157 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
158 extensionDefinition.setConfiguration(Collections.singletonMap(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, currentTestConfig));
159 resolver.setExtensionDefinition(extensionDefinition.build());
160
161 RouteContext context = new RouteContext();
162 DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
163 context.setDocumentContent(docContent);
164
165 List<Map<String, String>> maps = resolver.resolve(context);
166 verifyAccountmaps(maps);
167 }
168 }
169
170 private void verifyAccountmaps(List<Map<String, String>> maps) {
171 assertEquals("Incorrect number of attribute sets.", 4, maps.size());
172
173 String acctNumKey = "accountNumber";
174 Map<String, String> map1 = maps.get(0);
175 assertEquals(1, map1.size());
176 assertEquals("12345", map1.get(acctNumKey));
177 Map<String, String> map2 = maps.get(1);
178 assertEquals(1, map2.size());
179 assertEquals("54321", map2.get(acctNumKey));
180 Map<String, String> map3 = maps.get(2);
181 assertEquals(1, map3.size());
182 assertEquals("102030", map3.get(acctNumKey));
183 Map<String, String> map4 = maps.get(3);
184 assertEquals(1, map4.size());
185 assertEquals("302010", map4.get(acctNumKey));
186 }
187
188 @Test
189 public void testResolve_compoundMap1() throws Exception {
190
191
192
193
194
195
196 Set<String> configStrings = new HashSet<String>();
197 configStrings.add(COMPOUND_CONFIG_1);
198 configStrings.add(StringUtils.replace(COMPOUND_CONFIG_1, "attributes", "qualifier"));
199
200 for (String currentTestConfig : configStrings){
201 XPathQualifierResolver resolver = new XPathQualifierResolver();
202 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
203 extensionDefinition.setConfiguration(Collections.singletonMap(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, currentTestConfig));
204 resolver.setExtensionDefinition(extensionDefinition.build());
205
206 RouteContext context = new RouteContext();
207 DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_1);
208 context.setDocumentContent(docContent);
209
210 List<Map<String, String>> maps = resolver.resolve(context);
211 assertEquals("Incorrect number of attribute sets", 2, maps.size());
212
213 String chartKey = "chart";
214 String orgKey = "org";
215
216 Map<String, String> map1 = maps.get(0);
217 assertEquals(2, map1.size());
218 assertEquals("BL", map1.get(chartKey));
219 assertEquals("BUS", map1.get(orgKey));
220
221 Map<String, String> map2 = maps.get(1);
222 assertEquals(2, map2.size());
223 assertEquals("IN", map2.get(chartKey));
224 assertEquals("MED", map2.get(orgKey));
225 }
226 }
227
228 @Test
229 public void testResolve_compoundMap() throws Exception {
230
231
232
233
234
235
236 Set<String> configStrings = new HashSet<String>();
237 configStrings.add(COMPOUND_CONFIG_1);
238 configStrings.add(StringUtils.replace(COMPOUND_CONFIG_1, "attributes", "qualifier"));
239
240 for (String currentTestConfig : configStrings){
241 XPathQualifierResolver resolver = new XPathQualifierResolver();
242 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
243 extensionDefinition.setConfiguration(Collections.singletonMap(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, currentTestConfig));
244 resolver.setExtensionDefinition(extensionDefinition.build());
245
246 RouteContext context = new RouteContext();
247 DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
248 context.setDocumentContent(docContent);
249
250
251 try {
252 resolver.resolve(context);
253 fail("Invalid XML was encountered, resolver should have thrown an exception");
254 } catch (Exception e) {
255 e.printStackTrace();
256 }
257
258
259 context = new RouteContext();
260 docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
261 context.setDocumentContent(docContent);
262
263 List<Map<String, String>> maps = resolver.resolve(context);
264 assertEquals(1, maps.size());
265 assertEquals("BL", maps.get(0).get("chart"));
266 assertEquals("BUS", maps.get(0).get("org"));
267 }
268 }
269
270 @Test
271 public void testResolve_compoundMap_noBaseXPath() throws Exception {
272
273
274
275
276
277
278 Set<String> configStrings = new HashSet<String>();
279 configStrings.add(COMPOUND_CONFIG_2);
280 configStrings.add(StringUtils.replace(COMPOUND_CONFIG_2, "attributes", "qualifier"));
281
282 for (String currentTestConfig : configStrings){
283 XPathQualifierResolver resolver = new XPathQualifierResolver();
284 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
285 extensionDefinition.setConfiguration(Collections.singletonMap(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, currentTestConfig));
286 resolver.setExtensionDefinition(extensionDefinition.build());
287
288 RouteContext context = new RouteContext();
289 DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
290 context.setDocumentContent(docContent);
291
292
293 try {
294 resolver.resolve(context);
295 fail("Invalid XML was encountered, resolver should have thrown an exception");
296 } catch (Exception e) {
297 e.printStackTrace();
298 }
299
300
301 context = new RouteContext();
302 docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
303 context.setDocumentContent(docContent);
304
305 List<Map<String, String>> maps = resolver.resolve(context);
306 assertEquals(1, maps.size());
307 assertEquals("BL", maps.get(0).get("chart"));
308 assertEquals("BUS", maps.get(0).get("org"));
309 }
310
311 }
312
313 }