blob: fcf6c397836c5cbc3cfedce3a4cbb3c59b65b2fe [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;
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 min-elements listener.
38 */
39public class MinElementsListenerTest {
40
41 @Rule
42 public ExpectedException thrown = ExpectedException.none();
43
44 private final YangUtilsParserManager manager = new YangUtilsParserManager();
45
46 /**
47 * Checks min-elements as sub-statements of leaf-list.
48 */
49 @Test
50 public void processLeafListSubStatementMinElements() throws IOException, ParserException {
51
52 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementMinElements.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
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 assertThat(leafListInfo.getName(), is("invalid-interval"));
rama-huawei6c728a92016-07-11 14:48:12 +053068 assertThat(leafListInfo.getMinElements().getMinElement(), is(3));
Vidyashree Rama49abe712016-02-13 22:22:12 +053069 }
70
71 /**
72 * Checks min-elements as sub-statements of list.
73 */
74 @Test
75 public void processListSubStatementMinElements() throws IOException, ParserException {
76
77 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementMinElements.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"));
rama-huawei6c728a92016-07-11 14:48:12 +053091 assertThat(yangList.getMinElements().getMinElement(), is(3));
Vidyashree Rama49abe712016-02-13 22:22:12 +053092 }
93
94 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053095 * Checks whether exception is thrown when invalid min-elements value is
96 * given as input.
Vidyashree Rama49abe712016-02-13 22:22:12 +053097 */
98 @Test
99 public void processMinElementsInvalidValue() throws IOException, ParserException {
100 thrown.expect(ParserException.class);
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530101 thrown.expectMessage("YANG file error : min-elements value asd is not valid.");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530102 YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidValue.yang");
103 }
104
105 /**
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530106 * Checks whether exception is thrown when invalid min-elements value is
107 * given as input.
108 */
109 @Test
110 public void processMinElementsMaxValue() throws IOException, ParserException {
111 thrown.expect(ParserException.class);
112 thrown.expectMessage("YANG file error : min-elements value 77777777777777777777777 is not valid.");
113 YangNode node = manager.getDataModel("src/test/resources/MinElementsMaxValue.yang");
114 }
115
116 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 * Checks whether exception is thrown when min-elements statement without
118 * statement end is given as input.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530119 */
120 @Test
121 public void processMinElementsWithoutStatementEnd() throws IOException, ParserException {
122 thrown.expect(ParserException.class);
123 thrown.expectMessage("missing ';' at 'description'");
124 YangNode node = manager.getDataModel("src/test/resources/MinElementsWithoutStatementEnd.yang");
125 }
126
127 /**
128 * Checks whether exception is thrown when min-elements cardinality is not
129 * as per the grammar.
130 */
131 @Test
132 public void processMinElementsInvalidCardinality() throws IOException, ParserException {
133 thrown.expect(ParserException.class);
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530134 thrown.expectMessage("YANG file error: \"min-elements\" is defined more than once in \"leaf-list " +
135 "invalid-interval\".");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530136 YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidCardinality.yang");
137 }
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530138
139 /**
140 * Checks min-element's default value.
141 */
142 @Test
143 public void processMinElementsDefaultValue() throws IOException, ParserException {
144
145 YangNode node = manager.getDataModel("src/test/resources/MinElementsDefaultValue.yang");
146
147 // Check whether the data model tree returned is of type module.
148 assertThat((node instanceof YangModule), is(true));
149
150 // Check whether the node type is set properly to module.
151 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
152
153 // Check whether the module name is set correctly.
154 YangModule yangNode = (YangModule) node;
155 assertThat(yangNode.getName(), is("Test"));
156
157 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
158 YangLeafList leafListInfo = leafListIterator.next();
159
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530160 assertThat(leafListInfo.getName(), is("invalid-interval"));
rama-huawei6c728a92016-07-11 14:48:12 +0530161 assertThat(leafListInfo.getMinElements().getMinElement(), is(0));
Vidyashree Ramaa0568d22016-02-24 11:26:42 +0530162 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163}