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