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.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class GeneratedTypesTest {
25 public void testMultipleModulesResolving() {
26 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(
27 YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
28 "/abstract-topology.yang", "/ietf-models/ietf-inet-types.yang"));
30 assertNotNull(genTypes);
31 assertEquals(29, genTypes.size());
35 public void testContainerResolving() {
36 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
37 "/simple-container-demo.yang"));
39 assertNotNull(genTypes);
40 assertEquals(3, genTypes.size());
42 GeneratedType simpleContainer = genTypes.get(1);
43 GeneratedType nestedContainer = genTypes.get(2);
44 for (GeneratedType t : genTypes) {
45 if ("SimpleContainer".equals(t.getName())) {
47 } else if ("NestedContainer".equals(t.getName())) {
51 assertNotNull(simpleContainer);
52 assertNotNull(nestedContainer);
53 // FIXME: split this into getter/default/static asserts
54 assertEquals(9, simpleContainer.getMethodDefinitions().size());
55 // FIXME: split this into getter/default/static asserts
56 assertEquals(8, 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<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
120 "/simple-leaf-list-demo.yang"));
122 assertNotNull(genTypes);
123 assertEquals(3, genTypes.size());
125 GeneratedType simpleContainer = genTypes.get(1);
126 GeneratedType nestedContainer = genTypes.get(2);
127 for (GeneratedType t : genTypes) {
128 if ("SimpleContainer".equals(t.getName())) {
130 } else if ("NestedContainer".equals(t.getName())) {
134 assertNotNull(simpleContainer);
135 assertNotNull(nestedContainer);
136 // FIXME: split this into getter/default/static asserts
137 assertEquals(9, simpleContainer.getMethodDefinitions().size());
138 // FIXME: split this into getter/default/static asserts
139 assertEquals(8, 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<GeneratedType> 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 for (final GeneratedType genType : genTypes) {
227 if (!(genType instanceof GeneratedTransferObject)) {
228 if (genType.getName().equals("ListParentContainer")) {
229 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
230 } else if (genType.getName().equals("SimpleList")) {
231 simpleListMethodsCount = genType.getMethodDefinitions().size();
232 final List<MethodSignature> methods = genType.getMethodDefinitions();
233 for (final MethodSignature method : methods) {
234 switch (method.getName()) {
235 case BindingMapping.IDENTIFIABLE_KEY_NAME:
236 getSimpleListKeyMethodCount++;
237 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
239 case "getListChildContainer":
240 getListChildContainerMethodCount++;
241 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
249 case "getSimpleLeafList":
250 getSimpleLeafListMethodCount++;
252 case "setSimpleLeafList":
253 setSimpleLeafListMethodCount++;
261 } else if (genType.getName().equals("ListChildContainer")) {
262 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
265 final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
266 final List<GeneratedProperty> properties = genTO.getProperties();
267 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
268 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
270 assertEquals("Unexpected key", 0, listKeyClassCount++);
271 assertEquals(1, properties.size());
272 assertEquals("listKey", properties.get(0).getName());
273 assertEquals("Byte", properties.get(0).getReturnType().getName());
274 assertTrue(properties.get(0).isReadOnly());
276 assertEquals(1, hashProps.size());
277 assertEquals("listKey", hashProps.get(0).getName());
278 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
280 assertEquals(1, equalProps.size());
281 assertEquals("listKey", equalProps.get(0).getName());
282 assertEquals("Byte", equalProps.get(0).getReturnType().getName());
286 // FIXME: split this into getter/default/static asserts
287 assertEquals(6, listParentContainerMethodsCount);
288 // FIXME: split this into getter/default/static asserts
289 assertEquals(6, listChildContainerMethodsCount);
290 assertEquals(1, getSimpleListKeyMethodCount);
291 assertEquals(1, listKeyClassCount);
293 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
295 assertEquals(1, getListChildContainerMethodCount);
296 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
297 assertEquals(1, getFooMethodCount);
298 assertEquals(0, setFooMethodCount);
299 assertEquals(1, getSimpleLeafListMethodCount);
300 assertEquals(0, setSimpleLeafListMethodCount);
301 assertEquals(1, getBarMethodCount);
303 // FIXME: split this into getter/default/static asserts
304 assertEquals(14, simpleListMethodsCount);
308 public void testListCompositeKeyResolving() {
309 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
310 "/list-composite-key.yang"));
312 assertNotNull(genTypes);
313 assertEquals(7, genTypes.size());
315 int genTypesCount = 0;
318 int compositeKeyListKeyPropertyCount = 0;
319 int compositeKeyListKeyCount = 0;
320 int innerListKeyPropertyCount = 0;
322 for (final GeneratedType type : genTypes) {
323 if (!(type instanceof GeneratedTransferObject)) {
326 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
328 if (genTO.getName().equals("CompositeKeyListKey")) {
329 compositeKeyListKeyCount++;
330 final List<GeneratedProperty> properties = genTO.getProperties();
331 for (final GeneratedProperty prop : properties) {
332 if (prop.getName().equals("key1") || prop.getName().equals("key2")) {
333 compositeKeyListKeyPropertyCount++;
337 } else if (genTO.getName().equals("InnerListKey")) {
338 final List<GeneratedProperty> properties = genTO.getProperties();
339 innerListKeyPropertyCount = properties.size();
344 assertEquals(1, compositeKeyListKeyCount);
345 assertEquals(2, compositeKeyListKeyPropertyCount);
347 assertEquals(1, innerListKeyPropertyCount);
349 assertEquals(5, genTypesCount);
350 assertEquals(2, genTOsCount);
354 public void testGeneratedTypes() {
355 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
356 "/demo-topology.yang"));
358 assertNotNull(genTypes);
359 assertEquals(14, genTypes.size());
361 int genTypesCount = 0;
363 for (final GeneratedType type : genTypes) {
364 if (type instanceof GeneratedTransferObject) {
371 assertEquals(11, genTypesCount);
372 assertEquals(3, genTOsCount);
376 public void testAugmentRpcInput() {
377 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
378 "/augment-rpc-input.yang"));
379 assertEquals(5, genTypes.size());