Yang parser refactoring.
[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.*;
15 import java.util.*;
16
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
19 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
20 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
21
22 public class YangParserNegativeTest {
23
24     @Test
25     public void testInvalidImport() throws IOException {
26         try {
27             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
28                     .getPath())) {
29                 TestUtils.loadModule(stream);
30                 fail("ValidationException should by thrown");
31             }
32         } catch (YangValidationException e) {
33             assertTrue(e.getMessage().contains("Not existing module imported"));
34         }
35     }
36
37     @Test
38     public void testTypeNotFound() throws IOException {
39         try {
40             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
41                     .getPath())) {
42                 TestUtils.loadModule(stream);
43                 fail("YangParseException should by thrown");
44             }
45         } catch (YangParseException e) {
46             assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
47         }
48     }
49
50     @Test
51     public void testInvalidAugmentTarget() throws IOException {
52         try {
53             final List<InputStream> streams = new ArrayList<>(2);
54             try (InputStream testFile0 = new FileInputStream(getClass()
55                     .getResource("/negative-scenario/testfile0.yang").getPath())) {
56                 streams.add(testFile0);
57                 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
58                         "/negative-scenario/testfile3.yang").getPath())) {
59                     streams.add(testFile3);
60                     assertEquals("Expected loaded files count is 2", 2, streams.size());
61                     TestUtils.loadModules(streams);
62                     fail("YangParseException should by thrown");
63                 }
64             }
65         } catch (YangParseException e) {
66             assertEquals(
67                     "Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target: augment /data:unknown",
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     @Test
200     public void testMandatoryInAugment() throws IOException {
201         try {
202             try (InputStream stream1 = new FileInputStream(getClass().getResource("/negative-scenario/testfile8.yang")
203                     .getPath());
204                     InputStream stream2 = new FileInputStream(getClass().getResource(
205                             "/negative-scenario/testfile7.yang").getPath())) {
206                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
207                 fail("YangParseException should by thrown");
208             }
209         } catch (YangParseException e) {
210             String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node linkleaf";
211             assertEquals(expected, e.getMessage());
212         }
213     }
214
215     @Test
216     public void testWrongDependenciesDir() throws Exception {
217         try {
218             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
219             File dependenciesDir = new File("/invalid");
220             YangModelParser parser = new YangParserImpl();
221             parser.parseYangModels(yangFile, dependenciesDir);
222             fail("Exception should by thrown");
223         } catch (IllegalStateException e) {
224             String expected = File.separator + "invalid does not exists";
225             assertEquals(expected, e.getMessage());
226         }
227     }
228
229     @Test
230     public void testWrongDependenciesDir2() throws Exception {
231         try {
232             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
233             File dependenciesDir = new File(getClass().getResource("/model").getPath());
234             YangModelParser parser = new YangParserImpl();
235             parser.parseYangModels(yangFile, dependenciesDir);
236             fail("Exception should by thrown");
237         } catch (YangValidationException e) {
238             String expected = "Not existing module imported";
239             assertTrue(e.getMessage().contains(expected));
240         }
241     }
242
243 }