blob: 2649d4da3674e5c233ea109130b168d9b23f19e8 [file] [log] [blame]
Vidyashree Ramadeac28b2016-06-20 15:12:43 +05301/*
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.junit.Test;
20import org.onosproject.yangutils.datamodel.YangMust;
21import org.onosproject.yangutils.datamodel.YangModule;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangContainer;
24import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
27
28import java.io.IOException;
29import java.util.List;
30import java.util.ListIterator;
31
32import static org.hamcrest.core.Is.is;
33import static org.junit.Assert.assertThat;
34
35/**
36 * Test cases for testing must listener functionality.
37 */
38public 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 }
75}