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.*;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
21 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
23 public class YangParserNegativeTest {
26 public void testInvalidImport() throws IOException {
28 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
30 TestUtils.loadModule(stream);
31 fail("ValidationException should by thrown");
33 } catch (YangValidationException e) {
34 assertTrue(e.getMessage().contains("Not existing module imported"));
39 public void testTypeNotFound() throws IOException {
41 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
43 TestUtils.loadModule(stream);
44 fail("YangParseException should by thrown");
46 } catch (YangParseException e) {
47 assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
52 public void testInvalidAugmentTarget() throws IOException {
54 final List<InputStream> streams = new ArrayList<>(2);
55 try (InputStream testFile0 = new FileInputStream(getClass()
56 .getResource("/negative-scenario/testfile0.yang").getPath())) {
57 streams.add(testFile0);
58 try (InputStream testFile3 = new FileInputStream(getClass().getResource(
59 "/negative-scenario/testfile3.yang").getPath())) {
60 streams.add(testFile3);
61 assertEquals("Expected loaded files count is 2", 2, streams.size());
62 TestUtils.loadModules(streams);
63 fail("YangParseException should by thrown");
66 } catch (YangParseException e) {
67 assertEquals("Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target",
73 public void testInvalidRefine() throws IOException {
75 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
77 TestUtils.loadModule(stream);
78 fail("YangParseException should by thrown");
80 } catch (YangParseException e) {
81 assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
86 public void testInvalidLength() throws IOException {
88 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
90 TestUtils.loadModule(stream);
91 fail("YangParseException should by thrown");
93 } catch (YangParseException e) {
94 assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
99 public void testInvalidRange() throws IOException {
101 try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
103 TestUtils.loadModule(stream);
104 fail("YangParseException should by thrown");
106 } catch (YangParseException e) {
107 assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
112 public void testDuplicateContainer() throws IOException {
114 try (InputStream stream = new FileInputStream(getClass().getResource(
115 "/negative-scenario/duplicity/container.yang").getPath())) {
116 TestUtils.loadModule(stream);
117 fail("YangParseException should by thrown");
119 } catch (YangParseException e) {
120 String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name already declared at line 6";
121 assertEquals(expected, e.getMessage());
126 public void testDuplicateContainerList() throws IOException {
128 try (InputStream stream = new FileInputStream(getClass().getResource(
129 "/negative-scenario/duplicity/container-list.yang").getPath())) {
130 TestUtils.loadModule(stream);
131 fail("YangParseException should by thrown");
133 } catch (YangParseException e) {
134 String expected = "Error in module 'container-list' at line 10: Can not add 'list foo': node with same name already declared at line 6";
135 assertEquals(expected, e.getMessage());
140 public void testDuplicateContainerLeaf() throws IOException {
142 try (InputStream stream = new FileInputStream(getClass().getResource(
143 "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
144 TestUtils.loadModule(stream);
145 fail("YangParseException should by thrown");
147 } catch (YangParseException e) {
148 String expected = "Error in module 'container-leaf' at line 10: Can not add 'leaf foo': node with same name already declared at line 6";
149 assertEquals(expected, e.getMessage());
154 public void testDuplicateTypedef() throws IOException {
156 try (InputStream stream = new FileInputStream(getClass().getResource(
157 "/negative-scenario/duplicity/typedef.yang").getPath())) {
158 TestUtils.loadModule(stream);
159 fail("YangParseException should by thrown");
161 } catch (YangParseException e) {
162 String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6";
163 assertEquals(expected, e.getMessage());
168 public void testDuplicityInAugmentTarget1() throws Exception {
170 try (InputStream stream1 = new FileInputStream(getClass().getResource(
171 "/negative-scenario/duplicity/augment0.yang").getPath());
172 InputStream stream2 = new FileInputStream(getClass().getResource(
173 "/negative-scenario/duplicity/augment1.yang").getPath())) {
174 TestUtils.loadModules(Arrays.asList(stream1, stream2));
175 fail("YangParseException should by thrown");
177 } catch (YangParseException e) {
178 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";
179 assertEquals(expected, e.getMessage());
184 public void testDuplicityInAugmentTarget2() throws Exception {
186 try (InputStream stream1 = new FileInputStream(getClass().getResource(
187 "/negative-scenario/duplicity/augment0.yang").getPath());
188 InputStream stream2 = new FileInputStream(getClass().getResource(
189 "/negative-scenario/duplicity/augment2.yang").getPath())) {
190 TestUtils.loadModules(Arrays.asList(stream1, stream2));
191 fail("YangParseException should by thrown");
193 } catch (YangParseException e) {
194 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";
195 assertEquals(expected, e.getMessage());