Cleanup codecs a bit
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
index 9c69016e19c4a086dd2496be4523fcf9b0d02b1b..e7799ba4a16d4967e58e1e857f569df67a20610a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,20 +7,22 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
-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 java.io.InputStream;
+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 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.YangInstanceIdentifier;
+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;
@@ -41,42 +43,26 @@ 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.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class NormalizedDataBuilderTest {
 
     private ContainerSchemaNode containerNode;
     private SchemaContext schema;
 
-    SchemaContext parseTestSchema(final String... yangPath) {
-        YangParserImpl yangParserImpl = new YangParserImpl();
-        Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
-        return yangParserImpl.resolveSchemaContext(modules);
-    }
-
-    List<InputStream> getTestYangs(final String... yangPaths) {
-
-        return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
-                new Function<String, InputStream>() {
-            @Override
-            public InputStream apply(final String input) {
-                InputStream resourceAsStream = getClass().getResourceAsStream(input);
-                Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
-                return resourceAsStream;
-            }
-        }));
-    }
-
     @Before
-    public void setUp() throws Exception {
-        schema = parseTestSchema("test.yang");
+    public void setUp() throws URISyntaxException, FileNotFoundException, ReactorException {
+        final File resourceFile = new File(getClass().getResource(
+                "test.yang").toURI());
+        schema = YangParserTestUtils.parseYangSources(resourceFile);
         containerNode = (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
     }
 
     @Test
-    public void testSchemaUnaware() throws Exception {
+    public void testSchemaUnaware() {
         // Container
-        DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+        DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
 
         // leaf
@@ -91,8 +77,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
@@ -100,12 +86,12 @@ public class NormalizedDataBuilderTest {
                 .mapEntryBuilder()
                 .withChild(
                         Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"))
-                        .withValue(1).build())
-                        .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
-                        .withNodeIdentifier(
-                                new YangInstanceIdentifier.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(), 1))).build();
 
         MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list"))
                 .build();
@@ -114,9 +100,9 @@ public class NormalizedDataBuilderTest {
         AugmentationNode augmentation = Builders
                 .augmentationBuilder()
                 .withNodeIdentifier(
-                        new YangInstanceIdentifier.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);
@@ -128,8 +114,8 @@ public class NormalizedDataBuilderTest {
     }
 
     @Test
-    public void testSchemaAware() throws Exception {
-        DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+    public void testSchemaAware() {
+        DataContainerNodeBuilder<NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder(containerNode);
 
         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
@@ -158,7 +144,7 @@ public class NormalizedDataBuilderTest {
                 augmentUint32SchemaNode.getQName());
 
         AugmentationNode augmentation = Builders.augmentationBuilder(augmentationSchema)
-                .withChild(Builders.<Integer>leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
+                .withChild(Builders.<Integer> leafBuilder(augmentUint32SchemaNode).withValue(11).build()).build();
 
         builder.withChild(augmentation);
 
@@ -169,15 +155,13 @@ public class NormalizedDataBuilderTest {
         LeafSchemaNode augumentString2SchemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "augmentString2");
 
         ChoiceSchemaNode choice1SchemaNode = (ChoiceSchemaNode) getSchemaNode(schema, "test", "choice");
-        ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder
-                .create(choice1SchemaNode)
+        ChoiceNode choice = ImmutableChoiceNodeSchemaAwareBuilder.create(choice1SchemaNode)
                 .withChild(Builders.<String> leafBuilder(augumentString1SchemaNode).withValue("case1").build())
                 // This should fail, since child node belongs to different case
                 // .withChild(Builders.<String>leafBuilder(augumentString2SchemaNode).withValue("case2")
                 // .build())
                 .build();
 
-        ;
         builder.withChild(choice);
 
         // This should fail, child from case
@@ -185,7 +169,8 @@ public class NormalizedDataBuilderTest {
         // .build());
     }
 
-    private AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode, final QName qName) {
+    private static AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode,
+            final QName qName) {
         for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) {
             if (augmentationSchema.getDataChildByName(qName) != null) {
                 return augmentationSchema;
@@ -194,20 +179,21 @@ public class NormalizedDataBuilderTest {
         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
     }
 
-    private YangInstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) {
-        return new YangInstanceIdentifier.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(final String localName) {
+    private static QName getQName(final String localName) {
         String namespace = "namespace";
         return new QName(URI.create(namespace), localName);
     }
 
-    private YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
-        return new YangInstanceIdentifier.NodeIdentifier(getQName(localName));
+    private static NodeIdentifier getNodeIdentifier(final String localName) {
+        return new NodeIdentifier(getQName(localName));
     }
 
-    public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, final 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,7 +205,7 @@ public class NormalizedDataBuilderTest {
     }
 
     private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
-        List<DataNodeContainer> containers = Lists.newArrayList();
+        List<DataNodeContainer> containers = new ArrayList<>();
 
         for (DataSchemaNode dataSchemaNode : children) {
             if (dataSchemaNode.getQName().getLocalName().equals(name)) {