*/
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;
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 {
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)
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();
}
@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)
.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)
.withChild(
Builders.leafBuilder()
.withNodeIdentifier(leafFromCase)
+ .withValue("")
.build()).build()).build()).build())
.build();
}
@Test
- public void testListLastChildOverride() throws Exception {
+ public void testListLastChildOverride() {
final MapEntryNode outerListEntry = Builders
.mapEntryBuilder()
.withNodeIdentifier(outerListWithKey)
}
@Test
- public void testLeafList() throws Exception {
+ public void testLeafList() {
final ContainerNode expectedFilter = Builders
.containerBuilder()
.withNodeIdentifier(rootContainer)
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());
+ }
+}