2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
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;
30 public class YangParserNegativeTest {
32 private final PrintStream stdout = System.out;
33 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
34 private String testLog;
37 public void setUp() throws UnsupportedEncodingException {
38 System.setOut(new PrintStream(output, true, "UTF-8"));
42 public void cleanUp() {
43 System.setOut(stdout);
47 public void testInvalidImport() throws Exception {
48 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
50 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
51 TestUtils.loadModule(stream);
52 fail("SomeModifiersUnresolvedException should be thrown");
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."));
63 public void testTypeNotFound() throws Exception {
64 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
66 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
67 TestUtils.loadModule(stream);
68 fail("InferenceException should be thrown");
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."));
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());
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");
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"));
102 public void testInvalidRefine() throws Exception {
103 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
105 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
106 TestUtils.loadModule(stream);
107 fail("SourceException should be thrown");
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'."));
117 public void testInvalidLength() throws Exception {
118 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
120 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
121 TestUtils.loadModule(stream);
122 fail("YangParseException should be thrown");
124 } catch (SourceException e) {
125 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
130 public void testInvalidRange() throws Exception {
131 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
133 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
134 TestUtils.loadModule(stream);
135 fail("Exception should be thrown");
137 } catch (SourceException e) {
138 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
143 public void testDuplicateContainer() throws Exception {
144 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
146 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
147 TestUtils.loadModule(stream);
148 fail("SourceException should be thrown");
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));
159 public void testDuplicateContainerList() throws Exception {
160 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
162 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
163 TestUtils.loadModule(stream);
164 fail("SourceException should be thrown");
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));
175 public void testDuplicateContainerLeaf() throws Exception {
176 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
178 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
179 TestUtils.loadModule(stream);
180 fail("SourceException should be thrown");
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));
191 public void testDuplicateTypedef() throws Exception {
192 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
194 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
195 TestUtils.loadModule(stream);
196 fail("SourceException should be thrown");
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));
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"));
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"));
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"));
244 public void testInvalidListKeyDefinition() throws Exception {
245 File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
247 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) {
248 TestUtils.loadModule(stream1);
249 fail("InferenceException should be thrown");
251 } catch (InferenceException e) {
252 String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" +
254 assertTrue(e.getMessage().startsWith(expected));