blob: 001e5a9ed96e2232860373bb1aa9827bf66e666b [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
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangNodeType;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.parser.exceptions.ParserException;
27import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
28
29import java.io.IOException;
30import java.util.ListIterator;
31
32import 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
63 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
64 YangLeaf leafInfo = leafIterator.next();
65
66 // Check whether the mandatory value is set correctly.
67 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
68 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
89 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
90 YangLeaf leafInfo = leafIterator.next();
91
92 // Check whether the mandatory value is set correctly.
93 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
94 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
115 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
116 YangLeaf leafInfo = leafIterator.next();
117
118 // Check whether the mandatory value is set correctly.
119 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
120 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);
129 thrown.expectMessage("missing {'false', 'true'} at ';'");
130 YangNode node = manager.getDataModel("src/test/resources/MandatoryEmptyStatement.yang");
131 }
132
133 /**
134 * Checks invalid mandatory statement(without statement end) and expects exception.
135 */
136 @Test
137 public void processMandatoryWithoutStatementEnd() throws IOException, ParserException {
138 thrown.expect(ParserException.class);
139 thrown.expectMessage("missing ';' at '}'");
140 YangNode node = manager.getDataModel("src/test/resources/MandatoryWithoutStatementEnd.yang");
141 }
142
143 /**
144 * Checks mandatory statement as sub-statement of module and expects exception.
145 */
146 @Test
147 public void processModuleSubStatementMandatory() throws IOException, ParserException {
148 thrown.expect(ParserException.class);
149 thrown.expectMessage("mismatched input 'mandatory' expecting {'augment', 'choice', 'contact', 'container'," +
150 " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include'," +
151 " 'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference'," +
152 " 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
153 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementMandatory.yang");
154 }
155}