Rehost BindingMapping in yang.binding.contract.Naming
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
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.yangtools.yang.binding.contract.Naming;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class GeneratedTypesTest {
24     @Test
25     public void testMultipleModulesResolving() {
26         final var genTypes = DefaultBindingGenerator.generateFor(
27             YangParserTestUtils.parseYangResources(GeneratedTypesTest.class,
28                 "/abstract-topology.yang", "/ietf-models/ietf-inet-types.yang"));
29         assertEquals(27, genTypes.size());
30     }
31
32     @Test
33     public void testContainerResolving() {
34         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
35             "/simple-container-demo.yang"));
36
37         assertNotNull(genTypes);
38         assertEquals(3, genTypes.size());
39
40         GeneratedType simpleContainer = genTypes.get(1);
41         GeneratedType nestedContainer = genTypes.get(2);
42         for (GeneratedType t : genTypes) {
43             if ("SimpleContainer".equals(t.getName())) {
44                 simpleContainer = t;
45             } else if ("NestedContainer".equals(t.getName())) {
46                 nestedContainer = t;
47             }
48         }
49         assertNotNull(simpleContainer);
50         assertNotNull(nestedContainer);
51         // FIXME: split this into getter/default/static asserts
52         assertEquals(10, simpleContainer.getMethodDefinitions().size());
53         // FIXME: split this into getter/default/static asserts
54         assertEquals(8, nestedContainer.getMethodDefinitions().size());
55
56         int getFooMethodCounter = 0;
57         int getBarMethodCounter = 0;
58         int getNestedContainerCounter = 0;
59
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();
67             }
68
69             if (method.getName().equals("getBar")) {
70                 getBarMethodCounter++;
71                 getBarMethodReturnTypeName = method.getReturnType().getName();
72             }
73
74             if (method.getName().equals("getNestedContainer")) {
75                 getNestedContainerCounter++;
76                 getNestedContainerReturnTypeName = method.getReturnType().getName();
77             }
78         }
79
80         assertEquals(1, getFooMethodCounter);
81         assertEquals("Integer", getFooMethodReturnTypeName);
82
83         assertEquals(1, getBarMethodCounter);
84         assertEquals("String", getBarMethodReturnTypeName);
85
86         assertEquals(1, getNestedContainerCounter);
87         assertEquals("NestedContainer", getNestedContainerReturnTypeName);
88
89         getFooMethodCounter = 0;
90         getBarMethodCounter = 0;
91
92         getFooMethodReturnTypeName = "";
93         getBarMethodReturnTypeName = "";
94
95         for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
96
97             if (method.getName().equals("getFoo")) {
98                 getFooMethodCounter++;
99                 getFooMethodReturnTypeName = method.getReturnType().getName();
100             }
101
102             if (method.getName().equals("getBar")) {
103                 getBarMethodCounter++;
104                 getBarMethodReturnTypeName = method.getReturnType().getName();
105             }
106         }
107
108         assertEquals(1, getFooMethodCounter);
109         assertEquals("Uint8", getFooMethodReturnTypeName);
110
111         assertEquals(1, getBarMethodCounter);
112         assertEquals("String", getBarMethodReturnTypeName);
113     }
114
115     @Test
116     public void testLeafListResolving() {
117         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
118             "/simple-leaf-list-demo.yang"));
119
120         assertNotNull(genTypes);
121         assertEquals(3, genTypes.size());
122
123         GeneratedType simpleContainer = genTypes.get(1);
124         GeneratedType nestedContainer = genTypes.get(2);
125         for (GeneratedType t : genTypes) {
126             if ("SimpleContainer".equals(t.getName())) {
127                 simpleContainer = t;
128             } else if ("NestedContainer".equals(t.getName())) {
129                 nestedContainer = t;
130             }
131         }
132         assertNotNull(simpleContainer);
133         assertNotNull(nestedContainer);
134         // FIXME: split this into getter/default/static asserts
135         assertEquals(10, simpleContainer.getMethodDefinitions().size());
136         // FIXME: split this into getter/default/static asserts
137         assertEquals(8, nestedContainer.getMethodDefinitions().size());
138
139         int getFooMethodCounter = 0;
140         int getBarMethodCounter = 0;
141         int getNestedContainerCounter = 0;
142
143         String getFooMethodReturnTypeName = "";
144         String getBarMethodReturnTypeName = "";
145         String getNestedContainerReturnTypeName = "";
146         for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
147             if (method.isDefault()) {
148                 continue;
149             }
150             if (method.getName().equals("getFoo")) {
151                 getFooMethodCounter++;
152                 getFooMethodReturnTypeName = method.getReturnType().getName();
153             }
154
155             if (method.getName().equals("getBar")) {
156                 getBarMethodCounter++;
157                 getBarMethodReturnTypeName = method.getReturnType().getName();
158             }
159
160             if (method.getName().equals("getNestedContainer")) {
161                 getNestedContainerCounter++;
162                 getNestedContainerReturnTypeName = method.getReturnType().getName();
163             }
164         }
165
166         assertEquals(1, getFooMethodCounter);
167         assertEquals("Set", getFooMethodReturnTypeName);
168
169         assertEquals(1, getBarMethodCounter);
170         assertEquals("String", getBarMethodReturnTypeName);
171
172         assertEquals(1, getNestedContainerCounter);
173         assertEquals("NestedContainer", getNestedContainerReturnTypeName);
174
175         getFooMethodCounter = 0;
176         getBarMethodCounter = 0;
177
178         getFooMethodReturnTypeName = "";
179         getBarMethodReturnTypeName = "";
180
181         for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
182             if (method.getName().equals("getFoo")) {
183                 getFooMethodCounter++;
184                 getFooMethodReturnTypeName = method.getReturnType().getName();
185             }
186
187             if (method.getName().equals("getBar")) {
188                 getBarMethodCounter++;
189                 getBarMethodReturnTypeName = method.getReturnType().getName();
190             }
191         }
192
193         assertEquals(1, getFooMethodCounter);
194         assertEquals("Uint8", getFooMethodReturnTypeName);
195
196         assertEquals(1, getBarMethodCounter);
197         assertEquals("Set", getBarMethodReturnTypeName);
198     }
199
200     @Test
201     public void testListResolving() {
202         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
203             "/simple-list-demo.yang"));
204
205         assertNotNull(genTypes);
206         assertEquals(5, genTypes.size());
207
208         int listParentContainerMethodsCount = 0;
209         int simpleListMethodsCount = 0;
210         int listChildContainerMethodsCount = 0;
211         int listKeyClassCount = 0;
212
213         int getSimpleListKeyMethodCount = 0;
214         int getListChildContainerMethodCount = 0;
215         int getFooMethodCount = 0;
216         int setFooMethodCount = 0;
217         int getSimpleLeafListMethodCount = 0;
218         int setSimpleLeafListMethodCount = 0;
219         int getBarMethodCount = 0;
220
221         String getSimpleListKeyMethodReturnTypeName = "";
222         String getListChildContainerMethodReturnTypeName = "";
223
224         for (final GeneratedType genType : genTypes) {
225             if (!(genType instanceof GeneratedTransferObject genTO)) {
226                 if (genType.getName().equals("ListParentContainer")) {
227                     listParentContainerMethodsCount = genType.getMethodDefinitions().size();
228                 } else if (genType.getName().equals("SimpleList")) {
229                     simpleListMethodsCount = genType.getMethodDefinitions().size();
230                     final List<MethodSignature> methods = genType.getMethodDefinitions();
231                     for (final MethodSignature method : methods) {
232                         switch (method.getName()) {
233                             case Naming.IDENTIFIABLE_KEY_NAME:
234                                 getSimpleListKeyMethodCount++;
235                                 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
236                                 break;
237                             case "getListChildContainer":
238                                 getListChildContainerMethodCount++;
239                                 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
240                                 break;
241                             case "getFoo":
242                                 getFooMethodCount++;
243                                 break;
244                             case "setFoo":
245                                 setFooMethodCount++;
246                                 break;
247                             case "getSimpleLeafList":
248                                 getSimpleLeafListMethodCount++;
249                                 break;
250                             case "setSimpleLeafList":
251                                 setSimpleLeafListMethodCount++;
252                                 break;
253                             case "getBar":
254                                 getBarMethodCount++;
255                                 break;
256                             default:
257                         }
258                     }
259                 } else if (genType.getName().equals("ListChildContainer")) {
260                     listChildContainerMethodsCount = genType.getMethodDefinitions().size();
261                 }
262             } else {
263                 final List<GeneratedProperty> properties = genTO.getProperties();
264                 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
265                 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
266
267                 assertEquals("Unexpected key", 0, listKeyClassCount++);
268                 assertEquals(1, properties.size());
269                 assertEquals("listKey", properties.get(0).getName());
270                 assertEquals("Byte", properties.get(0).getReturnType().getName());
271                 assertTrue(properties.get(0).isReadOnly());
272
273                 assertEquals(1, hashProps.size());
274                 assertEquals("listKey", hashProps.get(0).getName());
275                 assertEquals("Byte", hashProps.get(0).getReturnType().getName());
276
277                 assertEquals(1, equalProps.size());
278                 assertEquals("listKey", equalProps.get(0).getName());
279                 assertEquals("Byte",  equalProps.get(0).getReturnType().getName());
280             }
281         }
282
283         // FIXME: split this into getter/default/static asserts
284         assertEquals(6, listParentContainerMethodsCount);
285         // FIXME: split this into getter/default/static asserts
286         assertEquals(6, listChildContainerMethodsCount);
287         assertEquals(1, getSimpleListKeyMethodCount);
288         assertEquals(1, listKeyClassCount);
289
290         assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
291
292         assertEquals(1, getListChildContainerMethodCount);
293         assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
294         assertEquals(1, getFooMethodCount);
295         assertEquals(0, setFooMethodCount);
296         assertEquals(1, getSimpleLeafListMethodCount);
297         assertEquals(0, setSimpleLeafListMethodCount);
298         assertEquals(1, getBarMethodCount);
299
300         // FIXME: split this into getter/default/static asserts
301         assertEquals(15, simpleListMethodsCount);
302     }
303
304     @Test
305     public void testListCompositeKeyResolving() {
306         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
307             "/list-composite-key.yang"));
308
309         assertNotNull(genTypes);
310         assertEquals(7, genTypes.size());
311
312         int genTypesCount = 0;
313         int genTOsCount = 0;
314
315         int compositeKeyListKeyPropertyCount = 0;
316         int compositeKeyListKeyCount = 0;
317         int innerListKeyPropertyCount = 0;
318
319         for (final GeneratedType type : genTypes) {
320             if (!(type instanceof GeneratedTransferObject genTO)) {
321                 genTypesCount++;
322             } else if (genTO.getName().equals("CompositeKeyListKey")) {
323                 compositeKeyListKeyCount++;
324                 final List<GeneratedProperty> properties = genTO.getProperties();
325                 for (final GeneratedProperty prop : properties) {
326                     if (prop.getName().equals("key1") || prop.getName().equals("key2")) {
327                         compositeKeyListKeyPropertyCount++;
328                     }
329                 }
330                 genTOsCount++;
331             } else if (genTO.getName().equals("InnerListKey")) {
332                 final List<GeneratedProperty> properties = genTO.getProperties();
333                 innerListKeyPropertyCount = properties.size();
334                 genTOsCount++;
335             }
336         }
337         assertEquals(1, compositeKeyListKeyCount);
338         assertEquals(2, compositeKeyListKeyPropertyCount);
339
340         assertEquals(1, innerListKeyPropertyCount);
341
342         assertEquals(5, genTypesCount);
343         assertEquals(2, genTOsCount);
344     }
345
346     @Test
347     public void testGeneratedTypes() {
348         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
349             "/demo-topology.yang"));
350
351         assertNotNull(genTypes);
352         assertEquals(14, genTypes.size());
353
354         int genTypesCount = 0;
355         int genTOsCount = 0;
356         for (final GeneratedType type : genTypes) {
357             if (type instanceof GeneratedTransferObject) {
358                 genTOsCount++;
359             } else {
360                 genTypesCount++;
361             }
362         }
363
364         assertEquals(11, genTypesCount);
365         assertEquals(3, genTOsCount);
366     }
367
368     @Test
369     public void testAugmentRpcInput() {
370         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
371             "/augment-rpc-input.yang"));
372         assertEquals(7, genTypes.size());
373     }
374 }