2 * Copyright (c) 2016 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.stmt;
10 import static org.hamcrest.CoreMatchers.allOf;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.CoreMatchers.endsWith;
13 import static org.hamcrest.CoreMatchers.startsWith;
14 import static org.hamcrest.MatcherAssert.assertThat;
15 import static org.hamcrest.Matchers.isA;
16 import static org.junit.Assert.assertThrows;
18 import com.google.common.base.Throwables;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 public class YangParserNegativeTest {
26 public void testInvalidImport() {
27 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
28 () -> TestUtils.parseYangSource("/negative-scenario/testfile1.yang"));
30 final Throwable rootCause = Throwables.getRootCause(ex);
31 assertThat(rootCause, isA(InferenceException.class));
32 assertThat(rootCause.getMessage(), startsWith("Imported module"));
33 assertThat(rootCause.getMessage(), containsString("was not found."));
37 public void testTypeNotFound() {
38 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
39 () -> TestUtils.parseYangSource("/negative-scenario/testfile2.yang"));
40 final Throwable rootCause = Throwables.getRootCause(ex);
41 assertThat(rootCause, isA(InferenceException.class));
42 assertThat(rootCause.getMessage(),
43 startsWith("Type [(urn:simple.types.data.demo?revision=2013-02-27)int-ext] was not found."));
47 public void testInvalidAugmentTarget() {
48 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
49 () -> TestUtils.parseYangSource(
50 "/negative-scenario/testfile0.yang", "/negative-scenario/testfile3.yang"));
51 final Throwable rootCause = Throwables.getRootCause(ex);
52 assertThat(rootCause, isA(InferenceException.class));
53 assertThat(rootCause.getMessage(), startsWith(
54 "Augment target 'Absolute{qnames=[(urn:simple.container.demo)unknown]}' not found"));
58 public void testInvalidRefine() {
59 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
60 () -> TestUtils.parseYangSource("/negative-scenario/testfile4.yang"));
61 final Throwable cause = ex.getCause();
62 assertThat(cause, isA(SourceException.class));
63 assertThat(cause.getMessage(), containsString("Error in module 'test4' in the refine of uses "
64 + "'Descendant{qnames=[(urn:simple.container.demo)node]}': can not perform refine of 'PRESENCE' for"
65 + " the target 'LEAF_LIST'."));
69 public void testInvalidLength() {
70 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
71 () -> TestUtils.parseYangSource("/negative-scenario/testfile5.yang"));
72 final Throwable cause = ex.getCause();
73 assertThat(cause, isA(SourceException.class));
74 assertThat(cause.getMessage(), containsString("Invalid length constraint [4..10]"));
78 public void testInvalidRange() {
79 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
80 () -> TestUtils.parseYangSource("/negative-scenario/testfile6.yang"));
81 final Throwable cause = ex.getCause();
82 assertThat(cause, isA(SourceException.class));
83 assertThat(cause.getMessage(), startsWith("Invalid range constraint: [[5..20]]"));
87 public void testDuplicateContainer() {
88 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
89 () -> TestUtils.parseYangSource("/negative-scenario/duplicity/container.yang"));
90 final Throwable cause = ex.getCause();
91 assertThat(cause, isA(SourceException.class));
92 assertThat(cause.getMessage(), containsString("Error in module 'container': cannot add "
93 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
98 public void testDuplicateContainerList() {
99 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
100 () -> TestUtils.parseYangSource("/negative-scenario/duplicity/container-list.yang"));
101 final Throwable cause = ex.getCause();
102 assertThat(cause, isA(SourceException.class));
103 assertThat(cause.getMessage(), containsString("Error in module 'container-list': cannot add "
104 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
109 public void testDuplicateContainerLeaf() {
110 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
111 () -> TestUtils.parseYangSource("/negative-scenario/duplicity/container-leaf.yang"));
112 final Throwable cause = ex.getCause();
113 assertThat(cause, isA(SourceException.class));
114 assertThat(cause.getMessage(), containsString("Error in module 'container-leaf': cannot add "
115 + "'(urn:simple.container.demo)foo'. Node name collision: '(urn:simple.container.demo)foo' already "
120 public void testDuplicateTypedef() {
121 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
122 () -> TestUtils.parseYangSource("/negative-scenario/duplicity/typedef.yang"));
123 final Throwable cause = ex.getCause();
124 assertThat(cause, isA(SourceException.class));
125 assertThat(cause.getMessage(), startsWith(
126 "Duplicate name for typedef (urn:simple.container.demo)int-ext [at"));
130 public void testDuplicityInAugmentTarget1() {
131 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
132 () -> TestUtils.parseYangSource(
133 "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment1.yang"));
134 final Throwable cause = ex.getCause();
135 assertThat(cause, isA(InferenceException.class));
136 assertThat(cause.getMessage(), startsWith(
137 "An augment cannot add node named 'id' because this name is already used in target"));
141 public void testDuplicityInAugmentTarget2() {
142 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
143 () -> TestUtils.parseYangSource(
144 "/negative-scenario/duplicity/augment0.yang", "/negative-scenario/duplicity/augment2.yang"));
145 final Throwable rootCause = Throwables.getRootCause(ex);
146 assertThat(rootCause, isA(SourceException.class));
147 assertThat(rootCause.getMessage(), allOf(
148 startsWith("Error in module 'augment0': cannot add "
149 + "'(urn:simple.augment2.demo?revision=2014-06-02)delta'. Node name collision: "
150 + "'(urn:simple.augment2.demo?revision=2014-06-02)delta' already declared at "),
151 endsWith("duplicity/augment2.yang:17:9]")));
155 public void testMandatoryInAugment() {
156 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
157 () -> TestUtils.parseYangSource(
158 "/negative-scenario/testfile8.yang", "/negative-scenario/testfile7.yang"));
159 final Throwable cause = ex.getCause();
160 assertThat(cause, isA(InferenceException.class));
161 assertThat(cause.getMessage(), startsWith(
162 "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
166 public void testInvalidListKeyDefinition() {
167 final SomeModifiersUnresolvedException ex = assertThrows(SomeModifiersUnresolvedException.class,
168 () -> TestUtils.parseYangSource("/negative-scenario/invalid-list-key-def.yang"));
169 final Throwable cause = ex.getCause();
170 assertThat(cause, isA(InferenceException.class));
171 assertThat(cause.getMessage(), startsWith(
172 "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def)application-map'"));