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
9 package org.opendaylight.yangtools.yang.stmt;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
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;
32 public class Bug5335Test {
33 private static final String FOO = "foo";
34 private static final String BAR = "bar";
35 private static final String REV = "2016-03-04";
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");
45 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
48 public void setUp() throws UnsupportedEncodingException {
49 System.setOut(new PrintStream(output, true, "UTF-8"));
53 public void cleanUp() {
54 System.setOut(System.out);
58 public void incorrectTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
59 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
60 assertNotNull(context);
62 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
63 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
64 assertNull(mandatoryLeaf);
66 final String testLog = output.toString();
68 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
72 public void incorrectTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
73 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
74 assertNotNull(context);
76 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
77 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
78 assertNull(mandatoryLeaf);
80 final String testLog = output.toString();
82 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
86 public void incorrectTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
87 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
88 assertNotNull(context);
90 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
92 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
93 assertNull(mandatoryLeaf);
95 final String testLog = output.toString();
97 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different than target"));
101 public void correctTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
102 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
103 assertNotNull(context);
105 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
106 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
107 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
111 public void correctTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
112 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
113 assertNotNull(context);
115 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
117 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
118 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
122 public void correctTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
123 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
124 assertNotNull(context);
126 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
128 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
129 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
133 public void correctTest4() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
134 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
135 assertNotNull(context);
137 final SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F);
138 final SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
139 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);