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