Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 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.ParsableDataType; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
| 30 | |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 31 | /** |
| 32 | * Test case for testing listener validation util. |
| 33 | */ |
| 34 | public class ListenerValidationTest { |
| 35 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 36 | @Rule |
| 37 | public ExpectedException thrown = ExpectedException.none(); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 38 | |
| 39 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 40 | * Checks for exception in case parsable stack is empty while validating for |
| 41 | * not empty scenario. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 42 | */ |
| 43 | @Test |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 44 | public void validateStackIsNotEmptyForEmptyStack() { |
| 45 | |
| 46 | String expectedError = ListenerErrorMessageConstruction |
| 47 | .constructListenerErrorMessage(ListenerErrorType.MISSING_HOLDER, ParsableDataType.YANGBASE_DATA, "", |
| 48 | ListenerErrorLocation.EXIT); |
| 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 | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 57 | ListenerValidation.checkStackIsNotEmpty(testWalker, ListenerErrorType.MISSING_HOLDER, |
| 58 | ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 62 | * Checks if there is no exception in case parsable stack is not empty while validating |
| 63 | * for not empty scenario. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 64 | */ |
| 65 | @Test |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 66 | public void validateStackIsNotEmptyForNonEmptyStack() { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 67 | |
| 68 | // Create test walker and assign test error to it. |
| 69 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 70 | |
| 71 | // Create a temporary node of parsable. |
| 72 | YangRevision tmpNode = new YangRevision(); |
| 73 | testWalker.getParsedDataStack().push(tmpNode); |
| 74 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 75 | ListenerValidation.checkStackIsNotEmpty(testWalker, ListenerErrorType.MISSING_HOLDER, |
| 76 | ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT); |
| 77 | } |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 78 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 79 | /** |
| 80 | * Checks for exception in case parsable stack is not empty while validating |
| 81 | * for empty scenario. |
| 82 | */ |
| 83 | @Test |
| 84 | public void validateStackIsEmptyForNonEmptyStack() { |
| 85 | |
| 86 | String expectedError = ListenerErrorMessageConstruction |
| 87 | .constructListenerErrorMessage(ListenerErrorType.MISSING_HOLDER, ParsableDataType.YANGBASE_DATA, "", |
| 88 | ListenerErrorLocation.EXIT); |
| 89 | |
| 90 | // Get the exception occurred during parsing. |
| 91 | thrown.expect(ParserException.class); |
| 92 | thrown.expectMessage(expectedError); |
| 93 | |
| 94 | // Create test walker and assign test error to it. |
| 95 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 96 | |
| 97 | // Create a temporary node of parsable. |
| 98 | YangRevision tmpNode = new YangRevision(); |
| 99 | testWalker.getParsedDataStack().push(tmpNode); |
| 100 | |
| 101 | ListenerValidation.checkStackIsEmpty(testWalker, ListenerErrorType.MISSING_HOLDER, |
| 102 | ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Checks if there is no exception in case parsable stack is empty while validating |
| 107 | * for empty scenario. |
| 108 | */ |
| 109 | @Test |
| 110 | public void validateStackIsEmptyForEmptyStack() { |
| 111 | |
| 112 | // Create test walker and assign test error to it. |
| 113 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 114 | |
| 115 | ListenerValidation.checkStackIsEmpty(testWalker, ListenerErrorType.MISSING_HOLDER, |
| 116 | ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 117 | } |
| 118 | } |