Bug 5085: Clean-up test and retest JUnit tests
[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
52 public class JaxenTest {
53     private ConverterNamespaceContext convertNctx;
54     private XPathSchemaContext xpathSchemaContext;
55     private XPathDocument xpathDocument;
56     private XPathExpression xpathExpression;
57     private NormalizedNodeNavigator navigator;
58
59     private QNameModule moduleQName;
60     private QName rootQName;
61     private QName listAQName;
62     private QName listBQName;
63     private QName leafAQName;
64     private QName leafBQName;
65     private QName leafDQName;
66     private QName containerAQName;
67     private QName containerBQName;
68
69     @Before
70     public void setup() throws URISyntaxException, IOException, ParseException, XPathExpressionException,
71             UnsupportedAxisException, ReactorException {
72         final SchemaContext schemaContext = createSchemaContext();
73         assertNotNull(schemaContext);
74
75         initQNames();
76         xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
77         assertNotNull(xpathSchemaContext);
78
79         xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath
80                 (false));
81         assertNotNull(xpathExpression);
82
83         xpathDocument = xpathSchemaContext.createDocument(createNormalizedNodes());
84         assertNotNull(xpathDocument);
85         String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
86         assertNotNull(rootNodeName);
87         assertEquals("root", rootNodeName);
88
89         Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
90                 .evaluate(xpathDocument, createYangInstanceIdentifier(false));
91         assertNotNull(resultExpressionEvaluate);
92         assertTrue(resultExpressionEvaluate.isPresent());
93         XPathResult<?> xPathResult = resultExpressionEvaluate.get();
94         Object value = ((XPathNodesetResult) xPathResult).getValue().iterator().next().getValue();
95         assertNotNull(value);
96         assertEquals("three", value);
97
98         convertNctx = new ConverterNamespaceContext(createPrefixes());
99         navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
100         assertNotNull(navigator);
101     }
102
103     @Test
104     public void testConverterNamespaceContextBackFront() {
105         assertEquals("test2", convertNctx.doBackward(moduleQName));
106         assertEquals(moduleQName, convertNctx.doForward("test2"));
107     }
108
109     @Test
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);
116     }
117
118     @Test
119     public void testCompileExpression() {
120         assertNotNull(xpathExpression.getApexPath());
121         assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
122     }
123
124     @Test
125     public void testJaxenXpath() throws XPathExpressionException {
126         assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier(false)));
127     }
128
129     @Test
130     public void testXpathWithPredicates() throws XPathExpressionException {
131         XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
132                 createPrefixes(), createXPath(true));
133
134         Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
135                 .evaluate(xpathDocument, createYangInstanceIdentifier(true));
136         assertTrue(resultExpressionEvaluate.isPresent());
137         XPathResult<?> xPathResult = resultExpressionEvaluate.get();
138         Object value = ((XPathNodesetResult) xPathResult).getValue().iterator().next().getValue();
139         assertEquals("two", value);
140     }
141
142     @Test(expected = VerifyException.class)
143     public void testIsMethodsInNodeNavigator() {
144         assertNotNull(navigator.isDocument("test"));
145     }
146
147     @Test(expected = XPathExpressionException.class)
148     public void testCompileExpressionException() throws XPathExpressionException {
149         assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
150     }
151
152     @Test(expected = UnresolvableException.class)
153     public void testYangFunctionContext() throws UnresolvableException, FunctionCallException {
154         final YangFunctionContext yangFun = YangFunctionContext.getInstance();
155         assertNotNull(yangFun);
156         final Function function = yangFun.getFunction("urn:opendaylight.test2", null, "current");
157         assertNotNull(function);
158
159         try {
160             final Context context = mock(Context.class);
161             final ArrayList<Object> list = new ArrayList<>();
162             function.call(context, list);
163             fail();
164         } catch (VerifyException e) {
165         }
166
167         yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
168     }
169
170     /**
171      * @return container-a -> container-b -> leaf-d
172      *         list-a -> list-b -> leaf-b
173      */
174     private YangInstanceIdentifier createYangInstanceIdentifier(Boolean withPredicates) {
175         YangInstanceIdentifier testYangInstanceIdentifier = YangInstanceIdentifier.of(containerAQName).node
176                 (containerBQName).node(leafDQName);
177         if (withPredicates) {
178             final Map<QName, Object> keys1 = new HashMap<>();
179             keys1.put(leafAQName, "bar");
180
181             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath1 = new YangInstanceIdentifier
182                     .NodeIdentifierWithPredicates(listAQName , keys1);
183
184             final Map<QName, Object> keys2 = new HashMap<>();
185             keys2.put(leafBQName, "two");
186
187             final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath2 = new YangInstanceIdentifier
188                     .NodeIdentifierWithPredicates(listBQName , keys2);
189
190             testYangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
191                     .node(listBQName).node(mapEntryPath2).node(leafBQName);
192         }
193         return testYangInstanceIdentifier;
194     }
195
196     private static String createXPath(boolean withPredicates) {
197         String xPath = "/container-a/container-b/leaf-d";
198         if (withPredicates) {
199             xPath = "/list-a[leaf-a='bar']/list-b[leaf-b='two']/leaf-b";
200         }
201         return xPath;
202     }
203
204     private Converter<String, QNameModule> createPrefixes() {
205         BiMap<String, QNameModule> currentConverter = HashBiMap.create();
206         currentConverter.put("test2", moduleQName);
207
208         return Maps.asConverter(currentConverter);
209     }
210
211     /**
212      * @return rootQName -> listAQName -> leafAQName
213      */
214     private  SchemaPath createSchemaPath() {
215         return SchemaPath.create(true, rootQName, listAQName, leafAQName);
216     }
217
218     private SchemaContext createSchemaContext() throws IOException, URISyntaxException, ReactorException {
219         return TestUtils.loadModules("/test/documentTest");
220     }
221
222     private static NormalizedNode<?, ?> createNormalizedNodes() {
223         return TestUtils.createNormalizedNodes();
224     }
225
226     private void initQNames() throws URISyntaxException, ParseException {
227         this.moduleQName = QNameModule.create(new URI("urn:opendaylight.test2"),
228                 SimpleDateFormatUtil.getRevisionFormat().parse("2015-08-08"));
229         this.rootQName = QName.create(moduleQName, "root");
230         this.listAQName = QName.create(moduleQName, "list-a");
231         this.listBQName = QName.create(moduleQName, "list-b");
232         this.leafAQName = QName.create(moduleQName, "leaf-a");
233         this.leafBQName = QName.create(moduleQName, "leaf-b");
234         this.leafDQName = QName.create(moduleQName, "leaf-d");
235         this.containerAQName = QName.create(moduleQName, "container-a");
236         this.containerBQName = QName.create(moduleQName, "container-b");
237     }
238 }