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.test.util.YangParserTestUtils;
24 public class GeneratedTypesTest {
26 public void testMultipleModulesResolving() {
27 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(
28 GeneratedTypesTest.class, "/abstract-topology.yang", "/ietf/ietf-inet-types.yang"));
30 assertNotNull(genTypes);
31 assertEquals(29, genTypes.size());
35 public void testContainerResolving() {
36 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
37 "/simple-container-demo.yang"));
39 assertNotNull(genTypes);
40 assertEquals(3, genTypes.size());
42 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
43 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
44 for (Type t : genTypes) {
45 if ("SimpleContainer".equals(t.getName())) {
46 simpleContainer = (GeneratedType) t;
47 } else if ("NestedContainer".equals(t.getName())) {
48 nestedContainer = (GeneratedType) t;
51 assertNotNull(simpleContainer);
52 assertNotNull(nestedContainer);
53 // FIXME: split this into getter/default/static asserts
54 assertEquals(5, simpleContainer.getMethodDefinitions().size());
55 // FIXME: split this into getter/default/static asserts
56 assertEquals(4, nestedContainer.getMethodDefinitions().size());
58 int getFooMethodCounter = 0;
59 int getBarMethodCounter = 0;
60 int getNestedContainerCounter = 0;
62 String getFooMethodReturnTypeName = "";
63 String getBarMethodReturnTypeName = "";
64 String getNestedContainerReturnTypeName = "";
65 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
66 if (method.getName().equals("getFoo")) {
67 getFooMethodCounter++;
68 getFooMethodReturnTypeName = method.getReturnType().getName();
71 if (method.getName().equals("getBar")) {
72 getBarMethodCounter++;
73 getBarMethodReturnTypeName = method.getReturnType().getName();
76 if (method.getName().equals("getNestedContainer")) {
77 getNestedContainerCounter++;
78 getNestedContainerReturnTypeName = method.getReturnType().getName();
82 assertEquals(1, getFooMethodCounter);
83 assertEquals("Integer", getFooMethodReturnTypeName);
85 assertEquals(1, getBarMethodCounter);
86 assertEquals("String", getBarMethodReturnTypeName);
88 assertEquals(1, getNestedContainerCounter);
89 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
91 getFooMethodCounter = 0;
92 getBarMethodCounter = 0;
94 getFooMethodReturnTypeName = "";
95 getBarMethodReturnTypeName = "";
97 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
99 if (method.getName().equals("getFoo")) {
100 getFooMethodCounter++;
101 getFooMethodReturnTypeName = method.getReturnType().getName();
104 if (method.getName().equals("getBar")) {
105 getBarMethodCounter++;
106 getBarMethodReturnTypeName = method.getReturnType().getName();
110 assertEquals(1, getFooMethodCounter);
111 assertEquals("Uint8", getFooMethodReturnTypeName);
113 assertEquals(1, getBarMethodCounter);
114 assertEquals("String", getBarMethodReturnTypeName);
118 public void testLeafListResolving() {
119 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
120 "/simple-leaf-list-demo.yang"));
122 assertNotNull(genTypes);
123 assertEquals(3, genTypes.size());
125 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
126 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
127 for (Type t : genTypes) {
128 if ("SimpleContainer".equals(t.getName())) {
129 simpleContainer = (GeneratedType) t;
130 } else if ("NestedContainer".equals(t.getName())) {
131 nestedContainer = (GeneratedType) t;
134 assertNotNull(simpleContainer);
135 assertNotNull(nestedContainer);
136 // FIXME: split this into getter/default/static asserts
137 assertEquals(5, simpleContainer.getMethodDefinitions().size());
138 // FIXME: split this into getter/default/static asserts
139 assertEquals(4, nestedContainer.getMethodDefinitions().size());
141 int getFooMethodCounter = 0;
142 int getBarMethodCounter = 0;
143 int getNestedContainerCounter = 0;
145 String getFooMethodReturnTypeName = "";
146 String getBarMethodReturnTypeName = "";
147 String getNestedContainerReturnTypeName = "";
148 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
149 if (method.isDefault()) {
152 if (method.getName().equals("getFoo")) {
153 getFooMethodCounter++;
154 getFooMethodReturnTypeName = method.getReturnType().getName();
157 if (method.getName().equals("getBar")) {
158 getBarMethodCounter++;
159 getBarMethodReturnTypeName = method.getReturnType().getName();
162 if (method.getName().equals("getNestedContainer")) {
163 getNestedContainerCounter++;
164 getNestedContainerReturnTypeName = method.getReturnType().getName();
168 assertEquals(1, getFooMethodCounter);
169 assertEquals("List", getFooMethodReturnTypeName);
171 assertEquals(1, getBarMethodCounter);
172 assertEquals("String", getBarMethodReturnTypeName);
174 assertEquals(1, getNestedContainerCounter);
175 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
177 getFooMethodCounter = 0;
178 getBarMethodCounter = 0;
180 getFooMethodReturnTypeName = "";
181 getBarMethodReturnTypeName = "";
183 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
184 if (method.getName().equals("getFoo")) {
185 getFooMethodCounter++;
186 getFooMethodReturnTypeName = method.getReturnType().getName();
189 if (method.getName().equals("getBar")) {
190 getBarMethodCounter++;
191 getBarMethodReturnTypeName = method.getReturnType().getName();
195 assertEquals(1, getFooMethodCounter);
196 assertEquals("Uint8", getFooMethodReturnTypeName);
198 assertEquals(1, getBarMethodCounter);
199 assertEquals("List", getBarMethodReturnTypeName);
203 public void testListResolving() {
204 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
205 "/simple-list-demo.yang"));
207 assertNotNull(genTypes);
208 assertEquals(5, genTypes.size());
210 int listParentContainerMethodsCount = 0;
211 int simpleListMethodsCount = 0;
212 int listChildContainerMethodsCount = 0;
213 int listKeyClassCount = 0;
215 int getSimpleListKeyMethodCount = 0;
216 int getListChildContainerMethodCount = 0;
217 int getFooMethodCount = 0;
218 int setFooMethodCount = 0;
219 int getSimpleLeafListMethodCount = 0;
220 int setSimpleLeafListMethodCount = 0;
221 int getBarMethodCount = 0;
223 String getSimpleListKeyMethodReturnTypeName = "";
224 String getListChildContainerMethodReturnTypeName = "";
226 int listKeyClassPropertyCount = 0;
227 String listKeyClassPropertyName = "";
228 String listKeyClassPropertyTypeName = "";
229 boolean listKeyClassPropertyReadOnly = false;
231 int hashMethodParameterCount = 0;
232 String hashMethodParameterName = "";
233 String hashMethodParameterReturnTypeName = "";
235 int equalMethodParameterCount = 0;
236 String equalMethodParameterName = "";
237 String equalMethodParameterReturnTypeName = "";
239 for (final Type type : genTypes) {
240 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
241 final GeneratedType genType = (GeneratedType) type;
242 if (genType.getName().equals("ListParentContainer")) {
243 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
244 } else if (genType.getName().equals("SimpleList")) {
245 simpleListMethodsCount = genType.getMethodDefinitions().size();
246 final List<MethodSignature> methods = genType.getMethodDefinitions();
247 for (final MethodSignature method : methods) {
248 switch (method.getName()) {
249 case BindingMapping.IDENTIFIABLE_KEY_NAME:
250 getSimpleListKeyMethodCount++;
251 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
253 case "getListChildContainer":
254 getListChildContainerMethodCount++;
255 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
263 case "getSimpleLeafList":
264 getSimpleLeafListMethodCount++;
266 case "setSimpleLeafList":
267 setSimpleLeafListMethodCount++;
275 } else if (genType.getName().equals("ListChildContainer")) {
276 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
278 } else if (type instanceof GeneratedTransferObject) {
279 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
280 final List<GeneratedProperty> properties = genTO.getProperties();
281 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
282 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
285 listKeyClassPropertyCount = properties.size();
286 listKeyClassPropertyName = properties.get(0).getName();
287 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
288 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
290 hashMethodParameterCount = hashProps.size();
291 hashMethodParameterName = hashProps.get(0).getName();
292 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
294 equalMethodParameterCount = equalProps.size();
295 equalMethodParameterName = equalProps.get(0).getName();
296 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
301 // FIXME: split this into getter/default/static asserts
302 assertEquals(4, listParentContainerMethodsCount);
303 // FIXME: split this into getter/default/static asserts
304 assertEquals(3, listChildContainerMethodsCount);
305 assertEquals(1, getSimpleListKeyMethodCount);
306 assertEquals(1, listKeyClassCount);
308 assertEquals(1, listKeyClassPropertyCount);
309 assertEquals("listKey", listKeyClassPropertyName);
310 assertEquals("Byte", listKeyClassPropertyTypeName);
311 assertTrue(listKeyClassPropertyReadOnly);
312 assertEquals(1, hashMethodParameterCount);
313 assertEquals("listKey", hashMethodParameterName);
314 assertEquals("Byte", hashMethodParameterReturnTypeName);
315 assertEquals(1, equalMethodParameterCount);
316 assertEquals("listKey", equalMethodParameterName);
317 assertEquals("Byte", equalMethodParameterReturnTypeName);
319 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
321 assertEquals(1, getListChildContainerMethodCount);
322 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
323 assertEquals(1, getFooMethodCount);
324 assertEquals(0, setFooMethodCount);
325 assertEquals(1, getSimpleLeafListMethodCount);
326 assertEquals(0, setSimpleLeafListMethodCount);
327 assertEquals(1, getBarMethodCount);
329 // FIXME: split this into getter/default/static asserts
330 assertEquals(8, simpleListMethodsCount);
334 public void testListCompositeKeyResolving() {
335 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
336 "/list-composite-key.yang"));
338 assertNotNull(genTypes);
339 assertEquals(7, genTypes.size());
341 int genTypesCount = 0;
344 int compositeKeyListKeyPropertyCount = 0;
345 int compositeKeyListKeyCount = 0;
346 int innerListKeyPropertyCount = 0;
348 for (final Type type : genTypes) {
349 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
351 } else if (type instanceof GeneratedTransferObject) {
352 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
354 if (genTO.getName().equals("CompositeKeyListKey")) {
355 compositeKeyListKeyCount++;
356 final List<GeneratedProperty> properties = genTO.getProperties();
357 for (final GeneratedProperty prop : properties) {
358 if (prop.getName().equals("key1")) {
359 compositeKeyListKeyPropertyCount++;
360 } else if (prop.getName().equals("key2")) {
361 compositeKeyListKeyPropertyCount++;
365 } else if (genTO.getName().equals("InnerListKey")) {
366 final List<GeneratedProperty> properties = genTO.getProperties();
367 innerListKeyPropertyCount = properties.size();
372 assertEquals(1, compositeKeyListKeyCount);
373 assertEquals(2, compositeKeyListKeyPropertyCount);
375 assertEquals(1, innerListKeyPropertyCount);
377 assertEquals(5, genTypesCount);
378 assertEquals(2, genTOsCount);
382 public void testGeneratedTypes() {
383 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
384 "/demo-topology.yang"));
386 assertNotNull(genTypes);
387 assertEquals(14, genTypes.size());
389 int genTypesCount = 0;
391 for (final Type type : genTypes) {
392 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
394 } else if (type instanceof GeneratedTransferObject) {
399 assertEquals(11, genTypesCount);
400 assertEquals(3, genTOsCount);