586baeda12f257ace36c9b27ea98ddfc2408e845
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug8126Test.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.assertEquals;
14 import static org.junit.Assert.assertThrows;
15
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;
25
26 public class Bug8126Test {
27     private static final XMLNamespace FOO_NS = XMLNamespace.of("foo");
28     private static final XMLNamespace BAR_NS = XMLNamespace.of("bar");
29
30     @Test
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")));
44     }
45
46     @Test
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 "));
54     }
55
56     @Test
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 "));
64     }
65
66     @Test
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 "));
74     }
75
76     private static QName foo(final String localName) {
77         return QName.create(FOO_NS, localName);
78     }
79
80     private static QName bar(final String localName) {
81         return QName.create(BAR_NS, localName);
82     }
83 }