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.*;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
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 IOException {
30 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.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 IOException {
43 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.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 IOException {
56 final List<InputStream> streams = new ArrayList<>(2);
57 try (InputStream testFile0 = new FileInputStream(getClass()
58 .getResource("/negative-scenario/testfile0.yang").getPath())) {
59 streams.add(testFile0);
60 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
61 "/negative-scenario/testfile3.yang").getPath())) {
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) {
69 assertEquals("Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target",
75 public void testInvalidRefine() throws IOException {
77 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
79 TestUtils.loadModule(stream);
80 fail("YangParseException should by thrown");
82 } catch (YangParseException e) {
83 assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
88 public void testInvalidLength() throws IOException {
90 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
92 TestUtils.loadModule(stream);
93 fail("YangParseException should by thrown");
95 } catch (YangParseException e) {
96 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
101 public void testInvalidRange() throws IOException {
103 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
105 TestUtils.loadModule(stream);
106 fail("YangParseException should by thrown");
108 } catch (YangParseException e) {
109 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
114 public void testDuplicateContainer() throws IOException {
116 try (InputStream stream = new FileInputStream(getClass().getResource(
117 "/negative-scenario/duplicity/container.yang").getPath())) {
118 TestUtils.loadModule(stream);
119 fail("YangParseException should by thrown");
121 } catch (YangParseException e) {
122 String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name 'foo' already declared at line 6.";
123 assertEquals(expected, e.getMessage());
128 public void testDuplicateContainerList() throws IOException {
130 try (InputStream stream = new FileInputStream(getClass().getResource(
131 "/negative-scenario/duplicity/container-list.yang").getPath())) {
132 TestUtils.loadModule(stream);
133 fail("YangParseException should by thrown");
135 } catch (YangParseException e) {
136 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.";
137 assertEquals(expected, e.getMessage());
142 public void testDuplicateContainerLeaf() throws IOException {
144 try (InputStream stream = new FileInputStream(getClass().getResource(
145 "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
146 TestUtils.loadModule(stream);
147 fail("YangParseException should by thrown");
149 } catch (YangParseException e) {
150 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.";
151 assertEquals(expected, e.getMessage());
156 public void testDuplicateTypedef() throws IOException {
158 try (InputStream stream = new FileInputStream(getClass().getResource(
159 "/negative-scenario/duplicity/typedef.yang").getPath())) {
160 TestUtils.loadModule(stream);
161 fail("YangParseException should by thrown");
163 } catch (YangParseException e) {
164 String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6.";
165 assertEquals(expected, e.getMessage());
170 public void testDuplicityInAugmentTarget1() throws Exception {
172 try (InputStream stream1 = new FileInputStream(getClass().getResource(
173 "/negative-scenario/duplicity/augment0.yang").getPath());
174 InputStream stream2 = new FileInputStream(getClass().getResource(
175 "/negative-scenario/duplicity/augment1.yang").getPath())) {
176 TestUtils.loadModules(Arrays.asList(stream1, stream2));
177 fail("YangParseException should by thrown");
179 } catch (YangParseException e) {
180 String expected = "Error in module 'augment1' at line 10: Failed to perform augmentation: Error in module 'augment0' at line 8: Can not add 'leaf id' to 'container bar' in module 'augment0': node with same name already declared at line 9";
181 assertEquals(expected, e.getMessage());
186 public void testDuplicityInAugmentTarget2() throws Exception {
188 try (InputStream stream1 = new FileInputStream(getClass().getResource(
189 "/negative-scenario/duplicity/augment0.yang").getPath());
190 InputStream stream2 = new FileInputStream(getClass().getResource(
191 "/negative-scenario/duplicity/augment2.yang").getPath())) {
192 TestUtils.loadModules(Arrays.asList(stream1, stream2));
193 fail("YangParseException should by thrown");
195 } catch (YangParseException e) {
196 String expected = "Error in module 'augment0' at line 17: Can not add 'anyxml delta' to node 'choice-ext' in module 'augment0': case with same name already declared at line 18";
197 assertEquals(expected, e.getMessage());
202 public void testMandatoryInAugment() throws IOException {
204 try (InputStream stream1 = new FileInputStream(getClass().getResource("/negative-scenario/testfile8.yang")
206 InputStream stream2 = new FileInputStream(getClass().getResource(
207 "/negative-scenario/testfile7.yang").getPath())) {
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";
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").getPath());
221 File dependenciesDir = new File("/invalid");
222 YangModelParser parser = new YangParserImpl();
223 parser.parseYangModels(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").getPath());
235 File dependenciesDir = new File(getClass().getResource("/model").getPath());
236 YangModelParser parser = new YangParserImpl();
237 parser.parseYangModels(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));