2 * Copyright (c) 2013 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.retest;
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.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;
33 public class YangParserNegativeTest {
35 private final PrintStream stdout = System.out;
36 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
37 private String testLog;
40 public void setUp() throws UnsupportedEncodingException {
41 System.setOut(new PrintStream(output, true, "UTF-8"));
45 public void cleanUp() {
46 System.setOut(stdout);
50 public void testInvalidImport() throws Exception {
51 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
53 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
54 TestUtils.loadModule(stream);
55 fail("SomeModifiersUnresolvedException should be thrown");
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."));
66 public void testTypeNotFound() throws Exception {
67 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
69 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
70 TestUtils.loadModule(stream);
71 fail("InferenceException should be thrown");
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."));
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());
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");
96 } catch (SomeModifiersUnresolvedException e) {
97 final Throwable rootCause = Throwables.getRootCause(e);
98 assertTrue(rootCause instanceof InferenceException);
99 assertTrue(rootCause.getMessage().startsWith(
100 "Augment target 'Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}' not found"));
105 public void testInvalidRefine() throws Exception {
106 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
108 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
109 TestUtils.loadModule(stream);
110 fail("SourceException should be thrown");
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'."));
120 public void testInvalidLength() throws Exception {
121 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
123 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
124 TestUtils.loadModule(stream);
125 fail("YangParseException should be thrown");
127 } catch (SourceException e) {
128 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
133 public void testInvalidRange() throws Exception {
134 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
136 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
137 TestUtils.loadModule(stream);
138 fail("Exception should be thrown");
140 } catch (SourceException e) {
141 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
146 public void testDuplicateContainer() throws Exception {
147 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
149 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
150 TestUtils.loadModule(stream);
151 fail("SourceException should be thrown");
153 } catch (SourceException e) {
154 String expected = "Error in module 'container': cannot 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));
162 public void testDuplicateContainerList() throws Exception {
163 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
165 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
166 TestUtils.loadModule(stream);
167 fail("SourceException should be thrown");
169 } catch (SourceException e) {
170 String expected = "Error in module 'container-list': cannot 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));
178 public void testDuplicateContainerLeaf() throws Exception {
179 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
181 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
182 TestUtils.loadModule(stream);
183 fail("SourceException should be thrown");
185 } catch (SourceException e) {
186 String expected = "Error in module 'container-leaf': cannot 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));
194 public void testDuplicateTypedef() throws Exception {
195 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
197 try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
198 TestUtils.loadModule(stream);
199 fail("SourceException should be thrown");
201 } catch (SourceException e) {
202 String expected = "Error in module 'typedef': cannot 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));
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"));
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"));
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"));
247 public void testWrongDependenciesDir() throws Exception {
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());
261 public void testWrongDependenciesDir2() throws Exception {
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));
275 public void testInvalidListKeyDefinition() throws Exception {
276 File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
278 try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) {
279 TestUtils.loadModule(stream1);
280 fail("InferenceException should be thrown");
282 } catch (InferenceException e) {
283 String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" +
285 assertTrue(e.getMessage().startsWith(expected));