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.test;
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.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;
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";
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");
44 private final ByteArrayOutputStream output = new ByteArrayOutputStream();
47 public void setUp() throws UnsupportedEncodingException {
48 System.setOut(new PrintStream(output, true, "UTF-8"));
52 public void cleanUp() {
53 System.setOut(System.out);
57 public void incorrectTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
58 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
59 assertNotNull(context);
61 SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
62 SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
63 assertNull(mandatoryLeaf);
65 String testLog = output.toString();
67 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
71 public void incorrectTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
72 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
73 assertNotNull(context);
75 SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
76 SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
77 assertNull(mandatoryLeaf);
79 String testLog = output.toString();
81 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
85 public void incorrectTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
86 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
87 assertNotNull(context);
89 SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
91 SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
92 assertNull(mandatoryLeaf);
94 String testLog = output.toString();
96 .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
100 public void correctTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
101 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
102 assertNotNull(context);
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);
110 public void correctTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
111 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
112 assertNotNull(context);
114 SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
116 SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
117 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
121 public void correctTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
122 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
123 assertNotNull(context);
125 SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
127 SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
128 assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
132 public void correctTest4() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
133 SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
134 assertNotNull(context);
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);