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 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 25 | import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher; |
| 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 belongsto listener functionality. |
| 35 | */ |
| 36 | public class BelongstoListenerTest { |
| 37 | |
| 38 | @Rule |
| 39 | public ExpectedException thrown = ExpectedException.none(); |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks if mandatory belongsto parameter "prefix" is not present. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processBelongsToWithoutPrefix() throws IOException, ParserException { |
| 48 | thrown.expect(ParserException.class); |
| 49 | thrown.expectMessage("mismatched input '}' expecting 'prefix'"); |
| 50 | thrown.expect(CustomExceptionMatcher.errorLocation(4, 0)); |
| 51 | YangNode node = manager.getDataModel("src/test/resources/BelongsToWithoutPrefix.yang"); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Checks that prefix must be present only once in belongsto. |
| 56 | */ |
| 57 | @Test |
| 58 | public void processBelongsToDualPrefix() throws IOException, ParserException { |
| 59 | thrown.expect(ParserException.class); |
| 60 | thrown.expectMessage("mismatched input 'prefix' expecting '}'"); |
| 61 | thrown.expect(CustomExceptionMatcher.errorLocation(5, 0)); |
| 62 | YangNode node = manager.getDataModel("src/test/resources/BelongsToDualPrefix.yang"); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Checks if belongsto listener updates the date model tree. |
| 67 | */ |
| 68 | @Test |
| 69 | public void processBelongsToWithPrefix() throws IOException, ParserException { |
| 70 | |
| 71 | YangNode node = manager.getDataModel("src/test/resources/BelongsToWithPrefix.yang"); |
| 72 | YangSubModule yangNode = (YangSubModule) node; |
| 73 | assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS")); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks if mandatory parameter "belongsto" is present. |
| 78 | */ |
| 79 | @Test |
| 80 | public void processSubModuleWithoutBelongsTo() throws IOException, ParserException { |
| 81 | thrown.expect(ParserException.class); |
| 82 | thrown.expectMessage("mismatched input '}' expecting 'belongs-to'"); |
| 83 | thrown.expect(CustomExceptionMatcher.errorLocation(3, 0)); |
| 84 | YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutBelongsTo.yang"); |
| 85 | } |
| 86 | } |