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
9 package org.opendaylight.yangtools.yang.data.jaxen;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
17 import com.google.common.base.Converter;
18 import com.google.common.base.VerifyException;
19 import com.google.common.collect.BiMap;
20 import com.google.common.collect.HashBiMap;
21 import com.google.common.collect.Maps;
22 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import java.text.ParseException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
29 import java.util.Optional;
30 import javax.xml.xpath.XPathExpressionException;
31 import org.jaxen.Context;
32 import org.jaxen.Function;
33 import org.jaxen.FunctionCallException;
34 import org.jaxen.UnresolvableException;
35 import org.jaxen.UnsupportedAxisException;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.common.Revision;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
44 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathExpression;
45 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathNodesetResult;
46 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathResult;
47 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 public class JaxenTest {
54 private ConverterNamespaceContext convertNctx;
55 private XPathSchemaContext xpathSchemaContext;
56 private XPathDocument xpathDocument;
57 private XPathExpression xpathExpression;
58 private NormalizedNodeNavigator navigator;
60 private QNameModule moduleQName;
61 private QName rootQName;
62 private QName listAQName;
63 private QName listBQName;
64 private QName leafAQName;
65 private QName leafBQName;
66 private QName leafDQName;
67 private QName containerAQName;
68 private QName containerBQName;
71 public void setup() throws URISyntaxException, IOException, ParseException, XPathExpressionException,
72 UnsupportedAxisException, ReactorException {
73 final SchemaContext schemaContext = createSchemaContext();
74 assertNotNull(schemaContext);
77 xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
78 assertNotNull(xpathSchemaContext);
80 xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath(
82 assertNotNull(xpathExpression);
84 xpathDocument = xpathSchemaContext.createDocument(createNormalizedNodes());
85 assertNotNull(xpathDocument);
86 String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
87 assertNotNull(rootNodeName);
88 assertEquals("root", rootNodeName);
90 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
91 .evaluate(xpathDocument, createYangInstanceIdentifier(false));
92 assertNotNull(resultExpressionEvaluate);
93 assertTrue(resultExpressionEvaluate.isPresent());
94 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
95 Object value = ((XPathNodesetResult) xpathResult).getValue().iterator().next().getValue();
97 assertEquals("three", value);
99 convertNctx = new ConverterNamespaceContext(createPrefixes());
100 navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
101 assertNotNull(navigator);
105 public void testConverterNamespaceContextBackFront() {
106 assertEquals("test2", convertNctx.doBackward(moduleQName));
107 assertEquals(moduleQName, convertNctx.doForward("test2"));
111 public void testConverterNamespaceContextPrefixJaxenName() {
112 assertNotNull(rootQName);
113 assertEquals("test2:root", convertNctx.jaxenQName(rootQName));
114 String prefix = convertNctx.translateNamespacePrefixToUri("test2");
115 assertNotNull(prefix);
116 assertEquals("urn:opendaylight.test2", prefix);
120 public void testCompileExpression() {
121 assertNotNull(xpathExpression.getApexPath());
122 assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
126 public void testJaxenXpath() throws XPathExpressionException {
127 assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
131 public void testXpathWithPredicates() throws XPathExpressionException {
132 XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
133 createPrefixes(), createXPath(true));
135 Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
136 .evaluate(xpathDocument, createYangInstanceIdentifier(true));
137 assertTrue(resultExpressionEvaluate.isPresent());
138 XPathResult<?> xpathResult = resultExpressionEvaluate.get();
139 Object value = ((XPathNodesetResult) xpathResult).getValue().iterator().next().getValue();
140 assertEquals("two", value);
143 @Test(expected = VerifyException.class)
144 public void testIsMethodsInNodeNavigator() {
145 assertTrue(navigator.isDocument("test"));
148 @Test(expected = XPathExpressionException.class)
149 public void testCompileExpressionException() throws XPathExpressionException {
150 assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
153 @Test(expected = UnresolvableException.class)
154 public void testYangFunctionContext() throws UnresolvableException, FunctionCallException {
155 final YangFunctionContext yangFun = YangFunctionContext.getInstance();
156 assertNotNull(yangFun);
157 final Function function = yangFun.getFunction("urn:opendaylight.test2", null, "current");
158 assertNotNull(function);
161 final Context context = mock(Context.class);
162 final ArrayList<Object> list = new ArrayList<>();
163 function.call(context, list);
165 } catch (VerifyException e) {
169 yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
173 * container-a -> container-b -> leaf-d
174 * list-a -> list-b -> leaf-b
176 private YangInstanceIdentifier createYangInstanceIdentifier(final boolean withPredicates) {
177 YangInstanceIdentifier testYangInstanceIdentifier = YangInstanceIdentifier.of(containerAQName).node(
178 containerBQName).node(leafDQName);
179 if (withPredicates) {
180 final Map<QName, Object> keys1 = new HashMap<>();
181 keys1.put(leafAQName, "bar");
183 final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath1 = new YangInstanceIdentifier
184 .NodeIdentifierWithPredicates(listAQName , keys1);
186 final Map<QName, Object> keys2 = new HashMap<>();
187 keys2.put(leafBQName, "two");
189 final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath2 = new YangInstanceIdentifier
190 .NodeIdentifierWithPredicates(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 static SchemaContext createSchemaContext() throws IOException, URISyntaxException, ReactorException {
215 return YangParserTestUtils.parseYangResourceDirectory("/test/documentTest");
218 private static NormalizedNode<?, ?> createNormalizedNodes() {
219 return TestUtils.createNormalizedNodes();
222 private void initQNames() throws URISyntaxException, ParseException {
223 this.moduleQName = QNameModule.create(new URI("urn:opendaylight.test2"), Revision.valueOf("2015-08-08"));
224 this.rootQName = QName.create(moduleQName, "root");
225 this.listAQName = QName.create(moduleQName, "list-a");
226 this.listBQName = QName.create(moduleQName, "list-b");
227 this.leafAQName = QName.create(moduleQName, "leaf-a");
228 this.leafBQName = QName.create(moduleQName, "leaf-b");
229 this.leafDQName = QName.create(moduleQName, "leaf-d");
230 this.containerAQName = QName.create(moduleQName, "container-a");
231 this.containerBQName = QName.create(moduleQName, "container-b");