Cleanup testing AugmentationIdentifier instantiations
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
index 970425e1f51715b5b50922425b1c52d10eb9f39f..7e9a77a335df98fd612c28a521ef44f1bb8c04cb 100644 (file)
@@ -7,15 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Sets;
+import static com.google.common.base.Preconditions.checkState;
+
+import com.google.common.collect.ImmutableSet;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -33,7 +31,8 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeSchemaAwareBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeSchemaAwareBuilder;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -43,7 +42,6 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 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.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class NormalizedDataBuilderTest {
@@ -52,10 +50,8 @@ public class NormalizedDataBuilderTest {
     private SchemaContext schema;
 
     @Before
-    public void setUp() throws URISyntaxException, FileNotFoundException, ReactorException {
-        final File resourceFile = new File(getClass().getResource(
-                "test.yang").toURI());
-        schema = YangParserTestUtils.parseYangFiles(resourceFile);
+    public void setUp() throws URISyntaxException {
+        schema = YangParserTestUtils.parseYangFiles(new File(getClass().getResource("test.yang").toURI()));
         containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
     }
 
@@ -87,8 +83,7 @@ public class NormalizedDataBuilderTest {
                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
                                 .withValue(1).build())
                 .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
-                .withNodeIdentifier(
-                        new NodeIdentifierWithPredicates(
+                .withNodeIdentifier(NodeIdentifierWithPredicates.of(
                                 getNodeIdentifier("list").getNodeType(), Collections.singletonMap(
                                 getNodeIdentifier("uint32InList").getNodeType(), 1))).build();
 
@@ -99,7 +94,7 @@ public class NormalizedDataBuilderTest {
         AugmentationNode augmentation = Builders
                 .augmentationBuilder()
                 .withNodeIdentifier(
-                        new AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
+                        new AugmentationIdentifier(ImmutableSet.of(getQName("augmentUint32"))))
                 .withChild(
                         Builders.<Integer>leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
                                 .withValue(11).build()).build();
@@ -138,7 +133,7 @@ public class NormalizedDataBuilderTest {
         builder.withChild(list);
 
         LeafSchemaNode augmentUint32SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentUint32");
-        AugmentationSchema augmentationSchema = getAugmentationSchemaForChild(containerNode,
+        AugmentationSchemaNode augmentationSchema = getAugmentationSchemaForChild(containerNode,
                 augmentUint32SchemaNode.getQName());
 
         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
@@ -168,10 +163,10 @@ public class NormalizedDataBuilderTest {
         // .build());
     }
 
-    private static AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
+    private static AugmentationSchemaNode getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
             final QName qname) {
-        for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
-            if (augmentationSchema.getDataChildByName(qname) != null) {
+        for (AugmentationSchemaNode augmentationSchema : containerNode.getAvailableAugmentations()) {
+            if (augmentationSchema.findDataChildByName(qname).isPresent()) {
                 return augmentationSchema;
             }
         }
@@ -183,8 +178,7 @@ public class NormalizedDataBuilderTest {
     }
 
     private static QName getQName(final String localName) {
-        String namespace = "namespace";
-        return new QName(URI.create(namespace), localName);
+        return QName.create(URI.create("namespace"), localName);
     }
 
     private static NodeIdentifier getNodeIdentifier(final String localName) {
@@ -195,35 +189,33 @@ public class NormalizedDataBuilderTest {
             final String childNodeName) {
         for (Module module : context.getModules()) {
             if (module.getName().equals(moduleName)) {
-                DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
-                Preconditions.checkState(found != null, "Unable to find %s", childNodeName);
+                DataSchemaNode found = findChildNode(module, childNodeName);
+                checkState(found != null, "Unable to find %s", childNodeName);
                 return found;
             }
         }
         throw new IllegalStateException("Unable to find child node " + childNodeName);
     }
 
-    private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
-        List<DataNodeContainer> containers = new ArrayList<>();
-
-        for (DataSchemaNode dataSchemaNode : children) {
+    private static DataSchemaNode findChildNode(final DataNodeContainer container, final String name) {
+        for (DataSchemaNode dataSchemaNode : container.getChildNodes()) {
             if (dataSchemaNode.getQName().getLocalName().equals(name)) {
                 return dataSchemaNode;
             }
             if (dataSchemaNode instanceof DataNodeContainer) {
-                containers.add((DataNodeContainer) dataSchemaNode);
+                DataSchemaNode retVal = findChildNode((DataNodeContainer) dataSchemaNode, name);
+                if (retVal != null) {
+                    return retVal;
+                }
             } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
-                containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
+                for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) dataSchemaNode).getCases().values()) {
+                    DataSchemaNode retVal = findChildNode(caseNode, name);
+                    if (retVal != null) {
+                        return retVal;
+                    }
+                }
             }
         }
-
-        for (DataNodeContainer container : containers) {
-            DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
-            if (retVal != null) {
-                return retVal;
-            }
-        }
-
         return null;
     }
 }