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