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