086f4f114c26f8a7a5c18400c80ef7e1fea4f2ae
[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.File;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
22 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
23 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
24
25 public class YangParserNegativeTest {
26
27     @Test
28     public void testInvalidImport() throws IOException {
29         try {
30             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
31                     .getPath())) {
32                 TestUtils.loadModule(stream);
33                 fail("ValidationException should by thrown");
34             }
35         } catch (YangValidationException e) {
36             assertTrue(e.getMessage().contains("Not existing module imported"));
37         }
38     }
39
40     @Test
41     public void testTypeNotFound() throws IOException {
42         try {
43             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
44                     .getPath())) {
45                 TestUtils.loadModule(stream);
46                 fail("YangParseException should by thrown");
47             }
48         } catch (YangParseException e) {
49             assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
50         }
51     }
52
53     @Test
54     public void testInvalidAugmentTarget() throws IOException {
55         try {
56             final List<InputStream> streams = new ArrayList<>(2);
57             try (InputStream testFile0 = new FileInputStream(getClass()
58                     .getResource("/negative-scenario/testfile0.yang").getPath())) {
59                 streams.add(testFile0);
60                 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
61                         "/negative-scenario/testfile3.yang").getPath())) {
62                     streams.add(testFile3);
63                     assertEquals("Expected loaded files count is 2", 2, streams.size());
64                     TestUtils.loadModules(streams);
65                     fail("YangParseException should by thrown");
66                 }
67             }
68         } catch (YangParseException e) {
69             assertEquals("Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target",
70                     e.getMessage());
71         }
72     }
73
74     @Test
75     public void testInvalidRefine() throws IOException {
76         try {
77             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
78                     .getPath())) {
79                 TestUtils.loadModule(stream);
80                 fail("YangParseException should by thrown");
81             }
82         } catch (YangParseException e) {
83             assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
84         }
85     }
86
87     @Test
88     public void testInvalidLength() throws IOException {
89         try {
90             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
91                     .getPath())) {
92                 TestUtils.loadModule(stream);
93                 fail("YangParseException should by thrown");
94             }
95         } catch (YangParseException e) {
96             assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
97         }
98     }
99
100     @Test
101     public void testInvalidRange() throws IOException {
102         try {
103             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
104                     .getPath())) {
105                 TestUtils.loadModule(stream);
106                 fail("YangParseException should by thrown");
107             }
108         } catch (YangParseException e) {
109             assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
110         }
111     }
112
113     @Test
114     public void testDuplicateContainer() throws IOException {
115         try {
116             try (InputStream stream = new FileInputStream(getClass().getResource(
117                     "/negative-scenario/duplicity/container.yang").getPath())) {
118                 TestUtils.loadModule(stream);
119                 fail("YangParseException should by thrown");
120             }
121         } catch (YangParseException e) {
122             String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
123             assertEquals(expected, e.getMessage());
124         }
125     }
126
127     @Test
128     public void testDuplicateContainerList() throws IOException {
129         try {
130             try (InputStream stream = new FileInputStream(getClass().getResource(
131                     "/negative-scenario/duplicity/container-list.yang").getPath())) {
132                 TestUtils.loadModule(stream);
133                 fail("YangParseException should by thrown");
134             }
135         } catch (YangParseException e) {
136             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.";
137             assertEquals(expected, e.getMessage());
138         }
139     }
140
141     @Test
142     public void testDuplicateContainerLeaf() throws IOException {
143         try {
144             try (InputStream stream = new FileInputStream(getClass().getResource(
145                     "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
146                 TestUtils.loadModule(stream);
147                 fail("YangParseException should by thrown");
148             }
149         } catch (YangParseException e) {
150             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.";
151             assertEquals(expected, e.getMessage());
152         }
153     }
154
155     @Test
156     public void testDuplicateTypedef() throws IOException {
157         try {
158             try (InputStream stream = new FileInputStream(getClass().getResource(
159                     "/negative-scenario/duplicity/typedef.yang").getPath())) {
160                 TestUtils.loadModule(stream);
161                 fail("YangParseException should by thrown");
162             }
163         } catch (YangParseException e) {
164             String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
165             assertEquals(expected, e.getMessage());
166         }
167     }
168
169     @Test
170     public void testDuplicityInAugmentTarget1() throws Exception {
171         try {
172             try (InputStream stream1 = new FileInputStream(getClass().getResource(
173                     "/negative-scenario/duplicity/augment0.yang").getPath());
174                     InputStream stream2 = new FileInputStream(getClass().getResource(
175                             "/negative-scenario/duplicity/augment1.yang").getPath())) {
176                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
177                 fail("YangParseException should by thrown");
178             }
179         } catch (YangParseException e) {
180             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";
181             assertEquals(expected, e.getMessage());
182         }
183     }
184
185     @Test
186     public void testDuplicityInAugmentTarget2() throws Exception {
187         try {
188             try (InputStream stream1 = new FileInputStream(getClass().getResource(
189                     "/negative-scenario/duplicity/augment0.yang").getPath());
190                     InputStream stream2 = new FileInputStream(getClass().getResource(
191                             "/negative-scenario/duplicity/augment2.yang").getPath())) {
192                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
193                 fail("YangParseException should by thrown");
194             }
195         } catch (YangParseException e) {
196             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";
197             assertEquals(expected, e.getMessage());
198         }
199     }
200
201     @Test
202     public void testMandatoryInAugment() throws IOException {
203         try {
204             try (InputStream stream1 = new FileInputStream(getClass().getResource("/negative-scenario/testfile8.yang")
205                     .getPath());
206                     InputStream stream2 = new FileInputStream(getClass().getResource(
207                             "/negative-scenario/testfile7.yang").getPath())) {
208                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
209                 fail("YangParseException should by thrown");
210             }
211         } catch (YangParseException e) {
212             String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node";
213             assertEquals(expected, e.getMessage());
214         }
215     }
216
217     @Test
218     public void testWrongDependenciesDir() throws Exception {
219         try {
220             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
221             File dependenciesDir = new File("/invalid");
222             YangModelParser parser = new YangParserImpl();
223             parser.parseYangModels(yangFile, dependenciesDir);
224             fail("Exception should by thrown");
225         } catch (IllegalStateException e) {
226             String expected = File.separator + "invalid does not exists";
227             assertEquals(expected, e.getMessage());
228         }
229     }
230
231     @Test
232     public void testWrongDependenciesDir2() throws Exception {
233         try {
234             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
235             File dependenciesDir = new File(getClass().getResource("/model").getPath());
236             YangModelParser parser = new YangParserImpl();
237             parser.parseYangModels(yangFile, dependenciesDir);
238             fail("Exception should by thrown");
239         } catch (YangValidationException e) {
240             String expected = "Not existing module imported";
241             assertTrue(e.getMessage().contains(expected));
242         }
243     }
244
245 }