Sal-rest-connector unit tests switch to new yang parser. 50/33950/2
authorPeter Kajsa <pkajsa@cisco.com>
Tue, 2 Feb 2016 16:08:36 +0000 (17:08 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Fri, 19 Feb 2016 14:22:59 +0000 (14:22 +0000)
Change-Id: I4efa8e265ca2827033cb0cd3f0b8be3924616842
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
(cherry picked from commit 2dd8dce2a8260edb330a7ade345e42b06538a414)

opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java
opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java
opendaylight/restconf/sal-rest-connector/src/test/resources/nn-to-json/augmentation/yang.yang

index c3fae1bcc45d3073f44739b00b0ac84579e1eb22..9dea0b95cd0a517b45f5ad50a6a11c4a9b0769e8 100644 (file)
@@ -8,18 +8,19 @@
 
 package org.opendaylight.controller.md.sal.rest.common;
 
-import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.io.InputStream;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+
 import org.opendaylight.controller.sal.rest.impl.test.providers.TestJsonBodyWriter;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
@@ -34,14 +35,19 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
+import com.google.common.base.Preconditions;
+
 /**
  * sal-rest-connector
  * org.opendaylight.controller.md.sal.rest.common
@@ -56,8 +62,6 @@ public class TestRestconfUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(TestRestconfUtils.class);
 
-    private static final YangContextParser parser = new YangParserImpl();
-
     private static final DocumentBuilderFactory BUILDERFACTORY;
 
     static {
@@ -89,7 +93,7 @@ public class TestRestconfUtils {
             if (schemaContext == null) {
                 return loadSchemaContext(yangPath);
             } else {
-                return addSchemaContext(yangPath, schemaContext);
+                throw new UnsupportedOperationException("Unable to add new yang sources to existing schema context.");
             }
         }
         catch (final Exception e) {
@@ -160,7 +164,7 @@ public class TestRestconfUtils {
         return null;
     }
 
-    private static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
+    public static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
         final String path = TestRestconfUtils.class.getResource(resourceDirectory).getPath();
         final File testDir = new File(path);
         final String[] fileList = testDir.list();
@@ -177,14 +181,21 @@ public class TestRestconfUtils {
         return testFiles;
     }
 
-    private static SchemaContext loadSchemaContext(final String resourceDirectory) throws IOException {
-        final Collection<File> testFiles = loadFiles(resourceDirectory);
-        return parser.parseFiles(testFiles);
+    public static SchemaContext loadSchemaContext(String resourceDirectory)
+            throws SourceException, ReactorException, FileNotFoundException,
+            URISyntaxException {
+        return parseYangSources(loadFiles(resourceDirectory));
     }
 
-    private static SchemaContext addSchemaContext(final String resourceDirectory,
-            final SchemaContext schemaContext) throws IOException, YangSyntaxErrorException {
-        final Collection<File> testFiles = loadFiles(resourceDirectory);
-        return parser.parseFiles(testFiles, schemaContext);
+    public static SchemaContext parseYangSources(Collection<File> testFiles)
+            throws SourceException, ReactorException, FileNotFoundException {
+        CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
+                .newBuild();
+        for (File testFile : testFiles) {
+            reactor.addSource(new YangStatementSourceImpl(
+                    new NamedFileInputStream(testFile, testFile.getPath())));
+        }
+
+        return reactor.buildEffective();
     }
 }
index ffd06f5d422cff7aab241c35a94bbed92f73df8c..9fa26d53767c0798e640582e8c16b4812aee9a9a 100644 (file)
@@ -11,13 +11,18 @@ package org.opendaylight.controller.sal.rest.impl.test.providers;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import com.google.common.base.Optional;
-import com.google.common.collect.Sets;
+
+import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.net.URI;
+import java.util.Collection;
+
 import javax.ws.rs.core.MediaType;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -30,6 +35,11 @@ import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 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.parser.spi.source.SourceException;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
 
 /**
  * sal-rest-connector
@@ -57,10 +67,11 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException, SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
         controllerContext.setSchemas(schemaContext);
     }
 
index 50abb701644e2f862ff28d7c5ba85f9e4cf96b8a..2790bb82c12d005af5515db5e067e4e6b9e76fa1 100644 (file)
@@ -14,7 +14,9 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.File;
 import java.io.InputStream;
+import java.util.Collection;
 
 import javax.ws.rs.core.MediaType;
 
@@ -22,6 +24,7 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
@@ -63,12 +66,12 @@ public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException,
-            SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang",
-                schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws Exception {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
+
         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
index 9440db47419a3ee985e83e7dd7e80261b350acfb..37d56bfbf1b2f2c6363f79ee25d986f3aea905e2 100644 (file)
@@ -11,13 +11,16 @@ package org.opendaylight.controller.sal.rest.impl.test.providers;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Collection;
 
 import javax.ws.rs.core.MediaType;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
@@ -50,12 +53,11 @@ public class TestJsonBodyWriter extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException,
-            SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang",
-                schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws Exception {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
         controllerContext.setSchemas(schemaContext);
     }
 
index bbb49f3e78ffa14ae5e49ec1e50fdbd7f09537f1..8eb1a1b76b1ed3f39e7c1fea9f761172baee5f4d 100644 (file)
@@ -11,13 +11,17 @@ package org.opendaylight.controller.sal.rest.impl.test.providers;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import com.google.common.base.Optional;
-import com.google.common.collect.Sets;
+
+import java.io.File;
 import java.io.InputStream;
 import java.net.URI;
+import java.util.Collection;
+
 import javax.ws.rs.core.MediaType;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -31,6 +35,9 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+
 /**
  * sal-rest-connector
  * org.opendaylight.controller.sal.rest.impl.test.providers
@@ -57,10 +64,11 @@ public class TestXmlBodyReader extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException, SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws Exception {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
         controllerContext.setSchemas(schemaContext);
     }
 
index a7beb1ac577a52241cfea4780a9e7316440001ed..be13ea19b62a14b857248ec8687242a7d8ba50f3 100644 (file)
@@ -14,7 +14,9 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.File;
 import java.io.InputStream;
+import java.util.Collection;
 
 import javax.ws.rs.core.MediaType;
 
@@ -22,6 +24,7 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
@@ -63,12 +66,12 @@ public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException,
-            SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang",
-                schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws Exception {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
+
         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
index b3a45bd970e021605bc283a441ea4d456aca6817..67e6b6ddf2af02d5274fafc99616af038b1daa24 100644 (file)
@@ -11,7 +11,9 @@ package org.opendaylight.controller.sal.rest.impl.test.providers;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.OutputStream;
+import java.util.Collection;
 
 import javax.ws.rs.core.MediaType;
 
@@ -47,12 +49,11 @@ public class TestXmlBodyWriter extends AbstractBodyReaderTest {
     }
 
     @BeforeClass
-    public static void initialization() throws NoSuchFieldException,
-            SecurityException {
-        schemaContext = schemaContextLoader("/instanceidentifier/yang",
-                schemaContext);
-        schemaContext = schemaContextLoader("/modules", schemaContext);
-        schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
+    public static void initialization() throws Exception {
+        Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
+        testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
+        testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
+        schemaContext = TestRestconfUtils.parseYangSources(testFiles);
         controllerContext.setSchemas(schemaContext);
     }
 
index 64bf6f56d3366490617ea58ce9197aeeb25dcf77..327280f157d37b11460c2c5c1cd741fbfe721bce 100644 (file)
@@ -27,4 +27,4 @@ module yang {
                    type string;
                }
        }
-}l
\ No newline at end of file
+}
\ No newline at end of file