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