Vidyashree Rama | 0b92073 | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 0b92073 | 2016-03-29 09:52:22 +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 | |
| 20 | import java.io.IOException; |
| 21 | import java.util.ListIterator; |
| 22 | |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 27 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
| 28 | import org.onosproject.yangutils.datamodel.YangRangeInterval; |
| 29 | import org.onosproject.yangutils.datamodel.YangModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 31 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 34 | |
| 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.core.Is.is; |
| 37 | |
| 38 | /** |
| 39 | * Test cases for range restriction listener. |
| 40 | */ |
| 41 | public class RangeRestrictionListenerTest { |
| 42 | |
| 43 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 44 | |
| 45 | /** |
| 46 | * Checks valid range statement as sub-statement of leaf statement. |
| 47 | */ |
| 48 | @Test |
| 49 | public void processValidRangeStatement() throws IOException, ParserException { |
| 50 | |
| 51 | YangNode node = manager.getDataModel("src/test/resources/ValidRangeStatement.yang"); |
| 52 | |
| 53 | assertThat((node instanceof YangModule), is(true)); |
| 54 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 55 | YangModule yangNode = (YangModule) node; |
| 56 | assertThat(yangNode.getName(), is("Test")); |
| 57 | |
| 58 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 59 | YangLeaf leafInfo = leafIterator.next(); |
| 60 | |
| 61 | assertThat(leafInfo.getLeafName(), is("invalid-interval")); |
| 62 | assertThat(leafInfo.getDataType().getDataTypeName(), is("int32")); |
| 63 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32)); |
| 64 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafInfo |
| 65 | .getDataType().getDataTypeExtendedInfo(); |
| 66 | |
| 67 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 68 | .listIterator(); |
| 69 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 70 | assertThat(rangeInterval.getStartValue(), is(10)); |
| 71 | assertThat(rangeInterval.getEndValue(), is(20)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks valid range statement as sub-statement of leaf-list. |
| 76 | */ |
| 77 | @Test |
| 78 | public void processRangeStatementInsideLeafList() throws IOException, ParserException { |
| 79 | |
| 80 | YangNode node = manager.getDataModel("src/test/resources/RangeStatementInsideLeafList.yang"); |
| 81 | |
| 82 | assertThat((node instanceof YangModule), is(true)); |
| 83 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 84 | YangModule yangNode = (YangModule) node; |
| 85 | assertThat(yangNode.getName(), is("Test")); |
| 86 | |
| 87 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 88 | YangLeafList leafListInfo = leafListIterator.next(); |
| 89 | |
| 90 | assertThat(leafListInfo.getLeafName(), is("invalid-interval")); |
| 91 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("int32")); |
| 92 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.INT32)); |
| 93 | YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafListInfo |
| 94 | .getDataType().getDataTypeExtendedInfo(); |
| 95 | |
| 96 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 97 | .listIterator(); |
| 98 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 99 | |
| 100 | assertThat(rangeInterval.getStartValue(), is(10)); |
| 101 | assertThat(rangeInterval.getEndValue(), is(20)); |
| 102 | } |
| 103 | } |