b66fb83e92d186742999bb1741faf27f7eed8b84
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserNegativeTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
21 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
22
23 public class YangParserNegativeTest {
24
25     @Test
26     public void testInvalidImport() throws IOException {
27         try {
28             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
29                     .getPath())) {
30                 TestUtils.loadModule(stream);
31                 fail("ValidationException should by thrown");
32             }
33         } catch (YangValidationException e) {
34             assertTrue(e.getMessage().contains("Not existing module imported"));
35         }
36     }
37
38     @Test
39     public void testTypeNotFound() throws IOException {
40         try {
41             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
42                     .getPath())) {
43                 TestUtils.loadModule(stream);
44                 fail("YangParseException should by thrown");
45             }
46         } catch (YangParseException e) {
47             assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
48         }
49     }
50
51     @Test
52     public void testInvalidAugmentTarget() throws IOException {
53         try {
54             final List<InputStream> streams = new ArrayList<>(2);
55             try (InputStream testFile0 = new FileInputStream(getClass()
56                     .getResource("/negative-scenario/testfile0.yang").getPath())) {
57                 streams.add(testFile0);
58                 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
59                         "/negative-scenario/testfile3.yang").getPath())) {
60                     streams.add(testFile3);
61                     assertEquals("Expected loaded files count is 2", 2, streams.size());
62                     TestUtils.loadModules(streams);
63                     fail("YangParseException should by thrown");
64                 }
65             }
66         } catch (YangParseException e) {
67             assertEquals("Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target",
68                     e.getMessage());
69         }
70     }
71
72     @Test
73     public void testInvalidRefine() throws IOException {
74         try {
75             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
76                     .getPath())) {
77                 TestUtils.loadModule(stream);
78                 fail("YangParseException should by thrown");
79             }
80         } catch (YangParseException e) {
81             assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
82         }
83     }
84
85     @Test
86     public void testInvalidLength() throws IOException {
87         try {
88             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
89                     .getPath())) {
90                 TestUtils.loadModule(stream);
91                 fail("YangParseException should by thrown");
92             }
93         } catch (YangParseException e) {
94             assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
95         }
96     }
97
98     @Test
99     public void testInvalidRange() throws IOException {
100         try {
101             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
102                     .getPath())) {
103                 TestUtils.loadModule(stream);
104                 fail("YangParseException should by thrown");
105             }
106         } catch (YangParseException e) {
107             assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
108         }
109     }
110
111     @Test
112     public void testDuplicateContainer() throws IOException {
113         try {
114             try (InputStream stream = new FileInputStream(getClass().getResource(
115                     "/negative-scenario/duplicity/container.yang").getPath())) {
116                 TestUtils.loadModule(stream);
117                 fail("YangParseException should by thrown");
118             }
119         } catch (YangParseException e) {
120             String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
121             assertEquals(expected, e.getMessage());
122         }
123     }
124
125     @Test
126     public void testDuplicateContainerList() throws IOException {
127         try {
128             try (InputStream stream = new FileInputStream(getClass().getResource(
129                     "/negative-scenario/duplicity/container-list.yang").getPath())) {
130                 TestUtils.loadModule(stream);
131                 fail("YangParseException should by thrown");
132             }
133         } catch (YangParseException e) {
134             String expected = "Error in module 'container-list' at line 10: Can not add 'list foo': node with same name 'foo' already declared at line 6.";
135             assertEquals(expected, e.getMessage());
136         }
137     }
138
139     @Test
140     public void testDuplicateContainerLeaf() throws IOException {
141         try {
142             try (InputStream stream = new FileInputStream(getClass().getResource(
143                     "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
144                 TestUtils.loadModule(stream);
145                 fail("YangParseException should by thrown");
146             }
147         } catch (YangParseException e) {
148             String expected = "Error in module 'container-leaf' at line 10: Can not add 'leaf foo': node with same name 'foo' already declared at line 6.";
149             assertEquals(expected, e.getMessage());
150         }
151     }
152
153     @Test
154     public void testDuplicateTypedef() throws IOException {
155         try {
156             try (InputStream stream = new FileInputStream(getClass().getResource(
157                     "/negative-scenario/duplicity/typedef.yang").getPath())) {
158                 TestUtils.loadModule(stream);
159                 fail("YangParseException should by thrown");
160             }
161         } catch (YangParseException e) {
162             String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
163             assertEquals(expected, e.getMessage());
164         }
165     }
166
167     @Test
168     public void testDuplicityInAugmentTarget1() throws Exception {
169         try {
170             try (InputStream stream1 = new FileInputStream(getClass().getResource(
171                     "/negative-scenario/duplicity/augment0.yang").getPath());
172                     InputStream stream2 = new FileInputStream(getClass().getResource(
173                             "/negative-scenario/duplicity/augment1.yang").getPath())) {
174                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
175                 fail("YangParseException should by thrown");
176             }
177         } catch (YangParseException e) {
178             String expected = "Error in module 'augment1' at line 10: Failed to perform augmentation: Error in module 'augment0' at line 8: Can not add 'leaf id' to 'container bar' in module 'augment0': node with same name already declared at line 9";
179             assertEquals(expected, e.getMessage());
180         }
181     }
182
183     @Test
184     public void testDuplicityInAugmentTarget2() throws Exception {
185         try {
186             try (InputStream stream1 = new FileInputStream(getClass().getResource(
187                     "/negative-scenario/duplicity/augment0.yang").getPath());
188                     InputStream stream2 = new FileInputStream(getClass().getResource(
189                             "/negative-scenario/duplicity/augment2.yang").getPath())) {
190                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
191                 fail("YangParseException should by thrown");
192             }
193         } catch (YangParseException e) {
194             String expected = "Error in module 'augment0' at line 17: Can not add 'anyxml delta' to node 'choice-ext' in module 'augment0': case with same name already declared at line 18";
195             assertEquals(expected, e.getMessage());
196         }
197     }
198
199 }