Adjust test suite parser update to conform with API changes
[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     @SuppressWarnings("checkstyle:regexpSinglelineJava")
50     public void cleanUp() {
51         System.setOut(System.out);
52     }
53
54     @Test
55     public void incorrectTest1() throws Exception {
56         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
57         assertNotNull(context);
58
59         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
60         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
61         assertNull(mandatoryLeaf);
62
63         final String testLog = output.toString();
64         assertTrue(testLog.contains(
65             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
66     }
67
68     @Test
69     public void incorrectTest2() throws Exception {
70         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
71         assertNotNull(context);
72
73         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
74         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
75         assertNull(mandatoryLeaf);
76
77         final String testLog = output.toString();
78         assertTrue(testLog.contains(
79             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
80     }
81
82     @Test
83     public void incorrectTest3() throws Exception {
84         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
85         assertNotNull(context);
86
87         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
88                 MANDATORY_LEAF_B);
89         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
90         assertNull(mandatoryLeaf);
91
92         final String testLog = output.toString();
93         assertTrue(testLog.contains(
94             "An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
95     }
96
97     @Test
98     public void correctTest1() throws Exception {
99         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
100         assertNotNull(context);
101
102         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
103         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
104         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
105     }
106
107     @Test
108     public void correctTest2() throws Exception {
109         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
110         assertNotNull(context);
111
112         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
113                 MANDATORY_LEAF_B);
114         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
115         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
116     }
117
118     @Test
119     public void correctTest3() throws Exception {
120         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
121         assertNotNull(context);
122
123         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
124                 MANDATORY_LEAF_B);
125         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
126         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
127     }
128
129     @Test
130     public void correctTest4() throws Exception {
131         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
132         assertNotNull(context);
133
134         final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F);
135         final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
136         assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
137     }
138 }