2 * Copyright (c) 2016 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.stmt;
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.assertEquals;
14 import static org.junit.Assert.assertThrows;
16 import java.util.Optional;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 public class Bug8126Test {
27 private static final XMLNamespace FOO_NS = XMLNamespace.of("foo");
28 private static final XMLNamespace BAR_NS = XMLNamespace.of("bar");
31 public void testValidAugments() throws Exception {
32 final ModuleEffectiveStatement fooModule = StmtTestUtils.parseYangSources("/bugs/bug8126/valid")
33 .getModuleStatement(QNameModule.create(FOO_NS));
34 assertThat(fooModule.findSchemaTreeNode(
35 foo("root"), bar("my-container"), bar("my-choice"), bar("one"), bar("one"), bar("mandatory-leaf"))
36 .orElse(null), instanceOf(LeafSchemaNode.class));
37 assertThat(fooModule.findSchemaTreeNode(foo("root"), bar("my-list"), bar("two"), bar("mandatory-leaf-2"))
38 .orElse(null), instanceOf(LeafSchemaNode.class));
39 assertEquals(Optional.empty(), fooModule.findSchemaTreeNode(foo("root"), bar("mandatory-list")));
40 assertEquals(Optional.empty(), fooModule.findSchemaTreeNode(
41 foo("root"), bar("mandatory-container"), bar("mandatory-choice")));
42 assertEquals(Optional.empty(), fooModule.findSchemaTreeNode(
43 foo("root"), bar("mandatory-container-2"), bar("one"), bar("mandatory-leaf-3")));
47 public void testAugmentMandatoryChoice() {
48 final ReactorException ex = assertThrows(ReactorException.class,
49 () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-choice"));
50 final Throwable cause = ex.getCause();
51 assertThat(cause, instanceOf(InferenceException.class));
52 assertThat(cause.getMessage(), startsWith(
53 "An augment cannot add node 'mandatory-choice' because it is mandatory and in module different than "));
57 public void testAugmentMandatoryList() {
58 final ReactorException ex = assertThrows(ReactorException.class,
59 () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-list"));
60 final Throwable cause = ex.getCause();
61 assertThat(cause, instanceOf(InferenceException.class));
62 assertThat(cause.getMessage(), startsWith(
63 "An augment cannot add node 'mandatory-list' because it is mandatory and in module different than "));
67 public void testAugmentMandatoryContainer() {
68 final ReactorException ex = assertThrows(ReactorException.class,
69 () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-cont"));
70 final Throwable cause = ex.getCause();
71 assertThat(cause, instanceOf(InferenceException.class));
72 assertThat(cause.getMessage(), startsWith(
73 "An augment cannot add node 'mandatory-leaf-3' because it is mandatory and in module different than "));
76 private static QName foo(final String localName) {
77 return QName.create(FOO_NS, localName);
80 private static QName bar(final String localName) {
81 return QName.create(BAR_NS, localName);