Rename binding-runtime-dynamic to binding-loader
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug7480Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
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;
16
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;
23
24 class Bug7480Test {
25     @Test
26     void libSourcesTest() throws Exception {
27         final var context = parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
28         assertNotNull(context);
29
30         final var modules = context.getModules();
31         assertEquals(8, modules.size());
32
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());
43
44         final var parentMod = context.findModule(XMLNamespace.of("parent-mod-ns"), Revision.of("2017-09-07"))
45             .orElseThrow();
46         assertEquals(1, parentMod.getSubmodules().size());
47     }
48
49     @Test
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."));
56     }
57
58     @Test
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"))
65             .addLibSources(
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"))
70             .buildEffective();
71         assertNotNull(schemaContext);
72     }
73
74     @Test
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"))
81             .addLibSources(
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"))
86             .buildEffective();
87         assertNotNull(schemaContext);
88     }
89
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))
95             .buildEffective();
96     }
97 }