blob: c3028de792307ef614144995677a6f877aa7eaaf [file] [log] [blame]
Gaurav Agrawala04483c2016-02-13 14:23:40 +05301/*
2 * Copyright 2016 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 org.junit.Test;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangNodeType;
22import org.onosproject.yangutils.datamodel.YangSubModule;
23import org.onosproject.yangutils.parser.exceptions.ParserException;
24import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
25
26import java.io.IOException;
27
28import static org.hamcrest.core.Is.is;
29import static org.junit.Assert.assertThat;
30
31/**
32 * Test cases for testing submodule listener functionality.
33 */
34public class SubModuleListenerTest {
35
36 private final YangUtilsParserManager manager = new YangUtilsParserManager();
37
38 /**
39 * Checks if the sub module listeners updates the data model tree.
40 */
41 @Test
42 public void processSubModuleValidEntry() throws IOException, ParserException {
43
44 YangNode node = manager.getDataModel("src/test/resources/SubModuleValidEntry.yang");
45
46 // Check whether the data model tree returned is of type module.
47 assertThat((node instanceof YangSubModule), is(true));
48
49 // Check whether the node type is set properly to module.
50 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
51
52 YangSubModule yangNode = (YangSubModule) node;
53 // Check whether the module name is set correctly.
54 assertThat(yangNode.getName(), is("Test"));
55 // Checks for the version value in data model tree.
56 assertThat(yangNode.getVersion(), is((byte) 1));
57 // Checks identifier of belongsto in data model tree.
58 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
59 // Checks for the version value in data model tree.
60 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
61 }
62
63 /**
64 * Checks if the yang version and belongs to can come in any order in sub
65 * module.
66 */
67 @Test
68 public void processSubModuleOrder() throws IOException, ParserException {
69
70 YangNode node = manager.getDataModel("src/test/resources/SubModuleOrder.yang");
71
72 // Check whether the data model tree returned is of type module.
73 assertThat((node instanceof YangSubModule), is(true));
74
75 // Check whether the node type is set properly to module.
76 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
77
78 YangSubModule yangNode = (YangSubModule) node;
79 // Check whether the module name is set correctly.
80 assertThat(yangNode.getName(), is("Test"));
81 // Checks for the version value in data model tree.
82 assertThat(yangNode.getVersion(), is((byte) 1));
83 // Checks identifier of belongsto in data model tree.
84 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
85 // Checks for the version value in data model tree.
86 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
87 }
88
89 /**
90 * Checks if yang version is optional.
91 */
92 @Test
93 public void processSubModuleWithoutVersion() throws IOException, ParserException {
94
95 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutVersion.yang");
96
97 // Check whether the data model tree returned is of type module.
98 assertThat((node instanceof YangSubModule), is(true));
99
100 // Check whether the node type is set properly to module.
101 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
102
103 YangSubModule yangNode = (YangSubModule) node;
104 // Check whether the module name is set correctly.
105 assertThat(yangNode.getName(), is("Test"));
106 // Checks identifier of belongsto in data model tree.
107 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
108 // Checks for the version value in data model tree.
109 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
110 }
111
112 /**
113 * Checks if sub module name is correct.
114 */
115 @Test(expected = ParserException.class)
116 public void processSubModuleInvalidName() throws IOException, ParserException {
117
118 YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang");
119 }
120
121 /**
122 * Checks if sub module has invalid modules construct eg namespace.
123 */
124 @Test(expected = ParserException.class)
125 public void processSubModuleWithNamespace() throws IOException, ParserException {
126
127 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang");
128 }
129}