blob: ea5c6e657275beb926b8691529b3841903c172f9 [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
2 * Copyright 2016 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.Rule;
20import org.junit.Test;
21
22import org.junit.rules.ExpectedException;
23import org.onosproject.yangutils.datamodel.YangModule;
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangNodeType;
26import org.onosproject.yangutils.datamodel.YangContainer;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
29
30import java.io.IOException;
31
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.is;
34import static org.hamcrest.Matchers.nullValue;
35
36/**
37 * Test cases for presence listener.
38 */
39public class PresenceListenerTest {
40
41 @Rule
42 public ExpectedException thrown = ExpectedException.none();
43
44 private final YangUtilsParserManager manager = new YangUtilsParserManager();
45
46 /**
47 * Checks presence statement as sub-statement of container.
48 */
49 @Test
50 public void processContainerSubStatementPresence() throws IOException, ParserException {
51
52 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementPresence.yang");
53
54 assertThat((node instanceof YangModule), is(true));
55
56 // Check whether the node type is set properly to module.
57 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
58
59 // Check whether the module name is set correctly.
60 YangModule yangNode = (YangModule) node;
61 assertThat(yangNode.getName(), is("Test"));
62
63 // Check whether the list is child of module
64 YangContainer yangContainer = (YangContainer) yangNode.getChild();
65 assertThat(yangContainer.getName(), is("valid"));
66 assertThat(yangContainer.getPresence(), is("\"invalid\""));
67 }
68
69 /**
70 * checks default value of presence statement.
71 */
72 @Test
73 public void processPresenceDefaultValue() throws IOException, ParserException {
74
75 YangNode node = manager.getDataModel("src/test/resources/PresenceDefaultValue.yang");
76
77 assertThat((node instanceof YangModule), is(true));
78
79 // Check whether the node type is set properly to module.
80 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
81
82 // Check whether the module name is set correctly.
83 YangModule yangNode = (YangModule) node;
84 assertThat(yangNode.getName(), is("Test"));
85
86 // Check whether the list is child of module
87 YangContainer yangContainer = (YangContainer) yangNode.getChild();
88 assertThat(yangContainer.getName(), is("valid"));
89 assertThat(yangContainer.getPresence(), is(nullValue()));
90 }
91
92 /**
93 * Checks presence statement without statement end.
94 */
95 @Test
96 public void processPresenceWithoutStatementEnd() throws IOException, ParserException {
97 thrown.expect(ParserException.class);
98 thrown.expectMessage("mismatched input 'leaf' expecting {';', '+'}");
99 YangNode node = manager.getDataModel("src/test/resources/PresenceWithoutStatementEnd.yang");
100 }
101}