2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import org.junit.Test;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
16 import org.opendaylight.yangtools.yang.binding.contract.Naming;
17 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
19 public class GeneratedTypesTest {
21 public void testMultipleModulesResolving() {
22 final var genTypes = DefaultBindingGenerator.generateFor(
23 YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
24 "/abstract-topology.yang", "/ietf-models/ietf-inet-types.yang"));
25 assertEquals(27, genTypes.size());
29 public void testContainerResolving() {
30 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
31 "/simple-container-demo.yang"));
33 assertNotNull(genTypes);
34 assertEquals(3, genTypes.size());
36 var simpleContainer = genTypes.get(1);
37 var nestedContainer = genTypes.get(2);
38 for (var t : genTypes) {
39 if ("SimpleContainer".equals(t.getName())) {
41 } else if ("NestedContainer".equals(t.getName())) {
45 assertNotNull(simpleContainer);
46 assertNotNull(nestedContainer);
47 // FIXME: split this into getter/default/static asserts
48 assertEquals(10, simpleContainer.getMethodDefinitions().size());
49 // FIXME: split this into getter/default/static asserts
50 assertEquals(8, nestedContainer.getMethodDefinitions().size());
52 int getFooMethodCounter = 0;
53 int getBarMethodCounter = 0;
54 int getNestedContainerCounter = 0;
56 String getFooMethodReturnTypeName = "";
57 String getBarMethodReturnTypeName = "";
58 String getNestedContainerReturnTypeName = "";
59 for (var method : simpleContainer.getMethodDefinitions()) {
60 if (method.getName().equals("getFoo")) {
61 getFooMethodCounter++;
62 getFooMethodReturnTypeName = method.getReturnType().getName();
65 if (method.getName().equals("getBar")) {
66 getBarMethodCounter++;
67 getBarMethodReturnTypeName = method.getReturnType().getName();
70 if (method.getName().equals("getNestedContainer")) {
71 getNestedContainerCounter++;
72 getNestedContainerReturnTypeName = method.getReturnType().getName();
76 assertEquals(1, getFooMethodCounter);
77 assertEquals("Integer", getFooMethodReturnTypeName);
79 assertEquals(1, getBarMethodCounter);
80 assertEquals("String", getBarMethodReturnTypeName);
82 assertEquals(1, getNestedContainerCounter);
83 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
85 getFooMethodCounter = 0;
86 getBarMethodCounter = 0;
88 getFooMethodReturnTypeName = "";
89 getBarMethodReturnTypeName = "";
91 for (var method : nestedContainer.getMethodDefinitions()) {
92 if (method.getName().equals("getFoo")) {
93 getFooMethodCounter++;
94 getFooMethodReturnTypeName = method.getReturnType().getName();
97 if (method.getName().equals("getBar")) {
98 getBarMethodCounter++;
99 getBarMethodReturnTypeName = method.getReturnType().getName();
103 assertEquals(1, getFooMethodCounter);
104 assertEquals("Uint8", getFooMethodReturnTypeName);
106 assertEquals(1, getBarMethodCounter);
107 assertEquals("String", getBarMethodReturnTypeName);
111 public void testLeafListResolving() {
112 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
113 "/simple-leaf-list-demo.yang"));
115 assertNotNull(genTypes);
116 assertEquals(3, genTypes.size());
118 var simpleContainer = genTypes.get(1);
119 var nestedContainer = genTypes.get(2);
120 for (var t : genTypes) {
121 if ("SimpleContainer".equals(t.getName())) {
123 } else if ("NestedContainer".equals(t.getName())) {
127 assertNotNull(simpleContainer);
128 assertNotNull(nestedContainer);
129 // FIXME: split this into getter/default/static asserts
130 assertEquals(10, simpleContainer.getMethodDefinitions().size());
131 // FIXME: split this into getter/default/static asserts
132 assertEquals(8, nestedContainer.getMethodDefinitions().size());
134 int getFooMethodCounter = 0;
135 int getBarMethodCounter = 0;
136 int getNestedContainerCounter = 0;
138 String getFooMethodReturnTypeName = "";
139 String getBarMethodReturnTypeName = "";
140 String getNestedContainerReturnTypeName = "";
141 for (var method : simpleContainer.getMethodDefinitions()) {
142 if (method.isDefault()) {
145 if (method.getName().equals("getFoo")) {
146 getFooMethodCounter++;
147 getFooMethodReturnTypeName = method.getReturnType().getName();
150 if (method.getName().equals("getBar")) {
151 getBarMethodCounter++;
152 getBarMethodReturnTypeName = method.getReturnType().getName();
155 if (method.getName().equals("getNestedContainer")) {
156 getNestedContainerCounter++;
157 getNestedContainerReturnTypeName = method.getReturnType().getName();
161 assertEquals(1, getFooMethodCounter);
162 assertEquals("Set", getFooMethodReturnTypeName);
164 assertEquals(1, getBarMethodCounter);
165 assertEquals("String", getBarMethodReturnTypeName);
167 assertEquals(1, getNestedContainerCounter);
168 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
170 getFooMethodCounter = 0;
171 getBarMethodCounter = 0;
173 getFooMethodReturnTypeName = "";
174 getBarMethodReturnTypeName = "";
176 for (var method : nestedContainer.getMethodDefinitions()) {
177 if (method.getName().equals("getFoo")) {
178 getFooMethodCounter++;
179 getFooMethodReturnTypeName = method.getReturnType().getName();
182 if (method.getName().equals("getBar")) {
183 getBarMethodCounter++;
184 getBarMethodReturnTypeName = method.getReturnType().getName();
188 assertEquals(1, getFooMethodCounter);
189 assertEquals("Uint8", getFooMethodReturnTypeName);
191 assertEquals(1, getBarMethodCounter);
192 assertEquals("Set", getBarMethodReturnTypeName);
196 public void testListResolving() {
197 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
198 "/simple-list-demo.yang"));
200 assertNotNull(genTypes);
201 assertEquals(5, genTypes.size());
203 int listParentContainerMethodsCount = 0;
204 int simpleListMethodsCount = 0;
205 int listChildContainerMethodsCount = 0;
206 int listKeyClassCount = 0;
208 int getSimpleListKeyMethodCount = 0;
209 int getListChildContainerMethodCount = 0;
210 int getFooMethodCount = 0;
211 int setFooMethodCount = 0;
212 int getSimpleLeafListMethodCount = 0;
213 int setSimpleLeafListMethodCount = 0;
214 int getBarMethodCount = 0;
216 String getSimpleListKeyMethodReturnTypeName = "";
217 String getListChildContainerMethodReturnTypeName = "";
219 for (var genType : genTypes) {
220 if (!(genType instanceof GeneratedTransferObject genTO)) {
221 if (genType.getName().equals("ListParentContainer")) {
222 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
223 } else if (genType.getName().equals("SimpleList")) {
224 simpleListMethodsCount = genType.getMethodDefinitions().size();
225 for (var method : genType.getMethodDefinitions()) {
226 switch (method.getName()) {
227 case Naming.KEY_AWARE_KEY_NAME:
228 getSimpleListKeyMethodCount++;
229 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
231 case "getListChildContainer":
232 getListChildContainerMethodCount++;
233 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
241 case "getSimpleLeafList":
242 getSimpleLeafListMethodCount++;
244 case "setSimpleLeafList":
245 setSimpleLeafListMethodCount++;
253 } else if (genType.getName().equals("ListChildContainer")) {
254 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
257 final var properties = genTO.getProperties();
258 final var hashProps = genTO.getHashCodeIdentifiers();
259 final var equalProps = genTO.getEqualsIdentifiers();
261 assertEquals("Unexpected key", 0, listKeyClassCount++);
262 assertEquals(1, properties.size());
263 assertEquals("listKey", properties.get(0).getName());
264 assertEquals("Byte", properties.get(0).getReturnType().getName());
265 assertTrue(properties.get(0).isReadOnly());
267 assertEquals(1, hashProps.size());
268 assertEquals("listKey", hashProps.get(0).getName());
269 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
271 assertEquals(1, equalProps.size());
272 assertEquals("listKey", equalProps.get(0).getName());
273 assertEquals("Byte", equalProps.get(0).getReturnType().getName());
277 // FIXME: split this into getter/default/static asserts
278 assertEquals(6, listParentContainerMethodsCount);
279 // FIXME: split this into getter/default/static asserts
280 assertEquals(6, listChildContainerMethodsCount);
281 assertEquals(1, getSimpleListKeyMethodCount);
282 assertEquals(1, listKeyClassCount);
284 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
286 assertEquals(1, getListChildContainerMethodCount);
287 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
288 assertEquals(1, getFooMethodCount);
289 assertEquals(0, setFooMethodCount);
290 assertEquals(1, getSimpleLeafListMethodCount);
291 assertEquals(0, setSimpleLeafListMethodCount);
292 assertEquals(1, getBarMethodCount);
294 // FIXME: split this into getter/default/static asserts
295 assertEquals(15, simpleListMethodsCount);
299 public void testListCompositeKeyResolving() {
300 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
301 "/list-composite-key.yang"));
303 assertNotNull(genTypes);
304 assertEquals(7, genTypes.size());
306 int genTypesCount = 0;
309 int compositeKeyListKeyPropertyCount = 0;
310 int compositeKeyListKeyCount = 0;
311 int innerListKeyPropertyCount = 0;
313 for (var type : genTypes) {
314 if (!(type instanceof GeneratedTransferObject genTO)) {
316 } else if (genTO.getName().equals("CompositeKeyListKey")) {
317 compositeKeyListKeyCount++;
318 for (var prop : genTO.getProperties()) {
319 if (prop.getName().equals("key1") || prop.getName().equals("key2")) {
320 compositeKeyListKeyPropertyCount++;
324 } else if (genTO.getName().equals("InnerListKey")) {
325 innerListKeyPropertyCount = genTO.getProperties().size();
329 assertEquals(1, compositeKeyListKeyCount);
330 assertEquals(2, compositeKeyListKeyPropertyCount);
332 assertEquals(1, innerListKeyPropertyCount);
334 assertEquals(5, genTypesCount);
335 assertEquals(2, genTOsCount);
339 public void testGeneratedTypes() {
340 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
341 "/demo-topology.yang"));
343 assertNotNull(genTypes);
344 assertEquals(14, genTypes.size());
346 int genTypesCount = 0;
348 for (var type : genTypes) {
349 if (type instanceof GeneratedTransferObject) {
356 assertEquals(11, genTypesCount);
357 assertEquals(3, genTOsCount);
361 public void testAugmentRpcInput() {
362 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
363 "/augment-rpc-input.yang"));
364 assertEquals(6, genTypes.size());