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;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
23 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
24 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
26 public class YangParserNegativeTest {
29 public void testInvalidImport() throws Exception {
30 File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
32 try (InputStream stream = new FileInputStream(yang)) {
33 TestUtils.loadModule(stream);
34 fail("ValidationException should by thrown");
36 } catch (YangValidationException e) {
37 assertTrue(e.getMessage().contains("Not existing module imported"));
42 public void testTypeNotFound() throws Exception {
43 File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
45 try (InputStream stream = new FileInputStream(yang)) {
46 TestUtils.loadModule(stream);
47 fail("YangParseException should by thrown");
49 } catch (YangParseException e) {
50 assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
55 public void testInvalidAugmentTarget() throws Exception {
56 File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
57 File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
59 final List<InputStream> streams = new ArrayList<>(2);
60 try (InputStream testFile0 = new FileInputStream(yang1)) {
61 streams.add(testFile0);
62 try (InputStream testFile3 = new FileInputStream(yang2)) {
63 streams.add(testFile3);
64 assertEquals("Expected loaded files count is 2", 2, streams.size());
65 TestUtils.loadModules(streams);
66 fail("YangParseException should by thrown");
69 } catch (YangParseException e) {
71 "Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target: augment /data:unknown",
77 public void testInvalidRefine() throws Exception {
78 File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
80 try (InputStream stream = new FileInputStream(yang)) {
81 TestUtils.loadModule(stream);
82 fail("YangParseException should by thrown");
84 } catch (YangParseException e) {
85 assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
90 public void testInvalidLength() throws Exception {
91 File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
93 try (InputStream stream = new FileInputStream(yang)) {
94 TestUtils.loadModule(stream);
95 fail("YangParseException should by thrown");
97 } catch (YangParseException e) {
98 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
103 public void testInvalidRange() throws Exception {
104 File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
106 try (InputStream stream = new FileInputStream(yang)) {
107 TestUtils.loadModule(stream);
108 fail("YangParseException should by thrown");
110 } catch (YangParseException e) {
111 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
116 public void testDuplicateContainer() throws Exception {
117 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
119 try (InputStream stream = new FileInputStream(yang)) {
120 TestUtils.loadModule(stream);
121 fail("YangParseException should by thrown");
123 } catch (YangParseException e) {
124 String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
125 assertEquals(expected, e.getMessage());
130 public void testDuplicateContainerList() throws Exception {
131 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
133 try (InputStream stream = new FileInputStream(yang)) {
134 TestUtils.loadModule(stream);
135 fail("YangParseException should by thrown");
137 } catch (YangParseException e) {
138 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.";
139 assertEquals(expected, e.getMessage());
144 public void testDuplicateContainerLeaf() throws Exception {
145 File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
147 try (InputStream stream = new FileInputStream(yang)) {
148 TestUtils.loadModule(stream);
149 fail("YangParseException should by thrown");
151 } catch (YangParseException e) {
152 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.";
153 assertEquals(expected, e.getMessage());
158 public void testDuplicateTypedef() throws Exception {
159 File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
161 try (InputStream stream = new FileInputStream(yang)) {
162 TestUtils.loadModule(stream);
163 fail("YangParseException should by thrown");
165 } catch (YangParseException e) {
166 String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
167 assertEquals(expected, e.getMessage());
172 public void testDuplicityInAugmentTarget1() throws Exception {
173 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
174 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
176 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
177 TestUtils.loadModules(Arrays.asList(stream1, stream2));
178 fail("YangParseException should by thrown");
180 } catch (YangParseException e) {
181 assertTrue(e.getMessage().contains("Error in module 'augment1'"));
182 assertTrue(e.getMessage().contains("Failed to perform augmentation:"));
188 public void testDuplicityInAugmentTarget2() throws Exception {
189 File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
190 File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
192 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
193 TestUtils.loadModules(Arrays.asList(stream1, stream2));
194 fail("YangParseException should by thrown");
196 } catch (YangParseException e) {
197 assertTrue(e.getMessage().contains("Error in module "));
198 assertTrue(e.getMessage().contains("case with same name already declared "));
203 public void testMandatoryInAugment() throws Exception {
204 File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
205 File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
207 try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
208 TestUtils.loadModules(Arrays.asList(stream1, stream2));
209 fail("YangParseException should by thrown");
211 } catch (YangParseException e) {
212 String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node linkleaf";
213 assertEquals(expected, e.getMessage());
218 public void testWrongDependenciesDir() throws Exception {
220 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
221 File dependenciesDir = new File("/invalid");
222 YangContextParser parser = new YangParserImpl();
223 parser.parseFile(yangFile, dependenciesDir);
224 fail("Exception should by thrown");
225 } catch (IllegalStateException e) {
226 String expected = File.separator + "invalid does not exists";
227 assertEquals(expected, e.getMessage());
232 public void testWrongDependenciesDir2() throws Exception {
234 File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
235 File dependenciesDir = new File(getClass().getResource("/model").toURI());
236 YangContextParser parser = new YangParserImpl();
237 parser.parseFile(yangFile, dependenciesDir);
238 fail("Exception should by thrown");
239 } catch (YangValidationException e) {
240 String expected = "Not existing module imported";
241 assertTrue(e.getMessage().contains(expected));