Split out AbstractValidation
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodesTest.java
index ca75650f941cc812f2eab59ac4cc6c89ccdca826..860a7425b2fb52c6d98ee6f4881bf9f95b4ae693 100644 (file)
@@ -7,12 +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.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;
@@ -32,24 +39,26 @@ 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");
+    private final NodeWithValue<?> leafListWithValue = new NodeWithValue<>(leafList.getNodeType(), "abcd");
 
     @BeforeClass
     public static void setUp() {
@@ -204,6 +213,23 @@ public class InstanceIdToNodesTest {
     @Test
     public void testEmptyInstanceIdentifier() {
         assertEquals(ImmutableNodes.containerNode(SchemaContext.NAME),
-            ImmutableNodes.fromInstanceId(ctx, YangInstanceIdentifier.EMPTY));
+            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());
     }
 }