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.impl; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 24 | |
| 25 | import java.io.BufferedWriter; |
| 26 | import java.io.File; |
| 27 | import java.io.FileWriter; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | /** |
| 31 | * Test case for testing YANG utils parser manager. |
| 32 | */ |
| 33 | public class YangUtilsParserManagerTest { |
| 34 | |
| 35 | YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 36 | File file; |
| 37 | BufferedWriter out; |
| 38 | |
| 39 | @Before |
| 40 | public void setUp() throws Exception { |
| 41 | file = new File("demo.yang"); |
| 42 | out = new BufferedWriter(new FileWriter(file)); |
| 43 | } |
| 44 | @After |
| 45 | public void tearDown() throws Exception { |
| 46 | file.delete(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This test case checks whether the null pointer exception is generated |
| 51 | * when the input YANG file is null. |
| 52 | */ |
| 53 | @Test(expected = NullPointerException.class) |
| 54 | public void getDataModelNullFileTest() throws IOException, ParserException { |
| 55 | YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 56 | YangNode node = manager.getDataModel(null); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * This test case checks whether the io exception is generated |
| 61 | * when the input YANG file is non existent. |
| 62 | */ |
| 63 | @Test(expected = IOException.class) |
| 64 | public void getDataModelNonExistentFileTest() throws IOException, ParserException { |
| 65 | |
| 66 | YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 67 | YangNode node = manager.getDataModel("nonexistent.yang"); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * This test case checks if the input YANG file is correct no exception |
| 72 | * should be generated. |
| 73 | */ |
| 74 | @Test |
| 75 | public void getDataModelCorrectFileTest() throws IOException, ParserException { |
| 76 | |
| 77 | out.write("module ONOS {\n"); |
| 78 | out.write("yang-version 1;\n"); |
| 79 | out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n"); |
| 80 | out.write("prefix On;\n"); |
| 81 | out.write("}\n"); |
| 82 | out.close(); |
| 83 | |
| 84 | YangNode node = manager.getDataModel("demo.yang"); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * This test case checks if the input YANG file with wrong YANG constructs |
| 89 | * than parser exception should be generated. |
| 90 | */ |
| 91 | @Test(expected = ParserException.class) |
| 92 | public void getDataModelIncorrectFileTest() throws IOException, ParserException { |
| 93 | |
| 94 | out.write("module ONOS {\n"); |
| 95 | out.write("yang-version 1\n"); |
| 96 | out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n"); |
| 97 | out.write("prefix On;\n"); |
| 98 | out.write("}\n"); |
| 99 | out.close(); |
| 100 | |
| 101 | YangNode node = manager.getDataModel("demo.yang"); |
| 102 | } |
| 103 | } |