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.generator.api.BindingGenerator;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 public class GeneratedTypesTest {
29 public void testMultipleModulesResolving() {
30 final SchemaContext context = YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
31 "/abstract-topology.yang", "/ietf/ietf-inet-types.yang");
32 assertNotNull(context);
34 final BindingGenerator bindingGen = new BindingGeneratorImpl();
35 final List<Type> genTypes = bindingGen.generateTypes(context);
37 assertNotNull(genTypes);
38 assertEquals(29, genTypes.size());
42 public void testContainerResolving() {
43 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-container-demo.yang");
44 assertNotNull(context);
46 final BindingGenerator bindingGen = new BindingGeneratorImpl();
47 final List<Type> genTypes = bindingGen.generateTypes(context);
49 assertNotNull(genTypes);
50 assertEquals(3, genTypes.size());
52 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
53 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
54 for (Type t : genTypes) {
55 if ("SimpleContainer".equals(t.getName())) {
56 simpleContainer = (GeneratedType) t;
57 } else if ("NestedContainer".equals(t.getName())) {
58 nestedContainer = (GeneratedType) t;
61 assertNotNull(simpleContainer);
62 assertNotNull(nestedContainer);
63 assertEquals(4, simpleContainer.getMethodDefinitions().size());
64 assertEquals(3, nestedContainer.getMethodDefinitions().size());
66 int getFooMethodCounter = 0;
67 int getBarMethodCounter = 0;
68 int getNestedContainerCounter = 0;
70 String getFooMethodReturnTypeName = "";
71 String getBarMethodReturnTypeName = "";
72 String getNestedContainerReturnTypeName = "";
73 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
74 if (method.getName().equals("getFoo")) {
75 getFooMethodCounter++;
76 getFooMethodReturnTypeName = method.getReturnType().getName();
79 if (method.getName().equals("getBar")) {
80 getBarMethodCounter++;
81 getBarMethodReturnTypeName = method.getReturnType().getName();
84 if (method.getName().equals("getNestedContainer")) {
85 getNestedContainerCounter++;
86 getNestedContainerReturnTypeName = method.getReturnType().getName();
90 assertEquals(1, getFooMethodCounter);
91 assertEquals("Integer", getFooMethodReturnTypeName);
93 assertEquals(1, getBarMethodCounter);
94 assertEquals("String", getBarMethodReturnTypeName);
96 assertEquals(1, getNestedContainerCounter);
97 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
99 getFooMethodCounter = 0;
100 getBarMethodCounter = 0;
102 getFooMethodReturnTypeName = "";
103 getBarMethodReturnTypeName = "";
105 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
107 if (method.getName().equals("getFoo")) {
108 getFooMethodCounter++;
109 getFooMethodReturnTypeName = method.getReturnType().getName();
112 if (method.getName().equals("getBar")) {
113 getBarMethodCounter++;
114 getBarMethodReturnTypeName = method.getReturnType().getName();
118 assertEquals(1, getFooMethodCounter);
119 assertEquals("Uint8", getFooMethodReturnTypeName);
121 assertEquals(1, getBarMethodCounter);
122 assertEquals("String", getBarMethodReturnTypeName);
126 public void testLeafListResolving() {
127 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-leaf-list-demo.yang");
128 assertNotNull(context);
130 final BindingGenerator bindingGen = new BindingGeneratorImpl();
131 final List<Type> genTypes = bindingGen.generateTypes(context);
133 assertNotNull(genTypes);
134 assertEquals(3, genTypes.size());
136 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
137 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
138 for (Type t : genTypes) {
139 if ("SimpleContainer".equals(t.getName())) {
140 simpleContainer = (GeneratedType) t;
141 } else if ("NestedContainer".equals(t.getName())) {
142 nestedContainer = (GeneratedType) t;
145 assertNotNull(simpleContainer);
146 assertNotNull(nestedContainer);
147 assertEquals(4, simpleContainer.getMethodDefinitions().size());
148 assertEquals(3, nestedContainer.getMethodDefinitions().size());
150 int getFooMethodCounter = 0;
151 int getBarMethodCounter = 0;
152 int getNestedContainerCounter = 0;
154 String getFooMethodReturnTypeName = "";
155 String getBarMethodReturnTypeName = "";
156 String getNestedContainerReturnTypeName = "";
157 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
158 if (method.isDefault()) {
161 if (method.getName().equals("getFoo")) {
162 getFooMethodCounter++;
163 getFooMethodReturnTypeName = method.getReturnType().getName();
166 if (method.getName().equals("getBar")) {
167 getBarMethodCounter++;
168 getBarMethodReturnTypeName = method.getReturnType().getName();
171 if (method.getName().equals("getNestedContainer")) {
172 getNestedContainerCounter++;
173 getNestedContainerReturnTypeName = method.getReturnType().getName();
177 assertEquals(1, getFooMethodCounter);
178 assertEquals("List", getFooMethodReturnTypeName);
180 assertEquals(1, getBarMethodCounter);
181 assertEquals("String", getBarMethodReturnTypeName);
183 assertEquals(1, getNestedContainerCounter);
184 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
186 getFooMethodCounter = 0;
187 getBarMethodCounter = 0;
189 getFooMethodReturnTypeName = "";
190 getBarMethodReturnTypeName = "";
192 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
193 if (method.getName().equals("getFoo")) {
194 getFooMethodCounter++;
195 getFooMethodReturnTypeName = method.getReturnType().getName();
198 if (method.getName().equals("getBar")) {
199 getBarMethodCounter++;
200 getBarMethodReturnTypeName = method.getReturnType().getName();
204 assertEquals(1, getFooMethodCounter);
205 assertEquals("Uint8", getFooMethodReturnTypeName);
207 assertEquals(1, getBarMethodCounter);
208 assertEquals("List", getBarMethodReturnTypeName);
212 public void testListResolving() {
213 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-list-demo.yang");
214 assertNotNull(context);
216 final BindingGenerator bindingGen = new BindingGeneratorImpl();
217 final List<Type> genTypes = bindingGen.generateTypes(context);
219 assertNotNull(genTypes);
220 assertEquals(5, genTypes.size());
222 int listParentContainerMethodsCount = 0;
223 int simpleListMethodsCount = 0;
224 int listChildContainerMethodsCount = 0;
225 int listKeyClassCount = 0;
227 int getSimpleListKeyMethodCount = 0;
228 int getListChildContainerMethodCount = 0;
229 int getFooMethodCount = 0;
230 int setFooMethodCount = 0;
231 int getSimpleLeafListMethodCount = 0;
232 int setSimpleLeafListMethodCount = 0;
233 int getBarMethodCount = 0;
235 String getSimpleListKeyMethodReturnTypeName = "";
236 String getListChildContainerMethodReturnTypeName = "";
238 int listKeyClassPropertyCount = 0;
239 String listKeyClassPropertyName = "";
240 String listKeyClassPropertyTypeName = "";
241 boolean listKeyClassPropertyReadOnly = false;
243 int hashMethodParameterCount = 0;
244 String hashMethodParameterName = "";
245 String hashMethodParameterReturnTypeName = "";
247 int equalMethodParameterCount = 0;
248 String equalMethodParameterName = "";
249 String equalMethodParameterReturnTypeName = "";
251 for (final Type type : genTypes) {
252 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
253 final GeneratedType genType = (GeneratedType) type;
254 if (genType.getName().equals("ListParentContainer")) {
255 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
256 } else if (genType.getName().equals("SimpleList")) {
257 simpleListMethodsCount = genType.getMethodDefinitions().size();
258 final List<MethodSignature> methods = genType.getMethodDefinitions();
259 for (final MethodSignature method : methods) {
260 switch (method.getName()) {
261 case BindingMapping.IDENTIFIABLE_KEY_NAME:
262 getSimpleListKeyMethodCount++;
263 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
265 case "getListChildContainer":
266 getListChildContainerMethodCount++;
267 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
275 case "getSimpleLeafList":
276 getSimpleLeafListMethodCount++;
278 case "setSimpleLeafList":
279 setSimpleLeafListMethodCount++;
287 } else if (genType.getName().equals("ListChildContainer")) {
288 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
290 } else if (type instanceof GeneratedTransferObject) {
291 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
292 final List<GeneratedProperty> properties = genTO.getProperties();
293 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
294 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
297 listKeyClassPropertyCount = properties.size();
298 listKeyClassPropertyName = properties.get(0).getName();
299 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
300 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
302 hashMethodParameterCount = hashProps.size();
303 hashMethodParameterName = hashProps.get(0).getName();
304 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
306 equalMethodParameterCount = equalProps.size();
307 equalMethodParameterName = equalProps.get(0).getName();
308 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
313 assertEquals(3, listParentContainerMethodsCount);
314 assertEquals(2, listChildContainerMethodsCount);
315 assertEquals(1, getSimpleListKeyMethodCount);
316 assertEquals(1, listKeyClassCount);
318 assertEquals(1, listKeyClassPropertyCount);
319 assertEquals("listKey", listKeyClassPropertyName);
320 assertEquals("Byte", listKeyClassPropertyTypeName);
321 assertTrue(listKeyClassPropertyReadOnly);
322 assertEquals(1, hashMethodParameterCount);
323 assertEquals("listKey", hashMethodParameterName);
324 assertEquals("Byte", hashMethodParameterReturnTypeName);
325 assertEquals(1, equalMethodParameterCount);
326 assertEquals("listKey", equalMethodParameterName);
327 assertEquals("Byte", equalMethodParameterReturnTypeName);
329 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
331 assertEquals(1, getListChildContainerMethodCount);
332 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
333 assertEquals(1, getFooMethodCount);
334 assertEquals(0, setFooMethodCount);
335 assertEquals(1, getSimpleLeafListMethodCount);
336 assertEquals(0, setSimpleLeafListMethodCount);
337 assertEquals(1, getBarMethodCount);
339 assertEquals(7, simpleListMethodsCount);
343 public void testListCompositeKeyResolving() {
344 final SchemaContext context = YangParserTestUtils.parseYangResource("/list-composite-key.yang");
345 assertNotNull(context);
347 final BindingGenerator bindingGen = new BindingGeneratorImpl();
348 final List<Type> genTypes = bindingGen.generateTypes(context);
350 assertNotNull(genTypes);
351 assertEquals(7, genTypes.size());
353 int genTypesCount = 0;
356 int compositeKeyListKeyPropertyCount = 0;
357 int compositeKeyListKeyCount = 0;
358 int innerListKeyPropertyCount = 0;
360 for (final Type type : genTypes) {
361 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
363 } else if (type instanceof GeneratedTransferObject) {
364 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
366 if (genTO.getName().equals("CompositeKeyListKey")) {
367 compositeKeyListKeyCount++;
368 final List<GeneratedProperty> properties = genTO.getProperties();
369 for (final GeneratedProperty prop : properties) {
370 if (prop.getName().equals("key1")) {
371 compositeKeyListKeyPropertyCount++;
372 } else if (prop.getName().equals("key2")) {
373 compositeKeyListKeyPropertyCount++;
377 } else if (genTO.getName().equals("InnerListKey")) {
378 final List<GeneratedProperty> properties = genTO.getProperties();
379 innerListKeyPropertyCount = properties.size();
384 assertEquals(1, compositeKeyListKeyCount);
385 assertEquals(2, compositeKeyListKeyPropertyCount);
387 assertEquals(1, innerListKeyPropertyCount);
389 assertEquals(5, genTypesCount);
390 assertEquals(2, genTOsCount);
394 public void testGeneratedTypes() {
395 final SchemaContext context = YangParserTestUtils.parseYangResource("/demo-topology.yang");
396 assertNotNull(context);
398 final BindingGenerator bindingGen = new BindingGeneratorImpl();
399 final List<Type> genTypes = bindingGen.generateTypes(context);
401 assertNotNull(genTypes);
402 assertEquals(14, genTypes.size());
404 int genTypesCount = 0;
406 for (final Type type : genTypes) {
407 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
409 } else if (type instanceof GeneratedTransferObject) {
414 assertEquals(11, genTypesCount);
415 assertEquals(3, genTOsCount);