Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.plugin.manager; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 23 | |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 24 | import org.apache.maven.plugin.MojoExecutionException; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 27 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 28 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo; |
| 31 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 34 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
| 35 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
| 36 | |
| 37 | import static org.hamcrest.CoreMatchers.notNullValue; |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 38 | import static org.hamcrest.CoreMatchers.nullValue; |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.core.Is.is; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils |
| 42 | .deleteDirectory; |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Test cases for testing YANG schema node. |
| 46 | */ |
| 47 | public class SchemaNodeTest { |
| 48 | |
| 49 | private final YangUtilManager utilManager = new YangUtilManager(); |
| 50 | |
| 51 | /** |
| 52 | * Checks method to get schema node from map. |
| 53 | * |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 54 | * @throws IOException a violation in IO rule |
| 55 | * @throws ParserException a violation in parser rule |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 56 | * @throws MojoExecutionException a violation in mojo rule |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 57 | * @throws DataModelException a violation in data model rule |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 58 | */ |
| 59 | @Test |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 60 | public void processSchemaNodeMap() |
| 61 | throws IOException, ParserException, |
| 62 | MojoExecutionException, DataModelException { |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 63 | |
| 64 | String searchDir = "src/test/resources/schemaMap"; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 65 | utilManager |
| 66 | .createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir)); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 67 | utilManager.parseYangFileInfoSet(); |
| 68 | utilManager.createYangNodeSet(); |
| 69 | utilManager.resolveDependenciesUsingLinker(); |
| 70 | YangPluginConfig yangPluginConfig = new YangPluginConfig(); |
| 71 | yangPluginConfig.setCodeGenDir("target/schemaMap/"); |
| 72 | utilManager.translateToJava(yangPluginConfig); |
| 73 | |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 74 | Iterator<YangNode> yangNodeIterator = |
| 75 | utilManager.getYangNodeSet().iterator(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 76 | YangNode rootNode = yangNodeIterator.next(); |
| 77 | |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 78 | // 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 Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 86 | // 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 Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 120 | assertThat(rootNode.getYsnContextInfoMap(), is(notNullValue())); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 121 | Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap = |
| 122 | rootNode.getYsnContextInfoMap(); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 123 | YangSchemaNodeIdentifier yangSchemaNodeIdentifier = |
| 124 | new YangSchemaNodeIdentifier(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 125 | yangSchemaNodeIdentifier.setName("testcontainer"); |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 126 | yangSchemaNodeIdentifier.setNameSpace("http://huawei.com"); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 127 | assertThat(schemaMap.get(yangSchemaNodeIdentifier), is(notNullValue())); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 128 | YangSchemaNodeContextInfo yangSchemaNodeContextInfo = |
| 129 | schemaMap.get(yangSchemaNodeIdentifier); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 130 | assertThat(yangSchemaNodeContextInfo.getSchemaNode(), |
| 131 | is(rootNode.getChild())); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 132 | |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 133 | assertThat(rootNode.getChild().getYsnContextInfoMap(), |
| 134 | is(notNullValue())); |
| 135 | Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap2 = |
| 136 | rootNode.getChild() |
| 137 | .getYsnContextInfoMap(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 138 | yangSchemaNodeIdentifier.setName("testleaf"); |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 139 | yangSchemaNodeIdentifier.setNameSpace("http://huawei.com"); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 140 | assertThat(schemaMap2.get(yangSchemaNodeIdentifier), |
| 141 | is(notNullValue())); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 142 | |
| 143 | yangSchemaNodeIdentifier.setName("pretzel"); |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 144 | yangSchemaNodeIdentifier.setNameSpace("http://huawei.com"); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 145 | assertThat(schemaMap2.get(yangSchemaNodeIdentifier), |
| 146 | is(notNullValue())); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 147 | |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 148 | assertThat(rootNode.getChild().getChild().getYsnContextInfoMap(), |
| 149 | is(notNullValue())); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 150 | Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap3 = |
| 151 | rootNode.getChild().getChild() |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 152 | .getYsnContextInfoMap(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 153 | yangSchemaNodeIdentifier.setName("pretzel"); |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 154 | yangSchemaNodeIdentifier.setNameSpace("http://huawei.com"); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 155 | assertThat(schemaMap3.get(yangSchemaNodeIdentifier), |
| 156 | is(notNullValue())); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 157 | YangSchemaNodeContextInfo yangSchemaNodeContextInfo3 = |
| 158 | schemaMap3.get(yangSchemaNodeIdentifier); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 159 | |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 160 | assertThat(rootNode.getChild().getChild().getChild() |
| 161 | .getYsnContextInfoMap(), |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 162 | is(notNullValue())); |
| 163 | Map<YangSchemaNodeIdentifier, YangSchemaNodeContextInfo> schemaMap4 = |
| 164 | rootNode.getChild().getChild().getChild() |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 165 | .getYsnContextInfoMap(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 166 | yangSchemaNodeIdentifier.setName("pretzel"); |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 167 | yangSchemaNodeIdentifier.setNameSpace("http://huawei.com"); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 168 | assertThat(schemaMap4.get(yangSchemaNodeIdentifier), |
| 169 | is(notNullValue())); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 170 | |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 171 | YangSchemaNodeContextInfo yangSchemaNodeContextInfo2 = |
| 172 | schemaMap4.get(yangSchemaNodeIdentifier); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 173 | List<YangLeaf> yangListOfLeaf = ((YangLeavesHolder) rootNode.getChild() |
| 174 | .getChild().getChild()).getListOfLeaf(); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 175 | YangLeaf yangLeaf = yangListOfLeaf.get(0); |
| 176 | assertThat(yangSchemaNodeContextInfo2.getSchemaNode(), is(yangLeaf)); |
| 177 | |
| 178 | assertThat(yangSchemaNodeContextInfo3.getSchemaNode(), is(yangLeaf)); |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 179 | assertThat(yangSchemaNodeContextInfo3.getContextSwitchedNode(), |
| 180 | is(rootNode.getChild().getChild().getChild())); |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 181 | |
| 182 | deleteDirectory("target/schemaMap/"); |
| 183 | } |
Gaurav Agrawal | e5057f0 | 2016-08-22 17:14:33 +0530 | [diff] [blame] | 184 | |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 185 | /** |
| 186 | * Checks that notification map shouldn't be present in other YANG node. |
| 187 | * |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 188 | * @throws IOException a violation in IO rule |
| 189 | * @throws ParserException a violation in parser rule |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 190 | * @throws MojoExecutionException a violation in mojo rule |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 191 | * @throws DataModelException a violation in data model rule |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 192 | */ |
| 193 | @Test(expected = DataModelException.class) |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 194 | public void processNotificationEnumMapInvalidScenario() |
| 195 | throws IOException, |
| 196 | ParserException, MojoExecutionException, |
| 197 | DataModelException { |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 198 | |
| 199 | String searchDir = "src/test/resources/schemaMap"; |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 200 | utilManager |
| 201 | .createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir)); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 202 | utilManager.parseYangFileInfoSet(); |
| 203 | utilManager.createYangNodeSet(); |
| 204 | utilManager.resolveDependenciesUsingLinker(); |
| 205 | YangPluginConfig yangPluginConfig = new YangPluginConfig(); |
| 206 | yangPluginConfig.setCodeGenDir("target/schemaMap/"); |
| 207 | utilManager.translateToJava(yangPluginConfig); |
| 208 | |
Gaurav Agrawal | 7a3a2f5 | 2016-09-03 11:14:27 +0530 | [diff] [blame] | 209 | Iterator<YangNode> yangNodeIterator = |
| 210 | utilManager.getYangNodeSet().iterator(); |
Gaurav Agrawal | 61edaa0 | 2016-08-31 16:52:56 +0530 | [diff] [blame] | 211 | YangNode rootNode = yangNodeIterator.next(); |
| 212 | |
| 213 | deleteDirectory("target/schemaMap/"); |
| 214 | |
| 215 | rootNode.getChild().getNotificationSchemaNode("TESTNOTIFICATION1"); |
| 216 | } |
Gaurav Agrawal | be2b331 | 2016-08-17 18:58:17 +0530 | [diff] [blame] | 217 | } |