blob: 321094847e121a56682776d99b5eb4afc5f32818 [file] [log] [blame]
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +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 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"));
janani ba3027492016-03-24 12:43:48 +0530110 //Checks the revision with current date is created for empty revision statement.
Bharat saraswal1edde622016-09-06 10:18:04 +0530111 assertThat(true, is(node.getRevision() == null));
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530112 }
113
114 /**
115 * Checks if sub module name is correct.
116 */
117 @Test(expected = ParserException.class)
118 public void processSubModuleInvalidName() throws IOException, ParserException {
119
120 YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang");
121 }
122
123 /**
124 * Checks if sub module has invalid modules construct eg namespace.
125 */
126 @Test(expected = ParserException.class)
127 public void processSubModuleWithNamespace() throws IOException, ParserException {
128
129 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang");
130 }
131}