BUG-4688: eliminate SimpleDateFormatUtil.DEFAULT_DATE_REV
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserNegativeTest.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.base.Throwables;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.PrintStream;
17 import java.io.UnsupportedEncodingException;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
25
26 public class YangParserNegativeTest {
27
28     private final PrintStream stdout = System.out;
29     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
30     private String testLog;
31
32     @Before
33     public void setUp() throws UnsupportedEncodingException {
34         System.setOut(new PrintStream(output, true, "UTF-8"));
35     }
36
37     @After
38     public void cleanUp() {
39         System.setOut(stdout);
40     }
41
42     @Test
43     public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
44         try {
45             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang");
46             fail("SomeModifiersUnresolvedException should be thrown");
47         } catch (final SomeModifiersUnresolvedException e) {
48             final Throwable rootCause = Throwables.getRootCause(e);
49             assertTrue(rootCause instanceof InferenceException);
50             assertTrue(rootCause.getMessage().startsWith("Imported module"));
51             assertTrue(rootCause.getMessage().contains("was not found."));
52         }
53     }
54
55     @Test
56     public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
57         try {
58             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang");
59             fail("InferenceException should be thrown");
60         } catch (final SomeModifiersUnresolvedException e) {
61             final Throwable rootCause = Throwables.getRootCause(e);
62             assertTrue(rootCause instanceof InferenceException);
63             assertTrue(rootCause.getMessage()
64                     .startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
65         }
66     }
67
68     @Test
69     public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
70         try {
71             TestUtils.loadModuleResources(getClass(),
72                 "/negative-scenario/testfile0.yang",
73                 "/negative-scenario/testfile3.yang");
74             fail("SomeModifiersUnresolvedException should be thrown");
75         } catch (final SomeModifiersUnresolvedException e) {
76             final Throwable rootCause = Throwables.getRootCause(e);
77             assertTrue(rootCause instanceof InferenceException);
78             assertTrue(rootCause.getMessage().startsWith(
79                 "Augment target 'Absolute{path=[(urn:simple.container.demo)unknown]}' not found"));
80         }
81     }
82
83     @Test
84     public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
85         try {
86             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang");
87             fail("ReactorException should be thrown");
88         } catch (final ReactorException e) {
89             assertTrue(e.getCause().getMessage().contains("Error in module 'test4' in the refine of uses " +
90                     "'Relative{path=[(urn:simple.container.demo)node]}': can not perform refine of 'PRESENCE' for" +
91                     " the target 'LEAF_LIST'."));
92         }
93     }
94
95     @Test
96     public void testInvalidLength() throws IOException, YangSyntaxErrorException {
97         try {
98             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang");
99             fail("ReactorException should be thrown");
100         } catch (final ReactorException e) {
101             final String message = e.getCause().getMessage();
102
103             assertTrue(message.contains("Invalid length constraint [4..10]"));
104         }
105     }
106
107     @Test
108     public void testInvalidRange() throws IOException, YangSyntaxErrorException {
109         try {
110             TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang");
111             fail("ReactorException should be thrown");
112         } catch (final ReactorException e) {
113             assertTrue(e.getCause().getMessage().contains("Invalid range constraint: <5, 20>"));
114         }
115     }
116
117     @Test
118     public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
119         try {
120             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang");
121             fail("SourceException should be thrown");
122         } catch (final ReactorException e) {
123             final String expected = "Error in module 'container': cannot add '(urn:simple.container" +
124                     ".demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already declared";
125             assertTrue(e.getCause().getMessage().contains(expected));
126         }
127     }
128
129     @Test
130     public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
131         try {
132             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang");
133             fail("SourceException should be thrown");
134         } catch (final ReactorException e) {
135             final String expected = "Error in module 'container-list': cannot add '(urn:simple.container" +
136                     ".demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already declared";
137             assertTrue(e.getCause().getMessage().contains(expected));
138         }
139     }
140
141     @Test
142     public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
143         try {
144             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-leaf.yang");
145             fail("SourceException should be thrown");
146         } catch (final ReactorException e) {
147             final String expected = "Error in module 'container-leaf': cannot add '(urn:simple.container" +
148                     ".demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already declared";
149             assertTrue(e.getCause().getMessage().contains(expected));
150         }
151     }
152
153     @Test
154     public void testDuplicateTypedef() throws IOException, YangSyntaxErrorException {
155         try {
156             TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/typedef.yang");
157             fail("SourceException should be thrown");
158         } catch (final ReactorException e) {
159             assertTrue(e.getCause().getMessage().startsWith(
160                 "Duplicate name for typedef (urn:simple.container.demo)int-ext [at"));
161         }
162     }
163
164     @Test
165     public void testDuplicityInAugmentTarget1() throws IOException, ReactorException, YangSyntaxErrorException {
166         TestUtils.loadModuleResources(getClass(),
167             "/negative-scenario/duplicity/augment0.yang",
168                 "/negative-scenario/duplicity/augment1.yang");
169         testLog = output.toString();
170         assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
171     }
172
173     @Test
174     public void testDuplicityInAugmentTarget2() throws IOException, ReactorException, YangSyntaxErrorException {
175         TestUtils.loadModuleResources(getClass(),
176             "/negative-scenario/duplicity/augment0.yang",
177                 "/negative-scenario/duplicity/augment2.yang");
178         testLog = output.toString();
179         assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
180     }
181
182     @Test
183     public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException {
184         TestUtils.loadModuleResources(getClass(),
185             "/negative-scenario/testfile8.yang",
186                 "/negative-scenario/testfile7.yang");
187         testLog = output.toString();
188         assertTrue(testLog.contains(
189             "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
190     }
191
192     @Test
193     public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
194         try {
195             TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang");
196             fail("InferenceException should be thrown");
197         } catch (final ReactorException e) {
198             final String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def)application-map'";
199             assertTrue(e.getCause().getMessage().startsWith(expected));
200         }
201     }
202 }