Fix a few checkstyle warnings
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / IncludeRevisionsTest.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 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.spi.source.StatementStreamSource;
21 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
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 IncludeRevisionsTest {
27
28     private static final YangStatementSourceImpl EQUAL_ROOT = new YangStatementSourceImpl("/revisions/equal-root.yang",
29             false);
30     private static final YangStatementSourceImpl EQUAL_REV = new YangStatementSourceImpl("/revisions/equal-rev.yang",
31             false);
32     private static final YangStatementSourceImpl UNEQUAL_ROOT = new YangStatementSourceImpl(
33             "/revisions/unequal-root.yang", false);
34     private static final YangStatementSourceImpl UNEQUAL_REV = new YangStatementSourceImpl(
35             "/revisions/unequal-rev.yang", false);
36     private static final YangStatementSourceImpl SUBMOD_ONLY_ROOT = new YangStatementSourceImpl(
37             "/revisions/submod-only-root.yang", false);
38     private static final YangStatementSourceImpl SUBMOD_ONLY_REV = new YangStatementSourceImpl(
39             "/revisions/submod-only-rev.yang", false);
40     private static final YangStatementSourceImpl MOD_ONLY_ROOT = new YangStatementSourceImpl(
41             "/revisions/mod-only-root.yang", false);
42     private static final YangStatementSourceImpl MOD_ONLY_REV = new YangStatementSourceImpl(
43             "/revisions/mod-only-rev.yang", false);
44     private static final YangStatementSourceImpl MOD_ONLY_1970_ROOT = new YangStatementSourceImpl(
45             "/revisions/mod-1970-root.yang", false);
46     private static final YangStatementSourceImpl MOD_ONLY_1970_REV = new YangStatementSourceImpl(
47             "/revisions/mod-1970-rev.yang", false);
48     private static final YangStatementSourceImpl NOWHERE_ROOT = new YangStatementSourceImpl(
49             "/revisions/nowhere-root.yang", false);
50     private static final YangStatementSourceImpl NOWHERE_REV = new YangStatementSourceImpl(
51             "/revisions/nowhere-rev.yang", false);
52
53     @Test
54     public void revsEqualTest() throws SourceException, ReactorException {
55
56         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57         addSources(reactor, EQUAL_REV, EQUAL_ROOT);
58
59         EffectiveModelContext result = reactor.build();
60         assertNotNull(result);
61     }
62
63     @Test
64     public void revsUnequalTest() throws SourceException, ReactorException {
65
66         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
67         addSources(reactor, UNEQUAL_REV, UNEQUAL_ROOT);
68
69         try {
70             reactor.build();
71             fail("reactor.process should fail due to unequal revisions in include and submodule");
72         } catch (ReactorException e) {
73             assertTrue(e instanceof SomeModifiersUnresolvedException);
74             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
75         }
76     }
77
78     @Test
79     public void revIncludeOnly() throws SourceException, ReactorException {
80
81         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
82         addSources(reactor, SUBMOD_ONLY_REV, SUBMOD_ONLY_ROOT);
83
84         EffectiveModelContext result = reactor.build();
85         assertNotNull(result);
86     }
87
88     @Test
89     public void revInModuleOnly() throws SourceException, ReactorException {
90
91         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
92         addSources(reactor, MOD_ONLY_REV, MOD_ONLY_ROOT);
93
94         try {
95             reactor.build();
96             fail("reactor.process should fail due to missing revision in included submodule");
97         } catch (ReactorException e) {
98             assertTrue(e instanceof SomeModifiersUnresolvedException);
99             assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, e.getPhase());
100         }
101     }
102
103     @Test
104     public void rev1970InModuleOnlyTest() throws SourceException, ReactorException {
105
106         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
107         addSources(reactor, MOD_ONLY_1970_REV, MOD_ONLY_1970_ROOT);
108
109         EffectiveModelContext result = reactor.build();
110         assertNotNull(result);
111     }
112
113     @Test
114     public void revNowhereTest() throws SourceException, ReactorException {
115
116         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
117         addSources(reactor, NOWHERE_REV, NOWHERE_ROOT);
118
119         EffectiveModelContext result = reactor.build();
120         assertNotNull(result);
121     }
122
123     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final StatementStreamSource... sources) {
124         for (StatementStreamSource source : sources) {
125             reactor.addSource(source);
126         }
127     }
128 }