blob: b52da4a617fef95e532c56b29af0c082379b60ec [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 import listener functionality.
33 */
34public class ImportListenerTest {
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 mandatory parameter prefix is present in import.
42 */
43 @Test(expected = ParserException.class)
44 public void processImportWithoutPrefix() throws IOException, ParserException {
45
46 YangNode node = manager.getDataModel("src/test/resources/ImportWithoutPrefix.yang");
47 }
48
49 /**
50 * Checks that prefix must be present only once in import.
51 */
52 @Test(expected = ParserException.class)
53 public void processImportWithDualPrefix() throws IOException, ParserException {
54
55 YangNode node = manager.getDataModel("src/test/resources/ImportWithDualPrefix.yang");
56 }
57
58 /**
59 * Checks for the correct order of prefix in import.
60 */
61 @Test(expected = ParserException.class)
62 public void processImportInvalidOrder() throws IOException, ParserException {
63
64 YangNode node = manager.getDataModel("src/test/resources/ImportInvalidOrder.yang");
65 }
66
67 /**
68 * Checks if import listener updates the data model tree.
69 */
70 @Test
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053071 public void processImportValidEntry() throws IOException, ParserException, ParseException {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072
73 YangNode node = manager.getDataModel("src/test/resources/ImportValidEntry.yang");
74
75 // Checks for the revision value in data model tree.
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053076 assertThat(((YangModule) node).getImportList().get(0).getRevision(), is(simpleDateFormat.parse("2015-02-03")));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053077 // Checks for the prefix id in data model tree.
78 assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
79 // Checks for the module name in data model tree.
80 assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
81 }
82
83 /**
84 * Checks if optional parameter revision is not mandatory in import.
85 */
86 @Test
87 public void processImportWithoutRevision() throws IOException, ParserException {
88
89 YangNode node = manager.getDataModel("src/test/resources/ImportWithoutRevision.yang");
90
91 // Checks for the prefix id in data model tree.
92 assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
93 // Checks for the module name in data model tree.
94 assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
95 }
96
97 /**
98 * Checks if multiple imports are allowed.
99 */
100 @Test()
101 public void processImportMultipleInstance() throws IOException, ParserException {
102
103 YangNode node = manager.getDataModel("src/test/resources/ImportMultipleInstance.yang");
104
105 // Checks for the prefix id in data model tree.
106 assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
107 // Checks for the module name in data model tree.
108 assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
109
110 // Checks for the prefix id in data model tree.
111 assertThat(((YangModule) node).getImportList().get(1).getPrefixId(), is("On3"));
112 // Checks for the module name in data model tree.
113 assertThat(((YangModule) node).getImportList().get(1).getModuleName(), is("itut"));
114 }
115}