blob: 70bb87502a1b9a15b5d96b0015a72e3776909e87 [file] [log] [blame]
janani be18b5342016-07-13 21:06:41 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import java.io.IOException;
20import java.util.ListIterator;
21
22import org.junit.Rule;
23import org.junit.Test;
24import org.junit.rules.ExpectedException;
25
26import org.onosproject.yangutils.datamodel.YangContainer;
27import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangLeafRef;
30import org.onosproject.yangutils.datamodel.YangModule;
31import org.onosproject.yangutils.datamodel.YangNode;
32import org.onosproject.yangutils.datamodel.YangNodeType;
33import org.onosproject.yangutils.datamodel.YangType;
34import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39/**
40 * Test cases for require-instance listener.
41 */
42public class RequireInstanceListenerTest {
43
44 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
47 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks require-statement with true as status.
51 */
52 @Test
53 public void processRequireInstanceTrue() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/RequireInstanceTrue.yang");
56
57 // Check whether the data model tree returned is of type module.
58 assertThat((node instanceof YangModule), is(true));
59
60 // Check whether the node type is set properly to module.
61 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
62
63 // Check whether the module name is set correctly.
64 YangModule yangNode = (YangModule) node;
65 assertThat(yangNode.getName(), is("PathListener"));
66
67 YangContainer container = (YangContainer) yangNode.getChild().getNextSibling();
68 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
69 YangLeaf leafInfo = leafIterator.next();
70
71 // Check whether the require-instance value is set correctly in leafref.
72 assertThat(leafInfo.getName(), is("ifname"));
73 YangLeafRef yangLeafRef = (YangLeafRef) leafInfo.getDataType().getDataTypeExtendedInfo();
74 assertThat(yangLeafRef.getRequireInstance(), is(true));
75 }
76
77 /**
78 * Checks require-statement with false as status.
79 */
80 @Test
81 public void processRequireInstanceFalse() throws IOException, ParserException {
82
83 YangNode node = manager.getDataModel("src/test/resources/RequireInstanceFalse.yang");
84
85 // Check whether the data model tree returned is of type module.
86 assertThat((node instanceof YangModule), is(true));
87
88 // Check whether the node type is set properly to module.
89 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
90
91 // Check whether the module name is set correctly.
92 YangModule yangNode = (YangModule) node;
93 assertThat(yangNode.getName(), is("PathListener"));
94
95 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
96 YangLeaf leafInfo = leafIterator.next();
97
98 // Check whether the require-instance value is set correctly in instance-identifier.
99 assertThat(leafInfo.getName(), is("admin-status"));
100
101 YangType type = leafInfo.getDataType();
102
103 assertThat(type.getDataType(), is(YangDataTypes.INSTANCE_IDENTIFIER));
104 boolean status = ((YangType<Boolean>) type).getDataTypeExtendedInfo();
105
106 assertThat(status, is(false));
107 }
108
109 /**
110 * Checks require-statement default value when its not there in YANG under instance-identifier.
111 */
112 @Test
113 public void processRequireInstanceDefaultValueInInstanceIdentifier() throws IOException, ParserException {
114
115 YangNode node = manager.getDataModel("src/test/resources/RequireInstanceDefaultValueInInstanceIdentifier.yang");
116
117 // Check whether the data model tree returned is of type module.
118 assertThat((node instanceof YangModule), is(true));
119
120 // Check whether the node type is set properly to module.
121 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
122
123 // Check whether the module name is set correctly.
124 YangModule yangNode = (YangModule) node;
125 assertThat(yangNode.getName(), is("PathListener"));
126
127 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
128 YangLeaf leafInfo = leafIterator.next();
129
130 // Check whether the require-instance value is set correctly in instance-identifier.
131 assertThat(leafInfo.getName(), is("admin-status"));
132
133 YangType type = leafInfo.getDataType();
134
135 assertThat(type.getDataType(), is(YangDataTypes.INSTANCE_IDENTIFIER));
136
137 boolean status = ((YangType<Boolean>) type).getDataTypeExtendedInfo();
138 assertThat(status, is(true));
139 }
140
141 /**
142 * Checks require-statement default value when its not there in YANG under leafref.
143 */
144 @Test
145 public void processRequireInstanceDefaultValueForLeafref() throws IOException, ParserException {
146
147 YangNode node = manager.getDataModel("src/test/resources/RequireInstanceDefaultValueForLeafref.yang");
148
149 // Check whether the data model tree returned is of type module.
150 assertThat((node instanceof YangModule), is(true));
151
152 // Check whether the node type is set properly to module.
153 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
154
155 // Check whether the module name is set correctly.
156 YangModule yangNode = (YangModule) node;
157 assertThat(yangNode.getName(), is("PathListener"));
158
159 YangContainer container = (YangContainer) yangNode.getChild().getNextSibling();
160 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
161 YangLeaf leafInfo = leafIterator.next();
162
163 // Check whether the require-instance value is set correctly in leafref.
164 assertThat(leafInfo.getName(), is("ifname"));
165 YangLeafRef yangLeafRef = (YangLeafRef) leafInfo.getDataType().getDataTypeExtendedInfo();
166 assertThat(yangLeafRef.getRequireInstance(), is(true));
167 }
168}