93557596f6bca4fcdbdcb27ae561e3e2c7e8a4ae
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / JaxenTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.jaxen;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Converter;
16 import com.google.common.base.Optional;
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.io.IOException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.text.ParseException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.xml.xpath.XPathExpressionException;
28 import org.jaxen.UnresolvableException;
29 import org.jaxen.UnsupportedAxisException;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
38 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathExpression;
39 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathNodesetResult;
40 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathResult;
41 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44
45 public class JaxenTest {
46     private ConverterNamespaceContext convertNctx;
47     private XPathSchemaContext xpathSchemaContext;
48     private XPathDocument xpathDocument;
49     private XPathExpression xpathExpression;
50     private NormalizedNodeNavigator navigator;
51
52     private QNameModule moduleQName;
53     private QName rootQName;
54     private QName listAQName;
55     private QName listBQName;
56     private QName leafAQName;
57     private QName leafBQName;
58     private QName leafDQName;
59     private QName containerAQName;
60     private QName containerBQName;
61
62     @Before
63     public void setup() throws URISyntaxException, IOException, ParseException, XPathExpressionException,
64             UnsupportedAxisException {
65         final SchemaContext schemaContext = createSchemaContext();
66         assertNotNull(schemaContext);
67
68         initQNames();
69         xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
70         assertNotNull(xpathSchemaContext);
71
72         xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath
73                 (false));
74         assertNotNull(xpathExpression);
75
76         xpathDocument = xpathSchemaContext.createDocument(createNormalizedNodes());
77         assertNotNull(xpathDocument);
78         String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
79         assertNotNull(rootNodeName);
80         assertEquals("root", rootNodeName);
81
82         Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
83                 .evaluate(xpathDocument, createYangInstanceIdentifier(false));
84         assertNotNull(resultExpressionEvaluate);
85         assertTrue(resultExpressionEvaluate.isPresent());
86         XPathResult<?> xPathResult = resultExpressionEvaluate.get();
87         Object value = ((XPathNodesetResult) xPathResult).getValue().iterator().next().getValue();
88         assertNotNull(value);
89         assertEquals("three", value);
90
91         convertNctx = new ConverterNamespaceContext(createPrefixes());
92         navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
93         assertNotNull(navigator);
94     }
95
96     @Test
97     public void testConverterNamespaceContextBackFront() {
98         assertEquals("test2", convertNctx.doBackward(moduleQName));
99         assertEquals(moduleQName, convertNctx.doForward("test2"));
100     }
101
102     @Test
103     public void testConverterNamespaceContextPrefixJaxenName() {
104         assertNotNull(rootQName);
105         assertEquals("test2:root", convertNctx.jaxenQName(rootQName));
106         String prefix = convertNctx.translateNamespacePrefixToUri("test2");
107         assertNotNull(prefix);
108         assertEquals("urn:opendaylight.test2", prefix);
109     }
110
111     @Test
112     public void testCompileExpression() {
113         assertNotNull(xpathExpression.getApexPath());
114         assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
115     }
116
117     @Test
118     public void testJaxenXpath() throws XPathExpressionException {
119         assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
120     }
121
122     @Test
123     public void testXpathWithPredicates() throws XPathExpressionException {
124         XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
125                 createPrefixes(), createXPath(true));
126
127         Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
128                 .evaluate(xpathDocument, createYangInstanceIdentifier(true));
129         assertTrue(resultExpressionEvaluate.isPresent());
130         XPathResult<?> xPathResult = resultExpressionEvaluate.get();
131         Object value = ((XPathNodesetResult) xPathResult).getValue().iterator().next().getValue();
132         assertEquals("two", value);
133     }
134
135     @Test(expected = VerifyException.class)
136     public void testIsMethodsInNodeNavigator() {
137         assertNotNull(navigator.isDocument("test"));
138     }
139
140     @Test(expected = XPathExpressionException.class)
141     public void testCompileExpressionException() throws XPathExpressionException {
142         assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
143     }
144
145     @Test(expected = UnresolvableException.class)
146     public void testYangFunctionContext() throws UnresolvableException {
147         YangFunctionContext yangFun = YangFunctionContext.getInstance();
148         assertNotNull(yangFun);
149         assertNotNull(yangFun.getFunction("urn:opendaylight.test2", null, "current"));
150         yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
151     }
152
153     /**
154      * @return container-a -> container-b -> leaf-d
155      *         list-a -> list-b -> leaf-b
156      */
157     private YangInstanceIdentifier createYangInstanceIdentifier(Boolean withPredicates) {
158         YangInstanceIdentifier testYangInstanceIdentifier = YangInstanceIdentifier.of(containerAQName).node
159                 (containerBQName).node(leafDQName);
160         if (withPredicates) {
161             final Map<QName, Object> keys1 = new HashMap<>();
162             keys1.put(leafAQName, "bar");
163
164             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath1 = new YangInstanceIdentifier
165                     .NodeIdentifierWithPredicates(listAQName , keys1);
166
167             final Map<QName, Object> keys2 = new HashMap<>();
168             keys2.put(leafBQName, "two");
169
170             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath2 = new YangInstanceIdentifier
171                     .NodeIdentifierWithPredicates(listBQName , keys2);
172
173             testYangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
174                     .node(listBQName).node(mapEntryPath2).node(leafBQName);
175         }
176         return testYangInstanceIdentifier;
177     }
178
179     private static String createXPath(boolean withPredicates) {
180         String xPath = "/container-a/container-b/leaf-d";
181         if (withPredicates) {
182             xPath = "/list-a[leaf-a='bar']/list-b[leaf-b='two']/leaf-b";
183         }
184         return xPath;
185     }
186
187     private Converter<String, QNameModule> createPrefixes() {
188         BiMap<String, QNameModule> currentConverter = HashBiMap.create();
189         currentConverter.put("test2", moduleQName);
190
191         return Maps.asConverter(currentConverter);
192     }
193
194     /**
195      * @return rootQName -> listAQName -> leafAQName
196      */
197     private  SchemaPath createSchemaPath() {
198         return SchemaPath.create(true, rootQName, listAQName, leafAQName);
199     }
200
201     private SchemaContext createSchemaContext() throws IOException, URISyntaxException {
202         return TestUtils.loadSchemaContext(getClass().getResource("/test/documentTest").toURI());
203     }
204
205     private static NormalizedNode<?, ?> createNormalizedNodes() {
206         return TestUtils.createNormalizedNodes();
207     }
208
209     private void initQNames() throws URISyntaxException, ParseException {
210         this.moduleQName = QNameModule.create(new URI("urn:opendaylight.test2"),
211                 SimpleDateFormatUtil.getRevisionFormat().parse("2015-08-08"));
212         this.rootQName = QName.create(moduleQName, "root");
213         this.listAQName = QName.create(moduleQName, "list-a");
214         this.listBQName = QName.create(moduleQName, "list-b");
215         this.leafAQName = QName.create(moduleQName, "leaf-a");
216         this.leafBQName = QName.create(moduleQName, "leaf-b");
217         this.leafDQName = QName.create(moduleQName, "leaf-d");
218         this.containerAQName = QName.create(moduleQName, "container-a");
219         this.containerBQName = QName.create(moduleQName, "container-b");
220     }
221 }