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;
14 import com.google.common.base.Throwables;
15 import java.io.ByteArrayOutputStream;
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;
31 public class YangParserNegativeTest {
33 private final PrintStream stdout = System.out;
34 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
35 private String testLog;
38 public void setUp() throws UnsupportedEncodingException {
39 System.setOut(new PrintStream(output, true, "UTF-8"));
43 public void cleanUp() {
44 System.setOut(stdout);
48 public void testInvalidImport() throws Exception {
49 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
51 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
52 TestUtils.loadModule(stream);
53 fail("SomeModifiersUnresolvedException should be thrown");
55 } catch (SomeModifiersUnresolvedException e) {
56 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."));
64 public void testTypeNotFound() throws Exception {
65 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
67 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
68 TestUtils.loadModule(stream);
69 fail("InferenceException should be thrown");
71 } catch (SomeModifiersUnresolvedException e) {
72 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."));
80 public void testInvalidAugmentTarget() throws Exception {
81 File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
82 File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
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");
94 } catch (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"));
103 public void testInvalidRefine() throws Exception {
104 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
106 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
107 TestUtils.loadModule(stream);
108 fail("SourceException should be thrown");
110 } catch (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'."));
118 public void testInvalidLength() throws Exception {
119 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
121 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
122 TestUtils.loadModule(stream);
123 fail("YangParseException should be thrown");
125 } catch (ReactorException e) {
126 assertTrue(e.getCause().getMessage().contains("Invalid length constraint: <4, 10>"));
131 public void testInvalidRange() throws Exception {
132 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
134 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
135 TestUtils.loadModule(stream);
136 fail("Exception should be thrown");
138 } catch (ReactorException e) {
139 assertTrue(e.getCause().getMessage().contains("Invalid range constraint: <5, 20>"));
144 public void testDuplicateContainer() throws Exception {
145 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
147 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
148 TestUtils.loadModule(stream);
149 fail("SourceException should be thrown");
151 } catch (ReactorException e) {
152 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));
160 public void testDuplicateContainerList() throws Exception {
161 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
163 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
164 TestUtils.loadModule(stream);
165 fail("SourceException should be thrown");
167 } catch (ReactorException e) {
168 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));
176 public void testDuplicateContainerLeaf() throws Exception {
177 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
179 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
180 TestUtils.loadModule(stream);
181 fail("SourceException should be thrown");
183 } catch (ReactorException e) {
184 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));
192 public void testDuplicateTypedef() throws Exception {
193 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
195 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
196 TestUtils.loadModule(stream);
197 fail("SourceException should be thrown");
199 } catch (ReactorException e) {
200 String expected = "Error in module 'typedef': cannot add '(urn:simple.container" +
201 ".demo?revision=1970-01-01)int-ext'. Node name collision: '(urn:simple.container" +
202 ".demo?revision=1970-01-01)int-ext' already declared.";
203 assertTrue(e.getCause().getMessage().startsWith(expected));
208 public void testDuplicityInAugmentTarget1() throws Exception {
209 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
210 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
211 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
212 InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
213 TestUtils.loadModules(Arrays.asList(stream1, stream2));
214 testLog = output.toString();
215 assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
220 public void testDuplicityInAugmentTarget2() throws Exception {
221 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
222 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
223 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
224 InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
225 TestUtils.loadModules(Arrays.asList(stream1, stream2));
226 testLog = output.toString();
227 assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
232 public void testMandatoryInAugment() throws Exception {
233 File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
234 File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
235 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
236 InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
237 TestUtils.loadModules(Arrays.asList(stream1, stream2));
238 testLog = output.toString();
239 assertTrue(testLog.contains(
240 "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target"));
245 public void testInvalidListKeyDefinition() throws Exception {
246 File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
248 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) {
249 TestUtils.loadModule(stream1);
250 fail("InferenceException should be thrown");
252 } catch (ReactorException e) {
253 String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" +
255 assertTrue(e.getCause().getMessage().startsWith(expected));