Bug 3876: unit test coverage increase 42/27442/4
authorFilip Gregor <filip.gregor@pantheon.sk>
Fri, 25 Sep 2015 14:44:29 +0000 (16:44 +0200)
committerRobert Varga <rovarga@cisco.com>
Sat, 26 Sep 2015 12:37:13 +0000 (14:37 +0200)
Add a simple test suite, cleanup pom.xml and fix a couple of typos.

Change-Id: Ib40f1344bea339bcf89c6f7981ba2888b1fe255e
Signed-off-by: Filip Gregor <filip.gregor@pantheon.sk>
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-jaxen/pom.xml
yang/yang-data-jaxen/src/main/java/org/opendaylight/yangtools/yang/data/jaxen/NormalizedNodeContext.java
yang/yang-data-jaxen/src/main/java/org/opendaylight/yangtools/yang/data/jaxen/NormalizedNodeNavigator.java
yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/JaxenTest.java [new file with mode: 0644]
yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/TestUtils.java [new file with mode: 0644]
yang/yang-data-jaxen/src/test/resources/test/documentTest/test2.yang [new file with mode: 0644]

index 760b0b5cbd93a4afb3d1af21ec53e0accfc87b03..9cc89468d2ff7c6e51828fcbb0ca01933684baf6 100644 (file)
     <artifactId>yang-data-jaxen</artifactId>
     <name>${project.artifactId}</name>
     <description>${project.artifactId}</description>
-
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-surefire-plugin</artifactId>
-                <version>${maven.surefire.version}</version>
-                <configuration>
-                <argLine>-Dlog4j.configuration=log4j-test.xml
-                    -Xmx1500m ${jacoco.agent.ut.arg}</argLine>
-                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
-                </configuration>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.felix</groupId>
-                <artifactId>maven-bundle-plugin</artifactId>
-                <extensions>true</extensions>
-                <configuration>
-                    <instructions>
-                        <Export-Package>
-                            org.opendaylight.yangtools.yang.data.jaxen
-                        </Export-Package>
-                    </instructions>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
+    <packaging>bundle</packaging>
 
     <dependencies>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>yang-model-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>yang-parser-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
index 5175b6d46249ac896280d1252071c7e3b428ad17..6186d55cf5965708a02b0e11181b0641515bd334 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.data.jaxen;
 
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.jaxen.Context;
@@ -29,6 +30,8 @@ final class NormalizedNodeContext extends Context implements Function<Normalized
         super(contextSupport);
         this.node = Preconditions.checkNotNull(node);
         this.parent = parent;
+
+        setNodeSet(ImmutableList.of(this));
     }
 
     @Nullable NormalizedNodeContext getParent() {
index 390dbf52e40f99734c9cba95237e77c4a3d13fc2..922c46cc90678bf2a1c380242058718d0a7238a4 100644 (file)
@@ -222,7 +222,7 @@ final class NormalizedNodeNavigator extends DefaultNavigator implements NamedAcc
 
         final QName qname = resolveQName(node, namespacePrefix, localName);
         @SuppressWarnings({ "unchecked", "rawtypes" })
-        final Optional<NormalizedNode<?, ?>> maybeChild = ((DataContainerNode)contextNode).getChild(new NodeIdentifier(qname));
+        final Optional<NormalizedNode<?, ?>> maybeChild = ((DataContainerNode)node).getChild(new NodeIdentifier(qname));
         if (!maybeChild.isPresent()) {
             return null;
         }
diff --git a/yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/JaxenTest.java b/yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/JaxenTest.java
new file mode 100644 (file)
index 0000000..6fea9fc
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+ * 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");
+    }
+}
diff --git a/yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/TestUtils.java b/yang/yang-data-jaxen/src/test/java/org/opendaylight/yangtools/yang/data/jaxen/TestUtils.java
new file mode 100644 (file)
index 0000000..4064753
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * 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
diff --git a/yang/yang-data-jaxen/src/test/resources/test/documentTest/test2.yang b/yang/yang-data-jaxen/src/test/resources/test/documentTest/test2.yang
new file mode 100644 (file)
index 0000000..0eb8467
--- /dev/null
@@ -0,0 +1,58 @@
+module test2 {
+    yang-version 1;
+    namespace "urn:opendaylight.test2";
+    prefix "test2";
+
+    organization "opendaylight";
+    contact "urn:opendaylight.com";
+    description "test description";
+
+    revision "2015-08-08" {
+        reference "1st edit";
+    }
+
+    container root {
+        leaf leaf-c {
+            type string;
+        }
+
+        list list-a {
+            key "leaf-a";
+
+            leaf leaf-a {
+                type string;
+            }
+
+            choice choice-a {
+                case one {
+                    leaf one {
+                        type string;
+                    }
+                }
+                case two-three {
+                    leaf two {
+                        type string;
+                    }
+                    leaf three {
+                        type string;
+                    }
+                }
+            }
+
+            list list-b {
+                key "leaf-b";
+                leaf leaf-b {
+                    type string;
+                }
+            }
+        }
+    }
+
+    container container-a {
+        container container-b {
+            leaf leaf-d {
+                type string;
+            }
+        }
+    }
+}
\ No newline at end of file