Merge "BUG-1276: fixed generated union constructor"
[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.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
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 Exception {
29         File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
30         try {
31             try (InputStream stream = new FileInputStream(yang)) {
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 Exception {
42         File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
43         try {
44             try (InputStream stream = new FileInputStream(yang)) {
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 Exception {
55         File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
56         File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
57         try {
58             final List<InputStream> streams = new ArrayList<>(2);
59             try (InputStream testFile0 = new FileInputStream(yang1)) {
60                 streams.add(testFile0);
61                 try (InputStream testFile3 = new FileInputStream(yang2)) {
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(
70                     "Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target: augment /data:unknown",
71                     e.getMessage());
72         }
73     }
74
75     @Test
76     public void testInvalidRefine() throws Exception {
77         File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
78         try {
79             try (InputStream stream = new FileInputStream(yang)) {
80                 TestUtils.loadModule(stream);
81                 fail("YangParseException should by thrown");
82             }
83         } catch (YangParseException e) {
84             assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
85         }
86     }
87
88     @Test
89     public void testInvalidLength() throws Exception {
90         File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
91         try {
92             try (InputStream stream = new FileInputStream(yang)) {
93                 TestUtils.loadModule(stream);
94                 fail("YangParseException should by thrown");
95             }
96         } catch (YangParseException e) {
97             assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
98         }
99     }
100
101     @Test
102     public void testInvalidRange() throws Exception {
103         File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
104         try {
105             try (InputStream stream = new FileInputStream(yang)) {
106                 TestUtils.loadModule(stream);
107                 fail("YangParseException should by thrown");
108             }
109         } catch (YangParseException e) {
110             assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
111         }
112     }
113
114     @Test
115     public void testDuplicateContainer() throws Exception {
116         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
117         try {
118             try (InputStream stream = new FileInputStream(yang)) {
119                 TestUtils.loadModule(stream);
120                 fail("YangParseException should by thrown");
121             }
122         } catch (YangParseException e) {
123             String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
124             assertEquals(expected, e.getMessage());
125         }
126     }
127
128     @Test
129     public void testDuplicateContainerList() throws Exception {
130         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
131         try {
132             try (InputStream stream = new FileInputStream(yang)) {
133                 TestUtils.loadModule(stream);
134                 fail("YangParseException should by thrown");
135             }
136         } catch (YangParseException e) {
137             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.";
138             assertEquals(expected, e.getMessage());
139         }
140     }
141
142     @Test
143     public void testDuplicateContainerLeaf() throws Exception {
144         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
145         try {
146             try (InputStream stream = new FileInputStream(yang)) {
147                 TestUtils.loadModule(stream);
148                 fail("YangParseException should by thrown");
149             }
150         } catch (YangParseException e) {
151             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.";
152             assertEquals(expected, e.getMessage());
153         }
154     }
155
156     @Test
157     public void testDuplicateTypedef() throws Exception {
158         File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
159         try {
160             try (InputStream stream = new FileInputStream(yang)) {
161                 TestUtils.loadModule(stream);
162                 fail("YangParseException should by thrown");
163             }
164         } catch (YangParseException e) {
165             String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
166             assertEquals(expected, e.getMessage());
167         }
168     }
169
170     @Test
171     public void testDuplicityInAugmentTarget1() throws Exception {
172         File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
173         File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
174         try {
175             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
176                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
177                 fail("YangParseException should by thrown");
178             }
179         } catch (YangParseException e) {
180             assertTrue(e.getMessage().contains("Error in module 'augment1'"));
181             assertTrue(e.getMessage().contains("Failed to perform augmentation:"));
182
183         }
184     }
185
186     @Test
187     public void testDuplicityInAugmentTarget2() throws Exception {
188         File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
189         File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
190         try {
191             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
192                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
193                 fail("YangParseException should by thrown");
194             }
195         } catch (YangParseException e) {
196             assertTrue(e.getMessage().contains("Error in module "));
197             assertTrue(e.getMessage().contains("case with same name already declared "));
198         }
199     }
200
201     @Test
202     public void testMandatoryInAugment() throws Exception {
203         File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
204         File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
205         try {
206             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
207                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
208                 fail("YangParseException should by thrown");
209             }
210         } catch (YangParseException e) {
211             String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node linkleaf";
212             assertEquals(expected, e.getMessage());
213         }
214     }
215
216     @Test
217     public void testWrongDependenciesDir() throws Exception {
218         try {
219             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
220             File dependenciesDir = new File("/invalid");
221             YangContextParser parser = new YangParserImpl();
222             parser.parseFile(yangFile, dependenciesDir);
223             fail("Exception should by thrown");
224         } catch (IllegalStateException e) {
225             String expected = File.separator + "invalid does not exists";
226             assertEquals(expected, e.getMessage());
227         }
228     }
229
230     @Test
231     public void testWrongDependenciesDir2() throws Exception {
232         try {
233             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
234             File dependenciesDir = new File(getClass().getResource("/model").toURI());
235             YangContextParser parser = new YangParserImpl();
236             parser.parseFile(yangFile, dependenciesDir);
237             fail("Exception should by thrown");
238         } catch (YangValidationException e) {
239             String expected = "Not existing module imported";
240             assertTrue(e.getMessage().contains(expected));
241         }
242     }
243
244     @Test
245     public void testInvalidListKeyDefinition() throws Exception {
246         File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
247         try {
248             try (InputStream stream1 = new FileInputStream(yang1)) {
249                 TestUtils.loadModule(stream1);
250                 fail("YangParseException should by thrown");
251             }
252         } catch (YangParseException e) {
253             String expected = "Error in module 'invalid-list-key-def' at line 6: Failed to resolve list key for name rib-id";
254             assertEquals(expected, e.getMessage());
255         }
256     }
257
258 }