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.SimpleDateFormatUtil;
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.findModuleByNamespaceAndRevision(new URI("foo-imp"), SimpleDateFormatUtil
35 .getRevisionFormat().parse("2017-01-23")));
36 assertEquals(1, context.findModuleByNamespace(new URI("foo-imp-2")).size());
37 assertEquals(1, context.findModuleByNamespace(new URI("foo-imp-imp")).size());
38 assertEquals(1, context.findModuleByNamespace(new URI("bar")).size());
39 assertEquals(1, context.findModuleByNamespace(new URI("baz")).size());
40 assertNotNull(context.findModuleByNamespaceAndRevision(new URI("baz-imp"), SimpleDateFormatUtil
41 .getRevisionFormat().parse("2002-01-01")));
42 final Set<Module> foo = context.findModuleByNamespace(new URI("foo"));
43 assertEquals(1, foo.size());
44 final Set<Module> subFoos = foo.iterator().next().getSubmodules();
45 assertEquals(1, subFoos.size());
47 final Module parentMod = context.findModuleByNamespaceAndRevision(new URI("parent-mod-ns"),
48 SimpleDateFormatUtil.getRevisionFormat().parse("2017-09-07"));
49 assertNotNull(parentMod);
50 assertEquals(1, parentMod.getSubmodules().size());
54 public void missingRelevantImportTest() throws Exception {
56 StmtTestUtils.parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2");
57 fail("Test should fail due to missing import of required yang source from library");
58 } catch (final SomeModifiersUnresolvedException e) {
59 final String message = e.getSuppressed().length > 0 ? e.getSuppressed()[0].getCause().getMessage() : e
60 .getCause().getCause().getMessage();
61 assertTrue(message.startsWith("Imported module [missing-lib] was not found."));
66 public void testHandlingOfMainSourceConflictingWithLibSource() throws Exception {
67 // parent module as main source and as lib source at the same time
68 // parser should remove it from the required lib sources and thus avoid module namespace collision
69 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
70 reactor.addSource(StmtTestUtils.sourceForResource(
71 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"));
72 reactor.addLibSources(StmtTestUtils.sourceForResource(
73 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
74 StmtTestUtils.sourceForResource(
75 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"));
76 final SchemaContext schemaContext = reactor.buildEffective();
77 assertNotNull(schemaContext);
81 public void testHandlingOfMainSourceConflictingWithLibSource2() throws Exception {
82 // submodule as main source and as lib source at the same time
83 // parser should remove it from the required lib sources and thus avoid submodule name collision
84 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85 reactor.addSource(StmtTestUtils.sourceForResource(
86 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"));
87 reactor.addLibSources(StmtTestUtils.sourceForResource(
88 "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
89 StmtTestUtils.sourceForResource(
90 "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"));
91 final SchemaContext schemaContext = reactor.buildEffective();
92 assertNotNull(schemaContext);