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 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.datamodel.YangRevision; |
| 21 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 22 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerError; |
| 23 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
| 24 | |
| 25 | import static org.hamcrest.core.Is.is; |
| 26 | import static org.junit.Assert.assertThat; |
| 27 | |
| 28 | /** |
| 29 | * Test case for testing listener validation util. |
| 30 | */ |
| 31 | public class ListenerValidationTest { |
| 32 | |
| 33 | /** |
| 34 | * This test case checks in case error pre-exists, listener validate |
| 35 | * function returns true. |
| 36 | */ |
| 37 | @Test |
| 38 | public void listenerValidationErrorExists() { |
| 39 | |
| 40 | // Create an test error. |
| 41 | ListenerError testError = new ListenerError(); |
| 42 | testError.setErrorFlag(true); |
| 43 | testError.setErrorMsg("Test Error"); |
| 44 | |
| 45 | // Create test walker and assign test error to it. |
| 46 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 47 | testWalker.setErrorInformation(testError); |
| 48 | |
| 49 | // Create a temporary node of parsable. |
| 50 | YangRevision tmpNode = new YangRevision(); |
| 51 | testWalker.getParsedDataStack().push(tmpNode); |
| 52 | |
| 53 | boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest"); |
| 54 | |
| 55 | /** |
| 56 | * Check for the values set in syntax error function. If not set properly |
| 57 | * report an assert. |
| 58 | */ |
| 59 | assertThat(errorFlag, is(true)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This test case checks in case parsable stack is empty, listener validate |
| 64 | * function returns true. |
| 65 | */ |
| 66 | @Test |
| 67 | public void listenerValidationEmptyStack() { |
| 68 | |
| 69 | // Create test walker and assign test error to it. |
| 70 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 71 | |
| 72 | boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest"); |
| 73 | |
| 74 | /** |
| 75 | * Check for the values set in syntax error function. If not set properly |
| 76 | * report an assert. |
| 77 | */ |
| 78 | assertThat(errorFlag, is(true)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * This test case checks in case of error doesn't pre-exists and stack is, |
| 83 | * non empty, listener validate function returns false. |
| 84 | */ |
| 85 | @Test |
| 86 | public void listenerValidationNoErrorNotExists() { |
| 87 | |
| 88 | // Create test walker and assign test error to it. |
| 89 | TreeWalkListener testWalker = new TreeWalkListener(); |
| 90 | |
| 91 | // Create a temporary node of parsable. |
| 92 | YangRevision tmpNode = new YangRevision(); |
| 93 | testWalker.getParsedDataStack().push(tmpNode); |
| 94 | |
| 95 | boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest"); |
| 96 | |
| 97 | /** |
| 98 | * Check for the values set in syntax error function. If not set properly |
| 99 | * report an assert. |
| 100 | */ |
| 101 | assertThat(errorFlag, is(false)); |
| 102 | } |
| 103 | } |