297e96754edc48d937950bb5e4c6207b98d93fc7
[yangtools.git] / parser / 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.assertThrows;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22
23 public class Bug5335Test extends AbstractYangTest {
24     private static final String FOO = "foo";
25     private static final String BAR = "bar";
26     private static final String REV = "2016-03-04";
27
28     private static final QName ROOT = QName.create(FOO, REV, "root");
29     private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container");
30     private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf");
31     private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container");
32     private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container");
33     private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf");
34
35     @Test
36     public void incorrectTest1() {
37         final ReactorException ex = assertThrows(ReactorException.class,
38             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1"));
39         final Throwable cause = ex.getCause();
40         assertThat(cause, instanceOf(InferenceException.class));
41         assertThat(cause.getMessage(), startsWith(
42             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
43     }
44
45     @Test
46     public void incorrectTest2() {
47         final ReactorException ex = assertThrows(ReactorException.class,
48             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
49         final Throwable cause = ex.getCause();
50         assertThat(cause, instanceOf(InferenceException.class));
51         assertThat(cause.getMessage(), startsWith(
52             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
53     }
54
55     @Test
56     public void incorrectTest3() {
57         final ReactorException ex = assertThrows(ReactorException.class,
58             () -> StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2"));
59         final Throwable cause = ex.getCause();
60         assertThat(cause, instanceOf(InferenceException.class));
61         assertThat(cause.getMessage(), startsWith(
62             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
63     }
64
65     @Test
66     public void correctTest1() {
67         final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug5335/correct/case-1");
68         final DataSchemaNode mandatoryLeaf = context.findDataTreeChild(ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B)
69             .orElse(null);
70         assertThat(mandatoryLeaf, instanceOf(LeafSchemaNode.class));
71     }
72
73     @Test
74     public void correctTest2() {
75         final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug5335/correct/case-2");
76         final DataSchemaNode mandatoryLeaf = context.findDataTreeChild(ROOT, PRESENCE_CONTAINER_B,
77             NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B).orElse(null);
78         assertThat(mandatoryLeaf, instanceOf(LeafSchemaNode.class));
79     }
80
81     @Test
82     public void correctTest3() {
83         final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug5335/correct/case-3");
84         final DataSchemaNode mandatoryLeaf = context.findDataTreeChild(ROOT, PRESENCE_CONTAINER_B,
85             NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B).orElse(null);
86         assertThat(mandatoryLeaf, instanceOf(LeafSchemaNode.class));
87     }
88
89     @Test
90     public void correctTest4() {
91         final EffectiveModelContext context = assertEffectiveModelDir("/bugs/bug5335/correct/case-4");
92         final DataSchemaNode mandatoryLeaf = context.findDataTreeChild(ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F)
93             .orElse(null);
94         assertThat(mandatoryLeaf, instanceOf(LeafSchemaNode.class));
95     }
96 }