blob: d5f6645cedb6b3c73658444ea9b7ae22e092e0ea [file] [log] [blame]
Gaurav Agrawal9c512e02016-02-25 04:37:05 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal9c512e02016-02-25 04:37:05 +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 static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
Vidyashree Rama1db15562016-05-17 16:16:15 +053021
22import org.junit.Rule;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053023import org.junit.Test;
Vidyashree Rama1db15562016-05-17 16:16:15 +053024import org.junit.rules.ExpectedException;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053026import org.onosproject.yangutils.datamodel.YangEnum;
27import org.onosproject.yangutils.datamodel.YangEnumeration;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangModule;
30import org.onosproject.yangutils.datamodel.YangNode;
31import org.onosproject.yangutils.datamodel.YangNodeType;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
34
35import java.io.IOException;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053036import java.util.Iterator;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053037import java.util.ListIterator;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053038import java.util.SortedSet;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053039
40/**
41 * Test cases for enum listener.
42 */
43public class EnumListenerTest {
44
Vidyashree Rama1db15562016-05-17 16:16:15 +053045 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053048 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks enum statement without value.
52 */
53 @Test
54 public void processEnumTypeStatement() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/EnumTypeStatement.yang");
57
58 // Check whether the data model tree returned is of type module.
59 assertThat((node instanceof YangModule), is(true));
60
61 // Check whether the node type is set properly to module.
62 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
63
64 // Check whether the module name is set correctly.
65 YangModule yangNode = (YangModule) node;
66 assertThat(yangNode.getName(), is("Test"));
67
68 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
69 YangLeaf leafInfo = leafIterator.next();
70
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 assertThat(leafInfo.getName(), is("speed"));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053072 assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
73 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053074 assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
75 is("speed_enum"));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053076
Vidyashree Rama210c01d2016-05-20 16:29:25 +053077 SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053078 for (YangEnum tmp : enumSet) {
79 if (tmp.getNamedValue().equals("10m")) {
80 assertThat(tmp.getValue(), is(0));
81 } else if (tmp.getNamedValue().equals("100m")) {
82 assertThat(tmp.getValue(), is(1));
83 } else if (tmp.getNamedValue().equals("auto")) {
84 assertThat(tmp.getValue(), is(2));
85 }
86 }
87 }
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053088
89 /**
90 * Checks if enum with same name is not allowed.
91 */
92 @Test(expected = ParserException.class)
93 public void processEnumWithDuplicateName() throws IOException, ParserException {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053094 YangNode node = manager.getDataModel("src/test/resources/EnumWithDuplicateName.yang");
95 }
Vidyashree Rama1db15562016-05-17 16:16:15 +053096
97 /**
98 * Checks enum boundary value.
99 */
100 @Test
101 public void processEnumBoundaryValue() throws IOException, ParserException {
102 thrown.expect(ParserException.class);
103 thrown.expectMessage("YANG file error : value value 21474836472147483647 is not valid.");
104 YangNode node = manager.getDataModel("src/test/resources/EnumBoundaryValue.yang");
105 }
106
107 /**
108 * Checks whether exception is thrown if value is not specified following max enum value.
109 */
110 @Test
111 public void processEnumMaxNextValue() throws IOException, ParserException {
112 thrown.expect(ParserException.class);
113 thrown.expectMessage("YANG file error : "
114 + "An enum value MUST be specified for enum substatements following the one"
115 + "with the current highest value");
116 YangNode node = manager.getDataModel("src/test/resources/EnumMaxNextValue.yang");
117 }
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530118
119 /**
120 * Checks enum values stored are sorted.
121 */
122 @Test
123 public void processEnumSorted() throws IOException, ParserException {
124 YangNode node = manager.getDataModel("src/test/resources/EnumSorted.yang");
125 // Check whether the data model tree returned is of type module.
126 assertThat((node instanceof YangModule), is(true));
127
128 // Check whether the node type is set properly to module.
129 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
130
131 // Check whether the module name is set correctly.
132 YangModule yangNode = (YangModule) node;
133 assertThat(yangNode.getName(), is("Test"));
134
135 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
136 YangLeaf leafInfo = leafIterator.next();
137
138 assertThat(leafInfo.getName(), is("ifType"));
139 assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
140 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
141 assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
142 is("ifType_enum"));
143
144 SortedSet<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
145 Iterator<YangEnum> enumIterator = enumSet.iterator();
146 assertThat(enumIterator.next().getNamedValue(), is("five"));
147 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530148}