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 assertEquals(4, simpleContainer.getMethodDefinitions().size());
54 assertEquals(3, 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<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
118 "/simple-leaf-list-demo.yang"));
120 assertNotNull(genTypes);
121 assertEquals(3, genTypes.size());
123 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
124 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
125 for (Type t : genTypes) {
126 if ("SimpleContainer".equals(t.getName())) {
127 simpleContainer = (GeneratedType) t;
128 } else if ("NestedContainer".equals(t.getName())) {
129 nestedContainer = (GeneratedType) t;
132 assertNotNull(simpleContainer);
133 assertNotNull(nestedContainer);
134 assertEquals(4, simpleContainer.getMethodDefinitions().size());
135 assertEquals(3, nestedContainer.getMethodDefinitions().size());
137 int getFooMethodCounter = 0;
138 int getBarMethodCounter = 0;
139 int getNestedContainerCounter = 0;
141 String getFooMethodReturnTypeName = "";
142 String getBarMethodReturnTypeName = "";
143 String getNestedContainerReturnTypeName = "";
144 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
145 if (method.isDefault()) {
148 if (method.getName().equals("getFoo")) {
149 getFooMethodCounter++;
150 getFooMethodReturnTypeName = method.getReturnType().getName();
153 if (method.getName().equals("getBar")) {
154 getBarMethodCounter++;
155 getBarMethodReturnTypeName = method.getReturnType().getName();
158 if (method.getName().equals("getNestedContainer")) {
159 getNestedContainerCounter++;
160 getNestedContainerReturnTypeName = method.getReturnType().getName();
164 assertEquals(1, getFooMethodCounter);
165 assertEquals("List", getFooMethodReturnTypeName);
167 assertEquals(1, getBarMethodCounter);
168 assertEquals("String", getBarMethodReturnTypeName);
170 assertEquals(1, getNestedContainerCounter);
171 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
173 getFooMethodCounter = 0;
174 getBarMethodCounter = 0;
176 getFooMethodReturnTypeName = "";
177 getBarMethodReturnTypeName = "";
179 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
180 if (method.getName().equals("getFoo")) {
181 getFooMethodCounter++;
182 getFooMethodReturnTypeName = method.getReturnType().getName();
185 if (method.getName().equals("getBar")) {
186 getBarMethodCounter++;
187 getBarMethodReturnTypeName = method.getReturnType().getName();
191 assertEquals(1, getFooMethodCounter);
192 assertEquals("Uint8", getFooMethodReturnTypeName);
194 assertEquals(1, getBarMethodCounter);
195 assertEquals("List", getBarMethodReturnTypeName);
199 public void testListResolving() {
200 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
201 "/simple-list-demo.yang"));
203 assertNotNull(genTypes);
204 assertEquals(5, genTypes.size());
206 int listParentContainerMethodsCount = 0;
207 int simpleListMethodsCount = 0;
208 int listChildContainerMethodsCount = 0;
209 int listKeyClassCount = 0;
211 int getSimpleListKeyMethodCount = 0;
212 int getListChildContainerMethodCount = 0;
213 int getFooMethodCount = 0;
214 int setFooMethodCount = 0;
215 int getSimpleLeafListMethodCount = 0;
216 int setSimpleLeafListMethodCount = 0;
217 int getBarMethodCount = 0;
219 String getSimpleListKeyMethodReturnTypeName = "";
220 String getListChildContainerMethodReturnTypeName = "";
222 int listKeyClassPropertyCount = 0;
223 String listKeyClassPropertyName = "";
224 String listKeyClassPropertyTypeName = "";
225 boolean listKeyClassPropertyReadOnly = false;
227 int hashMethodParameterCount = 0;
228 String hashMethodParameterName = "";
229 String hashMethodParameterReturnTypeName = "";
231 int equalMethodParameterCount = 0;
232 String equalMethodParameterName = "";
233 String equalMethodParameterReturnTypeName = "";
235 for (final Type type : genTypes) {
236 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
237 final GeneratedType genType = (GeneratedType) type;
238 if (genType.getName().equals("ListParentContainer")) {
239 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
240 } else if (genType.getName().equals("SimpleList")) {
241 simpleListMethodsCount = genType.getMethodDefinitions().size();
242 final List<MethodSignature> methods = genType.getMethodDefinitions();
243 for (final MethodSignature method : methods) {
244 switch (method.getName()) {
245 case BindingMapping.IDENTIFIABLE_KEY_NAME:
246 getSimpleListKeyMethodCount++;
247 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
249 case "getListChildContainer":
250 getListChildContainerMethodCount++;
251 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
259 case "getSimpleLeafList":
260 getSimpleLeafListMethodCount++;
262 case "setSimpleLeafList":
263 setSimpleLeafListMethodCount++;
271 } else if (genType.getName().equals("ListChildContainer")) {
272 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
274 } else if (type instanceof GeneratedTransferObject) {
275 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
276 final List<GeneratedProperty> properties = genTO.getProperties();
277 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
278 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
281 listKeyClassPropertyCount = properties.size();
282 listKeyClassPropertyName = properties.get(0).getName();
283 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
284 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
286 hashMethodParameterCount = hashProps.size();
287 hashMethodParameterName = hashProps.get(0).getName();
288 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
290 equalMethodParameterCount = equalProps.size();
291 equalMethodParameterName = equalProps.get(0).getName();
292 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
297 assertEquals(3, listParentContainerMethodsCount);
298 assertEquals(2, listChildContainerMethodsCount);
299 assertEquals(1, getSimpleListKeyMethodCount);
300 assertEquals(1, listKeyClassCount);
302 assertEquals(1, listKeyClassPropertyCount);
303 assertEquals("listKey", listKeyClassPropertyName);
304 assertEquals("Byte", listKeyClassPropertyTypeName);
305 assertTrue(listKeyClassPropertyReadOnly);
306 assertEquals(1, hashMethodParameterCount);
307 assertEquals("listKey", hashMethodParameterName);
308 assertEquals("Byte", hashMethodParameterReturnTypeName);
309 assertEquals(1, equalMethodParameterCount);
310 assertEquals("listKey", equalMethodParameterName);
311 assertEquals("Byte", equalMethodParameterReturnTypeName);
313 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
315 assertEquals(1, getListChildContainerMethodCount);
316 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
317 assertEquals(1, getFooMethodCount);
318 assertEquals(0, setFooMethodCount);
319 assertEquals(1, getSimpleLeafListMethodCount);
320 assertEquals(0, setSimpleLeafListMethodCount);
321 assertEquals(1, getBarMethodCount);
323 assertEquals(7, simpleListMethodsCount);
327 public void testListCompositeKeyResolving() {
328 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
329 "/list-composite-key.yang"));
331 assertNotNull(genTypes);
332 assertEquals(7, genTypes.size());
334 int genTypesCount = 0;
337 int compositeKeyListKeyPropertyCount = 0;
338 int compositeKeyListKeyCount = 0;
339 int innerListKeyPropertyCount = 0;
341 for (final Type type : genTypes) {
342 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
344 } else if (type instanceof GeneratedTransferObject) {
345 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
347 if (genTO.getName().equals("CompositeKeyListKey")) {
348 compositeKeyListKeyCount++;
349 final List<GeneratedProperty> properties = genTO.getProperties();
350 for (final GeneratedProperty prop : properties) {
351 if (prop.getName().equals("key1")) {
352 compositeKeyListKeyPropertyCount++;
353 } else if (prop.getName().equals("key2")) {
354 compositeKeyListKeyPropertyCount++;
358 } else if (genTO.getName().equals("InnerListKey")) {
359 final List<GeneratedProperty> properties = genTO.getProperties();
360 innerListKeyPropertyCount = properties.size();
365 assertEquals(1, compositeKeyListKeyCount);
366 assertEquals(2, compositeKeyListKeyPropertyCount);
368 assertEquals(1, innerListKeyPropertyCount);
370 assertEquals(5, genTypesCount);
371 assertEquals(2, genTOsCount);
375 public void testGeneratedTypes() {
376 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
377 "/demo-topology.yang"));
379 assertNotNull(genTypes);
380 assertEquals(14, genTypes.size());
382 int genTypesCount = 0;
384 for (final Type type : genTypes) {
385 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
387 } else if (type instanceof GeneratedTransferObject) {
392 assertEquals(11, genTypesCount);
393 assertEquals(3, genTOsCount);