Remove deprecated Yin/YangStatementSourceImpl
[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 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.base.Converter;
18 import com.google.common.base.Optional;
19 import com.google.common.base.VerifyException;
20 import com.google.common.collect.BiMap;
21 import com.google.common.collect.HashBiMap;
22 import com.google.common.collect.Maps;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
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.SimpleDateFormatUtil;
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;
52
53 public class JaxenTest {
54     private ConverterNamespaceContext convertNctx;
55     private XPathSchemaContext xpathSchemaContext;
56     private XPathDocument xpathDocument;
57     private XPathExpression xpathExpression;
58     private NormalizedNodeNavigator navigator;
59
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;
69
70     @Before
71     public void setup() throws URISyntaxException, IOException, ParseException, XPathExpressionException,
72             UnsupportedAxisException, ReactorException {
73         final SchemaContext schemaContext = createSchemaContext();
74         assertNotNull(schemaContext);
75
76         initQNames();
77         xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
78         assertNotNull(xpathSchemaContext);
79
80         xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath(
81                     false));
82         assertNotNull(xpathExpression);
83
84         xpathDocument = xpathSchemaContext.createDocument(createNormalizedNodes());
85         assertNotNull(xpathDocument);
86         String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
87         assertNotNull(rootNodeName);
88         assertEquals("root", rootNodeName);
89
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();
96         assertNotNull(value);
97         assertEquals("three", value);
98
99         convertNctx = new ConverterNamespaceContext(createPrefixes());
100         navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
101         assertNotNull(navigator);
102     }
103
104     @Test
105     public void testConverterNamespaceContextBackFront() {
106         assertEquals("test2", convertNctx.doBackward(moduleQName));
107         assertEquals(moduleQName, convertNctx.doForward("test2"));
108     }
109
110     @Test
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);
117     }
118
119     @Test
120     public void testCompileExpression() {
121         assertNotNull(xpathExpression.getApexPath());
122         assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
123     }
124
125     @Test
126     public void testJaxenXpath() throws XPathExpressionException {
127         assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
128     }
129
130     @Test
131     public void testXpathWithPredicates() throws XPathExpressionException {
132         XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
133                 createPrefixes(), createXPath(true));
134
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);
141     }
142
143     @Test(expected = VerifyException.class)
144     public void testIsMethodsInNodeNavigator() {
145         assertTrue(navigator.isDocument("test"));
146     }
147
148     @Test(expected = XPathExpressionException.class)
149     public void testCompileExpressionException() throws XPathExpressionException {
150         assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
151     }
152
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);
159
160         try {
161             final Context context = mock(Context.class);
162             final ArrayList<Object> list = new ArrayList<>();
163             function.call(context, list);
164             fail();
165         } catch (VerifyException e) {
166             // Expected
167         }
168
169         yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
170     }
171
172     /*
173      * container-a -> container-b -> leaf-d
174      *           list-a -> list-b -> leaf-b
175      */
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");
182
183             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath1 = new YangInstanceIdentifier
184                     .NodeIdentifierWithPredicates(listAQName , keys1);
185
186             final Map<QName, Object> keys2 = new HashMap<>();
187             keys2.put(leafBQName, "two");
188
189             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath2 = new YangInstanceIdentifier
190                     .NodeIdentifierWithPredicates(listBQName , keys2);
191
192             testYangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
193                     .node(listBQName).node(mapEntryPath2).node(leafBQName);
194         }
195         return testYangInstanceIdentifier;
196     }
197
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";
200     }
201
202     private Converter<String, QNameModule> createPrefixes() {
203         BiMap<String, QNameModule> currentConverter = HashBiMap.create();
204         currentConverter.put("test2", moduleQName);
205
206         return Maps.asConverter(currentConverter);
207     }
208
209     // rootQName -> listAQName -> leafAQName
210     private  SchemaPath createSchemaPath() {
211         return SchemaPath.create(true, rootQName, listAQName, leafAQName);
212     }
213
214     private static SchemaContext createSchemaContext() throws IOException, URISyntaxException, ReactorException {
215         return YangParserTestUtils.parseYangResourceDirectory("/test/documentTest");
216     }
217
218     private static NormalizedNode<?, ?> createNormalizedNodes() {
219         return TestUtils.createNormalizedNodes();
220     }
221
222     private void initQNames() throws URISyntaxException, ParseException {
223         this.moduleQName = QNameModule.create(new URI("urn:opendaylight.test2"),
224                 SimpleDateFormatUtil.getRevisionFormat().parse("2015-08-08"));
225         this.rootQName = QName.create(moduleQName, "root");
226         this.listAQName = QName.create(moduleQName, "list-a");
227         this.listBQName = QName.create(moduleQName, "list-b");
228         this.leafAQName = QName.create(moduleQName, "leaf-a");
229         this.leafBQName = QName.create(moduleQName, "leaf-b");
230         this.leafDQName = QName.create(moduleQName, "leaf-d");
231         this.containerAQName = QName.create(moduleQName, "container-a");
232         this.containerBQName = QName.create(moduleQName, "container-b");
233     }
234 }