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;
22 import java.util.ArrayList;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.Optional;
27 import javax.xml.xpath.XPathExpressionException;
28 import org.jaxen.Context;
29 import org.jaxen.Function;
30 import org.jaxen.FunctionCallException;
31 import org.jaxen.UnresolvableException;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
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.api.schema.xpath.XPathDocument;
41 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathExpression;
42 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathNodesetResult;
43 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathResult;
44 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/test/documentTest");
69 assertNotNull(schemaContext);
72 xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
73 assertNotNull(xpathSchemaContext);
75 xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath(
77 assertNotNull(xpathExpression);
79 xpathDocument = xpathSchemaContext.createDocument(TestUtils.createNormalizedNodes());
80 assertNotNull(xpathDocument);
81 String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
82 assertNotNull(rootNodeName);
83 assertEquals("root", rootNodeName);
85 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
86 .evaluate(xpathDocument, createYangInstanceIdentifier(false));
87 assertNotNull(resultExpressionEvaluate);
88 assertTrue(resultExpressionEvaluate.isPresent());
89 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
90 assertTrue(xpathResult instanceof XPathNodesetResult);
91 XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
93 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = nodeset.getValue().iterator().next();
95 assertEquals("three", entry.getValue().getValue());
97 convertNctx = new ConverterNamespaceContext(createPrefixes());
98 navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
99 assertNotNull(navigator);
103 public void testConverterNamespaceContextBackFront() {
104 assertEquals("test2", convertNctx.doBackward(moduleQName));
105 assertEquals(moduleQName, convertNctx.doForward("test2"));
109 public void testConverterNamespaceContextPrefixJaxenName() {
110 assertNotNull(rootQName);
111 assertEquals("test2:root", convertNctx.jaxenQName(rootQName));
112 String prefix = convertNctx.translateNamespacePrefixToUri("test2");
113 assertNotNull(prefix);
114 assertEquals("urn:opendaylight.test2", prefix);
118 public void testCompileExpression() {
119 assertNotNull(xpathExpression.getApexPath());
120 assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
124 public void testJaxenXpath() throws XPathExpressionException {
125 assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
129 public void testXpathWithPredicates() throws XPathExpressionException {
130 XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
131 createPrefixes(), createXPath(true));
133 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
134 .evaluate(xpathDocument, createYangInstanceIdentifier(true));
135 assertTrue(resultExpressionEvaluate.isPresent());
136 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
137 assertTrue(xpathResult instanceof XPathNodesetResult);
138 XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
140 Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = nodeset.getValue().iterator().next();
141 assertNotNull(entry);
142 assertEquals("two", entry.getValue().getValue());
145 @Test(expected = VerifyException.class)
146 public void testIsMethodsInNodeNavigator() {
147 assertTrue(navigator.isDocument("test"));
150 @Test(expected = XPathExpressionException.class)
151 public void testCompileExpressionException() throws XPathExpressionException {
152 assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
155 @Test(expected = UnresolvableException.class)
156 public void testYangFunctionContext() throws UnresolvableException, FunctionCallException {
157 final YangFunctionContext yangFun = YangFunctionContext.getInstance();
158 assertNotNull(yangFun);
159 final Function function = yangFun.getFunction("urn:opendaylight.test2", null, "current");
160 assertNotNull(function);
163 final Context context = mock(Context.class);
164 final ArrayList<Object> list = new ArrayList<>();
165 function.call(context, list);
167 } catch (VerifyException e) {
171 yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
175 * container-a -> container-b -> leaf-d
176 * list-a -> list-b -> leaf-b
178 private YangInstanceIdentifier createYangInstanceIdentifier(final boolean withPredicates) {
179 YangInstanceIdentifier testYangInstanceIdentifier = YangInstanceIdentifier.of(containerAQName).node(
180 containerBQName).node(leafDQName);
181 if (withPredicates) {
182 final Map<QName, Object> keys1 = new HashMap<>();
183 keys1.put(leafAQName, "bar");
185 final NodeIdentifierWithPredicates mapEntryPath1 = NodeIdentifierWithPredicates.of(listAQName , keys1);
187 final Map<QName, Object> keys2 = new HashMap<>();
188 keys2.put(leafBQName, "two");
190 final NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(listBQName , keys2);
192 testYangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
193 .node(listBQName).node(mapEntryPath2).node(leafBQName);
195 return testYangInstanceIdentifier;
198 private static String createXPath(final boolean withPredicates) {
199 return withPredicates ? "/list-a[leaf-a='bar']/list-b[leaf-b='two']/leaf-b" : "/container-a/container-b/leaf-d";
202 private Converter<String, QNameModule> createPrefixes() {
203 BiMap<String, QNameModule> currentConverter = HashBiMap.create();
204 currentConverter.put("test2", moduleQName);
206 return Maps.asConverter(currentConverter);
209 // rootQName -> listAQName -> leafAQName
210 private SchemaPath createSchemaPath() {
211 return SchemaPath.create(true, rootQName, listAQName, leafAQName);
214 private void initQNames() {
215 this.moduleQName = QNameModule.create(URI.create("urn:opendaylight.test2"), Revision.of("2015-08-08"));
216 this.rootQName = QName.create(moduleQName, "root");
217 this.listAQName = QName.create(moduleQName, "list-a");
218 this.listBQName = QName.create(moduleQName, "list-b");
219 this.leafAQName = QName.create(moduleQName, "leaf-a");
220 this.leafBQName = QName.create(moduleQName, "leaf-b");
221 this.leafDQName = QName.create(moduleQName, "leaf-d");
222 this.containerAQName = QName.create(moduleQName, "container-a");
223 this.containerBQName = QName.create(moduleQName, "container-b");