Do not catch instantiation exceptions during augment
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6669Test.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.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
16
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27
28 public class Bug6669Test {
29     private static final String REV = "2016-09-08";
30     private static final String FOO_NS = "foo";
31     private static final String BAR_NS = "bar";
32     private static final QName ROOT = QName.create(FOO_NS, REV, "root");
33     private static final QName BAR = QName.create(BAR_NS, REV, "bar");
34     private static final QName BAR_1 = QName.create(BAR_NS, REV, "bar1");
35     private static final QName BAR_2 = QName.create(BAR_NS, REV, "bar2");
36     private static final QName M = QName.create(BAR_NS, REV, "m");
37
38     @Test
39     public void testInvalidAugment() {
40         final ReactorException ex = assertThrows(ReactorException.class,
41             () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test1"));
42         final Throwable cause = ex.getCause();
43         assertThat(cause, instanceOf(InferenceException.class));
44         assertThat(cause.getMessage(), startsWith(
45             "An augment cannot add node 'm' because it is mandatory and in module different than target"));
46     }
47
48     @Test
49     public void testInvalidAugment2() {
50         final ReactorException ex = assertThrows(ReactorException.class,
51             () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test2"));
52         final Throwable cause = ex.getCause();
53         assertThat(cause, instanceOf(InferenceException.class));
54         assertThat(cause.getMessage(), startsWith(
55             "An augment cannot add node 'm' because it is mandatory and in module different than target"));
56     }
57
58     @Test
59     public void testInvalidAugment3() {
60         final ReactorException ex = assertThrows(ReactorException.class,
61             () ->  StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test3"));
62         final Throwable cause = ex.getCause();
63         assertThat(cause, instanceOf(InferenceException.class));
64         assertThat(cause.getMessage(), startsWith(
65             "An augment cannot add node 'l' because it is mandatory and in module different than target"));
66     }
67
68     @Test
69     public void testValidAugment() throws Exception {
70         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/valid/test1");
71         assertNotNull(context);
72
73         final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
74                 SchemaPath.create(true, ROOT, BAR, BAR_1, M));
75         assertTrue(findDataSchemaNode instanceof LeafSchemaNode);
76     }
77
78     @Test
79     public void testValidAugment2() throws Exception {
80         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/valid/test2");
81         assertNotNull(context);
82
83         final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
84                 SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, M));
85         assertTrue(findDataSchemaNode instanceof LeafSchemaNode);
86     }
87
88     @Test
89     public void testValidAugment3() throws Exception {
90         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/valid/test3");
91         assertNotNull(context);
92
93         final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
94                 SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, QName.create(BAR_NS, REV, "l")));
95         assertTrue(findDataSchemaNode instanceof ListSchemaNode);
96     }
97 }