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