Separate out Module and Submodule interfaces
[yangtools.git] / yang / 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.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.Collection;
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.model.api.Submodule;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24
25 public class Bug7480Test {
26     @Test
27     public void libSourcesTest() throws Exception {
28         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
29         assertNotNull(context);
30
31         final Collection<? extends Module> modules = context.getModules();
32         assertEquals(8, modules.size());
33
34         assertNotNull(context.findModule(new URI("foo-imp"), Revision.of("2017-01-23")));
35         assertEquals(1, context.findModules(new URI("foo-imp-2")).size());
36         assertEquals(1, context.findModules(new URI("foo-imp-imp")).size());
37         assertEquals(1, context.findModules(new URI("bar")).size());
38         assertEquals(1, context.findModules(new URI("baz")).size());
39         assertTrue(context.findModule(new URI("baz-imp"), Revision.of("2002-01-01")).isPresent());
40         final Collection<? extends Module> foo = context.findModules(new URI("foo"));
41         assertEquals(1, foo.size());
42         final Collection<? extends Submodule> subFoos = foo.iterator().next().getSubmodules();
43         assertEquals(1, subFoos.size());
44
45         final Module parentMod = context.findModule(new URI("parent-mod-ns"), Revision.of("2017-09-07")).get();
46         assertEquals(1, parentMod.getSubmodules().size());
47     }
48
49     @Test
50     public void missingRelevantImportTest() throws Exception {
51         try {
52             StmtTestUtils.parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2");
53             fail("Test should fail due to missing import of required yang source from library");
54         } catch (final SomeModifiersUnresolvedException e) {
55             final String message = e.getSuppressed().length > 0 ? e.getSuppressed()[0].getCause().getMessage() : e
56                     .getCause().getCause().getMessage();
57             assertTrue(message.startsWith("Imported module [missing-lib] was not found."));
58         }
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 }