Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5335Test.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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.PrintStream;
17 import java.io.UnsupportedEncodingException;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
27
28 public class Bug5335Test {
29     private static final String FOO = "foo";
30     private static final String BAR = "bar";
31     private static final String REV = "2016-03-04";
32
33     private static final QName ROOT = QName.create(FOO, REV, "root");
34     private static final QName PRESENCE_CONTAINER_F = QName.create(FOO, REV, "presence-container");
35     private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container");
36     private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf");
37     private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container");
38     private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container");
39     private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf");
40
41     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
42
43     @Before
44     public void setUp() throws UnsupportedEncodingException {
45         System.setOut(new PrintStream(output, true, "UTF-8"));
46     }
47
48     @After
49     public void cleanUp() {
50         System.setOut(System.out);
51     }
52
53     @Test
54     public void incorrectTest1() throws Exception {
55         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
56         assertNotNull(context);
57
58         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
59         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
60         assertNull(mandatoryLeaf);
61
62         final String testLog = output.toString();
63         assertTrue(testLog
64                 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
65     }
66
67     @Test
68     public void incorrectTest2() throws Exception {
69         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
70         assertNotNull(context);
71
72         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
73         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
74         assertNull(mandatoryLeaf);
75
76         final String testLog = output.toString();
77         assertTrue(testLog
78                 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
79     }
80
81     @Test
82     public void incorrectTest3() throws Exception {
83         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
84         assertNotNull(context);
85
86         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
87                 MANDATORY_LEAF_B);
88         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
89         assertNull(mandatoryLeaf);
90
91         final String testLog = output.toString();
92         assertTrue(testLog
93                 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
94     }
95
96     @Test
97     public void correctTest1() throws Exception {
98         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
99         assertNotNull(context);
100
101         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
102         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
103         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
104     }
105
106     @Test
107     public void correctTest2() throws Exception {
108         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
109         assertNotNull(context);
110
111         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
112                 MANDATORY_LEAF_B);
113         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
114         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
115     }
116
117     @Test
118     public void correctTest3() throws Exception {
119         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
120         assertNotNull(context);
121
122         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
123                 MANDATORY_LEAF_B);
124         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
125         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
126     }
127
128     @Test
129     public void correctTest4() throws Exception {
130         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
131         assertNotNull(context);
132
133         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F);
134         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
135         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
136     }
137 }