Bug 2366 - Effective statements impl for new yang parser.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / augment / AugmentTest.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.augment;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
21
22 public class AugmentTest {
23
24     private static final TestAugmentSource IMPORTED = new TestAugmentSource("imp", "/a");
25     private static final TestAugmentSource VALID_ABS = new TestAugmentSource("root", "/aug1/aug11/aug111");
26     private static final TestAugmentSource VALID_ABS_PREFIXED = new TestAugmentSource("root",
27             "/imp:aug1/imp:aug11/imp:aug111", "imp");
28     private static final TestAugmentSource VALID_REL = new TestAugmentSource("root", "aug1/aug11");
29     private static final TestAugmentSource VALID_REL_PREFIXED = new TestAugmentSource("root",
30             "imp:aug1/imp:aug11/imp:aug111", "imp");
31     private static final TestAugmentSource INVALID_REL_WHITE_SPACE = new TestAugmentSource("root", "..   /aug1/aug11");
32     private static final TestAugmentSource INVALID_REL1 = new TestAugmentSource("root", "./aug1/aug11");
33     private static final TestAugmentSource INVALID_REL2 = new TestAugmentSource("root", "../aug1/aug11");
34     private static final TestAugmentSource INVALID_ABS = new TestAugmentSource("root", "//aug1/aug11/aug111");
35     private static final TestAugmentSource INVALID_ABS_PREFIXED_NO_IMP = new TestAugmentSource("root",
36             "imp:aug1/imp:aug11/imp:aug111");
37     private static final TestAugmentSource INVALID_EMPTY = new TestAugmentSource("root", "");
38     private static final TestAugmentSource INVALID_XPATH = new TestAugmentSource("root", "/aug1/-");
39
40     @Test
41     public void validAugAbsTest() throws SourceException, ReactorException {
42
43         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
44         addSources(reactor, VALID_ABS);
45
46         try {
47             reactor.build();
48         } catch (Exception e) {
49             // if augment argument is correct we only catch an exception that it cannot be found in mock model
50             assertEquals(NullPointerException.class, e.getClass());
51         }
52     }
53
54     @Test
55     public void validAugAbsPrefixedTest() throws SourceException, ReactorException {
56
57         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         addSources(reactor, IMPORTED, VALID_ABS_PREFIXED);
59
60         try {
61             reactor.build();
62         } catch (Exception e) {
63             // if augment argument is correct we only catch an exception that it cannot be found in mock model
64             assertEquals(NullPointerException.class, e.getClass());
65         }
66     }
67
68     @Test
69     public void validAugRelTest() throws SourceException, ReactorException {
70
71         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
72         addSources(reactor, VALID_REL);
73
74         try {
75             reactor.build();
76         } catch (Exception e) {
77             // if augment argument is correct we only catch an exception that it cannot be found in mock model
78             assertEquals(NullPointerException.class, e.getClass());
79         }
80     }
81
82     @Test
83     public void validAugRelPrefixedTest() throws SourceException, ReactorException {
84
85         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
86         addSources(reactor, IMPORTED, VALID_REL_PREFIXED);
87
88         try {
89             reactor.build();
90         } catch (Exception e) {
91             // if augment argument is correct we only catch an exception that it cannot be found in mock model
92             assertEquals(NullPointerException.class, e.getClass());
93         }
94     }
95
96     @Test
97     public void validAugRelWhiteSpaceTest() throws SourceException, ReactorException {
98
99         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
100         addSources(reactor, INVALID_REL_WHITE_SPACE);
101
102         try {
103             reactor.build();
104             fail("reactor.process should fail due to invalid relative path");
105         } catch (Exception e) {
106             assertEquals(IllegalArgumentException.class, e.getClass());
107         }
108     }
109
110     @Test
111     public void invalidAugRel1Test() throws SourceException, ReactorException {
112
113         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
114         addSources(reactor, INVALID_REL1);
115
116         try {
117             reactor.build();
118             fail("reactor.process should fail due to invalid relative path");
119         } catch (Exception e) {
120             assertEquals(IllegalArgumentException.class, e.getClass());
121         }
122     }
123
124     @Test
125     public void invalidAugRel2Test() throws SourceException, ReactorException {
126
127         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
128         addSources(reactor, INVALID_REL2);
129
130         try {
131             reactor.build();
132             fail("reactor.process should fail due to invalid relative path");
133         } catch (Exception e) {
134             assertEquals(IllegalArgumentException.class, e.getClass());
135         }
136     }
137
138     @Test
139     public void invalidAugAbs() throws SourceException, ReactorException {
140
141         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
142         addSources(reactor, INVALID_ABS);
143
144         try {
145             reactor.build();
146             fail("reactor.process should fail due to invalid absolute path");
147         } catch (Exception e) {
148             assertEquals(IllegalArgumentException.class, e.getClass());
149         }
150     }
151
152     @Test
153     public void invalidAugAbsPrefixedNoImp() throws SourceException, ReactorException {
154
155         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
156         addSources(reactor, INVALID_ABS_PREFIXED_NO_IMP);
157
158         try {
159             reactor.build();
160             fail("reactor.process should fail due to missing import from augment path");
161         } catch (Exception e) {
162             assertEquals(IllegalArgumentException.class, e.getClass());
163         }
164     }
165
166     @Test
167     public void invalidAugEmptyTest() throws SourceException {
168
169         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
170         addSources(reactor, INVALID_EMPTY);
171
172         try {
173             reactor.build();
174             fail("reactor.process should fail due to empty path");
175         } catch (Exception e) {
176             assertEquals(IllegalArgumentException.class, e.getClass());
177         }
178     }
179
180     @Test
181     public void invalidAugXPathTest() throws SourceException {
182
183         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
184         addSources(reactor, INVALID_XPATH);
185
186         try {
187             reactor.build();
188             fail("reactor.process should fail due to invalid XPath");
189         } catch (Exception e) {
190             assertEquals(IllegalArgumentException.class, e.getClass());
191         }
192     }
193
194     private void addSources(BuildAction reactor, TestAugmentSource... sources) {
195         for (TestAugmentSource source : sources) {
196             reactor.addSource(source);
197         }
198     }
199
200 }