Clean up TreeNode API
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / StoreTreeNodesTest.java
index cf747c343c6e0ecb43e0f3dd11d225328d3f6c7d..16ba7750b9f5d2b1b789d0f7878104203fe553c9 100644 (file)
@@ -7,37 +7,28 @@
  */
 package org.opendaylight.yangtools.yang.data.tree.impl;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-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.util.Map.Entry;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 import java.util.Optional;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
-import org.opendaylight.yangtools.yang.data.spi.tree.TreeNodeFactory;
-import org.opendaylight.yangtools.yang.data.spi.tree.Version;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
+import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
+import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class StoreTreeNodesTest extends AbstractTestModelTest {
-    private static final Logger LOG = LoggerFactory.getLogger(StoreTreeNodesTest.class);
 
+class StoreTreeNodesTest extends AbstractTestModelTest {
     private static final Short ONE_ID = 1;
     private static final Short TWO_ID = 2;
     private static final String TWO_ONE_NAME = "one";
@@ -58,135 +49,140 @@ public class StoreTreeNodesTest extends AbstractTestModelTest {
             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME)
             .build();
 
-    private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
-            .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME)
-                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
-                    .withChild(mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
-                    .build())
-                    .build();
+    private static final MapEntryNode BAR_NODE = ImmutableNodes.newMapEntryBuilder()
+        .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID))
+        .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, TWO_ID))
+        .withChild(ImmutableNodes.newSystemMapBuilder()
+            .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
+            .withChild(ImmutableNodes.newMapEntryBuilder()
+                .withNodeIdentifier(
+                    NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_ONE_NAME))
+                .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_ONE_NAME))
+                .build())
+            .withChild(ImmutableNodes.newMapEntryBuilder()
+                .withNodeIdentifier(
+                    NodeIdentifierWithPredicates.of(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, TWO_TWO_NAME))
+                .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, TWO_TWO_NAME))
+                .build())
+            .build())
+        .build();
 
     private RootApplyStrategy rootOper;
 
-    @Before
-    public void prepare() throws ExcludedDataSchemaNodeException {
+    @BeforeEach
+    void prepare() throws ExcludedDataSchemaNodeException {
         rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT,
             DataTreeConfiguration.DEFAULT_OPERATIONAL));
     }
 
-    public NormalizedNode createDocumentOne() {
-        return ImmutableContainerNodeBuilder
-                .create()
-                .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
-                .withChild(createTestContainer()).build();
-
+    public static ContainerNode createDocumentOne() {
+        return ImmutableNodes.newContainerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
+            .withChild(createTestContainer())
+            .build();
     }
 
     @Test
-    public void findNodeTestNodeFound() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
+    void findNodeTestNodeFound() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final var node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
         assertPresentAndType(node, TreeNode.class);
     }
 
     @Test
-    public void findNodeTestNodeNotFound() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+    void findNodeTestNodeNotFound() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
                 .build();
-        final Optional<? extends TreeNode> node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
+        final var node = StoreTreeNodes.findNode(rootNode, outerList1InvalidPath);
         assertFalse(node.isPresent());
     }
 
     @Test
-    public void findNodeCheckedTestNodeFound() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        TreeNode foundNode = null;
-        try {
-            foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
-        } catch (final IllegalArgumentException e) {
-            fail("Illegal argument exception was thrown and should not have been" + e.getMessage());
-        }
+    void findNodeCheckedTestNodeFound() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        TreeNode foundNode = StoreTreeNodes.findNodeChecked(rootNode, OUTER_LIST_1_PATH);
         assertNotNull(foundNode);
     }
 
     @Test
-    public void findNodeCheckedTestNodeNotFound() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
+    void findNodeCheckedTestNodeNotFound() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final var outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
                 .build();
-        try {
-            StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath);
-            fail("Illegal argument exception should have been thrown");
-        } catch (final IllegalArgumentException e) {
-            LOG.debug("Illegal argument exception was thrown as expected: '{}' - '{}'", e.getClass(), e.getMessage());
-        }
+        assertThrows(IllegalArgumentException.class,
+            () -> StoreTreeNodes.findNodeChecked(rootNode, outerList1InvalidPath));
     }
 
     @Test
-    public void findClosestOrFirstMatchTestNodeExists() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
+    void findClosestOrFirstMatchTestNodeExists() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final var expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
         assertPresentAndType(expectedNode, TreeNode.class);
 
-        final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
-        assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
+        final var actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
+        assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
     }
 
     @Test
-    public void findClosestOrFirstMatchTestNodeDoesNotExist() {
-        final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
-                TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
-        final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
-        final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
+    void findClosestOrFirstMatchTestNodeDoesNotExist() {
+        final var inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(SCHEMA_CONTEXT,
+                TreeNode.of(createDocumentOne(), Version.initial()), rootOper);
+        final var rootNode = inMemoryDataTreeSnapshot.getRootNode();
+        final var outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
                 .node(TestModel.INNER_LIST_QNAME)
                 .build();
-        final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
+        final var twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
                 .node(TestModel.INNER_LIST_QNAME)
                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
                 .build();
-        final Optional<? extends TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
+        final var expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
         assertPresentAndType(expectedNode, TreeNode.class);
 
-        final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
+        final var actualNode = StoreTreeNodes.findClosest(rootNode,
             twoTwoInvalidPath);
-        assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
+        assertTreeNodeEquals(expectedNode.orElseThrow(), actualNode.getValue());
     }
 
     private static ContainerNode createTestContainer() {
-        return ImmutableContainerNodeBuilder
-                .create()
-                .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
-                .withChild(
-                        mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
-                        .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
-                        .withChild(BAR_NODE).build()).build();
+        return ImmutableNodes.newContainerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
+            .withChild(ImmutableNodes.newSystemMapBuilder()
+                .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
+                .withChild(ImmutableNodes.newMapEntryBuilder()
+                    .withNodeIdentifier(
+                        NodeIdentifierWithPredicates.of(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
+                    .withChild(ImmutableNodes.leafNode(TestModel.ID_QNAME, ONE_ID))
+                    .build())
+                .withChild(BAR_NODE)
+                .build())
+            .build();
     }
 
     private static <T extends TreeNode> T assertPresentAndType(final Optional<? extends TreeNode> potential,
             final Class<T> type) {
         assertNotNull(potential);
         assertTrue(potential.isPresent());
-        assertTrue(type.isInstance(potential.get()));
-        return type.cast(potential.get());
+        assertTrue(type.isInstance(potential.orElseThrow()));
+        return type.cast(potential.orElseThrow());
     }
 
     private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
-        assertEquals(expected.getIdentifier(), actual.getIdentifier());
-        assertEquals(expected.getVersion(), actual.getVersion());
-        assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
-        assertEquals(expected.getData(), actual.getData());
+        assertEquals(expected.version(), actual.version());
+        assertEquals(expected.subtreeVersion(), actual.subtreeVersion());
+        assertEquals(expected.data(), actual.data());
         assertEquals(expected.toString(), actual.toString());
     }
 }