From faff80340e8c867c5408bc6aaa25f0e0b5bfe044 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 21 Oct 2020 22:07:31 +0200 Subject: [PATCH] Fixup resource loading in yang-parser-rfc7950 Going towards modular world requires proper context for resource loading, as we will be on our class loader. Make sure we centralize the policy in StmtTestUtils. This removes a bit of verbosity from individual tests. JIRA: YANGTOOLS-1151 Change-Id: I981245ba93826222950a764e2f9556421e90fc95 Signed-off-by: Robert Varga --- .../effective/ElementCountConstraintsTest.java | 16 ++++------------ .../yang/stmt/EffectiveSchemaContextTest.java | 15 ++++----------- .../yangtools/yang/stmt/OrderingTest.java | 3 +-- .../yangtools/yang/stmt/StmtTestUtils.java | 3 ++- .../yang/stmt/YangParserIdentityTest.java | 13 +++++-------- .../plugin/ThirdPartyExtensionPluginTest.java | 5 ++--- 6 files changed, 18 insertions(+), 37 deletions(-) diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ElementCountConstraintsTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ElementCountConstraintsTest.java index 9b2e7a912b..c4c49409c0 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ElementCountConstraintsTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/ElementCountConstraintsTest.java @@ -13,29 +13,21 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.net.URISyntaxException; -import java.text.ParseException; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; 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.model.repo.api.YangTextSchemaSource; -import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; public class ElementCountConstraintsTest { - @Test - public void testElementCountConstraints() throws ParseException, ReactorException, URISyntaxException, IOException, - YangSyntaxErrorException { - final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild() - .addSource(YangStatementStreamSource.create( - YangTextSchemaSource.forResource("/constraint-definitions-test/foo.yang"))) - .buildEffective(); + public void testElementCountConstraints() + throws YangSyntaxErrorException, ReactorException, URISyntaxException, IOException { + final var schemaContext = StmtTestUtils.parseYangSource("/constraint-definitions-test/foo.yang"); assertNotNull(schemaContext); final Module testModule = schemaContext.findModule("foo", Revision.of("2016-09-20")).get(); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveSchemaContextTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveSchemaContextTest.java index 896a37e951..d202952d88 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveSchemaContextTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveSchemaContextTest.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.yangtools.yang.stmt; import static org.junit.Assert.assertEquals; @@ -33,26 +32,20 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.opendaylight.yangtools.yang.model.util.SimpleSchemaContext; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext; public class EffectiveSchemaContextTest { - @Test public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException, YangSyntaxErrorException { final EffectiveSchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild() - .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource( - "/effective-schema-context-test/foo.yang"))) - .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource( - "/effective-schema-context-test/bar.yang"))) - .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource( - "/effective-schema-context-test/baz.yang"))) - .buildEffective(); + .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/foo.yang")) + .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/bar.yang")) + .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/baz.yang")) + .buildEffective(); assertNotNull(schemaContext); final Collection dataDefinitions = schemaContext.getDataDefinitions(); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/OrderingTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/OrderingTest.java index 503c1b52a5..75d763a232 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/OrderingTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/OrderingTest.java @@ -119,8 +119,7 @@ public class OrderingTest { @Test public void testOrderingNestedChildNodes3() throws Exception { - final Module justFoo = TestUtils.loadModuleResources(getClass(), "/ordering/foo.yang") - .getModules().iterator().next(); + final Module justFoo = StmtTestUtils.parseYangSource("/ordering/foo.yang").getModules().iterator().next(); final ContainerSchemaNode x = (ContainerSchemaNode) justFoo .getDataChildByName(QName.create(justFoo.getQNameModule(), "x")); final Collection childNodes = x.getChildNodes(); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java index a4c26fb24f..3c784d309e 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java @@ -79,7 +79,8 @@ public final class StmtTestUtils { public static StatementStreamSource sourceForResource(final String resourceName) { try { - return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName)); + return YangStatementStreamSource.create(YangTextSchemaSource.forResource( + StmtTestUtils.class, resourceName)); } catch (IOException | YangSyntaxErrorException e) { throw new IllegalArgumentException("Failed to create source", e); } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserIdentityTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserIdentityTest.java index 1da4672592..073bccfeb2 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserIdentityTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserIdentityTest.java @@ -10,22 +10,19 @@ package org.opendaylight.yangtools.yang.stmt; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import java.io.IOException; import java.util.Collection; import org.junit.Test; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleImport; -import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; public class YangParserIdentityTest { // base identity name equals identity name @Test(expected = SomeModifiersUnresolvedException.class) - public void testParsingIdentityTestModule() throws IOException, ReactorException, YangSyntaxErrorException { + public void testParsingIdentityTestModule() throws Exception { try { - TestUtils.loadModuleResources(getClass(), "/identity/identitytest.yang"); + StmtTestUtils.parseYangSource("/identity/identitytest.yang"); } catch (SomeModifiersUnresolvedException e) { StmtTestUtils.log(e, " "); throw e; @@ -34,9 +31,9 @@ public class YangParserIdentityTest { // same module prefixed base identity name equals identity name @Test(expected = SomeModifiersUnresolvedException.class) - public void testParsingPrefixIdentityTestModule() throws IOException, ReactorException, YangSyntaxErrorException { + public void testParsingPrefixIdentityTestModule() throws Exception { try { - TestUtils.loadModuleResources(getClass(), "/identity/prefixidentitytest.yang"); + StmtTestUtils.parseYangSource("/identity/prefixidentitytest.yang"); } catch (SomeModifiersUnresolvedException e) { StmtTestUtils.log(e, " "); throw e; @@ -47,7 +44,7 @@ public class YangParserIdentityTest { // prefix differs @Test public void testParsingImportPrefixIdentityTestModule() throws Exception { - Module module = TestUtils.findModule(TestUtils.loadModules(getClass().getResource("/identity/import").toURI()), + Module module = TestUtils.findModule(StmtTestUtils.parseYangSources("/identity/import"), "prefiximportidentitytest").get(); Collection imports = module.getImports(); assertEquals(imports.size(), 1); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/thirdparty/plugin/ThirdPartyExtensionPluginTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/thirdparty/plugin/ThirdPartyExtensionPluginTest.java index f82cb522cb..b680c3b8d3 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/thirdparty/plugin/ThirdPartyExtensionPluginTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/thirdparty/plugin/ThirdPartyExtensionPluginTest.java @@ -21,10 +21,9 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; +import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; public class ThirdPartyExtensionPluginTest { private static final String NS = "urn:opendaylight:yang:extension:third-party"; @@ -33,7 +32,7 @@ public class ThirdPartyExtensionPluginTest { @Test public void test() throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException { final CrossSourceStatementReactor.BuildAction reactor = CustomInferencePipeline.CUSTOM_REACTOR.newBuild(); - reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource("/plugin-test/foo.yang"))); + reactor.addSource(StmtTestUtils.sourceForResource("/plugin-test/foo.yang")); final SchemaContext schema = reactor.buildEffective(); final DataSchemaNode dataChildByName = schema.getDataChildByName(QName.create(NS, REV, "root")); -- 2.36.6