Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.datamodel.YangMust; |
| 21 | import org.onosproject.yangutils.datamodel.YangModule; |
| 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.util.List; |
| 30 | import java.util.ListIterator; |
| 31 | |
| 32 | import static org.hamcrest.core.Is.is; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | |
| 35 | /** |
| 36 | * Test cases for testing must listener functionality. |
| 37 | */ |
| 38 | public class MustListenerTest { |
| 39 | |
| 40 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 41 | |
| 42 | /** |
| 43 | * Checks if must listener updates the data model. |
| 44 | */ |
| 45 | @Test |
| 46 | public void processContainerSubStatementMust() throws IOException, ParserException { |
| 47 | YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementMust.yang"); |
| 48 | |
| 49 | YangModule yangNode = (YangModule) node; |
| 50 | assertThat(yangNode.getName(), is("Test")); |
| 51 | |
| 52 | YangContainer yangContainer = (YangContainer) yangNode.getChild(); |
| 53 | assertThat(yangContainer.getName(), is("interface")); |
| 54 | |
| 55 | String expectedConstraint = "ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)"; |
| 56 | List<YangMust> mustConstraintList = yangContainer.getListOfMust(); |
| 57 | assertThat(mustConstraintList.iterator().next().getConstraint(), is(expectedConstraint)); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks if must listener updates the data model. |
| 62 | */ |
| 63 | @Test |
| 64 | public void processLeafSubStatementMust() throws IOException, ParserException { |
| 65 | YangNode node = manager.getDataModel("src/test/resources/LeafSubStatementMust.yang"); |
| 66 | |
| 67 | YangModule yangNode = (YangModule) node; |
| 68 | assertThat(yangNode.getName(), is("Test")); |
| 69 | |
| 70 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 71 | YangLeaf leafInfo = leafIterator.next(); |
| 72 | |
| 73 | assertThat(leafInfo.getListOfMust().iterator().next().getConstraint(), is("ifType != 'ethernet'")); |
| 74 | } |
Vidyashree Rama | 052ada6 | 2016-08-17 14:03:29 +0530 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Checks whether there is no parser exception when must statement is sub-statement of |
| 78 | * unsupported yang construct. |
| 79 | */ |
| 80 | @Test |
| 81 | public void processUnsupportedSubStatementMust() throws IOException, ParserException { |
| 82 | YangNode node = manager.getDataModel("src/test/resources/UnsupportedYangConstructSubStatementMust.yang"); |
| 83 | } |
Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 84 | } |