blob: 6a53cbe71fe7c18d3d47471416a706f583519674 [file] [log] [blame]
Gaurav Agrawala04483c2016-02-13 14:23:40 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawala04483c2016-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;
janani bcc9ac302016-03-24 12:43:48 +053029import static org.hamcrest.core.IsNull.notNullValue;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053030import static org.junit.Assert.assertThat;
31
32/**
33 * Test cases for testing submodule listener functionality.
34 */
35public class SubModuleListenerTest {
36
37 private final YangUtilsParserManager manager = new YangUtilsParserManager();
38
39 /**
40 * Checks if the sub module listeners updates the data model tree.
41 */
42 @Test
43 public void processSubModuleValidEntry() throws IOException, ParserException {
44
45 YangNode node = manager.getDataModel("src/test/resources/SubModuleValidEntry.yang");
46
47 // Check whether the data model tree returned is of type module.
48 assertThat((node instanceof YangSubModule), is(true));
49
50 // Check whether the node type is set properly to module.
51 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
52
53 YangSubModule yangNode = (YangSubModule) node;
54 // Check whether the module name is set correctly.
55 assertThat(yangNode.getName(), is("Test"));
56 // Checks for the version value in data model tree.
57 assertThat(yangNode.getVersion(), is((byte) 1));
58 // Checks identifier of belongsto in data model tree.
59 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
60 // Checks for the version value in data model tree.
61 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
62 }
63
64 /**
65 * Checks if the yang version and belongs to can come in any order in sub
66 * module.
67 */
68 @Test
69 public void processSubModuleOrder() throws IOException, ParserException {
70
71 YangNode node = manager.getDataModel("src/test/resources/SubModuleOrder.yang");
72
73 // Check whether the data model tree returned is of type module.
74 assertThat((node instanceof YangSubModule), is(true));
75
76 // Check whether the node type is set properly to module.
77 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
78
79 YangSubModule yangNode = (YangSubModule) node;
80 // Check whether the module name is set correctly.
81 assertThat(yangNode.getName(), is("Test"));
82 // Checks for the version value in data model tree.
83 assertThat(yangNode.getVersion(), is((byte) 1));
84 // Checks identifier of belongsto in data model tree.
85 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
86 // Checks for the version value in data model tree.
87 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
88 }
89
90 /**
91 * Checks if yang version is optional.
92 */
93 @Test
94 public void processSubModuleWithoutVersion() throws IOException, ParserException {
95
96 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutVersion.yang");
97
98 // Check whether the data model tree returned is of type module.
99 assertThat((node instanceof YangSubModule), is(true));
100
101 // Check whether the node type is set properly to module.
102 assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
103
104 YangSubModule yangNode = (YangSubModule) node;
105 // Check whether the module name is set correctly.
106 assertThat(yangNode.getName(), is("Test"));
107 // Checks identifier of belongsto in data model tree.
108 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
109 // Checks for the version value in data model tree.
110 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
janani bcc9ac302016-03-24 12:43:48 +0530111 //Checks the revision with current date is created for empty revision statement.
janani bde4ffab2016-04-15 16:18:30 +0530112 assertThat(((YangSubModule) node).getRevision().getRevDate(), notNullValue());
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530113 }
114
115 /**
116 * Checks if sub module name is correct.
117 */
118 @Test(expected = ParserException.class)
119 public void processSubModuleInvalidName() throws IOException, ParserException {
120
121 YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang");
122 }
123
124 /**
125 * Checks if sub module has invalid modules construct eg namespace.
126 */
127 @Test(expected = ParserException.class)
128 public void processSubModuleWithNamespace() throws IOException, ParserException {
129
130 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang");
131 }
132}