2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
26 void libSourcesTest() throws Exception {
27 final var context = parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
28 assertNotNull(context);
30 final var modules = context.getModules();
31 assertEquals(8, modules.size());
33 assertNotNull(context.findModule(XMLNamespace.of("foo-imp"), Revision.of("2017-01-23")));
34 assertEquals(1, context.findModules(XMLNamespace.of("foo-imp-2")).size());
35 assertEquals(1, context.findModules(XMLNamespace.of("foo-imp-imp")).size());
36 assertEquals(1, context.findModules(XMLNamespace.of("bar")).size());
37 assertEquals(1, context.findModules(XMLNamespace.of("baz")).size());
38 assertTrue(context.findModule(XMLNamespace.of("baz-imp"), Revision.of("2002-01-01")).isPresent());
39 final var foo = context.findModules(XMLNamespace.of("foo"));
40 assertEquals(1, foo.size());
41 final var subFoos = foo.iterator().next().getSubmodules();
42 assertEquals(1, subFoos.size());
44 final var parentMod = context.findModule(XMLNamespace.of("parent-mod-ns"), Revision.of("2017-09-07"))
46 assertEquals(1, parentMod.getSubmodules().size());
50 void missingRelevantImportTest() throws Exception {
51 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
52 () -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
53 final var message = ex.getSuppressed().length > 0 ? ex.getSuppressed()[0].getMessage()
54 : ex.getCause().getMessage();
55 assertThat(message, startsWith("Imported module [missing-lib] was not found."));
59 void testHandlingOfMainSourceConflictingWithLibSource() throws Exception {
60 // parent module as main source and as lib source at the same time
61 // parser should remove it from the required lib sources and thus avoid module namespace collision
62 final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
63 .addSource(StmtTestUtils.sourceForResource(
64 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
66 StmtTestUtils.sourceForResource(
67 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
68 StmtTestUtils.sourceForResource(
69 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
71 assertNotNull(schemaContext);
75 void testHandlingOfMainSourceConflictingWithLibSource2() throws Exception {
76 // submodule as main source and as lib source at the same time
77 // parser should remove it from the required lib sources and thus avoid submodule name collision
78 final var schemaContext = RFC7950Reactors.defaultReactor().newBuild()
79 .addSource(StmtTestUtils.sourceForResource(
80 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
82 StmtTestUtils.sourceForResource(
83 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
84 StmtTestUtils.sourceForResource(
85 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
87 assertNotNull(schemaContext);
90 private static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
91 final String yangLibsDirectoryPath) throws Exception {
92 return RFC7950Reactors.defaultReactor().newBuild()
93 .addSources(TestUtils.loadSources(yangFilesDirectoryPath))
94 .addLibSources(TestUtils.loadSources(yangLibsDirectoryPath))