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 java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class GeneratedTypesTest {
25 public void testMultipleModulesResolving() {
26 final var genTypes = DefaultBindingGenerator.generateFor(
27 YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
28 "/abstract-topology.yang", "/ietf-models/ietf-inet-types.yang"));
29 assertEquals(27, genTypes.size());
33 public void testContainerResolving() {
34 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
35 "/simple-container-demo.yang"));
37 assertNotNull(genTypes);
38 assertEquals(3, genTypes.size());
40 GeneratedType simpleContainer = genTypes.get(1);
41 GeneratedType nestedContainer = genTypes.get(2);
42 for (GeneratedType t : genTypes) {
43 if ("SimpleContainer".equals(t.getName())) {
45 } else if ("NestedContainer".equals(t.getName())) {
49 assertNotNull(simpleContainer);
50 assertNotNull(nestedContainer);
51 // FIXME: split this into getter/default/static asserts
52 assertEquals(10, simpleContainer.getMethodDefinitions().size());
53 // FIXME: split this into getter/default/static asserts
54 assertEquals(8, nestedContainer.getMethodDefinitions().size());
56 int getFooMethodCounter = 0;
57 int getBarMethodCounter = 0;
58 int getNestedContainerCounter = 0;
60 String getFooMethodReturnTypeName = "";
61 String getBarMethodReturnTypeName = "";
62 String getNestedContainerReturnTypeName = "";
63 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
64 if (method.getName().equals("getFoo")) {
65 getFooMethodCounter++;
66 getFooMethodReturnTypeName = method.getReturnType().getName();
69 if (method.getName().equals("getBar")) {
70 getBarMethodCounter++;
71 getBarMethodReturnTypeName = method.getReturnType().getName();
74 if (method.getName().equals("getNestedContainer")) {
75 getNestedContainerCounter++;
76 getNestedContainerReturnTypeName = method.getReturnType().getName();
80 assertEquals(1, getFooMethodCounter);
81 assertEquals("Integer", getFooMethodReturnTypeName);
83 assertEquals(1, getBarMethodCounter);
84 assertEquals("String", getBarMethodReturnTypeName);
86 assertEquals(1, getNestedContainerCounter);
87 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
89 getFooMethodCounter = 0;
90 getBarMethodCounter = 0;
92 getFooMethodReturnTypeName = "";
93 getBarMethodReturnTypeName = "";
95 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
97 if (method.getName().equals("getFoo")) {
98 getFooMethodCounter++;
99 getFooMethodReturnTypeName = method.getReturnType().getName();
102 if (method.getName().equals("getBar")) {
103 getBarMethodCounter++;
104 getBarMethodReturnTypeName = method.getReturnType().getName();
108 assertEquals(1, getFooMethodCounter);
109 assertEquals("Uint8", getFooMethodReturnTypeName);
111 assertEquals(1, getBarMethodCounter);
112 assertEquals("String", getBarMethodReturnTypeName);
116 public void testLeafListResolving() {
117 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
118 "/simple-leaf-list-demo.yang"));
120 assertNotNull(genTypes);
121 assertEquals(3, genTypes.size());
123 GeneratedType simpleContainer = genTypes.get(1);
124 GeneratedType nestedContainer = genTypes.get(2);
125 for (GeneratedType t : genTypes) {
126 if ("SimpleContainer".equals(t.getName())) {
128 } else if ("NestedContainer".equals(t.getName())) {
132 assertNotNull(simpleContainer);
133 assertNotNull(nestedContainer);
134 // FIXME: split this into getter/default/static asserts
135 assertEquals(10, simpleContainer.getMethodDefinitions().size());
136 // FIXME: split this into getter/default/static asserts
137 assertEquals(8, nestedContainer.getMethodDefinitions().size());
139 int getFooMethodCounter = 0;
140 int getBarMethodCounter = 0;
141 int getNestedContainerCounter = 0;
143 String getFooMethodReturnTypeName = "";
144 String getBarMethodReturnTypeName = "";
145 String getNestedContainerReturnTypeName = "";
146 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
147 if (method.isDefault()) {
150 if (method.getName().equals("getFoo")) {
151 getFooMethodCounter++;
152 getFooMethodReturnTypeName = method.getReturnType().getName();
155 if (method.getName().equals("getBar")) {
156 getBarMethodCounter++;
157 getBarMethodReturnTypeName = method.getReturnType().getName();
160 if (method.getName().equals("getNestedContainer")) {
161 getNestedContainerCounter++;
162 getNestedContainerReturnTypeName = method.getReturnType().getName();
166 assertEquals(1, getFooMethodCounter);
167 assertEquals("Set", getFooMethodReturnTypeName);
169 assertEquals(1, getBarMethodCounter);
170 assertEquals("String", getBarMethodReturnTypeName);
172 assertEquals(1, getNestedContainerCounter);
173 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
175 getFooMethodCounter = 0;
176 getBarMethodCounter = 0;
178 getFooMethodReturnTypeName = "";
179 getBarMethodReturnTypeName = "";
181 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
182 if (method.getName().equals("getFoo")) {
183 getFooMethodCounter++;
184 getFooMethodReturnTypeName = method.getReturnType().getName();
187 if (method.getName().equals("getBar")) {
188 getBarMethodCounter++;
189 getBarMethodReturnTypeName = method.getReturnType().getName();
193 assertEquals(1, getFooMethodCounter);
194 assertEquals("Uint8", getFooMethodReturnTypeName);
196 assertEquals(1, getBarMethodCounter);
197 assertEquals("Set", getBarMethodReturnTypeName);
201 public void testListResolving() {
202 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
203 "/simple-list-demo.yang"));
205 assertNotNull(genTypes);
206 assertEquals(5, genTypes.size());
208 int listParentContainerMethodsCount = 0;
209 int simpleListMethodsCount = 0;
210 int listChildContainerMethodsCount = 0;
211 int listKeyClassCount = 0;
213 int getSimpleListKeyMethodCount = 0;
214 int getListChildContainerMethodCount = 0;
215 int getFooMethodCount = 0;
216 int setFooMethodCount = 0;
217 int getSimpleLeafListMethodCount = 0;
218 int setSimpleLeafListMethodCount = 0;
219 int getBarMethodCount = 0;
221 String getSimpleListKeyMethodReturnTypeName = "";
222 String getListChildContainerMethodReturnTypeName = "";
224 for (final GeneratedType genType : genTypes) {
225 if (!(genType instanceof GeneratedTransferObject genTO)) {
226 if (genType.getName().equals("ListParentContainer")) {
227 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
228 } else if (genType.getName().equals("SimpleList")) {
229 simpleListMethodsCount = genType.getMethodDefinitions().size();
230 final List<MethodSignature> methods = genType.getMethodDefinitions();
231 for (final MethodSignature method : methods) {
232 switch (method.getName()) {
233 case BindingMapping.IDENTIFIABLE_KEY_NAME:
234 getSimpleListKeyMethodCount++;
235 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
237 case "getListChildContainer":
238 getListChildContainerMethodCount++;
239 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
247 case "getSimpleLeafList":
248 getSimpleLeafListMethodCount++;
250 case "setSimpleLeafList":
251 setSimpleLeafListMethodCount++;
259 } else if (genType.getName().equals("ListChildContainer")) {
260 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
263 final List<GeneratedProperty> properties = genTO.getProperties();
264 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
265 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
267 assertEquals("Unexpected key", 0, listKeyClassCount++);
268 assertEquals(1, properties.size());
269 assertEquals("listKey", properties.get(0).getName());
270 assertEquals("Byte", properties.get(0).getReturnType().getName());
271 assertTrue(properties.get(0).isReadOnly());
273 assertEquals(1, hashProps.size());
274 assertEquals("listKey", hashProps.get(0).getName());
275 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
277 assertEquals(1, equalProps.size());
278 assertEquals("listKey", equalProps.get(0).getName());
279 assertEquals("Byte", equalProps.get(0).getReturnType().getName());
283 // FIXME: split this into getter/default/static asserts
284 assertEquals(6, listParentContainerMethodsCount);
285 // FIXME: split this into getter/default/static asserts
286 assertEquals(6, listChildContainerMethodsCount);
287 assertEquals(1, getSimpleListKeyMethodCount);
288 assertEquals(1, listKeyClassCount);
290 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
292 assertEquals(1, getListChildContainerMethodCount);
293 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
294 assertEquals(1, getFooMethodCount);
295 assertEquals(0, setFooMethodCount);
296 assertEquals(1, getSimpleLeafListMethodCount);
297 assertEquals(0, setSimpleLeafListMethodCount);
298 assertEquals(1, getBarMethodCount);
300 // FIXME: split this into getter/default/static asserts
301 assertEquals(15, simpleListMethodsCount);
305 public void testListCompositeKeyResolving() {
306 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
307 "/list-composite-key.yang"));
309 assertNotNull(genTypes);
310 assertEquals(7, genTypes.size());
312 int genTypesCount = 0;
315 int compositeKeyListKeyPropertyCount = 0;
316 int compositeKeyListKeyCount = 0;
317 int innerListKeyPropertyCount = 0;
319 for (final GeneratedType type : genTypes) {
320 if (!(type instanceof GeneratedTransferObject genTO)) {
322 } else if (genTO.getName().equals("CompositeKeyListKey")) {
323 compositeKeyListKeyCount++;
324 final List<GeneratedProperty> properties = genTO.getProperties();
325 for (final GeneratedProperty prop : properties) {
326 if (prop.getName().equals("key1") || prop.getName().equals("key2")) {
327 compositeKeyListKeyPropertyCount++;
331 } else if (genTO.getName().equals("InnerListKey")) {
332 final List<GeneratedProperty> properties = genTO.getProperties();
333 innerListKeyPropertyCount = properties.size();
337 assertEquals(1, compositeKeyListKeyCount);
338 assertEquals(2, compositeKeyListKeyPropertyCount);
340 assertEquals(1, innerListKeyPropertyCount);
342 assertEquals(5, genTypesCount);
343 assertEquals(2, genTOsCount);
347 public void testGeneratedTypes() {
348 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
349 "/demo-topology.yang"));
351 assertNotNull(genTypes);
352 assertEquals(14, genTypes.size());
354 int genTypesCount = 0;
356 for (final GeneratedType type : genTypes) {
357 if (type instanceof GeneratedTransferObject) {
364 assertEquals(11, genTypesCount);
365 assertEquals(3, genTOsCount);
369 public void testAugmentRpcInput() {
370 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
371 "/augment-rpc-input.yang"));
372 assertEquals(7, genTypes.size());