Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 19 | import org.junit.Rule; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 20 | import org.junit.Test; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 21 | import org.junit.rules.ExpectedException; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import static org.hamcrest.core.Is.is; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | |
| 33 | /** |
| 34 | * Test cases for testing module listener functionality. |
| 35 | */ |
| 36 | public class ModuleListenerTest { |
| 37 | |
| 38 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 39 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 40 | @Rule |
| 41 | public ExpectedException thrown = ExpectedException.none(); |
| 42 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 43 | /** |
| 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 Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 70 | |
| 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 Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 81 | } |