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