bfaea689bc988b3f42f23a90d2fa1c9ab42ad82b
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / 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.stmt.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
21 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
26 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
27
28 public class YangParserNegativeTest {
29
30     @Test
31     public void testInvalidImport() throws Exception {
32         File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
33         try {
34             try (InputStream stream = new FileInputStream(yang)) {
35                 TestUtils.loadModule(stream);
36                 fail("SomeModifiersUnresolvedException should be thrown");
37             }
38         } catch (SomeModifiersUnresolvedException e) {
39             final Throwable suppressed2levelsDown = e.getSuppressed()[0].getSuppressed()[0];
40             assertTrue(suppressed2levelsDown instanceof InferenceException);
41             assertTrue(suppressed2levelsDown.getMessage().startsWith("Imported module"));
42             assertTrue(suppressed2levelsDown.getMessage().endsWith("was not found."));
43         }
44     }
45
46     @Test
47     public void testTypeNotFound() throws Exception {
48         File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
49         try {
50             try (InputStream stream = new FileInputStream(yang)) {
51                 TestUtils.loadModule(stream);
52                 fail("IllegalArgumentException should be thrown");
53             }
54         } catch (IllegalStateException e) {
55             assertTrue(e.getMessage().startsWith(
56                     "Type '(urn:simple.types.data.demo?revision=2013-02-27)int-ext' was not found"));
57         }
58     }
59
60     @Test
61     public void testInvalidAugmentTarget() throws Exception {
62         File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
63         File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
64         try {
65             final List<InputStream> streams = new ArrayList<>(2);
66             try (InputStream testFile0 = new FileInputStream(yang1)) {
67                 streams.add(testFile0);
68                 try (InputStream testFile3 = new FileInputStream(yang2)) {
69                     streams.add(testFile3);
70                     assertEquals("Expected loaded files count is 2", 2, streams.size());
71                     TestUtils.loadModules(streams);
72                     fail("SomeModifiersUnresolvedException should be thrown");
73                 }
74             }
75         } catch (SomeModifiersUnresolvedException e) {
76             final Throwable suppressed2levelsDown = e.getSuppressed()[0].getSuppressed()[0];
77             assertTrue(suppressed2levelsDown instanceof InferenceException);
78             assertEquals(
79                     "Augment target not found: Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}",
80                     suppressed2levelsDown.getMessage());
81         }
82     }
83
84     @Test
85     public void testInvalidRefine() throws Exception {
86         File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
87         try {
88             try (InputStream stream = new FileInputStream(yang)) {
89                 TestUtils.loadModule(stream);
90                 fail("SourceException should be thrown");
91             }
92         } catch (SourceException e) {
93             assertTrue(e
94                     .getMessage()
95                     .contains(
96                             "Error in module 'test4' in the refine of uses 'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for the target 'LEAF_LIST'."));
97         }
98     }
99
100     @Test
101     public void testInvalidLength() throws Exception {
102         File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
103         try {
104             try (InputStream stream = new FileInputStream(yang)) {
105                 TestUtils.loadModule(stream);
106                 fail("YangParseException should be thrown");
107             }
108         } catch (YangParseException e) {
109             assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
110         }
111     }
112
113     @Test
114     public void testInvalidRange() throws Exception {
115         File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
116         try {
117             try (InputStream stream = new FileInputStream(yang)) {
118                 TestUtils.loadModule(stream);
119                 fail("Exception should be thrown");
120             }
121         } catch (YangParseException e) {
122             assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
123         }
124     }
125
126     @Test
127     public void testDuplicateContainer() throws Exception {
128         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
129         try {
130             try (InputStream stream = new FileInputStream(yang)) {
131                 TestUtils.loadModule(stream);
132                 fail("SourceException should be thrown");
133             }
134         } catch (SourceException e) {
135             String expected = "Error in module 'container': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
136             assertEquals(expected, e.getMessage());
137         }
138     }
139
140     @Test
141     public void testDuplicateContainerList() throws Exception {
142         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
143         try {
144             try (InputStream stream = new FileInputStream(yang)) {
145                 TestUtils.loadModule(stream);
146                 fail("SourceException should be thrown");
147             }
148         } catch (SourceException e) {
149             String expected = "Error in module 'container-list': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
150             assertEquals(expected, e.getMessage());
151         }
152     }
153
154     @Test
155     public void testDuplicateContainerLeaf() throws Exception {
156         File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
157         try {
158             try (InputStream stream = new FileInputStream(yang)) {
159                 TestUtils.loadModule(stream);
160                 fail("SourceException should be thrown");
161             }
162         } catch (SourceException e) {
163             String expected = "Error in module 'container-leaf': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared.";
164             assertEquals(expected, e.getMessage());
165         }
166     }
167
168     @Test
169     public void testDuplicateTypedef() throws Exception {
170         File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
171         try {
172             try (InputStream stream = new FileInputStream(yang)) {
173                 TestUtils.loadModule(stream);
174                 fail("IllegalArgumentException should be thrown");
175             }
176         } catch (IllegalArgumentException e) {
177             String expected = "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext";
178             assertEquals(expected, e.getMessage());
179         }
180     }
181
182     @Test
183     public void testDuplicityInAugmentTarget1() throws Exception {
184         File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
185         File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
186         try {
187             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
188                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
189                 fail("IllegalStateException should be thrown");
190             }
191         } catch (IllegalStateException e) {
192             assertEquals(e.getMessage(),
193                     "An augment cannot add node named 'id' because this name is already used in target");
194         }
195     }
196
197     @Test
198     public void testDuplicityInAugmentTarget2() throws Exception {
199         File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
200         File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
201         try {
202             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
203                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
204                 fail("IllegalStateException should be thrown");
205             }
206         } catch (IllegalStateException e) {
207             assertEquals(e.getMessage(),
208                     "An augment cannot add node named 'delta' because this name is already used in target");
209         }
210     }
211
212     @Test
213     public void testMandatoryInAugment() throws Exception {
214         File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
215         File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
216         try {
217             try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
218                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
219                 fail("IllegalArgumentException should be thrown");
220             }
221         } catch (IllegalArgumentException e) {
222             String expected = "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target";
223             assertEquals(expected, e.getMessage());
224         }
225     }
226
227     @Test
228     public void testWrongDependenciesDir() throws Exception {
229         try {
230             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
231             File dependenciesDir = new File("/invalid");
232             YangContextParser parser = new YangParserImpl();
233             parser.parseFile(yangFile, dependenciesDir);
234             fail("Exception should be thrown");
235         } catch (IllegalStateException e) {
236             String expected = File.separator + "invalid does not exists";
237             assertEquals(expected, e.getMessage());
238         }
239     }
240
241     @Test
242     public void testWrongDependenciesDir2() throws Exception {
243         try {
244             File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
245             File dependenciesDir = new File(getClass().getResource("/model").toURI());
246             YangContextParser parser = new YangParserImpl();
247             parser.parseFile(yangFile, dependenciesDir);
248             fail("Exception should be thrown");
249         } catch (YangValidationException e) {
250             String expected = "Not existing module imported";
251             assertTrue(e.getMessage().contains(expected));
252         }
253     }
254
255     @Test
256     public void testInvalidListKeyDefinition() throws Exception {
257         File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
258         try {
259             try (InputStream stream1 = new FileInputStream(yang1)) {
260                 TestUtils.loadModule(stream1);
261                 fail("IllegalArgumentException should be thrown");
262             }
263         } catch (IllegalArgumentException e) {
264             String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)application-map'";
265             assertTrue(e.getMessage().startsWith(expected));
266         }
267     }
268
269 }