blob: 61775f6cf004c6099f9dfad98dd81a2949c27721 [file] [log] [blame]
Vidyashree Rama6a72b792016-03-29 12:00:42 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama6a72b792016-03-29 12:00:42 +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.ListIterator;
21
22import org.junit.Test;
23import org.onosproject.yangutils.datamodel.YangModule;
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangNodeType;
26import org.onosproject.yangutils.datamodel.YangRpc;
27import org.onosproject.yangutils.datamodel.YangInput;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangList;
30import org.onosproject.yangutils.datamodel.YangContainer;
31import org.onosproject.yangutils.datamodel.YangTypeDef;
32import org.onosproject.yangutils.datamodel.YangStatusType;
33import org.onosproject.yangutils.datamodel.YangDataTypes;
34import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
36
37import static org.hamcrest.core.Is.is;
38import static org.junit.Assert.assertThat;
39
40/**
41 * Test cases for testing Input listener functionality.
42 */
43public class InputListenerTest {
44
45 private final YangUtilsParserManager manager = new YangUtilsParserManager();
46
47 /**
48 * Checks input statements with data definition statements as sub-statements.
49 */
50 @Test
51 public void processInputStatementWithDataDefinition() throws IOException, ParserException {
52
53 YangNode node = manager.getDataModel("src/test/resources/InputStatementWithDataDefinition.yang");
54
55 assertThat((node instanceof YangModule), is(true));
56 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
57 YangModule yangNode = (YangModule) node;
58 assertThat(yangNode.getName(), is("rock"));
59
60 YangRpc yangRpc = (YangRpc) yangNode.getChild();
61 assertThat(yangRpc.getName(), is("activate-software-image"));
62
63 YangInput yangInput = (YangInput) yangRpc.getChild();
Gaurav Agrawal56527662016-04-20 15:49:17 +053064 assertThat(yangInput.getName(), is("activate-software-image_input"));
Vidyashree Rama6a72b792016-03-29 12:00:42 +053065 ListIterator<YangLeaf> leafIterator = yangInput.getListOfLeaf().listIterator();
66 YangLeaf leafInfo = leafIterator.next();
67
68 assertThat(leafInfo.getLeafName(), is("image-name"));
69 assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
70
71 YangList yangList = (YangList) yangInput.getChild();
72 assertThat(yangList.getName(), is("ospf"));
73 assertThat(yangList.getKeyList().contains("invalid-interval"), is(true));
74 assertThat(yangList.isConfig(), is(true));
75 assertThat(yangList.getMaxElements(), is(10));
76 assertThat(yangList.getMinElements(), is(3));
77 leafIterator = yangList.getListOfLeaf().listIterator();
78 leafInfo = leafIterator.next();
79 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
80 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
81
82 YangContainer yangContainer = (YangContainer) yangList.getNextSibling();
83 assertThat(yangContainer.getName(), is("isis"));
84
85 leafIterator = yangContainer.getListOfLeaf().listIterator();
86 leafInfo = leafIterator.next();
87 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
88 assertThat(leafInfo.getDataType().getDataTypeName(), is("uint16"));
89 }
90
91 /**
92 * Checks input statements with type-def statement as sub-statements.
93 */
94 @Test
95 public void processInputStatementWithTypedef() throws IOException, ParserException {
96
97 YangNode node = manager.getDataModel("src/test/resources/InputStatementWithTypedef.yang");
98 YangModule yangNode = (YangModule) node;
99 assertThat(yangNode.getName(), is("rock"));
100
101 YangRpc yangRpc = (YangRpc) yangNode.getChild();
102 assertThat(yangRpc.getName(), is("activate-software-image"));
103
104 YangInput yangInput = (YangInput) yangRpc.getChild();
Gaurav Agrawal56527662016-04-20 15:49:17 +0530105 assertThat(yangInput.getName(), is("activate-software-image_input"));
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530106 YangTypeDef typeDef = (YangTypeDef) yangInput.getChild();
107 assertThat(typeDef.getName(), is("my-type"));
108 assertThat(typeDef.getStatus(), is(YangStatusType.DEPRECATED));
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530109 assertThat(typeDef.getTypeDefBaseType().getDataType(), is(YangDataTypes.INT32));
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530110 }
111}