janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
janani b | 9069eb4 | 2016-11-24 17:50:08 +0530 | [diff] [blame] | 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.yms.app.ytb; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.YtbDataType; |
| 21 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.YtbDataTypeOpParam; |
| 22 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.EnumDer1; |
| 23 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.EnumDer2; |
| 24 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.EnumLeafListUnion; |
| 25 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.UnionEnumUnion; |
| 26 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.enumder2.EnumDer2Enum; |
| 27 | import org.onosproject.yang.gen.v1.yms.test.ytb.data.type.rev20160826.ytbdatatype.enumleaflistunion.EnumLeafListUnionEnum1; |
| 28 | import org.onosproject.yang.gen.v1.yms.test.ytb.empty.type.rev20160826.YtbEmptyType; |
| 29 | import org.onosproject.yang.gen.v1.yms.test.ytb.empty.type.rev20160826.YtbEmptyTypeOpParam; |
| 30 | import org.onosproject.yang.gen.v1.yms.test.ytb.empty.type.rev20160826.ytbemptytype.EmpType; |
| 31 | import org.onosproject.yang.gen.v1.yms.test.ytb.empty.type.rev20160826.ytbemptytype.EmpType2; |
| 32 | import org.onosproject.yang.gen.v1.yms.test.ytb.simple.rpc.response.rev20160826.YtbSimpleRpcResponse; |
| 33 | import org.onosproject.yang.gen.v1.yms.test.ytb.simple.rpc.response.rev20160826.YtbSimpleRpcResponseOpParam; |
| 34 | import org.onosproject.yang.gen.v1.yms.test.ytb.simple.rpc.response.rev20160826.ytbsimplerpcresponse.Cumulative; |
| 35 | import org.onosproject.yang.gen.v1.yms.test.ytb.simple.rpc.response.rev20160826.ytbsimplerpcresponse.DefaultCumulative; |
| 36 | import org.onosproject.yms.app.ydt.YdtExtendedBuilder; |
| 37 | import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry; |
| 38 | import org.onosproject.yms.ydt.YdtContext; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.hamcrest.core.Is.is; |
| 46 | import static org.hamcrest.core.IsNull.nullValue; |
| 47 | import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST; |
| 48 | |
| 49 | /** |
| 50 | * Unit test cases for invalid node skip in YANG tree builder. |
| 51 | */ |
| 52 | public class YtbInvalidNodeSkipTest extends YtbErrMsgAndConstants { |
| 53 | |
| 54 | private static final String CUMULATIVE = "cumulative"; |
| 55 | private static final String SUM = "sum"; |
| 56 | private static final String FIVE = "5"; |
| 57 | private static final String TEN_NUM = "10"; |
| 58 | private static final String DATA_TYPE = "YtbDataType"; |
| 59 | private static final String ENUM = "enum"; |
| 60 | private static final String EMPTY = "empty"; |
| 61 | private static final String THOUSAND = "thousand"; |
| 62 | private static final String TEN = "ten"; |
| 63 | private static final String ENUM_LEAF_LIST = "enum-leaf-list"; |
| 64 | private static final String UNION_ENUM = "union-enum"; |
| 65 | private static final String ENUM_LEAF_REF = "leaf-ref-enum"; |
| 66 | private static final String EMPTY_MOD = "YtbEmptyType"; |
| 67 | private static final String EMPTY_REF_LIST = "empty-list-ref"; |
| 68 | private static final String EMPTY_TYPE = "empty-type"; |
| 69 | private static final String EMP_LIST_REF_TYPE = "empty-list-ref-type"; |
| 70 | |
| 71 | /** |
| 72 | * Processes RPC node which is the sibling to the empty current node. |
| 73 | */ |
| 74 | @Test |
| 75 | public void processRpcSiblingWhenNodeIsEmpty() { |
| 76 | |
| 77 | schemaProvider.processSchemaRegistry(null); |
| 78 | DefaultYangSchemaRegistry registry = schemaProvider |
| 79 | .getDefaultYangSchemaRegistry(); |
| 80 | |
| 81 | // As an application, creates the object. |
| 82 | Cumulative cumulative1 = new DefaultCumulative.CumulativeBuilder() |
| 83 | .sum((byte) 5).build(); |
| 84 | Cumulative cumulative2 = new DefaultCumulative.CumulativeBuilder() |
| 85 | .sum((byte) 10).build(); |
| 86 | List<Cumulative> list = new ArrayList<>(); |
| 87 | list.add(cumulative1); |
| 88 | list.add(cumulative2); |
| 89 | YtbSimpleRpcResponse rpc = new YtbSimpleRpcResponseOpParam |
| 90 | .YtbSimpleRpcResponseBuilder().cumulative(list).build(); |
| 91 | |
| 92 | // As YSB or YAB protocol sets the value for YTB. |
| 93 | List<Object> objectList = new ArrayList<>(); |
| 94 | objectList.add(rpc); |
| 95 | |
| 96 | // Builds YANG tree in YTB. |
| 97 | DefaultYangTreeBuilder treeBuilder = new DefaultYangTreeBuilder(); |
| 98 | YdtExtendedBuilder ydtBuilder = treeBuilder.getYdtBuilderForYo( |
| 99 | objectList, ROOT_NAME, ROOT_NAME_SPACE, |
| 100 | EDIT_CONFIG_REQUEST, registry); |
| 101 | |
| 102 | // Receives YDT context and checks the tree that is built. |
| 103 | YdtContext context = ydtBuilder.getRootNode(); |
| 104 | |
| 105 | // Gets the first module from logical root node. |
| 106 | YdtContext module = context.getFirstChild(); |
| 107 | assertThat(getInCrtName(MODULE, RPC_NAME), module.getName(), |
| 108 | is(RPC_NAME)); |
| 109 | |
| 110 | // Gets the first list content of cumulative. |
| 111 | YdtContext list1 = module.getFirstChild(); |
| 112 | assertThat(getInCrtName(LIST, CUMULATIVE), list1.getName(), |
| 113 | is(CUMULATIVE)); |
| 114 | |
| 115 | YdtContext sum1 = list1.getFirstChild(); |
| 116 | assertThat(getInCrtName(LEAF, SUM), sum1.getName(), is(SUM)); |
| 117 | assertThat(getInCrtLeafValue(SUM, FIVE), sum1.getValue(), is(FIVE)); |
| 118 | |
| 119 | // Gets the second list content of cumulative. |
| 120 | YdtContext list2 = list1.getNextSibling(); |
| 121 | assertThat(getInCrtName(LIST, CUMULATIVE), list2.getName(), |
| 122 | is(CUMULATIVE)); |
| 123 | |
| 124 | YdtContext sum2 = list2.getFirstChild(); |
| 125 | assertThat(getInCrtName(LEAF, SUM), sum2.getName(), is(SUM)); |
| 126 | assertThat(getInCrtLeafValue(SUM, TEN_NUM), sum2.getValue(), is(TEN_NUM)); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void processEnumDataType() { |
| 131 | |
| 132 | schemaProvider.processSchemaRegistry(null); |
| 133 | DefaultYangSchemaRegistry registry = schemaProvider |
| 134 | .getDefaultYangSchemaRegistry(); |
| 135 | |
| 136 | // As an application, creates the object. |
| 137 | |
| 138 | // Creates the enum hundred for leaf enum. |
| 139 | EnumDer2 der2 = new EnumDer2(EnumDer2Enum.HUNDRED); |
| 140 | EnumDer1 der1 = new EnumDer1(der2); |
| 141 | |
| 142 | // Creates the enum hundred and ten for leaf-list having union. |
| 143 | EnumLeafListUnion union1 = new EnumLeafListUnion(EnumLeafListUnionEnum1 |
| 144 | .HUNDRED); |
| 145 | EnumLeafListUnion union2 = new EnumLeafListUnion(EnumLeafListUnionEnum1 |
| 146 | .TEN); |
| 147 | |
| 148 | List<EnumLeafListUnion> leafList = new ArrayList<>(); |
| 149 | leafList.add(union1); |
| 150 | leafList.add(union2); |
| 151 | |
| 152 | // Creates a leaf having typedef in union, where as the typedef is enum. |
| 153 | UnionEnumUnion enumUnion = new UnionEnumUnion(der1); |
| 154 | |
| 155 | // Creates a leaf-list with leaf-ref pointing to leaf with enum. |
| 156 | EnumDer2 enum2 = new EnumDer2(EnumDer2Enum.THOUSAND); |
| 157 | EnumDer1 enum1 = new EnumDer1(enum2); |
| 158 | EnumDer2 enum4 = new EnumDer2(EnumDer2Enum.HUNDRED); |
| 159 | EnumDer1 enum3 = new EnumDer1(enum4); |
| 160 | |
| 161 | List<EnumDer1> enumDer1 = new ArrayList<>(); |
| 162 | enumDer1.add(enum1); |
| 163 | enumDer1.add(enum3); |
| 164 | |
| 165 | YtbDataType dataType = new YtbDataTypeOpParam.YtbDataTypeBuilder() |
| 166 | .yangAutoPrefixEnum(der1).enumLeafList(leafList) |
| 167 | .unionEnum(enumUnion).leafRefEnum(enumDer1).build(); |
| 168 | |
| 169 | // As YSB or YAB protocol sets the value for YTB. |
| 170 | List<Object> objectList = new ArrayList<>(); |
| 171 | objectList.add(dataType); |
| 172 | |
| 173 | // Builds YANG tree in YTB. |
| 174 | DefaultYangTreeBuilder treeBuilder = new DefaultYangTreeBuilder(); |
| 175 | YdtExtendedBuilder ydtBuilder = treeBuilder.getYdtBuilderForYo( |
| 176 | objectList, ROOT_NAME, ROOT_NAME_SPACE, |
| 177 | EDIT_CONFIG_REQUEST, registry); |
| 178 | |
| 179 | // Receives YDT context and checks the tree that is built. |
| 180 | YdtContext context = ydtBuilder.getRootNode(); |
| 181 | |
| 182 | // Gets the first module from logical root node. |
| 183 | YdtContext module = context.getFirstChild(); |
| 184 | assertThat(getInCrtName(MODULE, DATA_TYPE), module.getName(), |
| 185 | is(DATA_TYPE)); |
| 186 | |
| 187 | // Gets the first list content of cumulative. |
| 188 | YdtContext leafEnum = module.getFirstChild(); |
| 189 | assertThat(getInCrtName(LEAF, ENUM), leafEnum.getName(), is(ENUM)); |
| 190 | assertThat(getInCrtLeafValue(ENUM, HUNDRED), leafEnum.getValue(), |
| 191 | is(HUNDRED)); |
| 192 | |
| 193 | YdtContext unionEnum = leafEnum.getNextSibling(); |
| 194 | assertThat(getInCrtName(LEAF_LIST, UNION_ENUM), unionEnum.getName(), |
| 195 | is(UNION_ENUM)); |
| 196 | assertThat(getInCrtLeafValue(UNION_ENUM, HUNDRED), unionEnum.getValue(), |
| 197 | is(HUNDRED)); |
| 198 | |
| 199 | YdtContext leafListEnum = unionEnum.getNextSibling(); |
| 200 | Set leafListVal = leafListEnum.getValueSet(); |
| 201 | assertThat(getInCrtName(LEAF_LIST, ENUM_LEAF_LIST), |
| 202 | leafListEnum.getName(), is(ENUM_LEAF_LIST)); |
| 203 | assertThat(getInCrtLeafListValue(ENUM_LEAF_LIST, HUNDRED), |
| 204 | leafListVal.contains(HUNDRED), is(true)); |
| 205 | assertThat(getInCrtLeafListValue(ENUM_LEAF_LIST, TEN_NUM), |
| 206 | leafListVal.contains(TEN), is(true)); |
| 207 | |
| 208 | YdtContext leafRef = leafListEnum.getNextSibling(); |
| 209 | Set leafRefVal = leafRef.getValueSet(); |
| 210 | assertThat(getInCrtName(LEAF_LIST, ENUM_LEAF_REF), leafRef.getName(), |
| 211 | is(ENUM_LEAF_REF)); |
| 212 | assertThat(getInCrtLeafListValue(ENUM_LEAF_REF, HUNDRED), |
| 213 | leafRefVal.contains(HUNDRED), is(true)); |
| 214 | assertThat(getInCrtLeafListValue(ENUM_LEAF_REF, TEN_NUM), |
| 215 | leafRefVal.contains(THOUSAND), is(true)); |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void processEmptyDataType() { |
| 220 | |
| 221 | schemaProvider.processSchemaRegistry(null); |
| 222 | DefaultYangSchemaRegistry registry = schemaProvider |
| 223 | .getDefaultYangSchemaRegistry(); |
| 224 | |
| 225 | // As an application, creates the object. |
| 226 | |
| 227 | // For leaf-list empty-list. |
| 228 | List<Boolean> empList = new ArrayList<>(); |
| 229 | empList.add(false); |
| 230 | empList.add(true); |
| 231 | |
| 232 | // For leaf-list empty-list-ref-type and emp-ref-list. |
| 233 | List<Boolean> empRefList = new ArrayList<>(); |
| 234 | empRefList.add(true); |
| 235 | empRefList.add(false); |
| 236 | |
| 237 | // For leaf empty-type with typedef emp-type |
| 238 | EmpType2 type2 = new EmpType2(true); |
| 239 | EmpType type1 = new EmpType(type2); |
| 240 | |
| 241 | // For leaf-list empty-list-type with typedef emp-type |
| 242 | EmpType2 type4 = new EmpType2(false); |
| 243 | EmpType type3 = new EmpType(type4); |
| 244 | EmpType2 type6 = new EmpType2(true); |
| 245 | EmpType type5 = new EmpType(type6); |
| 246 | |
| 247 | List<EmpType> typeList = new ArrayList<>(); |
| 248 | typeList.add(type3); |
| 249 | typeList.add(type5); |
| 250 | |
| 251 | YtbEmptyType emType = new YtbEmptyTypeOpParam.YtbEmptyTypeBuilder() |
| 252 | .empty(true).emptyList(empList).emptyRef(false) |
| 253 | .emptyListRef(empRefList).emptyType(type1) |
| 254 | .emptyListType(typeList).emptyRefType(false) |
| 255 | .emptyListRefType(empRefList).build(); |
| 256 | |
| 257 | // As YSB or YAB protocol sets the value for YTB. |
| 258 | List<Object> objectList = new ArrayList<>(); |
| 259 | objectList.add(emType); |
| 260 | |
| 261 | // Builds YANG tree in YTB. |
| 262 | DefaultYangTreeBuilder treeBuilder = new DefaultYangTreeBuilder(); |
| 263 | YdtExtendedBuilder ydtBuilder = treeBuilder.getYdtBuilderForYo( |
| 264 | objectList, ROOT_NAME, ROOT_NAME_SPACE, |
| 265 | EDIT_CONFIG_REQUEST, registry); |
| 266 | |
| 267 | // Receives YDT context and checks the tree that is built. |
| 268 | YdtContext context = ydtBuilder.getRootNode(); |
| 269 | |
| 270 | // Gets the first module from logical root node. |
| 271 | YdtContext module = context.getFirstChild(); |
| 272 | assertThat(getInCrtName(MODULE, EMPTY_MOD), module.getName(), |
| 273 | is(EMPTY_MOD)); |
| 274 | |
| 275 | // Gets the first list content of cumulative. |
| 276 | YdtContext empty = module.getFirstChild(); |
| 277 | assertThat(getInCrtName(LEAF, EMPTY), empty.getName(), is(EMPTY)); |
| 278 | assertThat(empty.getValue(), nullValue()); |
| 279 | |
| 280 | YdtContext emptyType = empty.getNextSibling(); |
| 281 | assertThat(getInCrtName(LEAF_LIST, EMPTY_TYPE), emptyType.getName(), |
| 282 | is(EMPTY_TYPE)); |
| 283 | assertThat(emptyType.getValue(), nullValue()); |
| 284 | |
| 285 | YdtContext emptyRefList = emptyType.getNextSibling(); |
| 286 | assertThat(getInCrtName(LEAF_LIST, EMPTY_REF_LIST), |
| 287 | emptyRefList.getName(), is(EMPTY_REF_LIST)); |
| 288 | Set valueSet = emptyRefList.getValueSet(); |
| 289 | assertThat(valueSet.isEmpty(), is(true)); |
| 290 | |
| 291 | YdtContext emptyListRefType = emptyRefList.getNextSibling(); |
| 292 | assertThat(getInCrtName(LEAF_LIST, EMP_LIST_REF_TYPE), |
| 293 | emptyListRefType.getName(), is(EMP_LIST_REF_TYPE)); |
| 294 | Set valueSet1 = emptyListRefType.getValueSet(); |
| 295 | assertThat(valueSet1.isEmpty(), is(true)); |
| 296 | } |
| 297 | } |