blob: a38cf12fc943180a812f69cd9bf5fa93dffc872b [file] [log] [blame]
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +05301/*
2 * Copyright 2016-present 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
17package org.onosproject.yangutils.plugin.manager;
18
19import java.io.IOException;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Map;
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053023
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053024import org.apache.maven.plugin.MojoExecutionException;
25import org.junit.Test;
26import org.onosproject.yangutils.datamodel.YangLeaf;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053029import org.onosproject.yangutils.datamodel.YangSchemaNode;
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053030import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo;
31import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053032import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053033import org.onosproject.yangutils.parser.exceptions.ParserException;
34import org.onosproject.yangutils.utils.io.YangPluginConfig;
35import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
36
37import static org.hamcrest.CoreMatchers.notNullValue;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053038import static org.hamcrest.CoreMatchers.nullValue;
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053039import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.core.Is.is;
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils
42 .deleteDirectory;
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053043
44/**
45 * Test cases for testing YANG schema node.
46 */
47public class SchemaNodeTest {
48
49 private final YangUtilManager utilManager = new YangUtilManager();
50
51 /**
52 * Checks method to get schema node from map.
53 *
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053054 * @throws IOException a violation in IO rule
55 * @throws ParserException a violation in parser rule
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053056 * @throws MojoExecutionException a violation in mojo rule
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053057 * @throws DataModelException a violation in data model rule
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053058 */
59 @Test
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053060 public void processSchemaNodeMap()
61 throws IOException, ParserException,
62 MojoExecutionException, DataModelException {
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053063
64 String searchDir = "src/test/resources/schemaMap";
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053065 utilManager
66 .createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053067 utilManager.parseYangFileInfoSet();
68 utilManager.createYangNodeSet();
69 utilManager.resolveDependenciesUsingLinker();
70 YangPluginConfig yangPluginConfig = new YangPluginConfig();
71 yangPluginConfig.setCodeGenDir("target/schemaMap/");
72 utilManager.translateToJava(yangPluginConfig);
73
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053074 Iterator<YangNode> yangNodeIterator =
75 utilManager.getYangNodeSet().iterator();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +053076 YangNode rootNode = yangNodeIterator.next();
77
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053078 // Validate the notification enum map
79 assertThat(rootNode.getChild().getNextSibling(),
80 is(rootNode.getNotificationSchemaNode("TESTNOTIFICATION1")));
81
82 // Validate the notification enum map shouldn't have container
83 assertThat(rootNode.getNotificationSchemaNode("TESTCONTAINER"),
84 is(nullValue()));
85
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +053086 // Validation for RPC input/output node.
87 YangNode yangRpcNode = rootNode.getChild().getNextSibling()
88 .getNextSibling();
89 YangSchemaNodeIdentifier yangInputNode = new YangSchemaNodeIdentifier();
90 yangInputNode.setName("input");
91 yangInputNode.setNameSpace("http://huawei.com");
92 assertThat(yangRpcNode.getChildSchema(yangInputNode).getSchemaNode(),
93 is(yangRpcNode.getChild()));
94
95 YangSchemaNodeIdentifier yangOutputNode = new
96 YangSchemaNodeIdentifier();
97 yangOutputNode.setName("output");
98 yangOutputNode.setNameSpace("http://huawei.com");
99 assertThat(yangRpcNode.getChildSchema(yangOutputNode).getSchemaNode(),
100 is(yangRpcNode.getChild().getNextSibling()));
101
102 // Validate the input schema map
103 YangSchemaNode yangInput = yangRpcNode.getChild();
104 YangSchemaNodeIdentifier yangInputLeafNode = new
105 YangSchemaNodeIdentifier();
106 yangInputLeafNode.setName("image-name");
107 yangInputLeafNode.setNameSpace("http://huawei.com");
108 assertThat(yangInput.getChildSchema(yangInputLeafNode),
109 is(notNullValue()));
110
111 YangSchemaNode yangOutput = yangRpcNode.getChild().getNextSibling();
112 YangSchemaNodeIdentifier yangOutputLeafNode = new
113 YangSchemaNodeIdentifier();
114 yangOutputLeafNode.setName("image-name");
115 yangOutputLeafNode.setNameSpace("http://huawei.com");
116 assertThat(yangOutput.getChildSchema(yangOutputLeafNode),
117 is(notNullValue()));
118
119 // Validate schema node
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530120 assertThat(rootNode.getYsnContextInfoMap(), is(notNullValue()));
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530121 Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap =
122 rootNode.getYsnContextInfoMap();
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530123 YangSchemaNodeIdentifier yangSchemaNodeIdentifier =
124 new YangSchemaNodeIdentifier();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530125 yangSchemaNodeIdentifier.setName("testcontainer");
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530126 yangSchemaNodeIdentifier.setNameSpace("http://huawei.com");
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530127 assertThat(schemaMap.get(yangSchemaNodeIdentifier), is(notNullValue()));
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530128 YangSchemaNodeContextInfo yangSchemaNodeContextInfo =
129 schemaMap.get(yangSchemaNodeIdentifier);
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530130 assertThat(yangSchemaNodeContextInfo.getSchemaNode(),
131 is(rootNode.getChild()));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530132
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530133 assertThat(rootNode.getChild().getYsnContextInfoMap(),
134 is(notNullValue()));
135 Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap2 =
136 rootNode.getChild()
137 .getYsnContextInfoMap();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530138 yangSchemaNodeIdentifier.setName("testleaf");
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530139 yangSchemaNodeIdentifier.setNameSpace("http://huawei.com");
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530140 assertThat(schemaMap2.get(yangSchemaNodeIdentifier),
141 is(notNullValue()));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530142
143 yangSchemaNodeIdentifier.setName("pretzel");
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530144 yangSchemaNodeIdentifier.setNameSpace("http://huawei.com");
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530145 assertThat(schemaMap2.get(yangSchemaNodeIdentifier),
146 is(notNullValue()));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530147
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530148 assertThat(rootNode.getChild().getChild().getYsnContextInfoMap(),
149 is(notNullValue()));
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530150 Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap3 =
151 rootNode.getChild().getChild()
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530152 .getYsnContextInfoMap();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530153 yangSchemaNodeIdentifier.setName("pretzel");
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530154 yangSchemaNodeIdentifier.setNameSpace("http://huawei.com");
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530155 assertThat(schemaMap3.get(yangSchemaNodeIdentifier),
156 is(notNullValue()));
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530157 YangSchemaNodeContextInfo yangSchemaNodeContextInfo3 =
158 schemaMap3.get(yangSchemaNodeIdentifier);
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530159
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530160 assertThat(rootNode.getChild().getChild().getChild()
161 .getYsnContextInfoMap(),
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530162 is(notNullValue()));
163 Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap4 =
164 rootNode.getChild().getChild().getChild()
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530165 .getYsnContextInfoMap();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530166 yangSchemaNodeIdentifier.setName("pretzel");
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530167 yangSchemaNodeIdentifier.setNameSpace("http://huawei.com");
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530168 assertThat(schemaMap4.get(yangSchemaNodeIdentifier),
169 is(notNullValue()));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530170
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530171 YangSchemaNodeContextInfo yangSchemaNodeContextInfo2 =
172 schemaMap4.get(yangSchemaNodeIdentifier);
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530173 List<YangLeaf> yangListOfLeaf = ((YangLeavesHolder) rootNode.getChild()
174 .getChild().getChild()).getListOfLeaf();
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530175 YangLeaf yangLeaf = yangListOfLeaf.get(0);
176 assertThat(yangSchemaNodeContextInfo2.getSchemaNode(), is(yangLeaf));
177
178 assertThat(yangSchemaNodeContextInfo3.getSchemaNode(), is(yangLeaf));
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530179 assertThat(yangSchemaNodeContextInfo3.getContextSwitchedNode(),
180 is(rootNode.getChild().getChild().getChild()));
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530181
182 deleteDirectory("target/schemaMap/");
183 }
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530184
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530185 /**
186 * Checks that notification map shouldn't be present in other YANG node.
187 *
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530188 * @throws IOException a violation in IO rule
189 * @throws ParserException a violation in parser rule
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530190 * @throws MojoExecutionException a violation in mojo rule
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530191 * @throws DataModelException a violation in data model rule
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530192 */
193 @Test(expected = DataModelException.class)
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530194 public void processNotificationEnumMapInvalidScenario()
195 throws IOException,
196 ParserException, MojoExecutionException,
197 DataModelException {
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530198
199 String searchDir = "src/test/resources/schemaMap";
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530200 utilManager
201 .createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530202 utilManager.parseYangFileInfoSet();
203 utilManager.createYangNodeSet();
204 utilManager.resolveDependenciesUsingLinker();
205 YangPluginConfig yangPluginConfig = new YangPluginConfig();
206 yangPluginConfig.setCodeGenDir("target/schemaMap/");
207 utilManager.translateToJava(yangPluginConfig);
208
Gaurav Agrawal7a3a2f52016-09-03 11:14:27 +0530209 Iterator<YangNode> yangNodeIterator =
210 utilManager.getYangNodeSet().iterator();
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530211 YangNode rootNode = yangNodeIterator.next();
212
213 deleteDirectory("target/schemaMap/");
214
215 rootNode.getChild().getNotificationSchemaNode("TESTNOTIFICATION1");
216 }
Gaurav Agrawalbe2b3312016-08-17 18:58:17 +0530217}