Address FIXME for QueuedNotificationManager
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / ImportResolutionBasicTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.yangtools.yang.stmt.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
25
26 public class ImportResolutionBasicTest {
27
28     private static final YangStatementSourceImpl ROOT_WITHOUT_IMPORT = new YangStatementSourceImpl(
29             "/semantic-statement-parser/import-arg-parsing/nature.yang", false);
30     private static final YangStatementSourceImpl IMPORT_ROOT = new YangStatementSourceImpl(
31             "/semantic-statement-parser/import-arg-parsing/mammal.yang", false);
32     private static final YangStatementSourceImpl IMPORT_DERIVED = new YangStatementSourceImpl(
33             "/semantic-statement-parser/import-arg-parsing/human.yang", false);
34     private static final YangStatementSourceImpl IMPORT_SELF = new YangStatementSourceImpl(
35             "/semantic-statement-parser/import-arg-parsing/egocentric.yang", false);
36     private static final YangStatementSourceImpl CYCLE_YIN = new YangStatementSourceImpl(
37             "/semantic-statement-parser/import-arg-parsing/cycle-yin.yang", false);
38     private static final YangStatementSourceImpl CYCLE_YANG = new YangStatementSourceImpl(
39             "/semantic-statement-parser/import-arg-parsing/cycle-yang.yang", false);
40     private static final YangStatementSourceImpl FOO = new YangStatementSourceImpl(
41             "/semantic-statement-parser/bug2649/foo.yang", false);
42     private static final YangStatementSourceImpl IMPORT = new YangStatementSourceImpl(
43             "/semantic-statement-parser/bug2649/import-module.yang", false);
44
45
46     @Test
47     public void inImportOrderTest() throws SourceException, ReactorException {
48         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49         addSources(reactor, ROOT_WITHOUT_IMPORT, IMPORT_ROOT, IMPORT_DERIVED);
50         EffectiveModelContext result = reactor.build();
51         assertNotNull(result);
52     }
53
54     @Test
55     public void inInverseOfImportOrderTest() throws SourceException, ReactorException {
56         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57         addSources(reactor, IMPORT_DERIVED, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
58         EffectiveModelContext result = reactor.build();
59         assertNotNull(result);
60     }
61
62     @Test
63     public void missingImportedSourceTest() throws SourceException {
64         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
65         addSources(reactor, IMPORT_DERIVED, ROOT_WITHOUT_IMPORT);
66         try {
67             reactor.build();
68             fail("reactor.process should fail due to missing imported source");
69         } catch (ReactorException e) {
70             assertTrue(e instanceof SomeModifiersUnresolvedException);
71             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
72         }
73
74     }
75
76     @Test
77     public void circularImportsTest() throws SourceException {
78         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
79         addSources(reactor, CYCLE_YIN, CYCLE_YANG);
80         try {
81             reactor.build();
82             fail("reactor.process should fail due to circular import");
83         } catch (ReactorException e) {
84             assertTrue(e instanceof SomeModifiersUnresolvedException);
85             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
86         }
87     }
88
89     @Test
90     public void selfImportTest() throws SourceException {
91         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
92         addSources(reactor, IMPORT_SELF, IMPORT_ROOT, ROOT_WITHOUT_IMPORT);
93         try {
94             reactor.build();
95             fail("reactor.process should fail due to self import");
96         } catch (ReactorException e) {
97             assertTrue(e instanceof SomeModifiersUnresolvedException);
98             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
99         }
100     }
101
102     @Test
103     public void bug2649Test() throws SourceException, ReactorException{
104         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
105         addSources(reactor, FOO, IMPORT);
106
107         EffectiveSchemaContext buildEffective = reactor.buildEffective();
108         assertNotNull(buildEffective);
109     }
110
111     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
112         for (YangStatementSourceImpl source : sources) {
113             reactor.addSource(source);
114         }
115     }
116
117 }