From: Peter Kajsa Date: Tue, 2 Feb 2016 16:08:36 +0000 (+0100) Subject: Sal-rest-connector unit tests switch to new yang parser. X-Git-Tag: release/beryllium-sr1~5^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=58b2455e553a55c561d608dd360c9964dbcbc711;p=netconf.git Sal-rest-connector unit tests switch to new yang parser. Change-Id: I4efa8e265ca2827033cb0cd3f0b8be3924616842 Signed-off-by: Peter Kajsa (cherry picked from commit 2dd8dce2a8260edb330a7ade345e42b06538a414) --- diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java index c3fae1bcc4..9dea0b95cd 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java @@ -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 loadFiles(final String resourceDirectory) throws FileNotFoundException { + public static Collection 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 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 testFiles = loadFiles(resourceDirectory); - return parser.parseFiles(testFiles, schemaContext); + public static SchemaContext parseYangSources(Collection 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(); } } diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java index ffd06f5d42..9fa26d5376 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java @@ -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 testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); + testFiles.addAll(TestRestconfUtils.loadFiles("/modules")); + testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); + schemaContext = TestRestconfUtils.parseYangSources(testFiles); controllerContext.setSchemas(schemaContext); } diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java index 50abb70164..2790bb82c1 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java @@ -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 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); diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java index 9440db4741..37d56bfbf1 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java @@ -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 testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); + testFiles.addAll(TestRestconfUtils.loadFiles("/modules")); + testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); + schemaContext = TestRestconfUtils.parseYangSources(testFiles); controllerContext.setSchemas(schemaContext); } diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java index bbb49f3e78..8eb1a1b76b 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java @@ -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 testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); + testFiles.addAll(TestRestconfUtils.loadFiles("/modules")); + testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); + schemaContext = TestRestconfUtils.parseYangSources(testFiles); controllerContext.setSchemas(schemaContext); } diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java index a7beb1ac57..be13ea19b6 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java @@ -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 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); diff --git a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java index b3a45bd970..67e6b6ddf2 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java +++ b/opendaylight/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java @@ -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 testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); + testFiles.addAll(TestRestconfUtils.loadFiles("/modules")); + testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); + schemaContext = TestRestconfUtils.parseYangSources(testFiles); controllerContext.setSchemas(schemaContext); } diff --git a/opendaylight/restconf/sal-rest-connector/src/test/resources/nn-to-json/augmentation/yang.yang b/opendaylight/restconf/sal-rest-connector/src/test/resources/nn-to-json/augmentation/yang.yang index 64bf6f56d3..327280f157 100644 --- a/opendaylight/restconf/sal-rest-connector/src/test/resources/nn-to-json/augmentation/yang.yang +++ b/opendaylight/restconf/sal-rest-connector/src/test/resources/nn-to-json/augmentation/yang.yang @@ -27,4 +27,4 @@ module yang { type string; } } -}l \ No newline at end of file +} \ No newline at end of file