Reduce use of getChildByName()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / BuilderTest.java
index 0c2c6e8c5081a92e1971a2ca5ba85fc63d31d8b3..4af3852aad68860210f1a98afff7ef87ba26a1f5 100644 (file)
@@ -11,17 +11,19 @@ package org.opendaylight.yangtools.yang.data.impl.schema;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.mockito.Mockito.mock;
+
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.net.URISyntaxException;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyXmlSchemaNode;
 import org.opendaylight.yangtools.util.UnmodifiableCollection;
 import org.opendaylight.yangtools.yang.common.QName;
-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.YangInstanceIdentifier.NodeWithValue;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -60,28 +62,27 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ContainerEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class BuilderTest {
-    private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01", "root-container");
+    private static final QName ROOT_CONTAINER = QName.create("test.namespace.builder.test", "2016-01-01",
+        "root-container");
     private static final QName LIST_MAIN = QName.create(ROOT_CONTAINER, "list-ordered-by-user-with-key");
     private static final QName LEAF_LIST_MAIN = QName.create(ROOT_CONTAINER, "leaf-list-ordered-by-user");
     private static final QName LIST_MAIN_CHILD_QNAME_1 = QName.create(ROOT_CONTAINER, "leaf-a");
-    private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LIST = YangInstanceIdentifier.NodeIdentifier
-            .create(LIST_MAIN);
-    private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = YangInstanceIdentifier.NodeIdentifier
-            .create(LEAF_LIST_MAIN);
-    private static final YangInstanceIdentifier.NodeIdentifier NODE_IDENTIFIER_LEAF = YangInstanceIdentifier
-            .NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
-    private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 1);
-    private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 2);
-    private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1, 3);
-    private static final Integer SIZE = 3;
-    private static final NodeWithValue BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
-    private static final LeafSetEntryNode LEAF_SET_ENTRY_NODE = ImmutableLeafSetEntryNodeBuilder.create()
+    private static final NodeIdentifier NODE_IDENTIFIER_LIST = NodeIdentifier.create(LIST_MAIN);
+    private static final NodeIdentifier NODE_IDENTIFIER_LEAF_LIST = NodeIdentifier.create(LEAF_LIST_MAIN);
+    private static final NodeIdentifier NODE_IDENTIFIER_LEAF = NodeIdentifier.create(LIST_MAIN_CHILD_QNAME_1);
+    private static final MapEntryNode LIST_MAIN_CHILD_1 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
+            1);
+    private static final MapEntryNode LIST_MAIN_CHILD_2 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
+            2);
+    private static final MapEntryNode LIST_MAIN_CHILD_3 = ImmutableNodes.mapEntry(LIST_MAIN, LIST_MAIN_CHILD_QNAME_1,
+            3);
+    private static final int SIZE = 3;
+    private static final NodeWithValue<String> BAR_PATH = new NodeWithValue<>(LEAF_LIST_MAIN, "bar");
+    private static final LeafSetEntryNode<String> LEAF_SET_ENTRY_NODE =
+            ImmutableLeafSetEntryNodeBuilder.<String>create()
             .withNodeIdentifier(BAR_PATH)
             .withValue("bar")
             .build();
@@ -89,14 +90,14 @@ public class BuilderTest {
     private LeafListSchemaNode leafList;
 
     @Before
-    public void setup() throws FileNotFoundException, ReactorException, URISyntaxException {
+    public void setup() throws URISyntaxException {
         final File leafRefTestYang = new File(getClass().getResource("/builder-test/immutable-ordered-map-node.yang")
                 .toURI());
-        final SchemaContext schema = YangParserTestUtils.parseYangSources(leafRefTestYang);
+        final SchemaContext schema = YangParserTestUtils.parseYangFiles(leafRefTestYang);
         final Module module = schema.getModules().iterator().next();
-        final DataSchemaNode root = module.getDataChildByName(ROOT_CONTAINER);
-        list = (ListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LIST_MAIN);
-        leafList = (LeafListSchemaNode)((ContainerEffectiveStatementImpl) root).getDataChildByName(LEAF_LIST_MAIN);
+        final DataSchemaNode root = module.findDataChildByName(ROOT_CONTAINER).get();
+        list = (ListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LIST_MAIN).get();
+        leafList = (LeafListSchemaNode)((ContainerSchemaNode) root).findDataChildByName(LEAF_LIST_MAIN).get();
     }
 
     @Test
@@ -105,8 +106,7 @@ public class BuilderTest {
         mapEntryNodeColl.add(LIST_MAIN_CHILD_3);
         final Map<QName, Object> keys = new HashMap<>();
         keys.put(LIST_MAIN_CHILD_QNAME_1, 1);
-        final YangInstanceIdentifier.NodeIdentifierWithPredicates mapEntryPath = new YangInstanceIdentifier
-                .NodeIdentifierWithPredicates(LIST_MAIN, keys);
+        final NodeIdentifierWithPredicates mapEntryPath = new NodeIdentifierWithPredicates(LIST_MAIN, keys);
         final OrderedMapNode orderedMapNodeCreateNull = ImmutableOrderedMapNodeBuilder.create()
                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
                 .withChild(LIST_MAIN_CHILD_1)
@@ -122,24 +122,22 @@ public class BuilderTest {
         final OrderedMapNode orderedMapNodeSchemaAware = ImmutableOrderedMapNodeSchemaAwareBuilder.create(list)
                 .withChild(LIST_MAIN_CHILD_1)
                 .build();
-        final OrderedMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableOrderedMapNodeSchemaAwareBuilder.create
-                (list, getImmutableOrderedMapNode())
+        final OrderedMapNode orderedMapNodeSchemaAwareMapNodeConst = ImmutableOrderedMapNodeSchemaAwareBuilder.create(
+                list, getImmutableOrderedMapNode())
                 .build();
 
         assertNotNull(Builders.orderedMapBuilder(list));
-        assertEquals(SIZE, (Integer) orderedMapNodeCreateNull.getSize());
+        assertEquals(SIZE, orderedMapNodeCreateNull.getSize());
         assertEquals(orderedMapNodeCreateNode.getSize(), orderedMapNodeCreateNull.getSize() - 1);
         assertEquals(NODE_IDENTIFIER_LIST, orderedMapNodeCreateSize.getIdentifier());
-        assertEquals(orderedMapNodeCreateNull.getChild(0), LIST_MAIN_CHILD_1);
-        assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
-        assertNotNull(orderedMapNodeCreateNull.hashCode());
-        assertEquals((Integer) orderedMapNodeCreateNull.getValue().size(), SIZE);
+        assertEquals(LIST_MAIN_CHILD_1, orderedMapNodeCreateNull.getChild(0));
+        assertEquals(SIZE, orderedMapNodeCreateNull.getValue().size());
         assertEquals(orderedMapNodeSchemaAware.getChild(0), orderedMapNodeSchemaAwareMapNodeConst.getChild(0));
     }
 
     @Test
     public void immutableOrderedLeafSetNodeBuilderTest() {
-        final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.create()
+        final NormalizedNode<?, ?> orderedLeafSet = ImmutableOrderedLeafSetNodeBuilder.<String>create()
                 .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
                 .withChild(LEAF_SET_ENTRY_NODE)
                 .withChildValue("baz")
@@ -148,18 +146,15 @@ public class BuilderTest {
         final LinkedList<LeafSetNode<?>> mapEntryNodeColl = new LinkedList<>();
         mapEntryNodeColl.add((LeafSetNode<?>)orderedLeafSet);
         final UnmodifiableCollection<?> leafSetCollection = (UnmodifiableCollection<?>)orderedLeafSet.getValue();
-        final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList)
-                .withChildValue("baz")
-                .build();
-        final UnmodifiableCollection<?> SchemaAwareleafSetCollection = (UnmodifiableCollection<?>)orderedMapNodeSchemaAware
-                .getValue();
-        final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList,
-                (LeafSetNode<?>)orderedLeafSet)
-                .build();
+        final NormalizedNode<?, ?> orderedMapNodeSchemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
+            leafList).withChildValue("baz").build();
+        final UnmodifiableCollection<?> SchemaAwareleafSetCollection =
+                (UnmodifiableCollection<?>)orderedMapNodeSchemaAware.getValue();
+        final NormalizedNode<?, ?> orderedLeafSetShemaAware = ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(
+            leafList,(LeafSetNode<?>)orderedLeafSet).build();
 
         assertNotNull(Builders.orderedLeafSetBuilder(leafList));
         assertNotNull(Builders.anyXmlBuilder());
-        assertNotNull(orderedLeafSetShemaAware.hashCode());
         assertNotNull(orderedLeafSetShemaAware);
         assertEquals(1, ((OrderedLeafSetNode<?>)orderedLeafSet).getSize());
         assertEquals("baz", ((OrderedLeafSetNode<?>)orderedLeafSet).getChild(0).getValue());
@@ -177,8 +172,8 @@ public class BuilderTest {
         collectionNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
         collectionNodeBuilder.withValue(mapEntryNodeColl);
         final MapNode mapNode = collectionNodeBuilder.build();
-        assertNotNull(mapNode.hashCode());
-        final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
+        final MapNode mapNodeSchemaAware = ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode())
+                .build();
         assertNotNull(mapNodeSchemaAware);
         assertNotNull(Builders.mapBuilder(mapNode));
     }
@@ -191,8 +186,8 @@ public class BuilderTest {
         final UnkeyedListEntryNode unkeyedListEntryNodeSize = ImmutableUnkeyedListEntryNodeBuilder.create(1)
                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
                 .build();
-        final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder.create(unkeyedListEntryNode)
-                .build();
+        final UnkeyedListEntryNode unkeyedListEntryNodeNode = ImmutableUnkeyedListEntryNodeBuilder
+                .create(unkeyedListEntryNode).build();
         assertEquals(unkeyedListEntryNode.getNodeType().getLocalName(), unkeyedListEntryNodeSize.getNodeType()
                 .getLocalName());
         assertEquals(unkeyedListEntryNodeSize.getNodeType().getLocalName(), unkeyedListEntryNodeNode.getNodeType()
@@ -206,7 +201,8 @@ public class BuilderTest {
                 .build();
         final ImmutableUnkeyedListNodeBuilder immutableUnkeyedListNodeBuilder = (ImmutableUnkeyedListNodeBuilder)
                 ImmutableUnkeyedListNodeBuilder.create();
-        final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
+        final UnkeyedListNode unkeyedListNode = immutableUnkeyedListNodeBuilder
+                .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
                 .addChild(unkeyedListEntryNode)
                 .build();
         final UnkeyedListNode unkeyedListNodeSize = ImmutableUnkeyedListNodeBuilder.create(1)
@@ -217,9 +213,9 @@ public class BuilderTest {
         try {
             unkeyedListNodeSize.getChild(1);
         } catch (IndexOutOfBoundsException e) {
+            // Ignored on purpose
         }
 
-        assertNotNull(unkeyedListNodeSize.getSize());
         assertNotNull(unkeyedListNodeSize.getValue());
         assertEquals(unkeyedListEntryNode, unkeyedListNodeCreated.getChild(0));
         assertEquals(unkeyedListNode.getNodeType().getLocalName(), unkeyedListNodeSize.getNodeType()
@@ -236,12 +232,12 @@ public class BuilderTest {
     }
 
 
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void immutableAugmentationNodeBuilderExceptionTest() {
         ImmutableAugmentationNodeBuilder.create(1).build();
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void immutableContainerNodeBuilderExceptionTest() {
         final ContainerNode immutableContainerNode = ImmutableContainerNodeBuilder.create(1)
                 .withNodeIdentifier(NODE_IDENTIFIER_LIST)
@@ -253,79 +249,82 @@ public class BuilderTest {
                 .build();
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void immutableLeafSetNodeBuilderExceptionTest() {
-        final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
-                .build();
+        final LeafSetNode<?> leafSetNode = ImmutableLeafSetNodeBuilder.create(1)
+                .withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
         assertNotNull(leafSetNode);
         ImmutableLeafSetNodeSchemaAwareBuilder.create(mock(LeafListSchemaNode.class), leafSetNode).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableLeafSetEntryNodeSchemaAwareBuilderExceptionTest() {
         final LeafListSchemaNode leafListSchemaNode = mock(LeafListSchemaNode.class);
         ImmutableLeafSetEntryNodeSchemaAwareBuilder.create(leafListSchemaNode).withNodeIdentifier(BAR_PATH).build();
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void immutableMapEntryNodeBuilderExceptionTest() {
         ImmutableMapEntryNodeBuilder.create(1).build();
     }
 
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void immutableYangModeledAnyXmlNodeBuilderExceptionTest() {
         ImmutableYangModeledAnyXmlNodeBuilder.create(mock(YangModeledAnyXmlSchemaNode.class), 1);
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableUnkeyedListNodeBuilderExceptionTest() {
         ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LEAF)
                 .removeChild(NODE_IDENTIFIER_LIST).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableOrderedMapNotSchemaAwareExceptionTest1() {
         ImmutableOrderedMapNodeBuilder.create(getImmutableMapNode()).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableMapNodeSchemaAwareExceptionTest() {
-        ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
+        ImmutableMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).withNodeIdentifier(NODE_IDENTIFIER_LIST)
+        .build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableOrderedMapSchemaAwareExceptionTest1() {
         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list).withNodeIdentifier(NODE_IDENTIFIER_LIST).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableOrderedMapSchemaAwareExceptionTest2() {
         ImmutableOrderedMapNodeSchemaAwareBuilder.create(list, getImmutableMapNode()).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableOrderedLeafSetNodeExceptionTest1() {
         ImmutableOrderedLeafSetNodeBuilder.create(getImmutableLeafSetNode()).build();
     }
 
-    @Test(expected=UnsupportedOperationException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void immutableOrderedLeafSetNodeSchemaAwareExceptionTest1() {
-        ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST).build();
+        ImmutableOrderedLeafSetNodeSchemaAwareBuilder.create(leafList).withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST)
+        .build();
     }
 
     private static LeafSetNode<?> getImmutableLeafSetNode() {
-        final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders.leafSetBuilder();
+        final ListNodeBuilder<String, LeafSetEntryNode<String>> leafSetBuilder = Builders.leafSetBuilder();
         leafSetBuilder.withNodeIdentifier(NODE_IDENTIFIER_LEAF_LIST);
         leafSetBuilder.addChild(LEAF_SET_ENTRY_NODE);
         return leafSetBuilder.build();
     }
 
     private static MapNode getImmutableMapNode() {
-        return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1).build();
+        return ImmutableMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
+                .build();
     }
 
     private static MapNode getImmutableOrderedMapNode() {
-        return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST).withChild(LIST_MAIN_CHILD_1)
-                .build();
+        return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(NODE_IDENTIFIER_LIST)
+                .withChild(LIST_MAIN_CHILD_1).build();
     }
-}
\ No newline at end of file
+}