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