Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 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.parseutils; |
| 18 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.junit.Rule; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 20 | import org.junit.Test; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 21 | import org.junit.rules.ExpectedException; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangRevision; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.YangConstructType.YANGBASE_DATA; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 32 | |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 33 | /** |
| 34 | * Test case for testing listener validation util. |
| 35 | */ |
| 36 | public class ListenerValidationTest { |
| 37 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 38 | @Rule |
| 39 | public ExpectedException thrown = ExpectedException.none(); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 42 | * Checks for exception in case parsable stack is empty while validating for |
| 43 | * not empty scenario. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 44 | */ |
| 45 | @Test |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 46 | public void validateStackIsNotEmptyForEmptyStack() { |
| 47 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 48 | String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 49 | |
| 50 | // Get the exception occurred during parsing. |
| 51 | thrown.expect(ParserException.class); |
| 52 | thrown.expectMessage(expectedError); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 53 | |
| 54 | // Create test walker and assign test error to it. |
| 55 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 56 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 57 | checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 61 | * Checks if there is no exception in case parsable stack is not empty while |
| 62 | * validating for not empty scenario. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 63 | */ |
| 64 | @Test |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 65 | public void validateStackIsNotEmptyForNonEmptyStack() { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 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 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 74 | checkStackIsNotEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 75 | } |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 76 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 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 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 84 | String expectedError = constructListenerErrorMessage(MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 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 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 97 | checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 101 | * Checks if there is no exception in case parsable stack is empty while |
| 102 | * validating for empty scenario. |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 103 | */ |
| 104 | @Test |
| 105 | public void validateStackIsEmptyForEmptyStack() { |
| 106 | |
| 107 | // Create test walker and assign test error to it. |
| 108 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 109 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 110 | checkStackIsEmpty(testWalker, MISSING_HOLDER, YANGBASE_DATA, "", EXIT); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 111 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 112 | } |