001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.kew.role;
017    
018    import org.junit.Test;
019    import org.kuali.rice.kew.engine.RouteContext;
020    import org.kuali.rice.kew.routeheader.DocumentContent;
021    import org.kuali.rice.kew.routeheader.StandardDocumentContent;
022    import org.kuali.rice.kew.rule.bo.RuleAttribute;
023    import org.kuali.rice.kew.api.KewApiConstants;
024    import org.kuali.rice.test.BaseRiceTestCase;
025    
026    import java.util.List;
027    import java.util.Map;
028    
029    import static org.junit.Assert.assertEquals;
030    import static org.junit.Assert.fail;
031    
032    /**
033     * Tests the XPathQualifierResolver.
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     *
037     */
038    public class XPathQualifierResolverTest extends BaseRiceTestCase {
039    
040            private static final String SIMPLE_CONFIG_1 = "<resolverConfig>" +
041                    "<baseXPathExpression>/xmlData/accountNumbers</baseXPathExpression>" +
042                    "<qualifier name=\"accountNumber\">" +
043                    "<xPathExpression>./accountNumber</xPathExpression>" +
044                    "</qualifier>" +
045                    "</resolverConfig>";
046            
047            private static final String SIMPLE_DOC_XML_1 = "<xmlData>" +
048                    "<accountNumbers>" +
049                    "<accountNumber>12345</accountNumber>" +
050                    "<accountNumber>54321</accountNumber>" +
051                    "<accountNumber>102030</accountNumber>" +
052                    "<accountNumber>302010</accountNumber>" +
053                    "</accountNumbers>" +
054                    "</xmlData>";
055            
056            private static final String SIMPLE_CONFIG_2 = "<resolverConfig>" +
057            "<qualifier name=\"accountNumber\">" +
058            "<xPathExpression>//accountNumber</xPathExpression>" +
059            "</qualifier>" +
060            "</resolverConfig>";
061            
062            private static final String COMPOUND_CONFIG_1 = "<resolverConfig>" +
063                    "<baseXPathExpression>/xmlData/chartOrg</baseXPathExpression>" +
064                    "<qualifier name=\"chart\">" +
065                    "<xPathExpression>./chart</xPathExpression>" +
066                    "</qualifier>" +
067                    "<qualifier name=\"org\">" +
068                    "<xPathExpression>./org</xPathExpression>" +
069                    "</qualifier>" +
070                    "</resolverConfig>";
071    
072            private static final String COMPOUND_CONFIG_2 = "<resolverConfig>" +
073                    "<qualifier name=\"chart\">" +
074                    "<xPathExpression>//chart</xPathExpression>" +
075                    "</qualifier>" +
076                    "<qualifier name=\"org\">" +
077                    "<xPathExpression>//org</xPathExpression>" +
078                    "</qualifier>" +
079                    "</resolverConfig>";
080    
081            
082            private static final String COMPOUND_DOC_XML_1 =  "<xmlData>" +
083                    "<chartOrg>" +
084                    "<chart>BL</chart>" +
085                    "<org>BUS</org>" +
086                    "</chartOrg>" +
087                    "<chartOrg>" +
088                    "<chart>IN</chart>" +
089                    "<org>MED</org>" +
090                    "</chartOrg>" +
091                    "</xmlData>";
092            
093            private static final String COMPOUND_DOC_XML_2 =  "<xmlData>" +
094                    "<chartOrg>" +
095                    "<chart>BL</chart>" +
096                    "<org>BUS</org>" +
097                    "<chart>IN</chart>" +
098                    "<org>MED</org>" +
099                    "</chartOrg>" +
100                    "</xmlData>";
101            
102            private static final String COMPOUND_DOC_XML_3 =  "<xmlData>" +
103                    "<chartOrg>" +
104                    "<chart>BL</chart>" +
105                    "<org>BUS</org>" +
106                    "</chartOrg>" +
107                    "</xmlData>";
108            
109            @Test
110            public void testResolve_simpleMap() throws Exception {
111                    XPathQualifierResolver resolver = new XPathQualifierResolver();
112    
113                    resolver.setXmlConfigData(SIMPLE_CONFIG_1);
114                    
115                    RouteContext context = new RouteContext();
116                    DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
117                    context.setDocumentContent(docContent);
118                    
119                    List<Map<String, String>> maps = resolver.resolve(context);
120                    verifyAccountmaps(maps);
121            }
122            
123            @Test
124            public void testResolve_simpleMap_noBaseXPath() throws Exception {
125                    XPathQualifierResolver resolver = new XPathQualifierResolver();
126                    
127                    resolver.setXmlConfigData(SIMPLE_CONFIG_2);
128                    
129                    RouteContext context = new RouteContext();
130                    DocumentContent docContent = new StandardDocumentContent(SIMPLE_DOC_XML_1);
131                    context.setDocumentContent(docContent);
132                    
133                    List<Map<String, String>> maps = resolver.resolve(context);
134                    verifyAccountmaps(maps);
135                    
136            }
137            
138            private void verifyAccountmaps(List<Map<String, String>> maps) {
139                    assertEquals("Incorrect number of attribute sets.", 4, maps.size());
140                    
141                    String acctNumKey = "accountNumber";
142                    Map<String, String> map1 = maps.get(0);
143                    assertEquals(1, map1.size());
144                    assertEquals("12345", map1.get(acctNumKey));
145                    Map<String, String> map2 = maps.get(1);
146                    assertEquals(1, map2.size());
147                    assertEquals("54321", map2.get(acctNumKey));
148                    Map<String, String> map3 = maps.get(2);
149                    assertEquals(1, map3.size());
150                    assertEquals("102030", map3.get(acctNumKey));
151                    Map<String, String> map4 = maps.get(3);
152                    assertEquals(1, map4.size());
153                    assertEquals("302010", map4.get(acctNumKey));
154            }
155            
156            @Test
157            public void testResolve_compoundMap1() throws Exception {
158                    XPathQualifierResolver resolver = new XPathQualifierResolver();
159    
160                    resolver.setXmlConfigData(COMPOUND_CONFIG_1);
161                    
162                    RouteContext context = new RouteContext();
163                    DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_1);
164                    context.setDocumentContent(docContent);
165                    
166                    List<Map<String, String>> maps = resolver.resolve(context);
167                    assertEquals("Incorrect number of attribute sets", 2, maps.size());
168                    
169                    String chartKey = "chart";
170                    String orgKey = "org";
171                    
172                    Map<String, String> map1 = maps.get(0);
173                    assertEquals(2, map1.size());
174                    assertEquals("BL", map1.get(chartKey));
175                    assertEquals("BUS", map1.get(orgKey));
176                    
177                    Map<String, String> map2 = maps.get(1);
178                    assertEquals(2, map2.size());
179                    assertEquals("IN", map2.get(chartKey));
180                    assertEquals("MED", map2.get(orgKey));
181            }
182            
183            @Test
184            public void testResolve_compoundMap() throws Exception {
185                    XPathQualifierResolver resolver = new XPathQualifierResolver();
186    
187                    resolver.setXmlConfigData(COMPOUND_CONFIG_1);
188                    
189                    RouteContext context = new RouteContext();
190                    DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
191                    context.setDocumentContent(docContent);
192                    
193                    // should fail with this document content
194                    
195                    try {
196                            resolver.resolve(context);
197                            fail("Invalid XML was encountered, resolver should have thrown an exception");
198                    } catch (Exception e) {
199                            e.printStackTrace();
200                    }
201                    
202                    // however, should succeed with this
203                    
204                    context = new RouteContext();
205                    docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
206                    context.setDocumentContent(docContent);
207                    
208                    List<Map<String, String>> maps = resolver.resolve(context);
209                    assertEquals(1, maps.size());
210                    assertEquals("BL", maps.get(0).get("chart"));
211                    assertEquals("BUS", maps.get(0).get("org"));
212            }
213            
214            @Test
215            public void testResolve_compoundMap_noBaseXPath() throws Exception {
216                    XPathQualifierResolver resolver = new XPathQualifierResolver();
217                    
218                    resolver.setXmlConfigData(COMPOUND_CONFIG_2);
219                    
220                    RouteContext context = new RouteContext();
221                    DocumentContent docContent = new StandardDocumentContent(COMPOUND_DOC_XML_2);
222                    context.setDocumentContent(docContent);
223                    
224                    // should fail with this document content
225                    
226                    try {
227                            resolver.resolve(context);
228                            fail("Invalid XML was encountered, resolver should have thrown an exception");
229                    } catch (Exception e) {
230                            e.printStackTrace();
231                    }
232                    
233                    // however, should succeed with this
234                    
235                    context = new RouteContext();
236                    docContent = new StandardDocumentContent(COMPOUND_DOC_XML_3);
237                    context.setDocumentContent(docContent);
238                    
239                    List<Map<String, String>> maps = resolver.resolve(context);
240                    assertEquals(1, maps.size());
241                    assertEquals("BL", maps.get(0).get("chart"));
242                    assertEquals("BUS", maps.get(0).get("org"));
243                    
244            }
245            
246    }