blob: 68400b939dbecb7f730b8938377431486c72e5f3 [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
2 * Copyright 2014-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 static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangChoice;
23import org.onosproject.yangutils.datamodel.YangContainer;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
29
30import java.io.IOException;
31
32/**
33 * Test cases for choice listener.
34 */
35public class ChoiceListenerTest {
36
37 private final YangUtilsParserManager manager = new YangUtilsParserManager();
38
39 /**
40 * Checks choice statement without body.
41 */
42 @Test
43 public void processChoiceStatementWithoutBody() throws IOException, ParserException {
44
45 YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithoutBody.yang");
46
47 // Check whether the data model tree returned is of type module.
48 assertThat((node instanceof YangModule), is(true));
49
50 // Check whether the node type is set properly to module.
51 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
52
53 // Check whether the module name is set correctly.
54 YangModule yangNode = (YangModule) node;
55 assertThat(yangNode.getName(), is("Test"));
56
57 YangContainer yangContainer = (YangContainer) yangNode.getChild();
58 assertThat(yangContainer.getName(), is("food"));
59
60 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
61 assertThat(yangChoice.getName(), is("snack"));
62 }
63
64 /**
65 * Checks choice statement with stmt end.
66 */
67 @Test
68 public void processChoiceStatementWithStmtend() throws IOException, ParserException {
69
70 YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementWithStmtend.yang");
71
72 // Check whether the data model tree returned is of type module.
73 assertThat((node instanceof YangModule), is(true));
74
75 // Check whether the node type is set properly to module.
76 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
77
78 // Check whether the module name is set correctly.
79 YangModule yangNode = (YangModule) node;
80 assertThat(yangNode.getName(), is("Test"));
81
82 YangContainer yangContainer = (YangContainer) yangNode.getChild();
83 assertThat(yangContainer.getName(), is("food"));
84
85 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
86 assertThat(yangChoice.getName(), is("snack"));
87 }
88
89 /**
90 * Checks choice statement duplicate entry.
91 */
92 @Test(expected = ParserException.class)
93 public void processChoiceStatementDuplicateEntry() throws IOException, ParserException {
94
95 YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementDuplicateEntry.yang");
96 }
97
98 /**
99 * Checks choice statement with same entry in two different container.
100 */
101 @Test
102 public void processChoiceStatementSameEntryDifferentContainer() throws IOException, ParserException {
103
104 YangNode node = manager.getDataModel("src/test/resources/ChoiceStatementSameEntryDifferentContainer.yang");
105
106 // Check whether the data model tree returned is of type module.
107 assertThat((node instanceof YangModule), is(true));
108
109 // Check whether the node type is set properly to module.
110 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
111
112 // Check whether the module name is set correctly.
113 YangModule yangNode = (YangModule) node;
114 assertThat(yangNode.getName(), is("Test"));
115
116 YangContainer yangContainer1 = (YangContainer) yangNode.getChild();
117 assertThat(yangContainer1.getName(), is("food1"));
118
119 YangChoice yangChoice1 = (YangChoice) yangContainer1.getChild();
120 assertThat(yangChoice1.getName(), is("snack"));
121
122 YangContainer yangContainer2 = (YangContainer) yangNode.getChild().getNextSibling();
123 assertThat(yangContainer2.getName(), is("food2"));
124
125 YangChoice yangChoice2 = (YangChoice) yangContainer2.getChild();
126 assertThat(yangChoice2.getName(), is("snack"));
127 }
128}