blob: f4e284ebc5d1a4e17d4d198cc3d70671b65cf3f6 [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +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.parseutils;
18
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.datamodel.YangRevision;
23import org.onosproject.yangutils.parser.exceptions.ParserException;
24import org.onosproject.yangutils.parser.impl.TreeWalkListener;
25
26import static org.onosproject.yangutils.utils.YangConstructType.YANGBASE_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
32
33/**
34 * Test case for testing listener validation util.
35 */
36public class ListenerValidationTest {
37
38 @Rule
39 public ExpectedException thrown = ExpectedException.none();
40
41 /**
42 * Checks for exception in case parsable stack is empty while validating for
43 * not empty scenario.
44 */
45 @Test
46 public void validateStackIsNotEmptyForEmptyStack() {
47
48 String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
49
50 // Get the exception occurred during parsing.
51 thrown.expect(ParserException.class);
52 thrown.expectMessage(expectedError);
53
54 // Create test walker and assign test error to it.
55 TreeWalkListener testWalker = new TreeWalkListener();
56
57 checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
58 }
59
60 /**
61 * Checks if there is no exception in case parsable stack is not empty while
62 * validating for not empty scenario.
63 */
64 @Test
65 public void validateStackIsNotEmptyForNonEmptyStack() {
66
67 // Create test walker and assign test error to it.
68 TreeWalkListener testWalker = new TreeWalkListener();
69
70 // Create a temporary node of parsable.
71 YangRevision tmpNode = new YangRevision();
72 testWalker.getParsedDataStack().push(tmpNode);
73
74 checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
75 }
76
77 /**
78 * Checks for exception in case parsable stack is not empty while validating
79 * for empty scenario.
80 */
81 @Test
82 public void validateStackIsEmptyForNonEmptyStack() {
83
84 String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
85
86 // Get the exception occurred during parsing.
87 thrown.expect(ParserException.class);
88 thrown.expectMessage(expectedError);
89
90 // Create test walker and assign test error to it.
91 TreeWalkListener testWalker = new TreeWalkListener();
92
93 // Create a temporary node of parsable.
94 YangRevision tmpNode = new YangRevision();
95 testWalker.getParsedDataStack().push(tmpNode);
96
97 checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
98 }
99
100 /**
101 * Checks if there is no exception in case parsable stack is empty while
102 * validating for empty scenario.
103 */
104 @Test
105 public void validateStackIsEmptyForEmptyStack() {
106
107 // Create test walker and assign test error to it.
108 TreeWalkListener testWalker = new TreeWalkListener();
109
110 checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
111 }
112}