blob: 1fe72183bcfa9762d577d5507408320cc2cbe531 [file] [log] [blame]
Gaurav Agrawalbd804472016-03-25 11:25:36 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawalbd804472016-03-25 11:25:36 +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
19import java.io.IOException;
20import java.util.List;
21import java.util.ListIterator;
22import org.junit.Test;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053023import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangList;
27import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangNodeType;
30import org.onosproject.yangutils.datamodel.YangType;
31import org.onosproject.yangutils.datamodel.YangUnion;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.core.Is.is;
37
38/**
39 * Test cases for testing union listener.
40 */
41public class UnionListenerTest {
42
43 private final YangUtilsParserManager manager = new YangUtilsParserManager();
44
45 /**
46 * Checks union when type is in leaf.
47 */
48 @Test
49 public void processUnionWhenTypeInLeaf() throws IOException, ParserException {
50
51 YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeaf.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 YangList yangList = (YangList) yangNode.getChild();
64 assertThat(yangList.getName(), is("valid"));
65
66 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
67 YangLeaf leafInfo = leafIterator.next();
68
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +053070
71 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
72 assertThat(leafInfo.getDataType().getDataTypeName(), is("union"));
73
74 YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo();
75
76 List<YangType<?>> typeList = yangUnion.getTypeList();
77 ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
78 YangType<?> yangType = typeListIterator.next();
79
80 assertThat(yangType.getDataTypeName(), is("int32"));
81 assertThat(yangType.getDataType(), is(YangDataTypes.INT32));
82
83 YangType<?> yangTypeEnum = typeListIterator.next();
84
85 assertThat(yangTypeEnum.getDataTypeName(), is("enumeration"));
86 assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION));
87 }
88
89 /**
90 * Checks union when type is in leaflist.
91 */
92 @Test
93 public void processUnionWhenTypeInLeafList() throws IOException, ParserException {
94
95 YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeafList.yang");
96
97 // Check whether the data model tree returned is of type module.
98 assertThat((node instanceof YangModule), is(true));
99
100 // Check whether the node type is set properly to module.
101 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
102
103 // Check whether the module name is set correctly.
104 YangModule yangNode = (YangModule) node;
105 assertThat(yangNode.getName(), is("Test"));
106
107 YangList yangList = (YangList) yangNode.getChild();
108 assertThat(yangList.getName(), is("valid"));
109
110 ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator();
111 YangLeafList leafListInfo = leafListIterator.next();
112
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530114
115 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UNION));
116 assertThat(leafListInfo.getDataType().getDataTypeName(), is("union"));
117
118 YangUnion yangUnion = (YangUnion) leafListInfo.getDataType().getDataTypeExtendedInfo();
119
120 List<YangType<?>> typeList = yangUnion.getTypeList();
121 ListIterator<YangType<?>> typeListIterator = typeList.listIterator();
122 YangType<?> yangType = typeListIterator.next();
123
124 assertThat(yangType.getDataTypeName(), is("int32"));
125 assertThat(yangType.getDataType(), is(YangDataTypes.INT32));
126
127 YangType<?> yangTypeEnum = typeListIterator.next();
128
129 assertThat(yangTypeEnum.getDataTypeName(), is("enumeration"));
130 assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION));
131 }
132
133 /**
134 * Checks union with empty type.
135 */
136 @Test (expected = ParserException.class)
137 public void processUnionWithEmptyType() throws IOException, ParserException {
138
139 YangNode node = manager.getDataModel("src/test/resources/UnionWithEmptyType.yang");
140 }
Vidyashree Rama528ef302016-06-30 14:31:18 +0530141
142 /**
143 * Checks whether type union has atleast one type statement.
144 */
145 @Test (expected = ParserException.class)
146 public void processUnionWithoutChild() throws IOException, ParserException {
147 manager.getDataModel("src/test/resources/UnionWithoutChild.yang");
148 }
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530149}