blob: 3637f7c3429d588dd1fb0349b252b330813da5fa [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;
janani b6240d292016-05-17 18:20:33 +053020import org.junit.Rule;
Vidyashree Rama49abe712016-02-13 22:22:12 +053021import org.junit.Test;
janani b6240d292016-05-17 18:20:33 +053022import org.junit.rules.ExpectedException;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053023import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangLeafList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053026import org.onosproject.yangutils.datamodel.YangModule;
27import org.onosproject.yangutils.datamodel.YangNode;
28import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053029import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
Vidyashree Rama49abe712016-02-13 22:22:12 +053032import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.core.Is.is;
34
35/**
36 * Test case for type listener.
37 */
38public class TypeListenerTest {
39
janani b6240d292016-05-17 18:20:33 +053040 @Rule
41 public ExpectedException thrown = ExpectedException.none();
42
Vidyashree Rama49abe712016-02-13 22:22:12 +053043 private final YangUtilsParserManager manager = new YangUtilsParserManager();
44
45 /**
46 * Checks derived statement without contraints.
47 */
48 @Test
49 public void processDerivedTypeStatement() throws IOException, ParserException {
50
51 YangNode node = manager.getDataModel("src/test/resources/DerivedTypeStatement.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
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053063 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
64 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053065
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053066 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053067 assertThat(leafInfo.getDataType().getDataTypeName(), is("hello"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053068 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
69 }
70
71 /**
72 * Checks valid yang data type.
73 */
74 @Test
75 public void processIntegerTypeStatement() throws IOException, ParserException {
76
77 YangNode node = manager.getDataModel("src/test/resources/IntegerTypeStatement.yang");
78
79 // Check whether the data model tree returned is of type module.
80 assertThat((node instanceof YangModule), is(true));
81
82 // Check whether the node type is set properly to module.
83 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
84
85 // Check whether the module name is set correctly.
86 YangModule yangNode = (YangModule) node;
87 assertThat(yangNode.getName(), is("Test"));
88
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053089 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
90 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053091
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 assertThat(leafInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053093 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +053094 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
95 }
96
97 /**
98 * Checks type for leaf-list.
99 */
100 @Test
101 public void processLeafListSubStatementType() throws IOException, ParserException {
102
103 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementType.yang");
104
105 // Check whether the data model tree returned is of type module.
106 assertThat((node instanceof YangModule), is(true));
107
108 // Check whether the node type is set properly to module.
109 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
110
111 // Check whether the module name is set correctly.
112 YangModule yangNode = (YangModule) node;
113 assertThat(yangNode.getName(), is("Test"));
114
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
116 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530117
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 assertThat(leafListInfo.getName(), is("invalid-interval"));
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119 assertThat(leafListInfo.getDataType().getDataTypeName(), is("uint16"));
Vidyashree Rama49abe712016-02-13 22:22:12 +0530120 assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
121 }
janani b6240d292016-05-17 18:20:33 +0530122
123 /**
124 * Checks for unsupported type leafref.
125 */
126 @Test
127 public void processLeafrefType() throws IOException, ParserException {
128
129 thrown.expect(ParserException.class);
130 thrown.expectMessage("YANG file error : \"leafref\" is not supported in current version,"
131 + " please check wiki for YANG utils road map.");
132
133 YangNode node = manager
134 .getDataModel("src/test/resources/LeafrefInvalidIdentifier.yang");
135 }
136
137 /**
138 * Checks for unsupported type identityref.
139 */
140 @Test
141 public void processIdentityrefType() throws IOException, ParserException {
142
143 thrown.expect(ParserException.class);
144 thrown.expectMessage("YANG file error : \"identityref\" is not supported in current version,"
145 + " please check wiki for YANG utils road map.");
146
147 YangNode node = manager
148 .getDataModel("src/test/resources/IdentityrefInvalidIdentifier.yang");
149 }
150
151 /**
152 * Checks for unsupported type instance identifier.
153 */
154 @Test
155 public void processInstanceIdentifierType() throws IOException, ParserException {
156
157 thrown.expect(ParserException.class);
158 thrown.expectMessage("YANG file error : \"instance-identifier\" is not supported in current version,"
159 + " please check wiki for YANG utils road map.");
160
161 YangNode node = manager
162 .getDataModel("src/test/resources/InstanceIdentifierInvalidIdentifier.yang");
163 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164}