Do not catch instantiation exceptions during augment
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug8126Test.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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThrows;
15
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25
26 public class Bug8126Test {
27     private static final String FOO_NS = "foo";
28     private static final String BAR_NS = "bar";
29
30     @Test
31     public void testValidAugments() throws Exception {
32         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug8126/valid");
33         assertThat(findNode(context, foo("root"), bar("my-container"), bar("my-choice"), bar("one"), bar("one"),
34             bar("mandatory-leaf")), instanceOf(LeafSchemaNode.class));
35         assertThat(findNode(context, foo("root"), bar("my-list"), bar("two"), bar("mandatory-leaf-2")),
36             instanceOf(LeafSchemaNode.class));
37
38         assertNull(findNode(context, foo("root"), bar("mandatory-list")));
39         assertNull(findNode(context, foo("root"), bar("mandatory-container"), bar("mandatory-choice")));
40         assertNull(findNode(context, foo("root"), bar("mandatory-container-2"), bar("one"), bar("mandatory-leaf-3")));
41     }
42
43     @Test
44     public void testAugmentMandatoryChoice() {
45         final ReactorException ex = assertThrows(ReactorException.class,
46             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-choice"));
47         final Throwable cause = ex.getCause();
48         assertThat(cause, instanceOf(InferenceException.class));
49         assertThat(cause.getMessage(), startsWith(
50             "An augment cannot add node 'mandatory-choice' because it is mandatory and in module different than "));
51     }
52
53     @Test
54     public void testAugmentMandatoryList() {
55         final ReactorException ex = assertThrows(ReactorException.class,
56             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-list"));
57         final Throwable cause = ex.getCause();
58         assertThat(cause, instanceOf(InferenceException.class));
59         assertThat(cause.getMessage(), startsWith(
60             "An augment cannot add node 'mandatory-list' because it is mandatory and in module different than "));
61     }
62
63     @Test
64     public void testAugmentMandatoryContainer() {
65         final ReactorException ex = assertThrows(ReactorException.class,
66             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-cont"));
67         final Throwable cause = ex.getCause();
68         assertThat(cause, instanceOf(InferenceException.class));
69         assertThat(cause.getMessage(), startsWith(
70             "An augment cannot add node 'mandatory-leaf-3' because it is mandatory and in module different than "));
71     }
72
73
74     private static SchemaNode findNode(final SchemaContext context, final QName... qnames) {
75         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, qnames));
76     }
77
78     private static QName foo(final String localName) {
79         return QName.create(FOO_NS, localName);
80     }
81
82     private static QName bar(final String localName) {
83         return QName.create(BAR_NS, localName);
84     }
85 }