Clean up yang-parser-rfc7950 tests
[yangtools.git] / yang / 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.assertNull;
15 import static org.junit.Assert.assertThrows;
16
17 import java.util.Optional;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27
28 public class Bug8126Test {
29     private static final String FOO_NS = "foo";
30     private static final String BAR_NS = "bar";
31
32     @Test
33     public void testValidAugments() throws Exception {
34         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug8126/valid");
35         assertThat(findNode(context, foo("root"), bar("my-container"), bar("my-choice"), bar("one"), bar("one"),
36                 bar("mandatory-leaf")), instanceOf(LeafSchemaNode.class));
37         assertThat(context.findDataTreeChild(foo("root"), bar("my-list"), bar("two"), bar("mandatory-leaf-2")).get(),
38             instanceOf(LeafSchemaNode.class));
39         assertEquals(Optional.empty(), context.findDataTreeChild(foo("root"), bar("mandatory-list")));
40         assertNull(findNode(context, foo("root"), bar("mandatory-container"), bar("mandatory-choice")));
41         assertEquals(Optional.empty(), context.findDataTreeChild(foo("root"), bar("mandatory-container-2"), bar("one"),
42                 bar("mandatory-leaf-3")));
43     }
44
45     @Test
46     public void testAugmentMandatoryChoice() {
47         final ReactorException ex = assertThrows(ReactorException.class,
48             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-choice"));
49         final Throwable cause = ex.getCause();
50         assertThat(cause, instanceOf(InferenceException.class));
51         assertThat(cause.getMessage(), startsWith(
52             "An augment cannot add node 'mandatory-choice' because it is mandatory and in module different than "));
53     }
54
55     @Test
56     public void testAugmentMandatoryList() {
57         final ReactorException ex = assertThrows(ReactorException.class,
58             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-list"));
59         final Throwable cause = ex.getCause();
60         assertThat(cause, instanceOf(InferenceException.class));
61         assertThat(cause.getMessage(), startsWith(
62             "An augment cannot add node 'mandatory-list' because it is mandatory and in module different than "));
63     }
64
65     @Test
66     public void testAugmentMandatoryContainer() {
67         final ReactorException ex = assertThrows(ReactorException.class,
68             () -> StmtTestUtils.parseYangSources("/bugs/bug8126/inv-cont"));
69         final Throwable cause = ex.getCause();
70         assertThat(cause, instanceOf(InferenceException.class));
71         assertThat(cause.getMessage(), startsWith(
72             "An augment cannot add node 'mandatory-leaf-3' because it is mandatory and in module different than "));
73     }
74
75     private static SchemaNode findNode(final SchemaContext context, final QName... qnames) {
76         return SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, qnames));
77     }
78
79     private static QName foo(final String localName) {
80         return QName.create(FOO_NS, localName);
81     }
82
83     private static QName bar(final String localName) {
84         return QName.create(BAR_NS, localName);
85     }
86 }