Move SchemaNodeIdentifier to yang-common
[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.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.Collection;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.Submodule;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
27
28 public class Bug7480Test {
29     @Test
30     public void libSourcesTest() throws Exception {
31         final SchemaContext context = parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
32         assertNotNull(context);
33
34         final Collection<? extends Module> modules = context.getModules();
35         assertEquals(8, modules.size());
36
37         assertNotNull(context.findModule(XMLNamespace.of("foo-imp"), Revision.of("2017-01-23")));
38         assertEquals(1, context.findModules(XMLNamespace.of("foo-imp-2")).size());
39         assertEquals(1, context.findModules(XMLNamespace.of("foo-imp-imp")).size());
40         assertEquals(1, context.findModules(XMLNamespace.of("bar")).size());
41         assertEquals(1, context.findModules(XMLNamespace.of("baz")).size());
42         assertTrue(context.findModule(XMLNamespace.of("baz-imp"), Revision.of("2002-01-01")).isPresent());
43         final Collection<? extends Module> foo = context.findModules(XMLNamespace.of("foo"));
44         assertEquals(1, foo.size());
45         final Collection<? extends Submodule> subFoos = foo.iterator().next().getSubmodules();
46         assertEquals(1, subFoos.size());
47
48         final Module parentMod = context.findModule(XMLNamespace.of("parent-mod-ns"), Revision.of("2017-09-07")).get();
49         assertEquals(1, parentMod.getSubmodules().size());
50     }
51
52     @Test
53     public void missingRelevantImportTest() throws Exception {
54         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
55             () -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
56         final String message = ex.getSuppressed().length > 0
57             ? ex.getSuppressed()[0].getCause().getMessage() : ex.getCause().getCause().getMessage();
58         assertThat(message, startsWith("Imported module [missing-lib] was not found."));
59     }
60
61     @Test
62     public void testHandlingOfMainSourceConflictingWithLibSource() throws Exception {
63         // parent module as main source and as lib source at the same time
64         // parser should remove it from the required lib sources and thus avoid module namespace collision
65         final SchemaContext schemaContext =  RFC7950Reactors.defaultReactor().newBuild()
66             .addSource(StmtTestUtils.sourceForResource(
67                 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
68             .addLibSources(
69                 StmtTestUtils.sourceForResource(
70                     "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
71                 StmtTestUtils.sourceForResource(
72                     "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
73             .buildEffective();
74         assertNotNull(schemaContext);
75     }
76
77     @Test
78     public void testHandlingOfMainSourceConflictingWithLibSource2() throws Exception {
79         // submodule as main source and as lib source at the same time
80         // parser should remove it from the required lib sources and thus avoid submodule name collision
81         final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
82             .addSource(StmtTestUtils.sourceForResource(
83                 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
84             .addLibSources(
85                 StmtTestUtils.sourceForResource(
86                     "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
87                 StmtTestUtils.sourceForResource(
88                     "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
89             .buildEffective();
90         assertNotNull(schemaContext);
91     }
92
93     private static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
94             final String yangLibsDirectoryPath) throws Exception {
95         return RFC7950Reactors.defaultReactor().newBuild()
96             .addSources(TestUtils.loadSources(yangFilesDirectoryPath))
97             .addLibSources(TestUtils.loadSources(yangLibsDirectoryPath))
98             .buildEffective();
99     }
100 }