Use Collections.singletonList() instead of Arrays.asList()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
index 6fcc0f345db85206043ad4a230dc4b5467f7ffb2..9125f66180f8541993f187534031f277def83276 100644 (file)
@@ -7,17 +7,26 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
-import java.io.InputStream;
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
 import java.net.URI;
 import java.util.Collections;
 import java.util.List;
-import java.util.Set;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+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.AugmentationNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
@@ -27,7 +36,7 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContaine
 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.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -36,36 +45,30 @@ 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.model.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
 
-import com.google.common.base.Function;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-
 public class NormalizedDataBuilderTest {
 
     private ContainerSchemaNode containerNode;
     private SchemaContext schema;
 
-    SchemaContext parseTestSchema(String... yangPath) {
+    SchemaContext parseTestSchema(final String... yangPath) throws IOException, YangSyntaxErrorException {
         YangParserImpl yangParserImpl = new YangParserImpl();
-        Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
-        return yangParserImpl.resolveSchemaContext(modules);
+        return yangParserImpl.parseSources(getTestYangs(yangPath));
     }
 
-    List<InputStream> getTestYangs(String... yangPaths) {
+    List<ByteSource> getTestYangs(final String... yangPaths) {
 
         return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
-                new Function<String, InputStream>() {
-                    @Override
-                    public InputStream apply(String input) {
-                        InputStream resourceAsStream = getClass().getResourceAsStream(input);
-                        Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
-                        return resourceAsStream;
-                    }
-                }));
+                new Function<String, ByteSource>() {
+            @Override
+            public ByteSource apply(final String input) {
+                ByteSource resourceAsStream = Resources.asByteSource(getClass().getResource(input));
+                Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
+                return resourceAsStream;
+            }
+        }));
     }
 
     @Before
@@ -77,7 +80,7 @@ public class NormalizedDataBuilderTest {
     @Test
     public void testSchemaUnaware() throws Exception {
         // Container
-        DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+        DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
 
         // leaf
@@ -92,8 +95,8 @@ public class NormalizedDataBuilderTest {
                 .withChildValue(1)
                 .withChild(
                         Builders.<Integer> leafSetEntryBuilder()
-                                .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
-                .build();
+                        .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build())
+                        .build();
         builder.withChild(leafList);
 
         // list
@@ -101,12 +104,12 @@ public class NormalizedDataBuilderTest {
                 .mapEntryBuilder()
                 .withChild(
                         Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
-                                .withValue(1).build())
-                .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
-                .withNodeIdentifier(
-                        new InstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
-                                Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1)))
-                .build();
+                        .withValue(1).build())
+                        .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
+                        .withNodeIdentifier(
+                                new NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
+                                        Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1)))
+                                        .build();
 
         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
                 .build();
@@ -115,9 +118,9 @@ public class NormalizedDataBuilderTest {
         AugmentationNode augmentation = Builders
                 .augmentationBuilder()
                 .withNodeIdentifier(
-                        new InstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
-                .withChild(
-                        Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
+                        new AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
+                        .withChild(
+                                Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
                                 .withValue(11).build()).build();
 
         builder.withChild(augmentation);
@@ -130,7 +133,7 @@ public class NormalizedDataBuilderTest {
 
     @Test
     public void testSchemaAware() throws Exception {
-        DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+        DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder(containerNode);
 
         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
@@ -169,8 +172,8 @@ public class NormalizedDataBuilderTest {
         LeafSchemaNode augumentString1SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString1");
         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
 
-        ChoiceNode choice1SchemaNode = (ChoiceNode) getSchemaNode(schema, "test", "choice");
-        org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder
+        ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
+        ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder
                 .create(choice1SchemaNode)
                 .withChild(Builders.<String> leafBuilder(augumentString1SchemaNode).withValue("case1").build())
                 // This should fail, since child node belongs to different case
@@ -186,7 +189,7 @@ public class NormalizedDataBuilderTest {
         // .build());
     }
 
-    private AugmentationSchema getAugmentationSchemaForChild(ContainerSchemaNode containerNode, QName qName) {
+    private static AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode, final QName qName) {
         for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
             if (augmentationSchema.getDataChildByName(qName) != null) {
                 return augmentationSchema;
@@ -195,20 +198,20 @@ public class NormalizedDataBuilderTest {
         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
     }
 
-    private InstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(String localName, Object value) {
-        return new InstanceIdentifier.NodeWithValue(getQName(localName), value);
+    private static <T> NodeWithValue<T> getNodeWithValueIdentifier(final String localName, final T value) {
+        return new NodeWithValue<>(getQName(localName), value);
     }
 
-    private QName getQName(String localName) {
+    private static QName getQName(final String localName) {
         String namespace = "namespace";
         return new QName(URI.create(namespace), localName);
     }
 
-    private InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
-        return new InstanceIdentifier.NodeIdentifier(getQName(localName));
+    private static NodeIdentifier getNodeIdentifier(final String localName) {
+        return new NodeIdentifier(getQName(localName));
     }
 
-    public static DataSchemaNode getSchemaNode(SchemaContext context, String moduleName, String childNodeName) {
+    public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, final String childNodeName) {
         for (Module module : context.getModules()) {
             if (module.getName().equals(moduleName)) {
                 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
@@ -219,16 +222,17 @@ public class NormalizedDataBuilderTest {
         throw new IllegalStateException("Unable to find child node " + childNodeName);
     }
 
-    static DataSchemaNode findChildNode(Set<DataSchemaNode> children, String name) {
+    private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
         List<DataNodeContainer> containers = Lists.newArrayList();
 
         for (DataSchemaNode dataSchemaNode : children) {
-            if (dataSchemaNode.getQName().getLocalName().equals(name))
+            if (dataSchemaNode.getQName().getLocalName().equals(name)) {
                 return dataSchemaNode;
+            }
             if (dataSchemaNode instanceof DataNodeContainer) {
                 containers.add((DataNodeContainer) dataSchemaNode);
-            } else if (dataSchemaNode instanceof ChoiceNode) {
-                containers.addAll(((ChoiceNode) dataSchemaNode).getCases());
+            } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
+                containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
             }
         }