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.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.impl.DefaultReactors;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 public class Bug7480Test {
26 public void libSourcesTest() throws Exception {
27 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
28 assertNotNull(context);
30 final Set<Module> modules = context.getModules();
31 assertEquals(8, modules.size());
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());
44 final Module parentMod = context.findModule(new URI("parent-mod-ns"), Revision.of("2017-09-07")).get();
45 assertEquals(1, parentMod.getSubmodules().size());
49 public void missingRelevantImportTest() throws Exception {
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."));
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 = DefaultReactors.defaultReactor().newBuild()
65 .addSource(StmtTestUtils.sourceForResource(
66 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
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"))
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 SchemaContext schemaContext = DefaultReactors.defaultReactor().newBuild()
81 .addSource(StmtTestUtils.sourceForResource(
82 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
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"))
89 assertNotNull(schemaContext);