blob: 1737ea66694efd6cc7d7fac576595c0d233b7ecf [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present 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 */
Vidyashree Rama49abe712016-02-13 22:22:12 +053016package org.onosproject.yangutils.parser.impl.listeners;
17
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053018import java.io.IOException;
19import java.util.ListIterator;
Vidyashree Rama49abe712016-02-13 22:22:12 +053020import org.junit.Test;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053021import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053023import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
29
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.core.Is.is;
32
33/**
34 * Test case for type listener.
35 */
36public class TypeListenerTest {
37
38 private final YangUtilsParserManager manager = new YangUtilsParserManager();
39
40 /**
41 * Checks derived statement without contraints.
42 */
43 @Test
44 public void processDerivedTypeStatement() throws IOException, ParserException {
45
46 YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.yang");
47
48 // Check whether the data model tree returned is of type module.
49 assertThat((node instanceof YangModule), is(true));
50
51 // Check whether the node type is set properly to module.
52 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
53
54 // Check whether the module name is set correctly.
55 YangModule yangNode = (YangModule) node;
56 assertThat(yangNode.getName(), is("Test"));
57
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053058 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
59 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053060
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053062 assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053063 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
64 }
65
66 /**
67 * Checks valid yang data type.
68 */
69 @Test
70 public void processIntegerTypeStatement() throws IOException, ParserException {
71
72 YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang");
73
74 // Check whether the data model tree returned is of type module.
75 assertThat((node instanceof YangModule), is(true));
76
77 // Check whether the node type is set properly to module.
78 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
79
80 // Check whether the module name is set correctly.
81 YangModule yangNode = (YangModule) node;
82 assertThat(yangNode.getName(), is("Test"));
83
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053084 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
85 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053086
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053088 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053089 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
90 }
91
92 /**
93 * Checks type for leaf-list.
94 */
95 @Test
96 public void processLeafListSubStatementType() throws IOException, ParserException {
97
98 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang");
99
100 // Check whether the data model tree returned is of type module.
101 assertThat((node instanceof YangModule), is(true));
102
103 // Check whether the node type is set properly to module.
104 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
105
106 // Check whether the module name is set correctly.
107 YangModule yangNode = (YangModule) node;
108 assertThat(yangNode.getName(), is("Test"));
109
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530110 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
111 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530112
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530114 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530115 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
116 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117}