From c86d1d8d7a12391a8151c44dcb8e29329581edfc Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 7 Jan 2024 16:51:24 +0100 Subject: [PATCH] Expose URLYangTextSource Rather than having a plethora of static factory methods, expose only a few constructors. JIRA: YANGTOOLS-1561 Change-Id: I4dee89e9243191bdf4d6bf2f1d6e5f1f71a5fd68 Signed-off-by: Robert Varga --- ...TextSource.java => URLYangTextSource.java} | 43 +++++++---- .../yang/model/spi/source/YangTextSource.java | 72 ------------------- .../odlext/parser/ContextReferenceTest.java | 10 +-- .../yangtools/odlext/parser/MountTest.java | 8 ++- .../yangtools/rfc6241/parser/NetconfTest.java | 6 +- .../yangtools/rfc6536/parser/NACMTest.java | 6 +- .../IetfYangSmiv2ExtensionPluginTest.java | 10 +-- .../rfc7952/parser/AnnotationTest.java | 6 +- .../rfc8040/parser/AbstractYangDataTest.java | 4 +- .../rfc8528/parser/MountPointTest.java | 13 ++-- .../parser/SubscribedNotificationsTest.java | 38 +++++----- .../rfc8819/parser/ModuleTagTest.java | 5 +- .../repo/YangTextSchemaContextResolver.java | 4 +- .../yang/parser/impl/YT1193Test.java | 10 +-- .../repo/AbstractSchemaRepositoryTest.java | 14 +++- .../parser/repo/DependencyResolverTest.java | 4 +- .../repo/MultipleRevImportBug6875Test.java | 29 +++----- ...enconfigVerSharedSchemaRepositoryTest.java | 40 +++-------- ...haredEffectiveModelContextFactoryTest.java | 27 ++++--- .../repo/SharedSchemaRepositoryTest.java | 52 +++++--------- ...haredSchemaRepositoryWithFeaturesTest.java | 32 ++------- .../yang/test/util/YangParserTestUtils.java | 11 ++- 22 files changed, 170 insertions(+), 274 deletions(-) rename model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/{ResourceYangTextSource.java => URLYangTextSource.java} (54%) diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/ResourceYangTextSource.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/URLYangTextSource.java similarity index 54% rename from model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/ResourceYangTextSource.java rename to model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/URLYangTextSource.java index 220365eeb6..e4cdb1ef70 100644 --- a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/ResourceYangTextSource.java +++ b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/URLYangTextSource.java @@ -15,40 +15,57 @@ import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.opendaylight.yangtools.concepts.Delegator; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; /** - * A resource-backed {@link YangTextSource}. + * A {@link YangTextSource} backed by a {@link URL}. */ -final class ResourceYangTextSource extends YangTextSource implements Delegator { - private final @NonNull URL url; - private final @NonNull Charset charset; +@NonNullByDefault +public class URLYangTextSource extends YangTextSource implements Delegator { + private final URL url; + private final Charset charset; - ResourceYangTextSource(final SourceIdentifier sourceId, final URL url, final Charset charset) { + public URLYangTextSource(final SourceIdentifier sourceId, final URL url, final Charset charset) { super(sourceId); this.url = requireNonNull(url); this.charset = requireNonNull(charset); } + /** + * Utility constructor. The {@link SourceIdentifier} is derived from {@link URL#getPath()} and the character set is + * assumed to be UTF-8. + * + * @param url backing {@link URL} + */ + public URLYangTextSource(final URL url) { + this(SourceIdentifier.ofYangFileName(extractFileName(url.getPath())), url, StandardCharsets.UTF_8); + } + @Override - public URL getDelegate() { - return url; + public final Reader openStream() throws IOException { + return new InputStreamReader(url.openStream(), charset); } @Override - protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { - return super.addToStringAttributes(toStringHelper).add("url", url); + public final @NonNull String symbolicName() { + return url.toString(); } @Override - public Reader openStream() throws IOException { - return new InputStreamReader(url.openStream(), charset); + public final URL getDelegate() { + return url; } @Override - public String symbolicName() { - return url.toString(); + protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { + return super.addToStringAttributes(toStringHelper).add("url", url); + } + + private static String extractFileName(final String path) { + return path.substring(path.lastIndexOf('/') + 1); } } diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java index 9fbdb35a83..1e52e39b8f 100644 --- a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java +++ b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java @@ -12,10 +12,8 @@ import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.io.CharSource; -import com.google.common.io.Resources; import java.io.File; import java.io.InputStream; -import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -79,76 +77,6 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep throw new IllegalArgumentException("Supplied path " + path + " is not a regular file"); } - /** - * Create a new {@link YangTextSource} backed by a resource available in the ClassLoader where this - * class resides. - * - * @param resourceName Resource name - * @return A new instance. - * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format - */ - public static @NonNull YangTextSource forResource(final String resourceName) { - return forResource(YangTextSource.class, resourceName); - } - - /** - * Create a new {@link YangTextSource} backed by a resource by a resource available on the ClassLoader - * which loaded the specified class. - * - * @param clazz Class reference - * @param resourceName Resource name - * @return A new instance. - * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format - */ - public static @NonNull YangTextSource forResource(final Class clazz, final String resourceName) { - return forResource(clazz, resourceName, StandardCharsets.UTF_8); - } - - /** - * Create a new {@link YangTextSource} backed by a resource by a resource available on the ClassLoader - * which loaded the specified class. - * - * @param clazz Class reference - * @param resourceName Resource name - * @param charset Expected character set - * @return A new instance. - * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format - */ - public static @NonNull YangTextSource forResource(final Class clazz, final String resourceName, - final Charset charset) { - final var fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1); - final var identifier = SourceIdentifier.ofYangFileName(fileName); - final var url = Resources.getResource(clazz, resourceName); - return new ResourceYangTextSource(identifier, url, charset); - } - - - /** - * Create a new {@link YangTextSource} backed by a URL. - * - * @param url Backing URL - * @param identifier Source identifier - * @return A new instance. - * @throws NullPointerException if any argument is {@code null} - */ - public static @NonNull YangTextSource forURL(final URL url, final SourceIdentifier identifier) { - return forURL(url, identifier, StandardCharsets.UTF_8); - } - - /** - * Create a new {@link YangTextSource} backed by a URL. - * - * @param url Backing URL - * @param identifier Source identifier - * @param charset Expected character set - * @return A new instance. - * @throws NullPointerException if any argument is {@code null} - */ - public static @NonNull YangTextSource forURL(final URL url, final SourceIdentifier identifier, - final Charset charset) { - return new ResourceYangTextSource(identifier, url, charset); - } - @Override public final Class getType() { return YangTextSource.class; diff --git a/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/ContextReferenceTest.java b/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/ContextReferenceTest.java index 84e2e4b906..e4bbca6644 100644 --- a/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/ContextReferenceTest.java +++ b/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/ContextReferenceTest.java @@ -21,7 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -42,10 +42,10 @@ class ContextReferenceTest { .build(); final var foo = reactor.newBuild() - .addSource(YangStatementStreamSource.create(YangTextSource.forResource( - ContextReferenceTest.class, "/yang-ext.yang"))) - .addSource(YangStatementStreamSource.create(YangTextSource.forResource( - ContextReferenceTest.class, "/ctxref.yang"))) + .addSource(YangStatementStreamSource.create(new URLYangTextSource( + ContextReferenceTest.class.getResource("/yang-ext.yang")))) + .addSource(YangStatementStreamSource.create(new URLYangTextSource( + ContextReferenceTest.class.getResource("/ctxref.yang")))) .buildEffective() .getModuleStatements() .get(FOO); diff --git a/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/MountTest.java b/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/MountTest.java index a95c87777d..59f737540d 100644 --- a/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/MountTest.java +++ b/parser/odlext-parser-support/src/test/java/org/opendaylight/yangtools/odlext/parser/MountTest.java @@ -14,7 +14,7 @@ import org.opendaylight.yangtools.odlext.model.api.MountEffectiveStatement; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.XMLNamespace; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -30,8 +30,10 @@ class MountTest { new MountStatementSupport(YangParserConfiguration.DEFAULT)) .build(); final var foo = reactor.newBuild() - .addSource(YangStatementStreamSource.create(YangTextSource.forResource(MountTest.class, "/yang-ext.yang"))) - .addSource(YangStatementStreamSource.create(YangTextSource.forResource(MountTest.class, "/mount.yang"))) + .addSource(YangStatementStreamSource.create(new URLYangTextSource( + MountTest.class.getResource("/yang-ext.yang")))) + .addSource(YangStatementStreamSource.create(new URLYangTextSource( + MountTest.class.getResource("/mount.yang")))) .buildEffective() .getModuleStatements() .get(FOO); diff --git a/parser/rfc6241-parser-support/src/test/java/org/opendaylight/yangtools/rfc6241/parser/NetconfTest.java b/parser/rfc6241-parser-support/src/test/java/org/opendaylight/yangtools/rfc6241/parser/NetconfTest.java index 5fb6852e2f..f2917ccff0 100644 --- a/parser/rfc6241-parser-support/src/test/java/org/opendaylight/yangtools/rfc6241/parser/NetconfTest.java +++ b/parser/rfc6241-parser-support/src/test/java/org/opendaylight/yangtools/rfc6241/parser/NetconfTest.java @@ -17,7 +17,7 @@ import org.opendaylight.yangtools.rfc6241.model.api.NetconfConstants; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -34,9 +34,9 @@ class NetconfTest { .build(); final var context = reactor.newBuild() .addLibSources(YangStatementStreamSource.create( - YangTextSource.forResource(NetconfTest.class, "/ietf-inet-types@2013-07-15.yang"))) + new URLYangTextSource(NetconfTest.class.getResource("/ietf-inet-types@2013-07-15.yang")))) .addSource(YangStatementStreamSource.create( - YangTextSource.forResource(NetconfTest.class, "/ietf-netconf@2011-06-01.yang"))) + new URLYangTextSource(NetconfTest.class.getResource("/ietf-netconf@2011-06-01.yang")))) .buildEffective(); final var module = context.findModule(NetconfConstants.RFC6241_MODULE).orElseThrow(); diff --git a/parser/rfc6536-parser-support/src/test/java/org/opendaylight/yangtools/rfc6536/parser/NACMTest.java b/parser/rfc6536-parser-support/src/test/java/org/opendaylight/yangtools/rfc6536/parser/NACMTest.java index 42f8b8d62e..6ff7ed37fe 100644 --- a/parser/rfc6536-parser-support/src/test/java/org/opendaylight/yangtools/rfc6536/parser/NACMTest.java +++ b/parser/rfc6536-parser-support/src/test/java/org/opendaylight/yangtools/rfc6536/parser/NACMTest.java @@ -15,7 +15,7 @@ import org.opendaylight.yangtools.rfc6536.model.api.DefaultDenyAllSchemaNode; import org.opendaylight.yangtools.rfc6536.model.api.DefaultDenyWriteSchemaNode; import org.opendaylight.yangtools.rfc6536.model.api.NACMConstants; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -34,9 +34,9 @@ class NACMTest { final var context = reactor.newBuild() .addSources( YangStatementStreamSource.create( - YangTextSource.forResource(NACMTest.class, "/ietf-netconf-acm@2012-02-22.yang")), + new URLYangTextSource(NACMTest.class.getResource("/ietf-netconf-acm@2012-02-22.yang"))), YangStatementStreamSource.create( - YangTextSource.forResource(NACMTest.class, "/ietf-yang-types@2013-07-15.yang"))) + new URLYangTextSource(NACMTest.class.getResource("/ietf-yang-types@2013-07-15.yang")))) .buildEffective(); final var module = context.findModule(NACMConstants.RFC6536_MODULE).orElseThrow(); diff --git a/parser/rfc6643-parser-support/src/test/java/org/opendaylight/yangtools/rfc6643/parser/IetfYangSmiv2ExtensionPluginTest.java b/parser/rfc6643-parser-support/src/test/java/org/opendaylight/yangtools/rfc6643/parser/IetfYangSmiv2ExtensionPluginTest.java index 8eb6bb2419..fedaab441a 100644 --- a/parser/rfc6643-parser-support/src/test/java/org/opendaylight/yangtools/rfc6643/parser/IetfYangSmiv2ExtensionPluginTest.java +++ b/parser/rfc6643-parser-support/src/test/java/org/opendaylight/yangtools/rfc6643/parser/IetfYangSmiv2ExtensionPluginTest.java @@ -24,7 +24,7 @@ import org.opendaylight.yangtools.yang.common.Uint32; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -54,10 +54,10 @@ class IetfYangSmiv2ExtensionPluginTest { .build(); final var schema = reactor.newBuild() .addSources( - YangStatementStreamSource.create(YangTextSource.forResource( - IetfYangSmiv2ExtensionPluginTest.class, "/foo.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - IetfYangSmiv2ExtensionPluginTest.class, "/ietf-yang-smiv2.yang"))) + YangStatementStreamSource.create(new URLYangTextSource( + IetfYangSmiv2ExtensionPluginTest.class.getResource("/foo.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + IetfYangSmiv2ExtensionPluginTest.class.getResource("/ietf-yang-smiv2.yang")))) .buildEffective(); assertEquals(1, schema.getUnknownSchemaNodes().size()); diff --git a/parser/rfc7952-parser-support/src/test/java/org/opendaylight/yangtools/rfc7952/parser/AnnotationTest.java b/parser/rfc7952-parser-support/src/test/java/org/opendaylight/yangtools/rfc7952/parser/AnnotationTest.java index a82b0ee663..2af371e606 100644 --- a/parser/rfc7952-parser-support/src/test/java/org/opendaylight/yangtools/rfc7952/parser/AnnotationTest.java +++ b/parser/rfc7952-parser-support/src/test/java/org/opendaylight/yangtools/rfc7952/parser/AnnotationTest.java @@ -18,7 +18,7 @@ import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode; import org.opendaylight.yangtools.yang.common.AnnotationName; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -37,9 +37,9 @@ class AnnotationTest { final var context = reactor.newBuild() .addSources( YangStatementStreamSource.create( - YangTextSource.forResource(AnnotationTest.class, "/ietf-yang-metadata@2016-08-05.yang")), + new URLYangTextSource(AnnotationTest.class.getResource("/ietf-yang-metadata@2016-08-05.yang"))), YangStatementStreamSource.create( - YangTextSource.forResource(AnnotationTest.class, "/example-last-modified.yang"))) + new URLYangTextSource(AnnotationTest.class.getResource("/example-last-modified.yang")))) .buildEffective(); final var annotations = AnnotationSchemaNode.findAll(context); diff --git a/parser/rfc8040-parser-support/src/test/java/org/opendaylight/yangtools/rfc8040/parser/AbstractYangDataTest.java b/parser/rfc8040-parser-support/src/test/java/org/opendaylight/yangtools/rfc8040/parser/AbstractYangDataTest.java index 3816657d31..2ea53c122a 100644 --- a/parser/rfc8040-parser-support/src/test/java/org/opendaylight/yangtools/rfc8040/parser/AbstractYangDataTest.java +++ b/parser/rfc8040-parser-support/src/test/java/org/opendaylight/yangtools/rfc8040/parser/AbstractYangDataTest.java @@ -10,7 +10,7 @@ package org.opendaylight.yangtools.rfc8040.parser; import java.io.IOException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; @@ -42,7 +42,7 @@ abstract class AbstractYangDataTest { static StatementStreamSource sourceForResource(final String resourceName) { try { return YangStatementStreamSource.create( - YangTextSource.forResource(AbstractYangDataTest.class, resourceName)); + new URLYangTextSource(AbstractYangDataTest.class.getResource(resourceName))); } catch (IOException | YangSyntaxErrorException e) { throw new IllegalArgumentException("Failed to create source", e); } diff --git a/parser/rfc8528-parser-support/src/test/java/org/opendaylight/yangtools/rfc8528/parser/MountPointTest.java b/parser/rfc8528-parser-support/src/test/java/org/opendaylight/yangtools/rfc8528/parser/MountPointTest.java index 601731c0d9..c557f80020 100644 --- a/parser/rfc8528-parser-support/src/test/java/org/opendaylight/yangtools/rfc8528/parser/MountPointTest.java +++ b/parser/rfc8528-parser-support/src/test/java/org/opendaylight/yangtools/rfc8528/parser/MountPointTest.java @@ -17,7 +17,7 @@ import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -41,15 +41,16 @@ class MountPointTest { final var context = reactor.newBuild() .addLibSources( YangStatementStreamSource.create( - YangTextSource.forResource(MountPointTest.class, "/ietf-inet-types@2013-07-15.yang")), + new URLYangTextSource(MountPointTest.class.getResource("/ietf-inet-types@2013-07-15.yang"))), YangStatementStreamSource.create( - YangTextSource.forResource(MountPointTest.class, "/ietf-yang-schema-mount@2019-01-14.yang")), + new URLYangTextSource(MountPointTest.class.getResource("/ietf-yang-schema-mount@2019-01-14.yang"))), YangStatementStreamSource.create( - YangTextSource.forResource(MountPointTest.class, "/ietf-yang-types@2013-07-15.yang"))) + new URLYangTextSource(MountPointTest.class.getResource("/ietf-yang-types@2013-07-15.yang")))) .addSources( - YangStatementStreamSource.create(YangTextSource.forResource(MountPointTest.class, "/example-grp.yang")), YangStatementStreamSource.create( - YangTextSource.forResource(MountPointTest.class, "/example-uses.yang"))) + new URLYangTextSource(MountPointTest.class.getResource("/example-grp.yang"))), + YangStatementStreamSource.create( + new URLYangTextSource(MountPointTest.class.getResource("/example-uses.yang")))) .buildEffective(); assertEquals(5, context.getModules().size()); diff --git a/parser/rfc8639-parser-support/src/test/java/org/opendaylight/yangtools/rfc8639/parser/SubscribedNotificationsTest.java b/parser/rfc8639-parser-support/src/test/java/org/opendaylight/yangtools/rfc8639/parser/SubscribedNotificationsTest.java index 0ea631801e..2c97abf976 100644 --- a/parser/rfc8639-parser-support/src/test/java/org/opendaylight/yangtools/rfc8639/parser/SubscribedNotificationsTest.java +++ b/parser/rfc8639-parser-support/src/test/java/org/opendaylight/yangtools/rfc8639/parser/SubscribedNotificationsTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.Test; import org.opendaylight.yangtools.rfc8639.model.api.SubscribedNotificationsConstants; import org.opendaylight.yangtools.rfc8639.model.api.SubscriptionStateNotificationEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; @@ -30,25 +30,25 @@ class SubscribedNotificationsTest { final var context = reactor.newBuild() .addLibSources( - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-inet-types@2013-07-15.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-interfaces@2018-02-20.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-ip@2018-02-22.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-netconf-acm@2018-02-14.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-network-instance@2019-01-21.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-restconf@2017-01-26.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-yang-schema-mount@2019-01-14.yang")), - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-yang-types@2013-07-15.yang"))) + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-inet-types@2013-07-15.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-interfaces@2018-02-20.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-ip@2018-02-22.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-netconf-acm@2018-02-14.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-network-instance@2019-01-21.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-restconf@2017-01-26.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-yang-schema-mount@2019-01-14.yang"))), + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-yang-types@2013-07-15.yang")))) .addSources( - YangStatementStreamSource.create(YangTextSource.forResource( - SubscribedNotificationsTest.class, "/ietf-subscribed-notifications@2019-09-09.yang"))) + YangStatementStreamSource.create(new URLYangTextSource( + SubscribedNotificationsTest.class.getResource("/ietf-subscribed-notifications@2019-09-09.yang")))) .buildEffective(); final var notifications = context.getModuleStatement(SubscribedNotificationsConstants.RFC8639_MODULE) diff --git a/parser/rfc8819-parser-support/src/test/java/org/opendaylight/yangtools/rfc8819/parser/ModuleTagTest.java b/parser/rfc8819-parser-support/src/test/java/org/opendaylight/yangtools/rfc8819/parser/ModuleTagTest.java index b8707cf65b..dfb4870b6d 100644 --- a/parser/rfc8819-parser-support/src/test/java/org/opendaylight/yangtools/rfc8819/parser/ModuleTagTest.java +++ b/parser/rfc8819-parser-support/src/test/java/org/opendaylight/yangtools/rfc8819/parser/ModuleTagTest.java @@ -21,7 +21,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.opendaylight.yangtools.rfc8819.model.api.ModuleTagEffectiveStatement; import org.opendaylight.yangtools.rfc8819.model.api.Tag; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; @@ -89,7 +89,8 @@ public class ModuleTagTest { private static YangStatementStreamSource moduleFromResources(final String resourceName) { try { - return YangStatementStreamSource.create(YangTextSource.forResource(ModuleTagTest.class, resourceName)); + return YangStatementStreamSource.create(new URLYangTextSource( + ModuleTagTest.class.getResource(resourceName))); } catch (final YangSyntaxErrorException | IOException e) { throw new IllegalStateException("Failed to find resource " + resourceName, e); } diff --git a/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java b/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java index 3e64b0027a..850928fff1 100644 --- a/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java +++ b/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java @@ -17,6 +17,7 @@ import com.google.common.collect.Multimap; import com.google.common.util.concurrent.FluentFuture; import java.io.IOException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; @@ -50,6 +51,7 @@ import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Cost import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserFactory; @@ -177,7 +179,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem throws SchemaSourceException, IOException, YangSyntaxErrorException { final String path = url.getPath(); final String fileName = path.substring(path.lastIndexOf('/') + 1); - return registerSource(YangTextSource.forURL(url, guessSourceIdentifier(fileName))); + return registerSource(new URLYangTextSource(guessSourceIdentifier(fileName), url, StandardCharsets.UTF_8)); } /** diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java index 13e484a567..dfcf47e204 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YT1193Test.java @@ -16,17 +16,17 @@ import org.opendaylight.yangtools.yang.model.api.YangStmtMapping; import org.opendaylight.yangtools.yang.model.api.meta.DeclarationInText; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; -public class YT1193Test { +class YT1193Test { @Test void testDeclarationReference() throws Exception { final var declaredRoots = new DefaultYangParserFactory() .createParser(YangParserConfiguration.builder().retainDeclarationReferences(true).build()) - .addSource(YangTextSource.forResource(getClass(), "/yt1193/foo.yang")) - .addSource(YangTextSource.forResource(getClass(), "/yt1193/bar.yang")) - .addSource(YangTextSource.forResource(getClass(), "/yt1193/baz.yang")) + .addSource(new URLYangTextSource(YT1193Test.class.getResource("/yt1193/foo.yang"))) + .addSource(new URLYangTextSource(YT1193Test.class.getResource("/yt1193/bar.yang"))) + .addSource(new URLYangTextSource(YT1193Test.class.getResource("/yt1193/baz.yang"))) .buildDeclaredModel(); assertEquals(3, declaredRoots.size()); diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/AbstractSchemaRepositoryTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/AbstractSchemaRepositoryTest.java index a192c0ceb2..8289b2da2b 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/AbstractSchemaRepositoryTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/AbstractSchemaRepositoryTest.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.repo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import com.google.common.collect.SetMultimap; @@ -20,8 +22,8 @@ import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; @@ -61,13 +63,19 @@ public abstract class AbstractSchemaRepositoryTest { .createEffectiveModelContext(requiredSources); } - private static SettableSchemaProvider assertYangTextResource(final String resourceName) { + static final SettableSchemaProvider assertYangTextResource(final String resourceName) { final YangIRSchemaSource yangSource; try { - yangSource = TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)); + yangSource = TextToIRTransformer.transformText( + new URLYangTextSource(AbstractSchemaRepositoryTest.class.getResource(resourceName))); } catch (YangSyntaxErrorException | IOException e) { throw new AssertionError("Failed to parse " + resourceName, e); } return SettableSchemaProvider.createImmediate(yangSource, YangIRSchemaSource.class); } + + static final void assertSchemaContext(final EffectiveModelContext schemaContext, final int moduleSize) { + assertNotNull(schemaContext); + assertEquals(moduleSize, schemaContext.getModuleStatements().size()); + } } diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/DependencyResolverTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/DependencyResolverTest.java index 3e34069c7d..559842afde 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/DependencyResolverTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/DependencyResolverTest.java @@ -18,7 +18,7 @@ import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified; import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.BelongsTo; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangIRSourceInfoExtractor; class DependencyResolverTest { @@ -66,7 +66,7 @@ class DependencyResolverTest { final var map = new HashMap(); for (var resourceName : resourceNames) { final var info = YangIRSourceInfoExtractor.forYangText( - YangTextSource.forResource(DependencyResolverTest.class, resourceName)); + new URLYangTextSource(DependencyResolverTest.class.getResource(resourceName))); map.put(info.sourceId(), info); } return new RevisionDependencyResolver(map); diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/MultipleRevImportBug6875Test.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/MultipleRevImportBug6875Test.java index 3ba24706e0..df95c30ae0 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/MultipleRevImportBug6875Test.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/MultipleRevImportBug6875Test.java @@ -20,10 +20,8 @@ import org.junit.jupiter.api.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; -public class MultipleRevImportBug6875Test { +class MultipleRevImportBug6875Test extends AbstractSchemaRepositoryTest { private static final String BAR_NS = "bar"; private static final String BAR_REV_1 = "2017-02-06"; private static final String BAR_REV_2 = "1999-01-01"; @@ -31,13 +29,13 @@ public class MultipleRevImportBug6875Test { private static final String FOO_NS = "foo"; @Test - public void testYang11() throws Exception { + void testYang11() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test"); - final var foo = getSourceProvider("/rfc7950/bug6875/yang1-1/foo.yang"); - final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang"); - final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang"); - final var bar3 = getSourceProvider("/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang"); + final var foo = assertYangTextResource("/rfc7950/bug6875/yang1-1/foo.yang"); + final var bar1 = assertYangTextResource("/rfc7950/bug6875/yang1-1/bar@1999-01-01.yang"); + final var bar2 = assertYangTextResource("/rfc7950/bug6875/yang1-1/bar@2017-02-06.yang"); + final var bar3 = assertYangTextResource("/rfc7950/bug6875/yang1-1/bar@1970-01-01.yang"); setAndRegister(sharedSchemaRepository, foo); setAndRegister(sharedSchemaRepository, bar1); @@ -69,12 +67,12 @@ public class MultipleRevImportBug6875Test { } @Test - public void testYang10() throws Exception { + void testYang10() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-multiple-rev-import-test"); - final var foo = getSourceProvider("/rfc7950/bug6875/yang1-0/foo.yang"); - final var bar1 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang"); - final var bar2 = getSourceProvider("/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang"); + final var foo = assertYangTextResource("/rfc7950/bug6875/yang1-0/foo.yang"); + final var bar1 = assertYangTextResource("/rfc7950/bug6875/yang1-0/bar@1999-01-01.yang"); + final var bar2 = assertYangTextResource("/rfc7950/bug6875/yang1-0/bar@2017-02-06.yang"); setAndRegister(sharedSchemaRepository, foo); setAndRegister(sharedSchemaRepository, bar1); @@ -95,13 +93,6 @@ public class MultipleRevImportBug6875Test { source.setResult(); } - private static SettableSchemaProvider getSourceProvider(final String resourceName) - throws Exception { - return SettableSchemaProvider.createImmediate( - TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), - YangIRSchemaSource.class); - } - private static QName foo(final String localName) { return QName.create(FOO_NS, localName); } diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/OpenconfigVerSharedSchemaRepositoryTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/OpenconfigVerSharedSchemaRepositoryTest.java index bb79fabfa9..239e66b532 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/OpenconfigVerSharedSchemaRepositoryTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/OpenconfigVerSharedSchemaRepositoryTest.java @@ -7,53 +7,31 @@ */ package org.opendaylight.yangtools.yang.parser.repo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - +import com.google.common.util.concurrent.Futures; import org.junit.jupiter.api.Test; -import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; -import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; -public class OpenconfigVerSharedSchemaRepositoryTest { +class OpenconfigVerSharedSchemaRepositoryTest extends AbstractSchemaRepositoryTest { @Test - public void testSharedSchemaRepository() throws Exception { + void testSharedSchemaRepository() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-test"); - final var bar = getImmediateYangSourceProviderFromResource( - "/openconfig-version/shared-schema-repository/bar@2016-01-01.yang"); + final var bar = assertYangTextResource("/openconfig-version/shared-schema-repository/bar@2016-01-01.yang"); bar.register(sharedSchemaRepository); bar.setResult(); - final var foo = getImmediateYangSourceProviderFromResource( - "/openconfig-version/shared-schema-repository/foo.yang"); + final var foo = assertYangTextResource("/openconfig-version/shared-schema-repository/foo.yang"); foo.register(sharedSchemaRepository); foo.setResult(); - final var semVer = getImmediateYangSourceProviderFromResource( - "/openconfig-version/shared-schema-repository/openconfig-extensions.yang"); + final var semVer = assertYangTextResource( + "/openconfig-version/shared-schema-repository/openconfig-extensions.yang"); semVer.register(sharedSchemaRepository); semVer.setResult(); final var fact = sharedSchemaRepository.createEffectiveModelContextFactory(); final var inetAndTopologySchemaContextFuture = fact.createEffectiveModelContext(bar.getId(), foo.getId(), semVer.getId()); - assertTrue(inetAndTopologySchemaContextFuture.isDone()); - assertSchemaContext(inetAndTopologySchemaContextFuture.get(), 3); + assertSchemaContext(Futures.getDone(inetAndTopologySchemaContextFuture), 3); final var barSchemaContextFuture = fact.createEffectiveModelContext(bar.getId(), semVer.getId()); - assertTrue(barSchemaContextFuture.isDone()); - assertSchemaContext(barSchemaContextFuture.get(), 2); - } - - private static void assertSchemaContext(final EffectiveModelContext schemaContext, final int moduleSize) { - assertNotNull(schemaContext); - assertEquals(moduleSize, schemaContext.getModules().size()); - } - - static SettableSchemaProvider getImmediateYangSourceProviderFromResource( - final String resourceName) throws Exception { - return SettableSchemaProvider.createImmediate( - TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class); + assertSchemaContext(Futures.getDone(barSchemaContextFuture), 2); } } diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedEffectiveModelContextFactoryTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedEffectiveModelContextFactoryTest.java index ddfd17e502..26a6953ffc 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedEffectiveModelContextFactoryTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedEffectiveModelContextFactoryTest.java @@ -23,11 +23,12 @@ import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceExcepti import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; -public class SharedEffectiveModelContextFactoryTest { +class SharedEffectiveModelContextFactoryTest { private final SharedSchemaRepository repository = new SharedSchemaRepository("test"); private final SchemaContextFactoryConfiguration config = SchemaContextFactoryConfiguration.getDefault(); @@ -35,9 +36,9 @@ public class SharedEffectiveModelContextFactoryTest { private SourceIdentifier s2; @BeforeEach - public void setUp() { - final var source1 = YangTextSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang"); - final var source2 = YangTextSource.forResource("/ietf/iana-timezones@2012-07-09.yang"); + void setUp() { + final var source1 = assertYangText("/ietf/ietf-inet-types@2010-09-24.yang"); + final var source2 = assertYangText("/ietf/iana-timezones@2012-07-09.yang"); s1 = new SourceIdentifier("ietf-inet-types", "2010-09-24"); s2 = new SourceIdentifier("iana-timezones", "2012-07-09"); @@ -52,20 +53,20 @@ public class SharedEffectiveModelContextFactoryTest { } @Test - public void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception { + void testCreateSchemaContextWithDuplicateRequiredSources() throws Exception { final var sharedSchemaContextFactory = new SharedEffectiveModelContextFactory(repository, config); final var schemaContext = sharedSchemaContextFactory.createEffectiveModelContext(s1, s1, s2); assertNotNull(schemaContext.get()); } @Test - public void testSourceRegisteredWithDifferentSI() throws Exception { - final var source1 = YangTextSource.forResource("/ietf/ietf-inet-types@2010-09-24.yang"); - final var source2 = YangTextSource.forResource("/ietf/iana-timezones@2012-07-09.yang"); + void testSourceRegisteredWithDifferentSI() throws Exception { + final var source1 = assertYangText("/ietf/ietf-inet-types@2010-09-24.yang"); + final var source2 = assertYangText("/ietf/iana-timezones@2012-07-09.yang"); s1 = source1.sourceId(); s2 = source2.sourceId(); - final var provider = SharedSchemaRepositoryTest.getImmediateYangSourceProviderFromResource( + final var provider = AbstractSchemaRepositoryTest.assertYangTextResource( "/no-revision/imported@2012-12-12.yang"); provider.setResult(); provider.register(repository); @@ -82,11 +83,11 @@ public class SharedEffectiveModelContextFactoryTest { } @Test - public void testTransientFailureWhilreRetrievingSchemaSource() throws Exception { + void testTransientFailureWhilreRetrievingSchemaSource() throws Exception { final SourceIdentifier s3 = new SourceIdentifier("network-topology", "2013-10-21"); repository.registerSchemaSource(new TransientFailureProvider( - YangTextSource.forResource("/ietf/network-topology@2013-10-21.yang")), + assertYangText("/ietf/network-topology@2013-10-21.yang")), PotentialSchemaSource.create(s3, YangTextSource.class, 1)); final var sharedSchemaContextFactory = new SharedEffectiveModelContextFactory(repository, config); @@ -101,6 +102,10 @@ public class SharedEffectiveModelContextFactoryTest { assertNotNull(schemaContext.get()); } + private static URLYangTextSource assertYangText(final String resourceName) { + return new URLYangTextSource(AbstractSchemaRepositoryTest.class.getResource(resourceName)); + } + /** * Schema source provider that fails on first attempt of getSource() and succeeds on every subsequent call * to simulate transient failures of source retrieval. diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryTest.java index 79311a99a0..de01a3e5d8 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryTest.java @@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.repo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -20,16 +19,15 @@ import static org.mockito.Mockito.verify; import java.util.concurrent.ExecutionException; import org.junit.jupiter.api.Test; -import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; -public class SharedSchemaRepositoryTest { +class SharedSchemaRepositoryTest extends AbstractSchemaRepositoryTest { @Test - public void testSourceWithAndWithoutRevision() throws Exception { + void testSourceWithAndWithoutRevision() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts"); final var idNoRevision = loadAndRegisterSource(sharedSchemaRepository, "/no-revision/imported.yang"); @@ -43,7 +41,7 @@ public class SharedSchemaRepositoryTest { private static SourceIdentifier loadAndRegisterSource(final SharedSchemaRepository sharedSchemaRepository, final String resourceName) throws Exception { - final var sourceProvider = getImmediateYangSourceProviderFromResource(resourceName); + final var sourceProvider = assertYangTextResource(resourceName); sourceProvider.setResult(); final var idNoRevision = sourceProvider.getId(); sourceProvider.register(sharedSchemaRepository); @@ -51,11 +49,10 @@ public class SharedSchemaRepositoryTest { } @Test - public void testSimpleSchemaContext() throws Exception { + void testSimpleSchemaContext() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts"); - final var remoteInetTypesYang = - getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"); + final var remoteInetTypesYang = assertYangTextResource("/ietf/ietf-inet-types@2010-09-24.yang"); remoteInetTypesYang.register(sharedSchemaRepository); final var registeredSourceFuture = sharedSchemaRepository.getSchemaSource( remoteInetTypesYang.getId(), YangIRSchemaSource.class); @@ -85,19 +82,16 @@ public class SharedSchemaRepositoryTest { } @Test - public void testTwoSchemaContextsSharingSource() throws Exception { + void testTwoSchemaContextsSharingSource() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts"); - final var remoteInetTypesYang = - getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"); + final var remoteInetTypesYang = assertYangTextResource("/ietf/ietf-inet-types@2010-09-24.yang"); remoteInetTypesYang.register(sharedSchemaRepository); remoteInetTypesYang.setResult(); - final var remoteTopologyYang = - getImmediateYangSourceProviderFromResource("/ietf/network-topology@2013-10-21.yang"); + final var remoteTopologyYang = assertYangTextResource("/ietf/network-topology@2013-10-21.yang"); remoteTopologyYang.register(sharedSchemaRepository); remoteTopologyYang.setResult(); - final var remoteModuleNoRevYang = - getImmediateYangSourceProviderFromResource("/no-revision/module-without-revision.yang"); + final var remoteModuleNoRevYang = assertYangTextResource("/no-revision/module-without-revision.yang"); remoteModuleNoRevYang.register(sharedSchemaRepository); final var fact = sharedSchemaRepository.createEffectiveModelContextFactory(); @@ -116,11 +110,10 @@ public class SharedSchemaRepositoryTest { } @Test - public void testFailedSchemaContext() throws Exception { + void testFailedSchemaContext() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts"); - final var remoteInetTypesYang = - getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"); + final var remoteInetTypesYang = assertYangTextResource("/ietf/ietf-inet-types@2010-09-24.yang"); remoteInetTypesYang.register(sharedSchemaRepository); final var fact = sharedSchemaRepository.createEffectiveModelContextFactory(); @@ -136,11 +129,10 @@ public class SharedSchemaRepositoryTest { } @Test - public void testDifferentCosts() throws Exception { + void testDifferentCosts() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts"); - final var immediateInetTypesYang = spy( - getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang")); + final var immediateInetTypesYang = spy(assertYangTextResource("/ietf/ietf-inet-types@2010-09-24.yang")); immediateInetTypesYang.register(sharedSchemaRepository); immediateInetTypesYang.setResult(); @@ -159,20 +151,10 @@ public class SharedSchemaRepositoryTest { verify(immediateInetTypesYang).getSource(id); } - private static void assertSchemaContext(final EffectiveModelContext schemaContext, final int moduleSize) { - assertNotNull(schemaContext); - assertEquals(moduleSize, schemaContext.getModules().size()); - } - static SettableSchemaProvider getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception { - return SettableSchemaProvider.createRemote( - TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class); - } - - static SettableSchemaProvider getImmediateYangSourceProviderFromResource( - final String resourceName) throws Exception { - return SettableSchemaProvider.createImmediate( - TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class); + return SettableSchemaProvider.createRemote(TextToIRTransformer.transformText( + new URLYangTextSource(SharedSchemaRepositoryTest.class.getResource(resourceName))), + YangIRSchemaSource.class); } } diff --git a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryWithFeaturesTest.java b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryWithFeaturesTest.java index 4cfd3b3f0a..0df6bec0b6 100644 --- a/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryWithFeaturesTest.java +++ b/parser/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/repo/SharedSchemaRepositoryWithFeaturesTest.java @@ -17,22 +17,17 @@ import org.junit.jupiter.api.Test; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet; import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration; -import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource; -import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer; -public class SharedSchemaRepositoryWithFeaturesTest { +class SharedSchemaRepositoryWithFeaturesTest extends AbstractSchemaRepositoryTest { @Test - public void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception { + void testSharedSchemaRepositoryWithSomeFeaturesSupported() throws Exception { final var supportedFeatures = FeatureSet.of(QName.create("foobar-namespace", "test-feature-1")); final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test"); - final var foobar = getImmediateYangSourceProviderFromResource( - "/if-feature-resolution-test/shared-schema-repository/foobar.yang"); + final var foobar = assertYangTextResource("/if-feature-resolution-test/shared-schema-repository/foobar.yang"); foobar.register(sharedSchemaRepository); foobar.setResult(); @@ -60,11 +55,10 @@ public class SharedSchemaRepositoryWithFeaturesTest { } @Test - public void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception { + void testSharedSchemaRepositoryWithAllFeaturesSupported() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test"); - final var foobar = getImmediateYangSourceProviderFromResource( - "/if-feature-resolution-test/shared-schema-repository/foobar.yang"); + final var foobar = assertYangTextResource("/if-feature-resolution-test/shared-schema-repository/foobar.yang"); foobar.register(sharedSchemaRepository); foobar.setResult(); @@ -94,11 +88,10 @@ public class SharedSchemaRepositoryWithFeaturesTest { } @Test - public void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception { + void testSharedSchemaRepositoryWithNoFeaturesSupported() throws Exception { final var sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-with-features-test"); - final var foobar = getImmediateYangSourceProviderFromResource( - "/if-feature-resolution-test/shared-schema-repository/foobar.yang"); + final var foobar = assertYangTextResource("/if-feature-resolution-test/shared-schema-repository/foobar.yang"); foobar.register(sharedSchemaRepository); foobar.setResult(); @@ -117,15 +110,4 @@ public class SharedSchemaRepositoryWithFeaturesTest { assertInstanceOf(LeafSchemaNode.class, testContainerC.dataChildByName(QName.create(module.getQNameModule(), "test-leaf-c"))); } - - private static SettableSchemaProvider getImmediateYangSourceProviderFromResource( - final String resourceName) throws Exception { - return SettableSchemaProvider.createImmediate( - TextToIRTransformer.transformText(YangTextSource.forResource(resourceName)), YangIRSchemaSource.class); - } - - private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) { - assertNotNull(schemaContext); - assertEquals(moduleSize, schemaContext.getModules().size()); - } } diff --git a/parser/yang-test-util/src/main/java/org/opendaylight/yangtools/yang/test/util/YangParserTestUtils.java b/parser/yang-test-util/src/main/java/org/opendaylight/yangtools/yang/test/util/YangParserTestUtils.java index 77e19efb63..74fd6e2c34 100644 --- a/parser/yang-test-util/src/main/java/org/opendaylight/yangtools/yang/test/util/YangParserTestUtils.java +++ b/parser/yang-test-util/src/main/java/org/opendaylight/yangtools/yang/test/util/YangParserTestUtils.java @@ -28,6 +28,7 @@ import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation; import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet; +import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource; import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParser; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; @@ -108,7 +109,7 @@ public final class YangParserTestUtils { public static EffectiveModelContext parseYangResource(final String resource, final YangParserConfiguration config, final Set supportedFeatures) { return parseYangSources(config, supportedFeatures, - YangTextSource.forResource(YangParserTestUtils.class, resource)); + new URLYangTextSource(YangParserTestUtils.class.getResource(resource))); } /** @@ -275,11 +276,9 @@ public final class YangParserTestUtils { } public static EffectiveModelContext parseYangResources(final Class clazz, final Collection resources) { - final var sources = new ArrayList(resources.size()); - for (final String r : resources) { - sources.add(YangTextSource.forResource(clazz, r)); - } - return parseSources(YangParserConfiguration.DEFAULT, null, sources); + return parseSources(YangParserConfiguration.DEFAULT, null, resources.stream() + .map(resource -> new URLYangTextSource(clazz.getResource(resource))) + .toList()); } /** -- 2.36.6