blob: 9b3effc6faf3bcb8dfa4620059a6886a2e9a2d4e [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
17package org.onosproject.yangutils.parser.impl;
18
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.parser.exceptions.ParserException;
24
25import java.io.BufferedWriter;
26import java.io.File;
27import java.io.FileWriter;
28import java.io.IOException;
29
30/**
31 * Test case for testing YANG utils parser manager.
32 */
33public 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 */
Vidyashree Rama74453712016-04-18 12:29:39 +053063 @Test(expected = ParserException.class)
Gaurav Agrawal88897632016-02-12 18:37:50 +053064 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}