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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
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.spi.meta.SomeModifiersUnresolvedException;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 public class Bug7480Test {
27 public void libSourcesTest() throws Exception {
28 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
29 assertNotNull(context);
31 final Set<Module> modules = context.getModules();
32 assertEquals(8, modules.size());
34 assertNotNull(context.findModule(new URI("foo-imp"), QName.parseRevision("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"), QName.parseRevision("2002-01-01")).isPresent());
40 final Set<Module> foo = context.findModules(new URI("foo"));
41 assertEquals(1, foo.size());
42 final Set<Module> subFoos = foo.iterator().next().getSubmodules();
43 assertEquals(1, subFoos.size());
45 final Module parentMod = context.findModule(new URI("parent-mod-ns"), QName.parseRevision("2017-09-07")).get();
46 assertEquals(1, parentMod.getSubmodules().size());
50 public void missingRelevantImportTest() throws Exception {
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."));
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 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
66 reactor.addSource(StmtTestUtils.sourceForResource(
67 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"));
68 reactor.addLibSources(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 final SchemaContext schemaContext = reactor.buildEffective();
73 assertNotNull(schemaContext);
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 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
81 reactor.addSource(StmtTestUtils.sourceForResource(
82 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"));
83 reactor.addLibSources(StmtTestUtils.sourceForResource(
84 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
85 StmtTestUtils.sourceForResource(
86 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"));
87 final SchemaContext schemaContext = reactor.buildEffective();
88 assertNotNull(schemaContext);