Split out AbstractValidation
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodesTest.java
index 24081209befc582f1eaa8edc5e05f21a4da64b2e..860a7425b2fb52c6d98ee6f4881bf9f95b4ae693 100644 (file)
@@ -7,13 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.util.Collections;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import java.util.Collection;
+import java.util.Map;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.yangtools.util.ImmutableOffsetMap;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
@@ -26,8 +32,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.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class InstanceIdToNodesTest {
@@ -35,38 +39,40 @@ public class InstanceIdToNodesTest {
     private static final String NS = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:normalization:test";
     private static final String REVISION = "2014-03-13";
     private static final QName ID = QName.create(NS, REVISION, "id");
+    private static final QName FOO = QName.create(ID, "foo");
+    private static final QName BAR = QName.create(ID, "bar");
+    private static final NodeIdentifier TWO_KEY_LIST = NodeIdentifier.create(QName.create(ID, "two-key-list"));
+
     private static SchemaContext ctx;
 
     private final NodeIdentifier rootContainer = new NodeIdentifier(QName.create(NS, REVISION, "test"));
     private final NodeIdentifier outerContainer = new NodeIdentifier(QName.create(NS, REVISION, "outer-container"));
     private final NodeIdentifier augmentedLeaf = new NodeIdentifier(QName.create(NS, REVISION, "augmented-leaf"));
     private final AugmentationIdentifier augmentation = new AugmentationIdentifier(
-            Collections.singleton(augmentedLeaf.getNodeType()));
+            ImmutableSet.of(augmentedLeaf.getNodeType()));
 
-    private final NodeIdentifier outerList = new NodeIdentifier(
-            QName.create(NS, REVISION, "outer-list"));
-    private final NodeIdentifierWithPredicates outerListWithKey = new NodeIdentifierWithPredicates(
+    private final NodeIdentifier outerList = new NodeIdentifier(QName.create(NS, REVISION, "outer-list"));
+    private final NodeIdentifierWithPredicates outerListWithKey = NodeIdentifierWithPredicates.of(
             QName.create(NS, REVISION, "outer-list"), ID, 1);
     private final NodeIdentifier choice = new NodeIdentifier(QName.create(NS, REVISION, "outer-choice"));
     private final NodeIdentifier leafFromCase = new NodeIdentifier(QName.create(NS, REVISION, "one"));
 
     private final NodeIdentifier leafList = new NodeIdentifier(QName.create(NS, REVISION, "ordered-leaf-list"));
-    private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(
-            leafList.getNodeType(), "abcd");
-
-    static SchemaContext createTestContext() throws URISyntaxException, ReactorException, IOException,
-            YangSyntaxErrorException {
-        return YangParserTestUtils.parseYangResources(InstanceIdToNodesTest.class, "/filter-test.yang");
-    }
+    private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
 
     @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)
@@ -77,16 +83,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();
 
@@ -106,7 +111,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)
@@ -124,19 +130,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)
@@ -157,6 +163,7 @@ public class InstanceIdToNodesTest {
                                                                 .withChild(
                                                                         Builders.leafBuilder()
                                                                                 .withNodeIdentifier(leafFromCase)
+                                                                                .withValue("")
                                                                                 .build()).build()).build()).build())
                 .build();
 
@@ -166,7 +173,7 @@ public class InstanceIdToNodesTest {
     }
 
     @Test
-    public void testListLastChildOverride() throws Exception {
+    public void testListLastChildOverride() {
         final MapEntryNode outerListEntry = Builders
                 .mapEntryBuilder()
                 .withNodeIdentifier(outerListWithKey)
@@ -187,7 +194,7 @@ public class InstanceIdToNodesTest {
     }
 
     @Test
-    public void testLeafList() throws Exception {
+    public void testLeafList() {
         final ContainerNode expectedFilter = Builders
                 .containerBuilder()
                 .withNodeIdentifier(rootContainer)
@@ -202,4 +209,27 @@ 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()));
+    }
+
+    @Test
+    public void testKeyOrdering() {
+        final Map<QName, Object> misordered = ImmutableOffsetMap.orderedCopyOf(ImmutableMap.of(BAR, "bar", FOO, "foo"));
+        final NodeIdentifierWithPredicates id = NodeIdentifierWithPredicates.of(TWO_KEY_LIST.getNodeType(), misordered);
+        assertArrayEquals(new Object[] { BAR, FOO }, id.keySet().toArray());
+
+        final NormalizedNode<?, ?> filter = ImmutableNodes.fromInstanceId(ctx,
+            YangInstanceIdentifier.create(TWO_KEY_LIST, id));
+        assertThat(filter, isA(MapNode.class));
+        final Collection<MapEntryNode> value = ((MapNode) filter).getValue();
+        assertEquals(1, value.size());
+        final MapEntryNode entry = value.iterator().next();
+
+        // The entry must have a the proper order
+        assertArrayEquals(new Object[] { FOO, BAR }, entry.getIdentifier().keySet().toArray());
+    }
+}