blob: 2aab5567a4eae0a96bfe6d8d3e7748696243c1d1 [file] [log] [blame]
Vidyashree Rama1db15562016-05-17 16:16:15 +05301/*
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 */
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.YangNode;
24import org.onosproject.yangutils.datamodel.YangModule;
25import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangChoice;
27import org.onosproject.yangutils.datamodel.YangContainer;
28import org.onosproject.yangutils.datamodel.YangNodeType;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
31
32import static org.hamcrest.core.Is.is;
33import static org.junit.Assert.assertThat;
34
35/**
36 * Test cases for testing default listener functionality.
37 */
38public class DefaultListenerTest {
39
40 private final YangUtilsParserManager manager = new YangUtilsParserManager();
41
42 /**
43 * Checks if default value is set correctly.
44 */
45 @Test
46 public void processLeafSubStatementDefault() throws IOException, ParserException {
47
48 YangNode node = manager.getDataModel("src/test/resources/LeafSubStatementDefault.yang");
49 // Check whether the data model tree returned is of type module.
50 assertThat((node instanceof YangModule), is(true));
51
52 // Check whether the node type is set properly to module.
53 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
54
55 // Check whether the module name is set correctly.
56 YangModule yangNode = (YangModule) node;
57 assertThat(yangNode.getName(), is("Test"));
58
59 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
60 YangLeaf leafInfo = leafIterator.next();
61
62 assertThat(leafInfo.getName(), is("invalid-interval"));
63 assertThat(leafInfo.getDefaultValueInString(), is("\"1\""));
64 }
65
66 /**
67 * Checks if default value is set correctly.
68 */
69 @Test
70 public void processChoiceSubStatementDefault() throws IOException, ParserException {
71
72 YangNode node = manager.getDataModel("src/test/resources/ChoiceSubStatementDefault.yang");
73 // Check whether the data model tree returned is of type module.
74 assertThat((node instanceof YangModule), is(true));
75
76 // Check whether the node type is set properly to module.
77 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
78
79 // Check whether the module name is set correctly.
80 YangModule yangNode = (YangModule) node;
81 assertThat(yangNode.getName(), is("Test"));
82
83 YangContainer yangContainer = (YangContainer) yangNode.getChild();
84 assertThat(yangContainer.getName(), is("food"));
85
86 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
87 assertThat(yangChoice.getName(), is("snack"));
88 assertThat(yangChoice.getDefaultValueInString(), is("\"hello\""));
89 }
90}