blob: a78c65fc001d2b4188dcc1611b96720493d55631 [file] [log] [blame]
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal4ce3acf2016-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 Ramab6248172016-05-17 16:16:15 +053021
22import org.junit.Rule;
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053023import org.junit.Test;
Vidyashree Ramab6248172016-05-17 16:16:15 +053024import org.junit.rules.ExpectedException;
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053025import org.onosproject.yangutils.datamodel.YangDataTypes;
26import 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;
36import java.util.ListIterator;
37import java.util.Set;
38
39/**
40 * Test cases for enum listener.
41 */
42public class EnumListenerTest {
43
Vidyashree Ramab6248172016-05-17 16:16:15 +053044 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053047 private final YangUtilsParserManager manager = new YangUtilsParserManager();
48
49 /**
50 * Checks enum statement without value.
51 */
52 @Test
53 public void processEnumTypeStatement() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/EnumTypeStatement.yang");
56
57 // Check whether the data model tree returned is of type module.
58 assertThat((node instanceof YangModule), is(true));
59
60 // Check whether the node type is set properly to module.
61 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
62
63 // Check whether the module name is set correctly.
64 YangModule yangNode = (YangModule) node;
65 assertThat(yangNode.getName(), is("Test"));
66
67 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
68 YangLeaf leafInfo = leafIterator.next();
69
Vinod Kumar S79a374b2016-04-30 21:09:15 +053070 assertThat(leafInfo.getName(), is("speed"));
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053071 assertThat(leafInfo.getDataType().getDataTypeName(), is("enumeration"));
72 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.ENUMERATION));
Gaurav Agrawal26390042016-04-12 13:30:16 +053073 assertThat(((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getName(),
74 is("speed_enum"));
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +053075
76 Set<YangEnum> enumSet = ((YangEnumeration) leafInfo.getDataType().getDataTypeExtendedInfo()).getEnumSet();
77 for (YangEnum tmp : enumSet) {
78 if (tmp.getNamedValue().equals("10m")) {
79 assertThat(tmp.getValue(), is(0));
80 } else if (tmp.getNamedValue().equals("100m")) {
81 assertThat(tmp.getValue(), is(1));
82 } else if (tmp.getNamedValue().equals("auto")) {
83 assertThat(tmp.getValue(), is(2));
84 }
85 }
86 }
Gaurav Agrawal480a47d2016-02-26 02:47:39 +053087
88 /**
89 * Checks if enum with same name is not allowed.
90 */
91 @Test(expected = ParserException.class)
92 public void processEnumWithDuplicateName() throws IOException, ParserException {
Gaurav Agrawal480a47d2016-02-26 02:47:39 +053093 YangNode node = manager.getDataModel("src/test/resources/EnumWithDuplicateName.yang");
94 }
Vidyashree Ramab6248172016-05-17 16:16:15 +053095
96 /**
97 * Checks enum boundary value.
98 */
99 @Test
100 public void processEnumBoundaryValue() throws IOException, ParserException {
101 thrown.expect(ParserException.class);
102 thrown.expectMessage("YANG file error : value value 21474836472147483647 is not valid.");
103 YangNode node = manager.getDataModel("src/test/resources/EnumBoundaryValue.yang");
104 }
105
106 /**
107 * Checks whether exception is thrown if value is not specified following max enum value.
108 */
109 @Test
110 public void processEnumMaxNextValue() throws IOException, ParserException {
111 thrown.expect(ParserException.class);
112 thrown.expectMessage("YANG file error : "
113 + "An enum value MUST be specified for enum substatements following the one"
114 + "with the current highest value");
115 YangNode node = manager.getDataModel("src/test/resources/EnumMaxNextValue.yang");
116 }
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +0530117}