4b059177edeb6da61d47a25f5fa66354052258c3
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6876Test.java
1 /*
2  * Copyright (c) 2017 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.parser.stmt.rfc7950;
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 com.google.common.collect.ImmutableList;
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 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27
28 public class Bug6876Test {
29     private static final String BAR_NS = "bar";
30     private static final String BAR_REV = "2017-01-11";
31     private static final String FOO_NS = "foo";
32
33     @Test
34     public void yang11Test() throws Exception {
35         final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang11");
36         assertNotNull(context);
37
38         assertThat(findNode(context, ImmutableList.of(bar("augment-target"), bar("my-leaf"))),
39             instanceOf(LeafSchemaNode.class));
40         assertThat(findNode(context, ImmutableList.of(bar("augment-target"), foo("mandatory-leaf"))),
41             instanceOf(LeafSchemaNode.class));
42     }
43
44     @Test
45     public void yang10Test() {
46         final ReactorException ex = assertThrows(ReactorException.class,
47             () -> StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang10"));
48         final Throwable cause = ex.getCause();
49         assertThat(cause, instanceOf(InferenceException.class));
50         assertThat(cause.getMessage(), startsWith(
51             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
52     }
53
54     private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
55         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
56     }
57
58     private static QName foo(final String localName) {
59         return QName.create(FOO_NS, localName);
60     }
61
62     private static QName bar(final String localName) {
63         return QName.create(BAR_NS, BAR_REV, localName);
64     }
65 }