Bug 6022: Deviation statement is not fully available in the YANG parser output
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / IncludeResolutionTest.java
1 /*
2  * Copyright (c) 2016 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;
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 java.util.logging.Logger;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
25
26 public class IncludeResolutionTest {
27
28     private static final Logger log = Logger.getLogger(IncludeResolutionTest.class.getName());
29
30     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
31             "/semantic-statement-parser/include-arg-parsing/root-module.yang", false);
32     private static final YangStatementSourceImpl SUBMODULE1 = new YangStatementSourceImpl(
33             "/semantic-statement-parser/include-arg-parsing/submodule-1.yang", false);
34     private static final YangStatementSourceImpl SUBMODULE2 = new YangStatementSourceImpl(
35             "/semantic-statement-parser/include-arg-parsing/submodule-2.yang", false);
36     private static final YangStatementSourceImpl ERROR_MODULE = new YangStatementSourceImpl(
37             "/semantic-statement-parser/include-arg-parsing/error-module.yang", false);
38     private static final YangStatementSourceImpl ERROR_SUBMODULE = new YangStatementSourceImpl(
39             "/semantic-statement-parser/include-arg-parsing/error-submodule.yang", false);
40
41     private static final YangStatementSourceImpl MISSING_PARENT_MODULE = new YangStatementSourceImpl(
42             "/semantic-statement-parser/include-arg-parsing/missing-parent.yang", false);
43
44     @Test
45     public void includeTest() throws SourceException, ReactorException {
46         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
47         addSources(reactor, ROOT, SUBMODULE1, SUBMODULE2);
48         EffectiveModelContext result = reactor.build();
49         assertNotNull(result);
50     }
51
52     @Test
53     public void missingIncludedSourceTest() throws SourceException {
54         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
55         addSources(reactor, ERROR_MODULE);
56         try {
57             reactor.build();
58             fail("reactor.process should fail due to missing included source");
59         } catch (ReactorException e) {
60             assertTrue(e instanceof SomeModifiersUnresolvedException);
61             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
62             log.info(e.getMessage());
63         }
64
65     }
66
67     @Test
68     public void missingIncludedSourceTest2() throws SourceException {
69         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
70         addSources(reactor, ERROR_SUBMODULE);
71         try {
72             reactor.build();
73             fail("reactor.process should fail due to missing included source");
74         } catch (ReactorException e) {
75             assertTrue(e instanceof SomeModifiersUnresolvedException);
76             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
77             log.info(e.getMessage());
78         }
79
80     }
81
82     @Test
83     public void missingIncludedSourceTest3() throws SourceException, ReactorException {
84         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85         addSources(reactor, MISSING_PARENT_MODULE);
86         try {
87             reactor.build();
88             fail("reactor.process should fail due to missing belongsTo source");
89         } catch (ReactorException e) {
90             log.info(e.getMessage());
91         }
92
93     }
94
95     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
96         for (YangStatementSourceImpl source : sources) {
97             reactor.addSource(source);
98         }
99     }
100 }