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.model.api.Type;
21 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class GeneratedTypesTest {
28 public void testMultipleModulesResolving() {
29 final SchemaContext context = YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
30 "/abstract-topology.yang", "/ietf/ietf-inet-types.yang");
31 assertNotNull(context);
33 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
35 assertNotNull(genTypes);
36 assertEquals(29, genTypes.size());
40 public void testContainerResolving() {
41 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-container-demo.yang");
42 assertNotNull(context);
44 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
46 assertNotNull(genTypes);
47 assertEquals(3, genTypes.size());
49 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
50 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
51 for (Type t : genTypes) {
52 if ("SimpleContainer".equals(t.getName())) {
53 simpleContainer = (GeneratedType) t;
54 } else if ("NestedContainer".equals(t.getName())) {
55 nestedContainer = (GeneratedType) t;
58 assertNotNull(simpleContainer);
59 assertNotNull(nestedContainer);
60 assertEquals(4, simpleContainer.getMethodDefinitions().size());
61 assertEquals(3, nestedContainer.getMethodDefinitions().size());
63 int getFooMethodCounter = 0;
64 int getBarMethodCounter = 0;
65 int getNestedContainerCounter = 0;
67 String getFooMethodReturnTypeName = "";
68 String getBarMethodReturnTypeName = "";
69 String getNestedContainerReturnTypeName = "";
70 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
71 if (method.getName().equals("getFoo")) {
72 getFooMethodCounter++;
73 getFooMethodReturnTypeName = method.getReturnType().getName();
76 if (method.getName().equals("getBar")) {
77 getBarMethodCounter++;
78 getBarMethodReturnTypeName = method.getReturnType().getName();
81 if (method.getName().equals("getNestedContainer")) {
82 getNestedContainerCounter++;
83 getNestedContainerReturnTypeName = method.getReturnType().getName();
87 assertEquals(1, getFooMethodCounter);
88 assertEquals("Integer", getFooMethodReturnTypeName);
90 assertEquals(1, getBarMethodCounter);
91 assertEquals("String", getBarMethodReturnTypeName);
93 assertEquals(1, getNestedContainerCounter);
94 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
96 getFooMethodCounter = 0;
97 getBarMethodCounter = 0;
99 getFooMethodReturnTypeName = "";
100 getBarMethodReturnTypeName = "";
102 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
104 if (method.getName().equals("getFoo")) {
105 getFooMethodCounter++;
106 getFooMethodReturnTypeName = method.getReturnType().getName();
109 if (method.getName().equals("getBar")) {
110 getBarMethodCounter++;
111 getBarMethodReturnTypeName = method.getReturnType().getName();
115 assertEquals(1, getFooMethodCounter);
116 assertEquals("Uint8", getFooMethodReturnTypeName);
118 assertEquals(1, getBarMethodCounter);
119 assertEquals("String", getBarMethodReturnTypeName);
123 public void testLeafListResolving() {
124 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-leaf-list-demo.yang");
125 assertNotNull(context);
127 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
129 assertNotNull(genTypes);
130 assertEquals(3, genTypes.size());
132 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
133 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
134 for (Type t : genTypes) {
135 if ("SimpleContainer".equals(t.getName())) {
136 simpleContainer = (GeneratedType) t;
137 } else if ("NestedContainer".equals(t.getName())) {
138 nestedContainer = (GeneratedType) t;
141 assertNotNull(simpleContainer);
142 assertNotNull(nestedContainer);
143 assertEquals(4, simpleContainer.getMethodDefinitions().size());
144 assertEquals(3, nestedContainer.getMethodDefinitions().size());
146 int getFooMethodCounter = 0;
147 int getBarMethodCounter = 0;
148 int getNestedContainerCounter = 0;
150 String getFooMethodReturnTypeName = "";
151 String getBarMethodReturnTypeName = "";
152 String getNestedContainerReturnTypeName = "";
153 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
154 if (method.isDefault()) {
157 if (method.getName().equals("getFoo")) {
158 getFooMethodCounter++;
159 getFooMethodReturnTypeName = method.getReturnType().getName();
162 if (method.getName().equals("getBar")) {
163 getBarMethodCounter++;
164 getBarMethodReturnTypeName = method.getReturnType().getName();
167 if (method.getName().equals("getNestedContainer")) {
168 getNestedContainerCounter++;
169 getNestedContainerReturnTypeName = method.getReturnType().getName();
173 assertEquals(1, getFooMethodCounter);
174 assertEquals("List", getFooMethodReturnTypeName);
176 assertEquals(1, getBarMethodCounter);
177 assertEquals("String", getBarMethodReturnTypeName);
179 assertEquals(1, getNestedContainerCounter);
180 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
182 getFooMethodCounter = 0;
183 getBarMethodCounter = 0;
185 getFooMethodReturnTypeName = "";
186 getBarMethodReturnTypeName = "";
188 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
189 if (method.getName().equals("getFoo")) {
190 getFooMethodCounter++;
191 getFooMethodReturnTypeName = method.getReturnType().getName();
194 if (method.getName().equals("getBar")) {
195 getBarMethodCounter++;
196 getBarMethodReturnTypeName = method.getReturnType().getName();
200 assertEquals(1, getFooMethodCounter);
201 assertEquals("Uint8", getFooMethodReturnTypeName);
203 assertEquals(1, getBarMethodCounter);
204 assertEquals("List", getBarMethodReturnTypeName);
208 public void testListResolving() {
209 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-list-demo.yang");
210 assertNotNull(context);
212 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
214 assertNotNull(genTypes);
215 assertEquals(5, genTypes.size());
217 int listParentContainerMethodsCount = 0;
218 int simpleListMethodsCount = 0;
219 int listChildContainerMethodsCount = 0;
220 int listKeyClassCount = 0;
222 int getSimpleListKeyMethodCount = 0;
223 int getListChildContainerMethodCount = 0;
224 int getFooMethodCount = 0;
225 int setFooMethodCount = 0;
226 int getSimpleLeafListMethodCount = 0;
227 int setSimpleLeafListMethodCount = 0;
228 int getBarMethodCount = 0;
230 String getSimpleListKeyMethodReturnTypeName = "";
231 String getListChildContainerMethodReturnTypeName = "";
233 int listKeyClassPropertyCount = 0;
234 String listKeyClassPropertyName = "";
235 String listKeyClassPropertyTypeName = "";
236 boolean listKeyClassPropertyReadOnly = false;
238 int hashMethodParameterCount = 0;
239 String hashMethodParameterName = "";
240 String hashMethodParameterReturnTypeName = "";
242 int equalMethodParameterCount = 0;
243 String equalMethodParameterName = "";
244 String equalMethodParameterReturnTypeName = "";
246 for (final Type type : genTypes) {
247 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
248 final GeneratedType genType = (GeneratedType) type;
249 if (genType.getName().equals("ListParentContainer")) {
250 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
251 } else if (genType.getName().equals("SimpleList")) {
252 simpleListMethodsCount = genType.getMethodDefinitions().size();
253 final List<MethodSignature> methods = genType.getMethodDefinitions();
254 for (final MethodSignature method : methods) {
255 switch (method.getName()) {
256 case BindingMapping.IDENTIFIABLE_KEY_NAME:
257 getSimpleListKeyMethodCount++;
258 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
260 case "getListChildContainer":
261 getListChildContainerMethodCount++;
262 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
270 case "getSimpleLeafList":
271 getSimpleLeafListMethodCount++;
273 case "setSimpleLeafList":
274 setSimpleLeafListMethodCount++;
282 } else if (genType.getName().equals("ListChildContainer")) {
283 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
285 } else if (type instanceof GeneratedTransferObject) {
286 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
287 final List<GeneratedProperty> properties = genTO.getProperties();
288 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
289 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
292 listKeyClassPropertyCount = properties.size();
293 listKeyClassPropertyName = properties.get(0).getName();
294 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
295 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
297 hashMethodParameterCount = hashProps.size();
298 hashMethodParameterName = hashProps.get(0).getName();
299 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
301 equalMethodParameterCount = equalProps.size();
302 equalMethodParameterName = equalProps.get(0).getName();
303 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
308 assertEquals(3, listParentContainerMethodsCount);
309 assertEquals(2, listChildContainerMethodsCount);
310 assertEquals(1, getSimpleListKeyMethodCount);
311 assertEquals(1, listKeyClassCount);
313 assertEquals(1, listKeyClassPropertyCount);
314 assertEquals("listKey", listKeyClassPropertyName);
315 assertEquals("Byte", listKeyClassPropertyTypeName);
316 assertTrue(listKeyClassPropertyReadOnly);
317 assertEquals(1, hashMethodParameterCount);
318 assertEquals("listKey", hashMethodParameterName);
319 assertEquals("Byte", hashMethodParameterReturnTypeName);
320 assertEquals(1, equalMethodParameterCount);
321 assertEquals("listKey", equalMethodParameterName);
322 assertEquals("Byte", equalMethodParameterReturnTypeName);
324 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
326 assertEquals(1, getListChildContainerMethodCount);
327 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
328 assertEquals(1, getFooMethodCount);
329 assertEquals(0, setFooMethodCount);
330 assertEquals(1, getSimpleLeafListMethodCount);
331 assertEquals(0, setSimpleLeafListMethodCount);
332 assertEquals(1, getBarMethodCount);
334 assertEquals(7, simpleListMethodsCount);
338 public void testListCompositeKeyResolving() {
339 final SchemaContext context = YangParserTestUtils.parseYangResource("/list-composite-key.yang");
340 assertNotNull(context);
342 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
344 assertNotNull(genTypes);
345 assertEquals(7, genTypes.size());
347 int genTypesCount = 0;
350 int compositeKeyListKeyPropertyCount = 0;
351 int compositeKeyListKeyCount = 0;
352 int innerListKeyPropertyCount = 0;
354 for (final Type type : genTypes) {
355 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
357 } else if (type instanceof GeneratedTransferObject) {
358 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
360 if (genTO.getName().equals("CompositeKeyListKey")) {
361 compositeKeyListKeyCount++;
362 final List<GeneratedProperty> properties = genTO.getProperties();
363 for (final GeneratedProperty prop : properties) {
364 if (prop.getName().equals("key1")) {
365 compositeKeyListKeyPropertyCount++;
366 } else if (prop.getName().equals("key2")) {
367 compositeKeyListKeyPropertyCount++;
371 } else if (genTO.getName().equals("InnerListKey")) {
372 final List<GeneratedProperty> properties = genTO.getProperties();
373 innerListKeyPropertyCount = properties.size();
378 assertEquals(1, compositeKeyListKeyCount);
379 assertEquals(2, compositeKeyListKeyPropertyCount);
381 assertEquals(1, innerListKeyPropertyCount);
383 assertEquals(5, genTypesCount);
384 assertEquals(2, genTOsCount);
388 public void testGeneratedTypes() {
389 final SchemaContext context = YangParserTestUtils.parseYangResource("/demo-topology.yang");
390 assertNotNull(context);
392 final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
394 assertNotNull(genTypes);
395 assertEquals(14, genTypes.size());
397 int genTypesCount = 0;
399 for (final Type type : genTypes) {
400 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
402 } else if (type instanceof GeneratedTransferObject) {
407 assertEquals(11, genTypesCount);
408 assertEquals(3, genTOsCount);