2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
14 import java.io.ByteArrayOutputStream;
15 import java.io.PrintStream;
16 import java.io.UnsupportedEncodingException;
17 import java.nio.charset.StandardCharsets;
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;
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";
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");
41 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
44 public void setUp() throws UnsupportedEncodingException {
45 System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8));
49 @SuppressWarnings("checkstyle:regexpSinglelineJava")
50 public void cleanUp() {
51 System.setOut(System.out);
55 public void incorrectTest1() throws Exception {
56 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
57 assertNotNull(context);
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);
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"));
69 public void incorrectTest2() throws Exception {
70 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
71 assertNotNull(context);
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);
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"));
83 public void incorrectTest3() throws Exception {
84 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
85 assertNotNull(context);
87 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
89 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
90 assertNull(mandatoryLeaf);
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"));
98 public void correctTest1() throws Exception {
99 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
100 assertNotNull(context);
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);
108 public void correctTest2() throws Exception {
109 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
110 assertNotNull(context);
112 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
114 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
115 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
119 public void correctTest3() throws Exception {
120 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
121 assertNotNull(context);
123 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
125 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
126 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
130 public void correctTest4() throws Exception {
131 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
132 assertNotNull(context);
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);