blob: b9f12b07395d6559b2d940d780b5372effa428ce [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
2 * Copyright 2016 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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
21
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.junit.Rule;
23import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.junit.rules.ExpectedException;
25import org.onosproject.yangutils.datamodel.YangLeafList;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053026import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
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;
34import static org.hamcrest.core.Is.is;
35
36/**
37 * Test cases for testing max-elements listener.
38 */
39public class MaxElementsListenerTest {
40
41 @Rule
42 public ExpectedException thrown = ExpectedException.none();
43
44 private final YangUtilsParserManager manager = new YangUtilsParserManager();
45
46 /**
47 * Checks max-elements as sub-statements of leaf-list.
48 */
49 @Test
50 public void processLeafListSubStatementMaxElements() throws IOException, ParserException {
51
52 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMaxElements.yang");
53
54 // Check whether the data model tree returned is of type module.
55 assertThat((node instanceof YangModule), is(true));
56
57 // Check whether the node type is set properly to module.
58 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
59
60 // Check whether the module name is set correctly.
61 YangModule yangNode = (YangModule) node;
62 assertThat(yangNode.getName(), is("Test"));
63
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053064 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
65 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053066
67 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
68 assertThat(leafListInfo.getMaxElelements(), is(3));
69 }
70
71 /**
72 * Checks max-elements as sub-statements of list.
73 */
74 @Test
75 public void processListSubStatementMaxElements() throws IOException, ParserException {
76
77 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMaxElements.yang");
78
79 assertThat((node instanceof YangModule), is(true));
80
81 // Check whether the node type is set properly to module.
82 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
83
84 // Check whether the module name is set correctly.
85 YangModule yangNode = (YangModule) node;
86 assertThat(yangNode.getName(), is("Test"));
87
88 // Check whether the list is child of module
89 YangList yangList = (YangList) yangNode.getChild();
90 assertThat(yangList.getName(), is("valid"));
91 assertThat(yangList.getMaxElelements(), is(3));
92 }
93
94 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053095 * Checks whether exception is thrown when invalid max-elements keyword is
96 * given as input.
Vidyashree Rama49abe712016-02-13 22:22:12 +053097 */
98 @Test
99 public void processMaxElementsInvalidStatement() throws IOException, ParserException {
100 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 thrown.expectMessage("extraneous input 'max-element' expecting {'config', 'description', 'if-feature',"
102 + " 'max-elements', 'min-elements', 'must', 'ordered-by', 'reference', 'status', 'type', 'units', "
103 + "'when', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530104 YangNode node = manager.getDataModel("src/test/resources/MaxElementsInvalidStatement.yang");
105 }
106
107 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530108 * Checks whether exception is thrown when max-elements statement without
109 * statement end is given as input.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530110 */
111 @Test
112 public void processMaxElementsWithoutStatementEnd() throws IOException, ParserException {
113 thrown.expect(ParserException.class);
114 thrown.expectMessage("missing ';' at 'description'");
115 YangNode node = manager.getDataModel("src/test/resources/MaxElementsWithoutStatementEnd.yang");
116 }
117
118 /**
119 * Checks whether exception is thrown when max-elements cardinality is not
120 * as per the grammar.
121 */
122 @Test
123 public void processMaxElementsCardinality() throws IOException, ParserException {
124 thrown.expect(ParserException.class);
125 thrown.expectMessage("Internal parser error detected: Invalid cardinality in max-elements before processing.");
126 YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang");
127 }
128
129 /**
130 * Checks unbounded value of max-elements statement.
131 */
132 @Test
133 public void processMaxElementsUnbounded() throws IOException, ParserException {
134
135 YangNode node = manager.getDataModel("src/test/resources/MaxElementsUnbounded.yang");
136
137 // Check whether the data model tree returned is of type module.
138 assertThat((node instanceof YangModule), is(true));
139
140 // Check whether the node type is set properly to module.
141 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
142
143 // Check whether the module name is set correctly.
144 YangModule yangNode = (YangModule) node;
145 assertThat(yangNode.getName(), is("Test"));
146
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530147 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
148 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530149
150 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
151 assertThat(leafListInfo.getMaxElelements(), is(2147483647));
152 }
153}