Projects moved under correct parent.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / impl / YangParserNegativeTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.parser.impl;
9
10 import static org.junit.Assert.*;
11
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;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.yang.parser.util.YangParseException;
21 import org.opendaylight.controller.yang.parser.util.YangValidationException;
22
23 public class YangParserNegativeTest {
24
25     @Test
26     public void testInvalidImport() throws IOException {
27         try {
28             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
29                     .getPath())) {
30                 TestUtils.loadModule(stream);
31                 fail("ValidationException should by thrown");
32             }
33         } catch (YangValidationException e) {
34             assertTrue(e.getMessage().contains("Not existing module imported"));
35         }
36     }
37
38     @Test
39     public void testTypeNotFound() throws IOException {
40         try {
41             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
42                     .getPath())) {
43                 TestUtils.loadModule(stream);
44                 fail("YangParseException should by thrown");
45             }
46         } catch (YangParseException e) {
47             assertEquals(e.getMessage(), "Error in module 'test2' at line 24: Referenced type 'int-ext' not found.");
48         }
49     }
50
51     @Test
52     public void testInvalidAugmentTarget() throws IOException {
53         try {
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");
64                 }
65             }
66         } catch (YangParseException e) {
67             assertTrue(e.getMessage().contains("Failed to resolve augments in module 'test3'."));
68         }
69     }
70
71     @Test
72     public void testInvalidRefine() throws IOException {
73         try {
74             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
75                     .getPath())) {
76                 TestUtils.loadModule(stream);
77                 fail("YangParseException should by thrown");
78             }
79         } catch (YangParseException e) {
80             assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
81         }
82     }
83
84     @Test
85     public void testInvalidLength() throws IOException {
86         try {
87             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
88                     .getPath())) {
89                 TestUtils.loadModule(stream);
90                 fail("YangParseException should by thrown");
91             }
92         } catch (YangParseException e) {
93             assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
94         }
95     }
96
97     @Test
98     public void testInvalidRange() throws IOException {
99         try {
100             try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
101                     .getPath())) {
102                 TestUtils.loadModule(stream);
103                 fail("YangParseException should by thrown");
104             }
105         } catch (YangParseException e) {
106             assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
107         }
108     }
109
110     @Test
111     public void testDuplicateContainer() throws IOException {
112         try {
113             try (InputStream stream = new FileInputStream(getClass().getResource(
114                     "/negative-scenario/duplicity/container.yang").getPath())) {
115                 TestUtils.loadModule(stream);
116                 fail("YangParseException should by thrown");
117             }
118         } catch (YangParseException e) {
119             String expected = "Error in module 'container' at line 10: Can not add 'container foo': node with same name already declared at line 6";
120             assertEquals(expected, e.getMessage());
121         }
122     }
123
124     @Test
125     public void testDuplicateContainerList() throws IOException {
126         try {
127             try (InputStream stream = new FileInputStream(getClass().getResource(
128                     "/negative-scenario/duplicity/container-list.yang").getPath())) {
129                 TestUtils.loadModule(stream);
130                 fail("YangParseException should by thrown");
131             }
132         } catch (YangParseException e) {
133             String expected = "Error in module 'container-list' at line 10: Can not add 'list foo': node with same name already declared at line 6";
134             assertEquals(expected, e.getMessage());
135         }
136     }
137
138     @Test
139     public void testDuplicateContainerLeaf() throws IOException {
140         try {
141             try (InputStream stream = new FileInputStream(getClass().getResource(
142                     "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
143                 TestUtils.loadModule(stream);
144                 fail("YangParseException should by thrown");
145             }
146         } catch (YangParseException e) {
147             String expected = "Error in module 'container-leaf' at line 10: Can not add 'leaf foo': node with same name already declared at line 6";
148             assertEquals(expected, e.getMessage());
149         }
150     }
151
152     @Test
153     public void testDuplicateTypedef() throws IOException {
154         try {
155             try (InputStream stream = new FileInputStream(getClass().getResource(
156                     "/negative-scenario/duplicity/typedef.yang").getPath())) {
157                 TestUtils.loadModule(stream);
158                 fail("YangParseException should by thrown");
159             }
160         } catch (YangParseException e) {
161             String expected = "Error in module 'typedef' at line 10: typedef with same name 'int-ext' already declared at line 6";
162             assertEquals(expected, e.getMessage());
163         }
164     }
165
166     @Test
167     public void testDuplicityInAugmentTarget1() throws Exception {
168         try {
169             try (InputStream stream1 = new FileInputStream(getClass().getResource(
170                     "/negative-scenario/duplicity/augment0.yang").getPath());
171                     InputStream stream2 = new FileInputStream(getClass().getResource(
172                             "/negative-scenario/duplicity/augment1.yang").getPath())) {
173                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
174                 fail("YangParseException should by thrown");
175             }
176         } catch (YangParseException e) {
177             String expected = "Error in module 'augment1' at line 11: Can not add 'leaf id' to 'container bar' in module 'augment0': node with same name already declared at line 9";
178             assertEquals(expected, e.getMessage());
179         }
180     }
181
182     @Test
183     public void testDuplicityInAugmentTarget2() throws Exception {
184         try {
185             try (InputStream stream1 = new FileInputStream(getClass().getResource(
186                     "/negative-scenario/duplicity/augment0.yang").getPath());
187                     InputStream stream2 = new FileInputStream(getClass().getResource(
188                             "/negative-scenario/duplicity/augment2.yang").getPath())) {
189                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
190                 fail("YangParseException should by thrown");
191             }
192         } catch (YangParseException e) {
193             String expected = "Error in module 'augment2' at line 11: Can not add 'anyxml delta' to node 'choice-ext' in module 'augment0': case with same name already declared at line 18";
194             assertEquals(expected, e.getMessage());
195         }
196     }
197
198 }