blob: e2bb23994b5034d8f97717974d6b68cdc16189ae [file] [log] [blame]
Gaurav Agrawala04483c2016-02-13 14:23:40 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawala04483c2016-02-13 14:23:40 +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.listeners;
18
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053019import java.io.IOException;
20import java.text.ParseException;
21import java.text.SimpleDateFormat;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053022import org.junit.Test;
23import org.onosproject.yangutils.datamodel.YangModule;
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
27
Gaurav Agrawala04483c2016-02-13 14:23:40 +053028import static org.hamcrest.core.Is.is;
29import static org.junit.Assert.assertThat;
30
31/**
32 * Test cases for testing include listener functionality.
33 */
34public class IncludeListenerTest {
35
36 private final YangUtilsParserManager manager = new YangUtilsParserManager();
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053037 private static final String DATE_FORMAT = "yyyy-MM-dd";
38 private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053039
40 /**
41 * Checks if include listener with ; is valid and updates the data
42 * model tree.
43 */
44 @Test
45 public void processIncludeWithStmtend() throws IOException, ParserException {
46
47 YangNode node = manager.getDataModel("src/test/resources/IncludeWithStmtend.yang");
48
49 // Checks for the sub module name in data model tree.
50 assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
51 }
52
53 /**
54 * Checks if include listener with braces and without revision date is valid
55 * and updates the data model tree.
56 */
57 @Test
58 public void processIncludeWithEmptyBody() throws IOException, ParserException {
59
60 YangNode node = manager.getDataModel("src/test/resources/IncludeWithEmptyBody.yang");
61
62 // Checks for the sub module name in data model tree.
63 assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
64 }
65
66 /**
67 * Checks if include listener with braces and with revision date is valid
68 * and updates the data model tree.
69 */
70 @Test
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053071 public void processIncludeWithDate() throws IOException, ParserException, ParseException {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072
73 YangNode node = manager.getDataModel("src/test/resources/IncludeWithDate.yang");
74
75 // Checks for the sub module name in data model tree.
76 assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053077 assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078 }
79
80 /**
81 * Checks if include has more than one occurrence.
82 */
83 @Test
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053084 public void processIncludeMultiInstance() throws IOException, ParserException, ParseException {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085
86 YangNode node = manager.getDataModel("src/test/resources/IncludeMultiInstance.yang");
87
88 // Checks for the sub module name in data model tree.
89 assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053090 assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091 assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053092 assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is(simpleDateFormat.parse("2014-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053093 }
94
95 /**
96 * Checks if include and import can come in any order.
97 */
98 @Test
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053099 public void processIncludeImportAnyOrder() throws IOException, ParserException, ParseException {
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100
101 YangNode node = manager.getDataModel("src/test/resources/IncludeImportAnyOrder.yang");
102
103 // Checks for the sub module name in data model tree.
104 assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530105 assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is(simpleDateFormat.parse("2016-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530106 assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530107 assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is(simpleDateFormat.parse("2014-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530108 }
109
110 /**
111 * Checks if syntax of Include is not correct.
112 */
113 @Test(expected = ParserException.class)
114 public void processIncludeInvalidSyntax() throws IOException, ParserException {
115
116 YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidSyntax.yang");
117 }
118
119 /**
120 * Checks if syntax of revision date in Include is not correct.
121 */
122 @Test(expected = ParserException.class)
123 public void processIncludeInvalidDateSyntax() throws IOException, ParserException {
124
125 YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidDateSyntax.yang");
126 }
127}