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