*/
package org.opendaylight.yangtools.yang.stmt;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.Collection;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.Submodule;
public class Bug7480Test {
@Test
public void libSourcesTest() throws Exception {
- final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
+ final SchemaContext context = parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
assertNotNull(context);
final Collection<? extends Module> modules = context.getModules();
@Test
public void missingRelevantImportTest() throws Exception {
- try {
- StmtTestUtils.parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2");
- fail("Test should fail due to missing import of required yang source from library");
- } catch (final SomeModifiersUnresolvedException e) {
- final String message = e.getSuppressed().length > 0 ? e.getSuppressed()[0].getCause().getMessage() : e
- .getCause().getCause().getMessage();
- assertTrue(message.startsWith("Imported module [missing-lib] was not found."));
- }
+ final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+ () -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
+ final String message = ex.getSuppressed().length > 0
+ ? ex.getSuppressed()[0].getCause().getMessage() : ex.getCause().getCause().getMessage();
+ assertThat(message, startsWith("Imported module [missing-lib] was not found."));
}
@Test
// parent module as main source and as lib source at the same time
// parser should remove it from the required lib sources and thus avoid module namespace collision
final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
- .addSource(StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
- .addLibSources(
- StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
- StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
- .buildEffective();
+ .addSource(StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
+ .addLibSources(
+ StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
+ StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
+ .buildEffective();
assertNotNull(schemaContext);
}
// submodule as main source and as lib source at the same time
// parser should remove it from the required lib sources and thus avoid submodule name collision
final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
- .addSource(StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
- .addLibSources(
- StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
- StmtTestUtils.sourceForResource(
- "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
- .buildEffective();
+ .addSource(StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
+ .addLibSources(
+ StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
+ StmtTestUtils.sourceForResource(
+ "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
+ .buildEffective();
assertNotNull(schemaContext);
}
+
+ private static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
+ final String yangLibsDirectoryPath) throws Exception {
+ return RFC7950Reactors.defaultReactor().newBuild()
+ .addSources(TestUtils.loadSources(yangFilesDirectoryPath))
+ .addLibSources(TestUtils.loadSources(yangLibsDirectoryPath))
+ .buildEffective();
+ }
}
return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
}
- public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
- final String yangLibsDirectoryPath)
- throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
- return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
- }
-
- public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
- final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
- ReactorException, IOException, YangSyntaxErrorException {
- final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
- final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
-
- return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
- supportedFeatures);
- }
-
- private static EffectiveModelContext parseYangSources(final File[] yangFiles, final File[] libFiles,
- final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
- final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
- for (int i = 0; i < yangFiles.length; i++) {
- yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
- }
-
- final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
- for (int i = 0; i < libFiles.length; i++) {
- libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
- }
-
- return parseYangSources(yangSources, libSources, supportedFeatures);
- }
-
- private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
- final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
-
- final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
- .addSources(yangSources).addLibSources(libSources);
- if (supportedFeatures != null) {
- reactor.setSupportedFeatures(supportedFeatures);
- }
-
- return reactor.buildEffective();
- }
-
public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
throws URISyntaxException, SAXException, IOException, ReactorException {
return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);