2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.jaxen;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.base.Converter;
17 import com.google.common.base.VerifyException;
18 import com.google.common.collect.BiMap;
19 import com.google.common.collect.HashBiMap;
20 import com.google.common.collect.Maps;
21 import java.util.ArrayList;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import java.util.Optional;
26 import javax.xml.xpath.XPathExpressionException;
27 import org.jaxen.Context;
28 import org.jaxen.Function;
29 import org.jaxen.FunctionCallException;
30 import org.jaxen.UnresolvableException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.common.XMLNamespace;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathDocument;
41 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathExpression;
42 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathNodesetResult;
43 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathResult;
44 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathSchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
49 public class JaxenTest {
50 private ConverterNamespaceContext convertNctx;
51 private XPathSchemaContext xpathSchemaContext;
52 private XPathDocument xpathDocument;
53 private XPathExpression xpathExpression;
54 private NormalizedNodeNavigator navigator;
56 private QNameModule moduleQName;
57 private QName rootQName;
58 private QName listAQName;
59 private QName listBQName;
60 private QName leafAQName;
61 private QName leafBQName;
62 private QName leafDQName;
63 private QName containerAQName;
64 private QName containerBQName;
67 public void setup() throws XPathExpressionException {
68 final EffectiveModelContext schemaContext =
69 YangParserTestUtils.parseYangResourceDirectory("/test/documentTest");
70 assertNotNull(schemaContext);
73 xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
74 assertNotNull(xpathSchemaContext);
76 xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath(
78 assertNotNull(xpathExpression);
80 xpathDocument = xpathSchemaContext.createDocument(TestUtils.createNormalizedNodes());
81 assertNotNull(xpathDocument);
82 String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
83 assertNotNull(rootNodeName);
84 assertEquals("root", rootNodeName);
86 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
87 .evaluate(xpathDocument, createYangInstanceIdentifier(false));
88 assertNotNull(resultExpressionEvaluate);
89 assertTrue(resultExpressionEvaluate.isPresent());
90 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
91 assertTrue(xpathResult instanceof XPathNodesetResult);
92 XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
94 Entry<YangInstanceIdentifier, NormalizedNode> entry = nodeset.getValue().iterator().next();
96 assertEquals("three", entry.getValue().body());
98 convertNctx = new ConverterNamespaceContext(createPrefixes());
99 navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
100 assertNotNull(navigator);
104 public void testConverterNamespaceContextBackFront() {
105 assertEquals("test2", convertNctx.doBackward(moduleQName));
106 assertEquals(moduleQName, convertNctx.doForward("test2"));
110 public void testConverterNamespaceContextPrefixJaxenName() {
111 assertNotNull(rootQName);
112 assertEquals("test2:root", convertNctx.jaxenQName(rootQName));
113 String prefix = convertNctx.translateNamespacePrefixToUri("test2");
114 assertNotNull(prefix);
115 assertEquals("urn:opendaylight.test2", prefix);
119 public void testCompileExpression() {
120 assertNotNull(xpathExpression.getApexPath());
121 assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
125 public void testJaxenXpath() throws XPathExpressionException {
126 assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
130 public void testXpathWithPredicates() throws XPathExpressionException {
131 XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
132 createPrefixes(), createXPath(true));
134 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
135 .evaluate(xpathDocument, createYangInstanceIdentifier(true));
136 assertTrue(resultExpressionEvaluate.isPresent());
137 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
138 assertTrue(xpathResult instanceof XPathNodesetResult);
139 XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
141 Entry<YangInstanceIdentifier, NormalizedNode> entry = nodeset.getValue().iterator().next();
142 assertNotNull(entry);
143 assertEquals("two", entry.getValue().body());
146 @Test(expected = VerifyException.class)
147 public void testIsMethodsInNodeNavigator() {
148 assertTrue(navigator.isDocument("test"));
151 @Test(expected = XPathExpressionException.class)
152 public void testCompileExpressionException() throws XPathExpressionException {
153 assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
156 @Test(expected = UnresolvableException.class)
157 public void testYangFunctionContext() throws UnresolvableException, FunctionCallException {
158 final YangFunctionContext yangFun = YangFunctionContext.getInstance();
159 assertNotNull(yangFun);
160 final Function function = yangFun.getFunction("urn:opendaylight.test2", null, "current");
161 assertNotNull(function);
164 final Context context = mock(Context.class);
165 final ArrayList<Object> list = new ArrayList<>();
166 function.call(context, list);
168 } catch (VerifyException e) {
172 yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
176 * container-a -> container-b -> leaf-d
177 * list-a -> list-b -> leaf-b
179 private YangInstanceIdentifier createYangInstanceIdentifier(final boolean withPredicates) {
180 YangInstanceIdentifier testYangInstanceIdentifier = YangInstanceIdentifier.of(containerAQName).node(
181 containerBQName).node(leafDQName);
182 if (withPredicates) {
183 final Map<QName, Object> keys1 = new HashMap<>();
184 keys1.put(leafAQName, "bar");
186 final NodeIdentifierWithPredicates mapEntryPath1 = NodeIdentifierWithPredicates.of(listAQName , keys1);
188 final Map<QName, Object> keys2 = new HashMap<>();
189 keys2.put(leafBQName, "two");
191 final NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(listBQName , keys2);
193 testYangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
194 .node(listBQName).node(mapEntryPath2).node(leafBQName);
196 return testYangInstanceIdentifier;
199 private static String createXPath(final boolean withPredicates) {
200 return withPredicates ? "/list-a[leaf-a='bar']/list-b[leaf-b='two']/leaf-b" : "/container-a/container-b/leaf-d";
203 private Converter<String, QNameModule> createPrefixes() {
204 BiMap<String, QNameModule> currentConverter = HashBiMap.create();
205 currentConverter.put("test2", moduleQName);
207 return Maps.asConverter(currentConverter);
210 // rootQName -> listAQName -> leafAQName
211 private SchemaPath createSchemaPath() {
212 return SchemaPath.create(true, rootQName, listAQName, leafAQName);
215 private void initQNames() {
216 this.moduleQName = QNameModule.create(XMLNamespace.of("urn:opendaylight.test2"), Revision.of("2015-08-08"));
217 this.rootQName = QName.create(moduleQName, "root");
218 this.listAQName = QName.create(moduleQName, "list-a");
219 this.listBQName = QName.create(moduleQName, "list-b");
220 this.leafAQName = QName.create(moduleQName, "leaf-a");
221 this.leafBQName = QName.create(moduleQName, "leaf-b");
222 this.leafDQName = QName.create(moduleQName, "leaf-d");
223 this.containerAQName = QName.create(moduleQName, "container-a");
224 this.containerBQName = QName.create(moduleQName, "container-b");