--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yangtools.yang.data.jaxen;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import com.google.common.base.Converter;
+import com.google.common.base.Optional;
+import com.google.common.base.VerifyException;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.xml.xpath.XPathExpressionException;
+import org.jaxen.UnresolvableException;
+import org.jaxen.UnsupportedAxisException;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
+import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathExpression;
+import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathNodesetResult;
+import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathResult;
+import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class JaxenTest {
+ private ConverterNamespaceContext convertNctx;
+ private XPathSchemaContext xpathSchemaContext;
+ private XPathDocument xpathDocument;
+ private XPathExpression xpathExpression;
+ private NormalizedNodeNavigator navigator;
+
+ private QNameModule moduleQName;
+ private QName rootQName;
+ private QName listAQName;
+ private QName listBQName;
+ private QName leafAQName;
+ private QName leafBQName;
+ private QName leafDQName;
+ private QName containerAQName;
+ private QName containerBQName;
+
+ @Before
+ public void setup() throws URISyntaxException, IOException, ParseException, XPathExpressionException,
+ UnsupportedAxisException {
+ final SchemaContext schemaContext = createSchemaContext();
+ assertNotNull(schemaContext);
+
+ initQNames();
+ xpathSchemaContext = new JaxenSchemaContextFactory().createContext(schemaContext);
+ assertNotNull(xpathSchemaContext);
+
+ xpathExpression = xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), createXPath());
+ assertNotNull(xpathExpression);
+
+ xpathDocument = xpathSchemaContext.createDocument(createNormalizedNodes());
+ assertNotNull(xpathDocument);
+ String rootNodeName = xpathDocument.getRootNode().getNodeType().getLocalName();
+ assertNotNull(rootNodeName);
+ assertEquals("root", rootNodeName);
+
+ Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpression
+ .evaluate(xpathDocument, createYangInstanceIdentifier());
+ assertNotNull(resultExpressionEvaluate);
+ assertTrue(resultExpressionEvaluate.isPresent());
+ XPathResult<?> xPathResult = resultExpressionEvaluate.get();
+ Object value = ((XPathNodesetResult) xPathResult).getValue().iterator().next().getValue();
+ assertNotNull(value);
+ assertEquals("three", value);
+
+ convertNctx = new ConverterNamespaceContext(createPrefixes());
+
+ navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
+ assertNotNull(navigator);
+ }
+
+ @Test
+ public void testConverterNamespaceContextBackFront() {
+ assertEquals("test2", convertNctx.doBackward(moduleQName));
+ assertEquals(moduleQName, convertNctx.doForward("test2"));
+ }
+
+ @Test
+ public void testConverterNamespaceContextPrefixJaxenName() {
+ assertNotNull(rootQName);
+ assertEquals("test2:root", convertNctx.jaxenQName(rootQName));
+ String prefix = convertNctx.translateNamespacePrefixToUri("test2");
+ assertNotNull(prefix);
+ assertEquals("urn:opendaylight.test2", prefix);
+ }
+
+ @Test
+ public void testCompileExpression() {
+ assertNotNull(xpathExpression.getApexPath());
+ assertEquals(createSchemaPath(), xpathExpression.getEvaluationPath());
+ }
+
+ @Test
+ public void testJaxenXpath() throws XPathExpressionException {
+ assertNotNull(xpathExpression.evaluate(xpathDocument, createYangInstanceIdentifier()));
+ }
+
+ public void testXpathWithPredicates() throws XPathExpressionException {
+ final Map<QName, Object> keys1 = new HashMap<>();
+ keys1.put(leafAQName, "bar");
+
+ final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath1 = new YangInstanceIdentifier
+ .NodeIdentifierWithPredicates(listAQName , keys1);
+
+ final Map<QName, Object> keys2 = new HashMap<>();
+ keys2.put(leafBQName, "two");
+
+ final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath2 = new YangInstanceIdentifier
+ .NodeIdentifierWithPredicates(listBQName , keys2);
+
+ YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(listAQName).node(mapEntryPath1)
+ .node(listBQName).node(mapEntryPath2).node(leafBQName);
+
+ String xPath = "/list-a[leaf-a='bar']/list-b[leaf-b='two']/leaf-b";
+
+ XPathExpression xpathExpressionWithPredicates = xpathSchemaContext.compileExpression(createSchemaPath(),
+ createPrefixes(), xPath);
+
+ Optional<? extends XPathResult<?>> resultExpressionEvaluate = xpathExpressionWithPredicates
+ .evaluate(xpathDocument, yangInstanceIdentifier);
+ assertNotNull(resultExpressionEvaluate);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testUnsupportedOperationException() {
+ assertNotNull(navigator.getCommentStringValue("Test"));
+ }
+
+ @Test(expected = VerifyException.class)
+ public void testIsMethodsInNodeNavigator() {
+ assertNotNull(navigator.isAttribute("test"));
+ assertNotNull(navigator.isComment("test"));
+ assertNotNull(navigator.isElement("test"));
+ assertNotNull(navigator.isNamespace("test"));
+ assertNotNull(navigator.isText("test"));
+ assertNotNull(navigator.isProcessingInstruction("test"));
+ assertNotNull(navigator.isDocument("test"));
+ }
+
+ @Test(expected = XPathExpressionException.class)
+ public void testCompileExpressionException() throws XPathExpressionException {
+ assertNotNull(xpathSchemaContext.compileExpression(createSchemaPath(), createPrefixes(), "/broken-path*"));
+ }
+
+ @Test(expected = UnresolvableException.class)
+ public void testYangFunctionContext() throws UnresolvableException {
+ YangFunctionContext yangFun = YangFunctionContext.getInstance();
+ assertNotNull(yangFun);
+ assertNotNull(yangFun.getFunction("urn:opendaylight.test2", null, "current"));
+ yangFun.getFunction("urn:opendaylight.test2", "test2", "root");
+ }
+
+ /**
+ * @return container-a -> container-b -> leaf-d
+ */
+ private YangInstanceIdentifier createYangInstanceIdentifier() {
+ return YangInstanceIdentifier.of(containerAQName).node(containerBQName).node(leafDQName);
+ }
+
+ private static String createXPath() {
+ return "/container-a/container-b/leaf-d";
+ }
+
+ private Converter<String, QNameModule> createPrefixes() {
+ BiMap<String, QNameModule> currentConverter = HashBiMap.create();
+ currentConverter.put("test2", moduleQName);
+
+ return Maps.asConverter(currentConverter);
+ }
+
+ /**
+ * rootQName -> listAQName -> leafAQName
+ * @return
+ */
+ private SchemaPath createSchemaPath() {
+ return SchemaPath.create(true, rootQName, listAQName, leafAQName);
+ }
+
+ private SchemaContext createSchemaContext() throws IOException, URISyntaxException {
+ return TestUtils.loadSchemaContext(getClass().getResource("/test/documentTest").toURI());
+ }
+
+ private static NormalizedNode<?, ?> createNormalizedNodes() {
+ return TestUtils.createNormalizedNodes();
+ }
+
+ private void initQNames() throws URISyntaxException, ParseException {
+ this.moduleQName = QNameModule.create(new URI("urn:opendaylight.test2"),
+ SimpleDateFormatUtil.getRevisionFormat().parse("2015-08-08"));
+ this.rootQName = QName.create(moduleQName, "root");
+ this.listAQName = QName.create(moduleQName, "list-a");
+ this.listBQName = QName.create(moduleQName, "list-b");
+ this.leafAQName = QName.create(moduleQName, "leaf-a");
+ this.leafBQName = QName.create(moduleQName, "leaf-b");
+ this.leafDQName = QName.create(moduleQName, "leaf-d");
+ this.containerAQName = QName.create(moduleQName, "container-a");
+ this.containerBQName = QName.create(moduleQName, "container-b");
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yangtools.yang.data.jaxen;
+
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
+import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+final class TestUtils {
+
+ private static final QName ROOT_QNAME = QName.create("urn:opendaylight.test2", "2015-08-08", "root");
+ private static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
+ private static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
+ private static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
+ private static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
+ private static final QName LEAF_C_QNAME = QName.create(ROOT_QNAME, "leaf-c");
+ private static final QName LEAF_D_QNAME = QName.create(ROOT_QNAME, "leaf-d");
+ private static final QName CONTAINER_A_QNAME = QName.create(ROOT_QNAME, "container-a");
+ private static final QName CONTAINER_B_QNAME = QName.create(ROOT_QNAME, "container-b");
+ private static final String FOO = "foo";
+ private static final String BAR = "bar";
+ private static final String WAZ = "waz";
+ private static final String ONE = "one";
+ private static final String TWO = "two";
+ private static final String THREE = "three";
+
+ private TestUtils() {
+ }
+
+ public static SchemaContext loadSchemaContext(final URI resourceDirectory) throws IOException {
+ final YangParserImpl parser = new YangParserImpl();
+ final File testDir = new File(resourceDirectory);
+ final String[] fileList = testDir.list();
+ final List<File> testFiles = new ArrayList<>();
+ if (fileList == null) {
+ throw new FileNotFoundException(resourceDirectory.toString());
+ }
+ for (String fileName : fileList) {
+ testFiles.add(new File(testDir, fileName));
+ }
+ SchemaContext ctx = parser.parseFiles(testFiles);
+ return ctx;
+ }
+
+ /**
+ * Returns a test document
+ *
+ * <pre>
+ * root
+ * leaf-c "waz"
+ * list-a
+ * leaf-a "foo"
+ * list-a
+ * leaf-a "bar"
+ * list-b
+ * leaf-b "one"
+ * list-b
+ * leaf-b "two"
+ * container-a
+ * container-b
+ * leaf-d "three"
+ * </pre>
+ *
+ * @return
+ */
+ public static NormalizedNode<?, ?> createNormalizedNodes() {
+ return ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
+ .withChild(ImmutableNodes.leafNode(LEAF_C_QNAME, WAZ))
+ .withChild(mapNodeBuilder(LIST_A_QNAME)
+ .withChild(mapEntry(LIST_A_QNAME, LEAF_A_QNAME, FOO))
+ .withChild(mapEntryBuilder(LIST_A_QNAME, LEAF_A_QNAME, BAR)
+ .withChild(mapNodeBuilder(LIST_B_QNAME)
+ .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, ONE))
+ .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, TWO))
+ .build())
+ .build())
+ .build())
+ .withChild(ImmutableContainerNodeBuilder.create()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONTAINER_A_QNAME))
+ .withChild(ImmutableContainerNodeBuilder.create().
+ withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier
+ (CONTAINER_B_QNAME))
+ .withChild(ImmutableNodes.leafNode(LEAF_D_QNAME, THREE))
+ .build())
+ .build())
+ .build();
+ }
+}
\ No newline at end of file