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(3, simpleContainer.getMethodDefinitions().size());
64 assertEquals(2, 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("Short", 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(3, simpleContainer.getMethodDefinitions().size());
148 assertEquals(2, 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.getName().equals("getFoo")) {
159 getFooMethodCounter++;
160 getFooMethodReturnTypeName = method.getReturnType().getName();
163 if (method.getName().equals("getBar")) {
164 getBarMethodCounter++;
165 getBarMethodReturnTypeName = method.getReturnType().getName();
168 if (method.getName().equals("getNestedContainer")) {
169 getNestedContainerCounter++;
170 getNestedContainerReturnTypeName = method.getReturnType().getName();
174 assertEquals(1, getFooMethodCounter);
175 assertEquals("List", getFooMethodReturnTypeName);
177 assertEquals(1, getBarMethodCounter);
178 assertEquals("String", getBarMethodReturnTypeName);
180 assertEquals(1, getNestedContainerCounter);
181 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
183 getFooMethodCounter = 0;
184 getBarMethodCounter = 0;
186 getFooMethodReturnTypeName = "";
187 getBarMethodReturnTypeName = "";
189 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
190 if (method.getName().equals("getFoo")) {
191 getFooMethodCounter++;
192 getFooMethodReturnTypeName = method.getReturnType().getName();
195 if (method.getName().equals("getBar")) {
196 getBarMethodCounter++;
197 getBarMethodReturnTypeName = method.getReturnType().getName();
201 assertEquals(1, getFooMethodCounter);
202 assertEquals("Short", getFooMethodReturnTypeName);
204 assertEquals(1, getBarMethodCounter);
205 assertEquals("List", getBarMethodReturnTypeName);
209 public void testListResolving() {
210 final SchemaContext context = YangParserTestUtils.parseYangResource("/simple-list-demo.yang");
211 assertNotNull(context);
213 final BindingGenerator bindingGen = new BindingGeneratorImpl();
214 final List<Type> genTypes = bindingGen.generateTypes(context);
216 assertNotNull(genTypes);
217 assertEquals(5, genTypes.size());
219 int listParentContainerMethodsCount = 0;
220 int simpleListMethodsCount = 0;
221 int listChildContainerMethodsCount = 0;
222 int listKeyClassCount = 0;
224 int getSimpleListKeyMethodCount = 0;
225 int getListChildContainerMethodCount = 0;
226 int getFooMethodCount = 0;
227 int setFooMethodCount = 0;
228 int getSimpleLeafListMethodCount = 0;
229 int setSimpleLeafListMethodCount = 0;
230 int getBarMethodCount = 0;
232 String getSimpleListKeyMethodReturnTypeName = "";
233 String getListChildContainerMethodReturnTypeName = "";
235 int listKeyClassPropertyCount = 0;
236 String listKeyClassPropertyName = "";
237 String listKeyClassPropertyTypeName = "";
238 boolean listKeyClassPropertyReadOnly = false;
240 int hashMethodParameterCount = 0;
241 String hashMethodParameterName = "";
242 String hashMethodParameterReturnTypeName = "";
244 int equalMethodParameterCount = 0;
245 String equalMethodParameterName = "";
246 String equalMethodParameterReturnTypeName = "";
248 for (final Type type : genTypes) {
249 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
250 final GeneratedType genType = (GeneratedType) type;
251 if (genType.getName().equals("ListParentContainer")) {
252 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
253 } else if (genType.getName().equals("SimpleList")) {
254 simpleListMethodsCount = genType.getMethodDefinitions().size();
255 final List<MethodSignature> methods = genType.getMethodDefinitions();
256 for (final MethodSignature method : methods) {
257 switch (method.getName()) {
258 case BindingMapping.IDENTIFIABLE_KEY_NAME:
259 getSimpleListKeyMethodCount++;
260 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
262 case "getListChildContainer":
263 getListChildContainerMethodCount++;
264 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
272 case "getSimpleLeafList":
273 getSimpleLeafListMethodCount++;
275 case "setSimpleLeafList":
276 setSimpleLeafListMethodCount++;
284 } else if (genType.getName().equals("ListChildContainer")) {
285 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
287 } else if (type instanceof GeneratedTransferObject) {
288 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
289 final List<GeneratedProperty> properties = genTO.getProperties();
290 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
291 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
294 listKeyClassPropertyCount = properties.size();
295 listKeyClassPropertyName = properties.get(0).getName();
296 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
297 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
299 hashMethodParameterCount = hashProps.size();
300 hashMethodParameterName = hashProps.get(0).getName();
301 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
303 equalMethodParameterCount = equalProps.size();
304 equalMethodParameterName = equalProps.get(0).getName();
305 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
310 assertEquals(2, listParentContainerMethodsCount);
311 assertEquals(1, listChildContainerMethodsCount);
312 assertEquals(1, getSimpleListKeyMethodCount);
313 assertEquals(1, listKeyClassCount);
315 assertEquals(1, listKeyClassPropertyCount);
316 assertEquals("listKey", listKeyClassPropertyName);
317 assertEquals("Byte", listKeyClassPropertyTypeName);
318 assertTrue(listKeyClassPropertyReadOnly);
319 assertEquals(1, hashMethodParameterCount);
320 assertEquals("listKey", hashMethodParameterName);
321 assertEquals("Byte", hashMethodParameterReturnTypeName);
322 assertEquals(1, equalMethodParameterCount);
323 assertEquals("listKey", equalMethodParameterName);
324 assertEquals("Byte", equalMethodParameterReturnTypeName);
326 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
328 assertEquals(1, getListChildContainerMethodCount);
329 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
330 assertEquals(1, getFooMethodCount);
331 assertEquals(0, setFooMethodCount);
332 assertEquals(1, getSimpleLeafListMethodCount);
333 assertEquals(0, setSimpleLeafListMethodCount);
334 assertEquals(1, getBarMethodCount);
336 assertEquals(6, simpleListMethodsCount);
340 public void testListCompositeKeyResolving() {
341 final SchemaContext context = YangParserTestUtils.parseYangResource("/list-composite-key.yang");
342 assertNotNull(context);
344 final BindingGenerator bindingGen = new BindingGeneratorImpl();
345 final List<Type> genTypes = bindingGen.generateTypes(context);
347 assertNotNull(genTypes);
348 assertEquals(7, genTypes.size());
350 int genTypesCount = 0;
353 int compositeKeyListKeyPropertyCount = 0;
354 int compositeKeyListKeyCount = 0;
355 int innerListKeyPropertyCount = 0;
357 for (final Type type : genTypes) {
358 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
360 } else if (type instanceof GeneratedTransferObject) {
361 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
363 if (genTO.getName().equals("CompositeKeyListKey")) {
364 compositeKeyListKeyCount++;
365 final List<GeneratedProperty> properties = genTO.getProperties();
366 for (final GeneratedProperty prop : properties) {
367 if (prop.getName().equals("key1")) {
368 compositeKeyListKeyPropertyCount++;
369 } else if (prop.getName().equals("key2")) {
370 compositeKeyListKeyPropertyCount++;
374 } else if (genTO.getName().equals("InnerListKey")) {
375 final List<GeneratedProperty> properties = genTO.getProperties();
376 innerListKeyPropertyCount = properties.size();
381 assertEquals(1, compositeKeyListKeyCount);
382 assertEquals(2, compositeKeyListKeyPropertyCount);
384 assertEquals(1, innerListKeyPropertyCount);
386 assertEquals(5, genTypesCount);
387 assertEquals(2, genTOsCount);
391 public void testGeneratedTypes() {
392 final SchemaContext context = YangParserTestUtils.parseYangResource("/demo-topology.yang");
393 assertNotNull(context);
395 final BindingGenerator bindingGen = new BindingGeneratorImpl();
396 final List<Type> genTypes = bindingGen.generateTypes(context);
398 assertNotNull(genTypes);
399 assertEquals(14, genTypes.size());
401 int genTypesCount = 0;
403 for (final Type type : genTypes) {
404 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
406 } else if (type instanceof GeneratedTransferObject) {
411 assertEquals(11, genTypesCount);
412 assertEquals(3, genTOsCount);