Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / 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;
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.Ignore;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
23
24 public class AugmentTest {
25
26     private static final YangStatementSourceImpl IMPORTED = new YangStatementSourceImpl(
27             "/semantic-statement-parser/augment-arg-parsing/imported.yang", false);
28
29     private static YangStatementSourceImpl VALID_ARGS = new YangStatementSourceImpl(
30             "/semantic-statement-parser/augment-arg-parsing/root-valid-aug-args.yang", false);
31     private static YangStatementSourceImpl INVALID_REL1 = new YangStatementSourceImpl(
32             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel1.yang", false);
33     private static YangStatementSourceImpl INVALID_REL2 = new YangStatementSourceImpl(
34             "/semantic-statement-parser/augment-arg-parsing/root-invalid-rel2.yang", false);
35     private static YangStatementSourceImpl INVALID_ABS = new YangStatementSourceImpl(
36             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs.yang", false);
37     private static YangStatementSourceImpl INVALID_ABS_PREFIXED_NO_IMP = new YangStatementSourceImpl(
38             "/semantic-statement-parser/augment-arg-parsing/root-invalid-abs-no-imp.yang", false);
39     private static YangStatementSourceImpl INVALID_EMPTY = new YangStatementSourceImpl(
40             "/semantic-statement-parser/augment-arg-parsing/root-invalid-empty.yang", false);
41     private static YangStatementSourceImpl INVALID_XPATH = new YangStatementSourceImpl(
42             "/semantic-statement-parser/augment-arg-parsing/root-invalid-xpath.yang", false);
43
44     @Test
45     public void validAugAbsTest() throws SourceException, ReactorException {
46
47         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
48         addSources(reactor, IMPORTED, VALID_ARGS);
49
50         final EffectiveModelContext result = reactor.build();
51         assertNotNull(result);
52     }
53
54     @Test
55     public void invalidAugRel1Test() throws SourceException, ReactorException {
56
57         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
58         addSources(reactor, INVALID_REL1);
59
60         try {
61             reactor.build();
62             fail("reactor.process should fail due to invalid relative path");
63         } catch (Exception e) {
64             assertEquals(IllegalArgumentException.class, e.getClass());
65         }
66     }
67
68     @Test
69     public void invalidAugRel2Test() throws SourceException, ReactorException {
70
71         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
72         addSources(reactor, INVALID_REL2);
73
74         try {
75             reactor.build();
76             fail("reactor.process should fail due to invalid relative path");
77         } catch (Exception e) {
78             assertEquals(IllegalArgumentException.class, e.getClass());
79         }
80     }
81
82     @Test
83     public void invalidAugAbs() throws SourceException, ReactorException {
84
85         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
86         addSources(reactor, INVALID_ABS);
87
88         try {
89             reactor.build();
90             fail("reactor.process should fail due to invalid absolute path");
91         } catch (Exception e) {
92             assertEquals(IllegalArgumentException.class, e.getClass());
93         }
94     }
95
96     @Test
97     public void invalidAugAbsPrefixedNoImp() throws SourceException, ReactorException {
98
99         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
100         addSources(reactor, INVALID_ABS_PREFIXED_NO_IMP);
101
102         try {
103             reactor.build();
104             fail("reactor.process should fail due to missing import from augment path");
105         } catch (Exception e) {
106             assertEquals(IllegalArgumentException.class, e.getClass());
107         }
108     }
109
110     @Test
111     @Ignore
112     public void invalidAugEmptyTest() throws SourceException {
113
114         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
115         addSources(reactor, INVALID_EMPTY);
116
117         try {
118             reactor.build();
119             fail("reactor.process should fail due to empty path");
120         } catch (Exception e) {
121             assertEquals(IllegalArgumentException.class, e.getClass());
122         }
123     }
124
125     @Test
126     @Ignore
127     public void invalidAugXPathTest() throws SourceException {
128
129         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
130         addSources(reactor, INVALID_XPATH);
131
132         try {
133             reactor.build();
134             fail("reactor.process should fail due to invalid XPath");
135         } catch (Exception e) {
136             assertEquals(IllegalArgumentException.class, e.getClass());
137         }
138     }
139
140     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
141         for (YangStatementSourceImpl source : sources) {
142             reactor.addSource(source);
143         }
144     }
145 }