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