2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
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;
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;
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";
34 public void yang11Test() throws Exception {
35 final SchemaContext context = StmtTestUtils.parseYangSources("/rfc7950/bug6876/yang11");
36 assertNotNull(context);
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));
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"));
54 private static SchemaNode findNode(final SchemaContext context, final Iterable<QName> qnames) {
55 return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(qnames, true));
58 private static QName foo(final String localName) {
59 return QName.create(FOO_NS, localName);
62 private static QName bar(final String localName) {
63 return QName.create(BAR_NS, BAR_REV, localName);