blob: ce347d65a833d19bca4ccb4bf313a9c8a96550d3 [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal88897632016-02-12 18:37:50 +05303 *
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
Bharat saraswalef2e6392016-04-19 19:50:32 +053017package org.onosproject.yangutils.parser.impl.parseutils;
Gaurav Agrawal88897632016-02-12 18:37:50 +053018
Gaurav Agrawala04483c2016-02-13 14:23:40 +053019import org.junit.Rule;
Gaurav Agrawal88897632016-02-12 18:37:50 +053020import org.junit.Test;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053021import org.junit.rules.ExpectedException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053022import org.onosproject.yangutils.datamodel.YangRevision;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangConstructType.YANGBASE_DATA;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053027import 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;
Gaurav Agrawal88897632016-02-12 18:37:50 +053032
Gaurav Agrawal88897632016-02-12 18:37:50 +053033/**
34 * Test case for testing listener validation util.
35 */
36public class ListenerValidationTest {
37
Gaurav Agrawala04483c2016-02-13 14:23:40 +053038 @Rule
39 public ExpectedException thrown = ExpectedException.none();
Gaurav Agrawal88897632016-02-12 18:37:50 +053040
41 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053042 * Checks for exception in case parsable stack is empty while validating for
43 * not empty scenario.
Gaurav Agrawal88897632016-02-12 18:37:50 +053044 */
45 @Test
Gaurav Agrawala04483c2016-02-13 14:23:40 +053046 public void validateStackIsNotEmptyForEmptyStack() {
47
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053048 String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053049
50 // Get the exception occurred during parsing.
51 thrown.expect(ParserException.class);
52 thrown.expectMessage(expectedError);
Gaurav Agrawal88897632016-02-12 18:37:50 +053053
54 // Create test walker and assign test error to it.
55 TreeWalkListener testWalker = new TreeWalkListener();
56
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053057 checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawal88897632016-02-12 18:37:50 +053058 }
59
60 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053061 * Checks if there is no exception in case parsable stack is not empty while
62 * validating for not empty scenario.
Gaurav Agrawal88897632016-02-12 18:37:50 +053063 */
64 @Test
Gaurav Agrawala04483c2016-02-13 14:23:40 +053065 public void validateStackIsNotEmptyForNonEmptyStack() {
Gaurav Agrawal88897632016-02-12 18:37:50 +053066
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
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053074 checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053075 }
Gaurav Agrawal88897632016-02-12 18:37:50 +053076
Gaurav Agrawala04483c2016-02-13 14:23:40 +053077 /**
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
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053084 String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
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
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053097 checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053098 }
99
100 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530101 * Checks if there is no exception in case parsable stack is empty while
102 * validating for empty scenario.
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530103 */
104 @Test
105 public void validateStackIsEmptyForEmptyStack() {
106
107 // Create test walker and assign test error to it.
108 TreeWalkListener testWalker = new TreeWalkListener();
109
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530110 checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawal88897632016-02-12 18:37:50 +0530111 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +0530112}