blob: 66d8e9af382912fe1b745f63736e9ff7e4414e50 [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
Vidyashree Rama468f8282016-03-04 19:08:35 +053019import org.junit.Rule;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053020import org.junit.Test;
Vidyashree Rama468f8282016-03-04 19:08:35 +053021import org.junit.rules.ExpectedException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053022import org.onosproject.yangutils.datamodel.YangModule;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangNodeType;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
27
28import java.io.IOException;
29
30import static org.hamcrest.core.Is.is;
31import static org.junit.Assert.assertThat;
32
33/**
34 * Test cases for testing module listener functionality.
35 */
36public class ModuleListenerTest {
37
38 private final YangUtilsParserManager manager = new YangUtilsParserManager();
39
Vidyashree Rama468f8282016-03-04 19:08:35 +053040 @Rule
41 public ExpectedException thrown = ExpectedException.none();
42
Gaurav Agrawala04483c2016-02-13 14:23:40 +053043 /**
44 * Checks if module listener updates the data model root node.
45 */
46 @Test
47 public void processModuleValidEntry() throws IOException, ParserException {
48
49 YangNode node = manager.getDataModel("src/test/resources/ModuleValidEntry.yang");
50
51 // Check whether the data model tree returned is of type module.
52 assertThat((node instanceof YangModule), is(true));
53
54 // Check whether the node type is set properly to module.
55 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
56
57 // Check whether the module name is set correctly.
58 YangModule yangNode = (YangModule) node;
59 assertThat(yangNode.getName(), is("Test"));
60 }
61
62 /**
63 * Checks if module name is set correctly.
64 */
65 @Test(expected = ParserException.class)
66 public void processModuleInvalidEntryTest() throws IOException, ParserException {
67
68 YangNode node = manager.getDataModel("src/test/resources/ModuleWithInvalidIdentifier.yang");
69 }
Vidyashree Rama468f8282016-03-04 19:08:35 +053070
71 /**
72 * Checks whether exception is thrown when module length is greater than 64 characters.
73 */
74 @Test
75 public void processModuleInvalidIdentifierLength() throws IOException, ParserException {
76 thrown.expect(ParserException.class);
77 thrown.expectMessage("YANG file error : module name Testttttttttttttttttttttttttttttttttttttttttttttttttttt" +
78 "tttttttttt is greater than 64 characters.");
79 YangNode node = manager.getDataModel("src/test/resources/ModuleInvalidIdentifierLength.yang");
80 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081}