Bug 3899: Milestone: Increase test coverage for Yangtools
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedDataBuilderTest.java
index d15c6339c72b93d3c28f638d5ed197e940d2fe24..38cdb287e40528bb23b6dccfeac9a3d22229a7ff 100644 (file)
@@ -12,18 +12,18 @@ 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 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;
 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;
@@ -33,7 +33,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;
@@ -42,6 +42,7 @@ 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;
 
 public class NormalizedDataBuilderTest {
@@ -49,19 +50,18 @@ public class NormalizedDataBuilderTest {
     private ContainerSchemaNode containerNode;
     private SchemaContext schema;
 
-    SchemaContext parseTestSchema(final 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(final String... yangPaths) {
+    List<ByteSource> getTestYangs(final String... yangPaths) {
 
         return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
-                new Function<String, InputStream>() {
+                new Function<String, ByteSource>() {
             @Override
-            public InputStream apply(final String input) {
-                InputStream resourceAsStream = getClass().getResourceAsStream(input);
+            public ByteSource apply(final String input) {
+                ByteSource resourceAsStream = Resources.asByteSource(getClass().getResource(input));
                 Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
                 return resourceAsStream;
             }
@@ -77,7 +77,7 @@ public class NormalizedDataBuilderTest {
     @Test
     public void testSchemaUnaware() throws Exception {
         // Container
-        DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+        DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder().withNodeIdentifier(getNodeIdentifier("container"));
 
         // leaf
@@ -104,7 +104,7 @@ public class NormalizedDataBuilderTest {
                         .withValue(1).build())
                         .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build())
                         .withNodeIdentifier(
-                                new InstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
+                                new YangInstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(),
                                         Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1)))
                                         .build();
 
@@ -115,7 +115,7 @@ public class NormalizedDataBuilderTest {
         AugmentationNode augmentation = Builders
                 .augmentationBuilder()
                 .withNodeIdentifier(
-                        new InstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
+                        new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32"))))
                         .withChild(
                                 Builders.<Integer> leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32"))
                                 .withValue(11).build()).build();
@@ -130,7 +130,7 @@ public class NormalizedDataBuilderTest {
 
     @Test
     public void testSchemaAware() throws Exception {
-        DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
+        DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders
                 .containerBuilder(containerNode);
 
         LeafSchemaNode schemaNode = (LeafSchemaNode) getSchemaNode(schema, "test", "uint32");
@@ -169,8 +169,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 +186,7 @@ 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;
@@ -195,17 +195,17 @@ public class NormalizedDataBuilderTest {
         throw new IllegalStateException("Unable to find child augmentation in " + containerNode);
     }
 
-    private InstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) {
-        return new InstanceIdentifier.NodeWithValue(getQName(localName), value);
+    private static YangInstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) {
+        return new YangInstanceIdentifier.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 InstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
-        return new InstanceIdentifier.NodeIdentifier(getQName(localName));
+    private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
+        return new YangInstanceIdentifier.NodeIdentifier(getQName(localName));
     }
 
     public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, final String childNodeName) {
@@ -228,8 +228,8 @@ public class NormalizedDataBuilderTest {
             }
             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());
             }
         }