blob: fe2bed1c209470815866ecad6cd891a43da0b7b6 [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama49abe712016-02-13 22:22:12 +05303 *
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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
Vidyashree Rama49abe712016-02-13 22:22:12 +053021import org.junit.Rule;
22import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053023import org.junit.rules.ExpectedException;
24import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053026import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
32
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import static org.hamcrest.MatcherAssert.assertThat;
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import static org.hamcrest.Matchers.nullValue;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053035import static org.hamcrest.core.Is.is;
Vidyashree Rama49abe712016-02-13 22:22:12 +053036
37/**
38 * Test cases for units listener.
39 */
40public class UnitsListenerTest {
41
42 @Rule
43 public ExpectedException thrown = ExpectedException.none();
44
45 private final YangUtilsParserManager manager = new YangUtilsParserManager();
46
47 /**
48 * Checks valid units statement.
49 */
50 @Test
51 public void processUnitsStatement() throws IOException, ParserException {
52
53 YangNode node = manager.getDataModel("src/test/resources/UnitsStatement.yang");
54
55 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +053057
58 // Check whether the node type is set properly to module.
59 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
60
61 // Check whether the module name is set correctly.
62 YangModule yangNode = (YangModule) node;
63 assertThat(yangNode.getName(), is("Test"));
64
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053065 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
66 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053067
68 // Check whether units value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053070 assertThat(leafInfo.getUnits(), is("\"seconds\""));
71 }
72
73 /**
Vidyashree Rama49abe712016-02-13 22:22:12 +053074 * Checks invalid units statement(without statement end).
75 */
76 @Test
77 public void processUnitsWithoutStatementEnd() throws IOException, ParserException {
78 thrown.expect(ParserException.class);
79 thrown.expectMessage("mismatched input '}' expecting {';', '+'}");
80 YangNode node = manager.getDataModel("src/test/resources/UnitsWithoutStatementEnd.yang");
81 }
82
83 /**
84 * Checks order of units statement in leaf.
85 */
86 @Test
87 public void processUnitsStatementOrder() throws IOException, ParserException {
88
89 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementOrder.yang");
90
91 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053092 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +053093
94 // Check whether the node type is set properly to module.
95 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
96
97 // Check whether the module name is set correctly.
98 YangModule yangNode = (YangModule) node;
99 assertThat(yangNode.getName(), is("Test"));
100
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530101 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
102 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530103
104 // Check whether leaf properties is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530106 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530107 assertThat(leafInfo.getUnits(), is("\"seconds\""));
108 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
109 assertThat(leafInfo.isConfig(), is(true));
110 assertThat(leafInfo.isMandatory(), is(true));
111 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
112 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
113 }
114
115 /**
116 * Checks the default value of unit statement.
117 */
118 @Test
119 public void processUnitsDefaultValue() throws IOException, ParserException {
120
121 YangNode node = manager.getDataModel("src/test/resources/UnitsDefaultValue.yang");
122
123 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530124 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530125
126 // Check whether the node type is set properly to module.
127 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
128
129 // Check whether the module name is set correctly.
130 YangModule yangNode = (YangModule) node;
131 assertThat(yangNode.getName(), is("Test"));
132
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530133 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
134 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530135
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530137 assertThat(leafInfo.getUnits(), is(nullValue()));
138 }
139
140 /**
141 * Checks invalid occurance of units statement as sub-statement of leaf.
142 */
143 @Test
144 public void processUnitsStatementCardinality() throws IOException, ParserException {
145 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530146 thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530147 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
148 }
149
150 /**
151 * Checks valid units statement as sub-statement of leaf-list.
152 */
153 @Test
154 public void processLeafListSubStatementUnits() throws IOException, ParserException {
155
156 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementUnits.yang");
157
158 // Check whether the data model tree returned is of type module.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530159 assertThat(node instanceof YangModule, is(true));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530160
161 // Check whether the node type is set properly to module.
162 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
163
164 // Check whether the module name is set correctly.
165 YangModule yangNode = (YangModule) node;
166 assertThat(yangNode.getName(), is("Test"));
167
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530168 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
169 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530170
171 // Check whether units value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172 assertThat(leafListInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530173 assertThat(leafListInfo.getUnits(), is("\"seconds\""));
174 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530175}