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.parser.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
22 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
23 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
25 public class YangParserNegativeTest {
28 public void testInvalidImport() throws Exception {
29 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
31 try (InputStream stream = new FileInputStream(yang)) {
32 TestUtils.loadModule(stream);
33 fail("ValidationException should by thrown");
35 } catch (YangValidationException e) {
36 assertTrue(e.getMessage().contains("Not existing module imported"));
41 public void testTypeNotFound() throws Exception {
42 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
44 try (InputStream stream = new FileInputStream(yang)) {
45 TestUtils.loadModule(stream);
46 fail("YangParseException should by thrown");
48 } catch (YangParseException e) {
49 assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
54 public void testInvalidAugmentTarget() throws Exception {
55 File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
56 File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
58 final List<InputStream> streams = new ArrayList<>(2);
59 try (InputStream testFile0 = new FileInputStream(yang1)) {
60 streams.add(testFile0);
61 try (InputStream testFile3 = new FileInputStream(yang2)) {
62 streams.add(testFile3);
63 assertEquals("Expected loaded files count is 2", 2, streams.size());
64 TestUtils.loadModules(streams);
65 fail("YangParseException should by thrown");
68 } catch (YangParseException e) {
70 "Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target: augment /data:unknown",
76 public void testInvalidRefine() throws Exception {
77 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
79 try (InputStream stream = new FileInputStream(yang)) {
80 TestUtils.loadModule(stream);
81 fail("YangParseException should by thrown");
83 } catch (YangParseException e) {
84 assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
89 public void testInvalidLength() throws Exception {
90 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
92 try (InputStream stream = new FileInputStream(yang)) {
93 TestUtils.loadModule(stream);
94 fail("YangParseException should by thrown");
96 } catch (YangParseException e) {
97 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
102 public void testInvalidRange() throws Exception {
103 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
105 try (InputStream stream = new FileInputStream(yang)) {
106 TestUtils.loadModule(stream);
107 fail("YangParseException should by thrown");
109 } catch (YangParseException e) {
110 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
115 public void testDuplicateContainer() throws Exception {
116 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
118 try (InputStream stream = new FileInputStream(yang)) {
119 TestUtils.loadModule(stream);
120 fail("YangParseException should by thrown");
122 } catch (YangParseException e) {
123 String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
124 assertEquals(expected, e.getMessage());
129 public void testDuplicateContainerList() throws Exception {
130 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
132 try (InputStream stream = new FileInputStream(yang)) {
133 TestUtils.loadModule(stream);
134 fail("YangParseException should by thrown");
136 } catch (YangParseException e) {
137 String expected = "Error in module 'container-list' at line 10: Can not add 'list foo': node with same name 'foo' already declared at line 6.";
138 assertEquals(expected, e.getMessage());
143 public void testDuplicateContainerLeaf() throws Exception {
144 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
146 try (InputStream stream = new FileInputStream(yang)) {
147 TestUtils.loadModule(stream);
148 fail("YangParseException should by thrown");
150 } catch (YangParseException e) {
151 String expected = "Error in module 'container-leaf' at line 10: Can not add 'leaf foo': node with same name 'foo' already declared at line 6.";
152 assertEquals(expected, e.getMessage());
157 public void testDuplicateTypedef() throws Exception {
158 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
160 try (InputStream stream = new FileInputStream(yang)) {
161 TestUtils.loadModule(stream);
162 fail("YangParseException should by thrown");
164 } catch (YangParseException e) {
165 String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
166 assertEquals(expected, e.getMessage());
171 public void testDuplicityInAugmentTarget1() throws Exception {
172 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
173 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
175 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
176 TestUtils.loadModules(Arrays.asList(stream1, stream2));
177 fail("YangParseException should by thrown");
179 } catch (YangParseException e) {
180 assertTrue(e.getMessage().contains("Error in module 'augment1'"));
181 assertTrue(e.getMessage().contains("Failed to perform augmentation:"));
187 public void testDuplicityInAugmentTarget2() throws Exception {
188 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
189 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
191 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
192 TestUtils.loadModules(Arrays.asList(stream1, stream2));
193 fail("YangParseException should by thrown");
195 } catch (YangParseException e) {
196 assertTrue(e.getMessage().contains("Error in module "));
197 assertTrue(e.getMessage().contains("case with same name already declared "));
202 public void testMandatoryInAugment() throws Exception {
203 File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
204 File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
206 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
207 TestUtils.loadModules(Arrays.asList(stream1, stream2));
208 fail("YangParseException should by thrown");
210 } catch (YangParseException e) {
211 String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node linkleaf";
212 assertEquals(expected, e.getMessage());
217 public void testWrongDependenciesDir() throws Exception {
219 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
220 File dependenciesDir = new File("/invalid");
221 YangContextParser parser = new YangParserImpl();
222 parser.parseFile(yangFile, dependenciesDir);
223 fail("Exception should by thrown");
224 } catch (IllegalStateException e) {
225 String expected = File.separator + "invalid does not exists";
226 assertEquals(expected, e.getMessage());
231 public void testWrongDependenciesDir2() throws Exception {
233 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
234 File dependenciesDir = new File(getClass().getResource("/model").toURI());
235 YangContextParser parser = new YangParserImpl();
236 parser.parseFile(yangFile, dependenciesDir);
237 fail("Exception should by thrown");
238 } catch (YangValidationException e) {
239 String expected = "Not existing module imported";
240 assertTrue(e.getMessage().contains(expected));
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 FileInputStream(yang1)) {
249 TestUtils.loadModule(stream1);
250 fail("YangParseException should by thrown");
252 } catch (YangParseException e) {
253 String expected = "Error in module 'invalid-list-key-def' at line 6: Failed to resolve list key for name rib-id";
254 assertEquals(expected, e.getMessage());