Do not catch instantiation exceptions during augment
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5335Test.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.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26
27 public class Bug5335Test {
28     private static final String FOO = "foo";
29     private static final String BAR = "bar";
30     private static final String REV = "2016-03-04";
31
32     private static final QName ROOT = QName.create(FOO, REV, "root");
33     private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container");
34     private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf");
35     private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container");
36     private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container");
37     private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf");
38
39     @Test
40     public void incorrectTest1() {
41         final ReactorException ex = assertThrows(ReactorException.class,
42             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1"));
43         final Throwable cause = ex.getCause();
44         assertThat(cause, instanceOf(InferenceException.class));
45         assertThat(cause.getMessage(), startsWith(
46             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
47     }
48
49     @Test
50     public void incorrectTest2() {
51         final ReactorException ex = assertThrows(ReactorException.class,
52             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
53         final Throwable cause = ex.getCause();
54         assertThat(cause, instanceOf(InferenceException.class));
55         assertThat(cause.getMessage(), startsWith(
56             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
57     }
58
59     @Test
60     public void incorrectTest3() throws Exception {
61         final ReactorException ex = assertThrows(ReactorException.class,
62             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
63         final Throwable cause = ex.getCause();
64         assertThat(cause, instanceOf(InferenceException.class));
65         assertThat(cause.getMessage(), startsWith(
66             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
67     }
68
69     @Test
70     public void correctTest1() throws Exception {
71         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
72         assertNotNull(context);
73
74         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
75         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
76         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
77     }
78
79     @Test
80     public void correctTest2() throws Exception {
81         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
82         assertNotNull(context);
83
84         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
85                 MANDATORY_LEAF_B);
86         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
87         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
88     }
89
90     @Test
91     public void correctTest3() throws Exception {
92         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
93         assertNotNull(context);
94
95         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
96                 MANDATORY_LEAF_B);
97         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
98         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
99     }
100
101     @Test
102     public void correctTest4() throws Exception {
103         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
104         assertNotNull(context);
105
106         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F);
107         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
108         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
109     }
110 }