Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +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.yob; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.YmsTopologyOpParam; |
| 21 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.DefaultNode; |
| 22 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.Node; |
| 23 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.Case1a; |
| 24 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.Case1b; |
| 25 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.DefaultCase1a; |
| 26 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.DefaultCase1b; |
| 27 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.case1b.choice1b.Case1Bi; |
| 28 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.node.choice1.case1b.choice1b.DefaultCase1Bi; |
| 29 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 30 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 31 | import org.onosproject.yms.ydt.YdtContext; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertNotNull; |
| 37 | import static org.onosproject.yms.app.yob.YobTestUtils.LEAF_1A1; |
| 38 | import static org.onosproject.yms.app.yob.YobTestUtils.LEAF_1A2; |
| 39 | import static org.onosproject.yms.app.yob.YobTestUtils.LEAF_1BIA; |
| 40 | import static org.onosproject.yms.app.yob.YobTestUtils.LEAF_1BIB; |
| 41 | import static org.onosproject.yms.app.yob.YobTestUtils.NODE; |
| 42 | import static org.onosproject.yms.app.yob.YobTestUtils.ROOT_DATA_RESOURCE; |
| 43 | import static org.onosproject.yms.app.yob.YobTestUtils.STR_LEAF_VALUE; |
| 44 | import static org.onosproject.yms.app.yob.YobTestUtils.TOPOLOGY; |
| 45 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
| 46 | |
| 47 | /** |
| 48 | * Test the YANG object building for the YANG data tree based on the non |
| 49 | * schema choice and case nodes. |
| 50 | */ |
| 51 | public class YobChoiceTest { |
| 52 | |
| 53 | private YobTestUtils utils = YobTestUtils.instance(); |
| 54 | |
| 55 | @Test |
| 56 | public void caseInChoice() throws IOException { |
| 57 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 58 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 59 | |
| 60 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 61 | ydtBuilder.addChild(NODE, null); |
| 62 | ydtBuilder.addLeaf(LEAF_1A1, null, STR_LEAF_VALUE); |
| 63 | |
| 64 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 65 | YdtExtendedContext appRoot = |
| 66 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 67 | |
| 68 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 69 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 70 | utils.schemaRegistry()); |
| 71 | assertNotNull(yangObject); |
| 72 | assertEquals("YANG object created is not topology object", |
| 73 | YmsTopologyOpParam.class, yangObject.getClass()); |
| 74 | |
| 75 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 76 | assertNotNull("Failed to build the object", topology.node()); |
| 77 | assertEquals("Single node entry is expected", 1, |
| 78 | topology.node().size()); |
| 79 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 80 | topology.node().get(0).getClass()); |
| 81 | |
| 82 | Node node = topology.node().get(0); |
| 83 | assertNotNull("choice1 is not set in node", node.choice1()); |
| 84 | assertEquals("choice 1 type is not ", DefaultCase1a.class, |
| 85 | node.choice1().getClass()); |
| 86 | |
| 87 | Case1a case1a = (Case1a) node.choice1(); |
| 88 | assertNotNull("leaf1a1 is not set in case", case1a.leaf1A1()); |
| 89 | assertEquals("leaf1a1 type is not correct", String.class, |
| 90 | case1a.leaf1A1().getClass()); |
| 91 | assertEquals("leaf1a1 value is not correct", STR_LEAF_VALUE, |
| 92 | case1a.leaf1A1()); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void caseWithMultiAttribute() throws IOException { |
| 98 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 99 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 100 | |
| 101 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 102 | ydtBuilder.addChild(NODE, null); |
| 103 | ydtBuilder.addLeaf(LEAF_1A1, null, STR_LEAF_VALUE); |
| 104 | ydtBuilder.traverseToParent(); |
| 105 | ydtBuilder.addLeaf(LEAF_1A2, null, STR_LEAF_VALUE); |
| 106 | |
| 107 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 108 | YdtExtendedContext appRoot = |
| 109 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 110 | |
| 111 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 112 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 113 | utils.schemaRegistry()); |
| 114 | assertNotNull(yangObject); |
| 115 | assertEquals("YANG object created is not topology object", |
| 116 | YmsTopologyOpParam.class, yangObject.getClass()); |
| 117 | |
| 118 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 119 | assertNotNull("Failed to build the object", topology.node()); |
| 120 | assertEquals("Single node entry is expected", 1, |
| 121 | topology.node().size()); |
| 122 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 123 | topology.node().get(0).getClass()); |
| 124 | |
| 125 | Node node = topology.node().get(0); |
| 126 | assertNotNull("choice1 is not set in node", node.choice1()); |
| 127 | assertEquals("choice 1 type is not ", DefaultCase1a.class, |
| 128 | node.choice1().getClass()); |
| 129 | |
| 130 | Case1a case1a = (Case1a) node.choice1(); |
| 131 | assertNotNull("leaf1a1 is not set in case", case1a.leaf1A1()); |
| 132 | assertEquals("leaf1a1 type is not correct", String.class, |
| 133 | case1a.leaf1A1().getClass()); |
| 134 | assertEquals("leaf1a1 value is not correct", STR_LEAF_VALUE, |
| 135 | case1a.leaf1A1()); |
| 136 | |
| 137 | assertNotNull("leaf1a2 is not set in case", case1a.leaf1A2()); |
| 138 | assertEquals("leaf1a2 type is not correct", String.class, |
| 139 | case1a.leaf1A2().getClass()); |
| 140 | assertEquals("leaf1a1 value is not correct", STR_LEAF_VALUE, |
| 141 | case1a.leaf1A1()); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void recursiveChoice() throws IOException { |
| 147 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 148 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 149 | |
| 150 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 151 | ydtBuilder.addChild(NODE, null); |
| 152 | ydtBuilder.addLeaf(LEAF_1BIA, null, STR_LEAF_VALUE); |
| 153 | |
| 154 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 155 | YdtExtendedContext appRoot = |
| 156 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 157 | |
| 158 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 159 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 160 | utils.schemaRegistry()); |
| 161 | assertNotNull(yangObject); |
| 162 | assertEquals("YANG object created is not topology object", |
| 163 | YmsTopologyOpParam.class, yangObject.getClass()); |
| 164 | |
| 165 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 166 | assertNotNull("Failed to build the object", topology.node()); |
| 167 | assertEquals("Single node entry is expected", 1, |
| 168 | topology.node().size()); |
| 169 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 170 | topology.node().get(0).getClass()); |
| 171 | |
| 172 | Node node = topology.node().get(0); |
| 173 | assertNotNull("Choice 1 is not set in Node", node.choice1()); |
| 174 | assertEquals("Choice 1 is not of type DefaultCase1b", |
| 175 | DefaultCase1b.class, node.choice1().getClass()); |
| 176 | |
| 177 | Case1b case1b = (Case1b) node.choice1(); |
| 178 | assertNotNull("Case1b does not have child choice1b ", |
| 179 | case1b.choice1b()); |
| 180 | assertEquals("choice1b is not of type DefaultCase1Bi", |
| 181 | DefaultCase1Bi.class, case1b.choice1b().getClass()); |
| 182 | |
| 183 | Case1Bi case1Bi = (Case1Bi) case1b.choice1b(); |
| 184 | assertNotNull("leaf1bia is not set", case1Bi.leaf1Bia()); |
| 185 | assertEquals("leaf1bia type is not string", String.class, |
| 186 | case1Bi.leaf1Bia().getClass()); |
| 187 | assertEquals("leaf1bia value is wrong", STR_LEAF_VALUE, |
| 188 | case1Bi.leaf1Bia()); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void recursiveChoiceWithMultipleAttribute() throws IOException { |
| 193 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 194 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 195 | |
| 196 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 197 | ydtBuilder.addChild(NODE, null); |
| 198 | ydtBuilder.addLeaf(LEAF_1BIA, null, STR_LEAF_VALUE); |
| 199 | ydtBuilder.traverseToParent(); |
| 200 | ydtBuilder.addLeaf(LEAF_1BIB, null, STR_LEAF_VALUE); |
| 201 | |
| 202 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 203 | YdtExtendedContext appRoot = |
| 204 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 205 | |
| 206 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 207 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 208 | utils.schemaRegistry()); |
| 209 | assertNotNull(yangObject); |
| 210 | assertEquals("YANG object created is not topology object", |
| 211 | YmsTopologyOpParam.class, yangObject.getClass()); |
| 212 | |
| 213 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 214 | assertNotNull("Failed to build the object", topology.node()); |
| 215 | assertEquals("Single node entry is expected", 1, |
| 216 | topology.node().size()); |
| 217 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 218 | topology.node().get(0).getClass()); |
| 219 | |
| 220 | Node node = topology.node().get(0); |
| 221 | assertNotNull("Choice 1 is not set in Node", node.choice1()); |
| 222 | assertEquals("Choice 1 is not of type DefaultCase1b", |
| 223 | DefaultCase1b.class, |
| 224 | node.choice1().getClass()); |
| 225 | |
| 226 | Case1b case1b = (Case1b) node.choice1(); |
| 227 | assertNotNull("Case1b does not have child choice1b ", |
| 228 | case1b.choice1b()); |
| 229 | assertEquals("choice1b is not of type DefaultCase1Bi", |
| 230 | DefaultCase1Bi.class, |
| 231 | case1b.choice1b().getClass()); |
| 232 | |
| 233 | Case1Bi case1Bi = (Case1Bi) case1b.choice1b(); |
| 234 | assertNotNull("leaf1bia is not set", case1Bi.leaf1Bia()); |
| 235 | assertEquals("leaf1bia type is not string", String.class, |
| 236 | case1Bi.leaf1Bia().getClass()); |
| 237 | assertEquals("leaf1bia value is wrong", STR_LEAF_VALUE, |
| 238 | case1Bi.leaf1Bia()); |
| 239 | |
| 240 | assertNotNull("leaf1bib is not set", case1Bi.leaf1Bib()); |
| 241 | assertEquals("leaf1bia type is not string", String.class, |
| 242 | case1Bi.leaf1Bib().getClass()); |
| 243 | assertEquals("leaf1bia value is wrong", STR_LEAF_VALUE, |
| 244 | case1Bi.leaf1Bib()); |
| 245 | } |
| 246 | } |