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