Tolerate empty instance identifier in ImmutableNodes.fromInstanceId()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodesTest.java
index c362d44d3bdac7a774b0388b9dd2761eabd48acc..ca75650f941cc812f2eab59ac4cc6c89ccdca826 100644 (file)
@@ -8,10 +8,9 @@
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
 import static org.junit.Assert.assertEquals;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.net.URISyntaxException;
+
 import java.util.Collections;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -26,7 +25,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class InstanceIdToNodesTest {
@@ -53,19 +51,19 @@ public class InstanceIdToNodesTest {
     private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(
             leafList.getNodeType(), "abcd");
 
-    static SchemaContext createTestContext() throws URISyntaxException, FileNotFoundException, ReactorException {
-        final File resourceFile = new File(InstanceIdToNodesTest.class.getResource("/filter-test.yang").toURI());
-        return YangParserTestUtils.parseYangSources(resourceFile);
-    }
-
     @BeforeClass
-    public static void setUp() throws Exception {
-        ctx = createTestContext();
+    public static void setUp() {
+        ctx = YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
+    }
 
+    @AfterClass
+    public static void teardown() {
+        ctx = null;
     }
 
     @Test
-    public void testInAugment() throws Exception {
+    public void testInAugment() {
+        final LeafNode<?> leaf = Builders.leafBuilder().withNodeIdentifier(augmentedLeaf).withValue("").build();
         final ContainerNode expectedFilter = Builders
                 .containerBuilder()
                 .withNodeIdentifier(rootContainer)
@@ -76,16 +74,15 @@ public class InstanceIdToNodesTest {
                                         Builders.augmentationBuilder()
                                                 .withNodeIdentifier(augmentation)
                                                 .withChild(
-                                                        Builders.leafBuilder().withNodeIdentifier(augmentedLeaf)
-                                                                .build()).build()).build()).build();
+                                                        leaf).build()).build()).build();
 
         final NormalizedNode<?, ?> filter = ImmutableNodes.fromInstanceId(ctx,
-                YangInstanceIdentifier.create(rootContainer, outerContainer, augmentation, augmentedLeaf));
+                YangInstanceIdentifier.create(rootContainer, outerContainer, augmentation, augmentedLeaf), leaf);
         assertEquals(expectedFilter, filter);
     }
 
     @Test
-    public void testInAugmentLeafOverride() throws Exception {
+    public void testInAugmentLeafOverride() {
         final LeafNode<Object> lastLeaf = Builders.leafBuilder().withNodeIdentifier(augmentedLeaf)
                 .withValue("randomValue").build();
 
@@ -105,7 +102,8 @@ public class InstanceIdToNodesTest {
     }
 
     @Test
-    public void testListChoice() throws Exception {
+    public void testListChoice() {
+        final LeafNode<?> leaf = Builders.leafBuilder().withNodeIdentifier(leafFromCase).withValue("").build();
         final ContainerNode expectedFilter = Builders
                 .containerBuilder()
                 .withNodeIdentifier(rootContainer)
@@ -123,19 +121,19 @@ public class InstanceIdToNodesTest {
                                                 .withChild(
                                                         Builders.choiceBuilder()
                                                                 .withNodeIdentifier(choice)
-                                                                .withChild(
-                                                                        Builders.leafBuilder()
-                                                                                .withNodeIdentifier(leafFromCase)
-                                                                                .build()).build()).build()).build())
+                                                                .withChild(leaf)
+                                                                .build())
+                                                .build())
+                                .build())
                 .build();
 
         final NormalizedNode<?, ?> filter = ImmutableNodes.fromInstanceId(ctx,
-                YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey, choice, leafFromCase));
+                YangInstanceIdentifier.create(rootContainer, outerList, outerListWithKey, choice, leafFromCase), leaf);
         assertEquals(expectedFilter, filter);
     }
 
     @Test
-    public void testTopContainerLastChildOverride() throws Exception {
+    public void testTopContainerLastChildOverride() {
         final ContainerNode expectedStructure = Builders
                 .containerBuilder()
                 .withNodeIdentifier(rootContainer)
@@ -156,6 +154,7 @@ public class InstanceIdToNodesTest {
                                                                 .withChild(
                                                                         Builders.leafBuilder()
                                                                                 .withNodeIdentifier(leafFromCase)
+                                                                                .withValue("")
                                                                                 .build()).build()).build()).build())
                 .build();
 
@@ -165,7 +164,7 @@ public class InstanceIdToNodesTest {
     }
 
     @Test
-    public void testListLastChildOverride() throws Exception {
+    public void testListLastChildOverride() {
         final MapEntryNode outerListEntry = Builders
                 .mapEntryBuilder()
                 .withNodeIdentifier(outerListWithKey)
@@ -186,7 +185,7 @@ public class InstanceIdToNodesTest {
     }
 
     @Test
-    public void testLeafList() throws Exception {
+    public void testLeafList() {
         final ContainerNode expectedFilter = Builders
                 .containerBuilder()
                 .withNodeIdentifier(rootContainer)
@@ -201,4 +200,10 @@ public class InstanceIdToNodesTest {
                 YangInstanceIdentifier.create(rootContainer, leafList, leafListWithValue));
         assertEquals(expectedFilter, filter);
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testEmptyInstanceIdentifier() {
+        assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
+            ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.EMPTY));
+    }
+}