blob: 246d4b065e8237d4ef6b16ce762684164067ab65 [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;
24import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053029import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
Vidyashree Rama49abe712016-02-13 22:22:12 +053032import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.core.Is.is;
34
35/**
36 * Test case for mandatory listener.
37 */
38public class MandatoryListenerTest {
39
40 @Rule
41 public ExpectedException thrown = ExpectedException.none();
42
43 private final YangUtilsParserManager manager = new YangUtilsParserManager();
44
45 /**
46 * Checks valid mandatory with value true statement.
47 */
48 @Test
49 public void processMandatoryTrue() throws IOException, ParserException {
50
51 YangNode node = manager.getDataModel("src/test/resources/MandatoryTrue.yang");
52
53 // Check whether the data model tree returned is of type module.
54 assertThat((node instanceof YangModule), is(true));
55
56 // Check whether the node type is set properly to module.
57 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
58
59 // Check whether the module name is set correctly.
60 YangModule yangNode = (YangModule) node;
61 assertThat(yangNode.getName(), is("Test"));
62
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053063 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
64 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053065
66 // Check whether the mandatory value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053068 assertThat(leafInfo.isMandatory(), is(true));
69 }
70
71 /**
72 * Checks valid mandatory with value false statement.
73 */
74 @Test
75 public void processMandatoryFalse() throws IOException, ParserException {
76
77 YangNode node = manager.getDataModel("src/test/resources/MandatoryFalse.yang");
78
79 // Check whether the data model tree returned is of type module.
80 assertThat((node instanceof YangModule), is(true));
81
82 // Check whether the node type is set properly to module.
83 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
84
85 // Check whether the module name is set correctly.
86 YangModule yangNode = (YangModule) node;
87 assertThat(yangNode.getName(), is("Test"));
88
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053089 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
90 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053091
92 // Check whether the mandatory value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053094 assertThat(leafInfo.isMandatory(), is(false));
95 }
96
97 /**
98 * Checks default value of mandatory statement.
99 */
100 @Test
101 public void processMandatoryDefaultValue() throws IOException, ParserException {
102
103 YangNode node = manager.getDataModel("src/test/resources/MandatoryDefaultValue.yang");
104
105 // Check whether the data model tree returned is of type module.
106 assertThat((node instanceof YangModule), is(true));
107
108 // Check whether the node type is set properly to module.
109 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
110
111 // Check whether the module name is set correctly.
112 YangModule yangNode = (YangModule) node;
113 assertThat(yangNode.getName(), is("Test"));
114
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
116 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530117
118 // Check whether the mandatory value is set correctly.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 assertThat(leafInfo.getName(), is("invalid-interval"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530120 assertThat(leafInfo.isMandatory(), is(false));
121 }
122
123 /**
124 * Checks invalid of mandatory statement and expects exception.
125 */
126 @Test
127 public void processMandatoryEmptyStatement() throws IOException, ParserException {
128 thrown.expect(ParserException.class);
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530129 thrown.expectMessage("no viable alternative at input ';'");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530130 YangNode node = manager.getDataModel("src/test/resources/MandatoryEmptyStatement.yang");
131 }
132
133 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530134 * Checks invalid mandatory statement(without statement end) and expects
135 * exception.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530136 */
137 @Test
138 public void processMandatoryWithoutStatementEnd() throws IOException, ParserException {
139 thrown.expect(ParserException.class);
140 thrown.expectMessage("missing ';' at '}'");
141 YangNode node = manager.getDataModel("src/test/resources/MandatoryWithoutStatementEnd.yang");
142 }
143
144 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530145 * Checks mandatory statement as sub-statement of module and expects
146 * exception.
Vidyashree Rama49abe712016-02-13 22:22:12 +0530147 */
148 @Test
149 public void processModuleSubStatementMandatory() throws IOException, ParserException {
150 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530151 thrown.expectMessage("mismatched input 'mandatory' expecting {'augment', 'choice', 'contact', 'container',"
152 + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include',"
Vidyashree Ramabcd7fba2016-03-09 20:41:44 +0530153 + " 'leaf', 'leaf-list', 'list', 'notification', 'organization', 'reference',"
154 + " 'revision', 'rpc', 'typedef', 'uses', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530155 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementMandatory.yang");
156 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157}