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.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 public class GeneratedTypesTest {
32 public void testMultipleModulesResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
33 File abstractTopology = new File(getClass().getResource("/abstract-topology.yang").toURI());
34 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
36 final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInetTypes);
37 assertNotNull(context);
39 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
40 final List<Type> genTypes = bindingGen.generateTypes(context);
42 assertNotNull(genTypes);
43 assertEquals(29, genTypes.size());
47 public void testContainerResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
48 final File testFile = new File(getClass().getResource("/simple-container-demo.yang").toURI());
49 final SchemaContext context = YangParserTestUtils.parseYangSources(testFile);
50 assertNotNull(context);
52 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
53 final List<Type> genTypes = bindingGen.generateTypes(context);
55 assertNotNull(genTypes);
56 assertEquals(3, genTypes.size());
58 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
59 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
60 for (Type t : genTypes) {
61 if ("SimpleContainer".equals(t.getName())) {
62 simpleContainer = (GeneratedType) t;
63 } else if ("NestedContainer".equals(t.getName())) {
64 nestedContainer = (GeneratedType) t;
67 assertNotNull(simpleContainer);
68 assertNotNull(nestedContainer);
69 assertEquals(3, simpleContainer.getMethodDefinitions().size());
70 assertEquals(2, nestedContainer.getMethodDefinitions().size());
72 int getFooMethodCounter = 0;
73 int getBarMethodCounter = 0;
74 int getNestedContainerCounter = 0;
76 String getFooMethodReturnTypeName = "";
77 String getBarMethodReturnTypeName = "";
78 String getNestedContainerReturnTypeName = "";
79 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
80 if (method.getName().equals("getFoo")) {
81 getFooMethodCounter++;
82 getFooMethodReturnTypeName = method.getReturnType().getName();
85 if (method.getName().equals("getBar")) {
86 getBarMethodCounter++;
87 getBarMethodReturnTypeName = method.getReturnType().getName();
90 if (method.getName().equals("getNestedContainer")) {
91 getNestedContainerCounter++;
92 getNestedContainerReturnTypeName = method.getReturnType().getName();
96 assertEquals(1, getFooMethodCounter);
97 assertEquals("Integer", getFooMethodReturnTypeName);
99 assertEquals(1, getBarMethodCounter);
100 assertEquals("String", getBarMethodReturnTypeName);
102 assertEquals(1, getNestedContainerCounter);
103 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
105 getFooMethodCounter = 0;
106 getBarMethodCounter = 0;
108 getFooMethodReturnTypeName = "";
109 getBarMethodReturnTypeName = "";
111 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
113 if (method.getName().equals("getFoo")) {
114 getFooMethodCounter++;
115 getFooMethodReturnTypeName = method.getReturnType().getName();
118 if (method.getName().equals("getBar")) {
119 getBarMethodCounter++;
120 getBarMethodReturnTypeName = method.getReturnType().getName();
124 assertEquals(1, getFooMethodCounter);
125 assertEquals("Short", getFooMethodReturnTypeName);
127 assertEquals(1, getBarMethodCounter);
128 assertEquals("String", getBarMethodReturnTypeName);
132 public void testLeafListResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
133 final File testFile = new File(getClass().getResource("/simple-leaf-list-demo.yang").toURI());
134 final SchemaContext context = YangParserTestUtils.parseYangSources(testFile);
135 assertNotNull(context);
137 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
138 final List<Type> genTypes = bindingGen.generateTypes(context);
140 assertNotNull(genTypes);
141 assertEquals(3, genTypes.size());
143 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
144 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
145 for (Type t : genTypes) {
146 if ("SimpleContainer".equals(t.getName())) {
147 simpleContainer = (GeneratedType) t;
148 } else if ("NestedContainer".equals(t.getName())) {
149 nestedContainer = (GeneratedType) t;
152 assertNotNull(simpleContainer);
153 assertNotNull(nestedContainer);
154 assertEquals(3, simpleContainer.getMethodDefinitions().size());
155 assertEquals(2, nestedContainer.getMethodDefinitions().size());
157 int getFooMethodCounter = 0;
158 int getBarMethodCounter = 0;
159 int getNestedContainerCounter = 0;
161 String getFooMethodReturnTypeName = "";
162 String getBarMethodReturnTypeName = "";
163 String getNestedContainerReturnTypeName = "";
164 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
165 if (method.getName().equals("getFoo")) {
166 getFooMethodCounter++;
167 getFooMethodReturnTypeName = method.getReturnType().getName();
170 if (method.getName().equals("getBar")) {
171 getBarMethodCounter++;
172 getBarMethodReturnTypeName = method.getReturnType().getName();
175 if (method.getName().equals("getNestedContainer")) {
176 getNestedContainerCounter++;
177 getNestedContainerReturnTypeName = method.getReturnType().getName();
181 assertEquals(1, getFooMethodCounter);
182 assertEquals("List", getFooMethodReturnTypeName);
184 assertEquals(1, getBarMethodCounter);
185 assertEquals("String", getBarMethodReturnTypeName);
187 assertEquals(1, getNestedContainerCounter);
188 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
190 getFooMethodCounter = 0;
191 getBarMethodCounter = 0;
193 getFooMethodReturnTypeName = "";
194 getBarMethodReturnTypeName = "";
196 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
197 if (method.getName().equals("getFoo")) {
198 getFooMethodCounter++;
199 getFooMethodReturnTypeName = method.getReturnType().getName();
202 if (method.getName().equals("getBar")) {
203 getBarMethodCounter++;
204 getBarMethodReturnTypeName = method.getReturnType().getName();
208 assertEquals(1, getFooMethodCounter);
209 assertEquals("Short", getFooMethodReturnTypeName);
211 assertEquals(1, getBarMethodCounter);
212 assertEquals("List", getBarMethodReturnTypeName);
216 public void testListResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
217 final File testFile = new File(getClass().getResource("/simple-list-demo.yang").toURI());
218 final SchemaContext context = YangParserTestUtils.parseYangSources(testFile);
219 assertNotNull(context);
221 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
222 final List<Type> genTypes = bindingGen.generateTypes(context);
224 assertNotNull(genTypes);
225 assertEquals(5, genTypes.size());
227 int listParentContainerMethodsCount = 0;
228 int simpleListMethodsCount = 0;
229 int listChildContainerMethodsCount = 0;
230 int listKeyClassCount = 0;
232 int getSimpleListKeyMethodCount = 0;
233 int getListChildContainerMethodCount = 0;
234 int getFooMethodCount = 0;
235 int setFooMethodCount = 0;
236 int getSimpleLeafListMethodCount = 0;
237 int setSimpleLeafListMethodCount = 0;
238 int getBarMethodCount = 0;
240 String getSimpleListKeyMethodReturnTypeName = "";
241 String getListChildContainerMethodReturnTypeName = "";
243 int listKeyClassPropertyCount = 0;
244 String listKeyClassPropertyName = "";
245 String listKeyClassPropertyTypeName = "";
246 boolean listKeyClassPropertyReadOnly = false;
248 int hashMethodParameterCount = 0;
249 String hashMethodParameterName = "";
250 String hashMethodParameterReturnTypeName = "";
252 int equalMethodParameterCount = 0;
253 String equalMethodParameterName = "";
254 String equalMethodParameterReturnTypeName = "";
256 for (final Type type : genTypes) {
257 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
258 final GeneratedType genType = (GeneratedType) type;
259 if (genType.getName().equals("ListParentContainer")) {
260 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
261 } else if (genType.getName().equals("SimpleList")) {
262 simpleListMethodsCount = genType.getMethodDefinitions().size();
263 final List<MethodSignature> methods = genType.getMethodDefinitions();
264 for (final MethodSignature method : methods) {
265 if (method.getName().equals("getKey")) {
266 getSimpleListKeyMethodCount++;
267 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
268 } else if (method.getName().equals("getListChildContainer")) {
269 getListChildContainerMethodCount++;
270 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
271 } else if (method.getName().equals("getFoo")) {
273 } else if (method.getName().equals("setFoo")) {
275 } else if (method.getName().equals("getSimpleLeafList")) {
276 getSimpleLeafListMethodCount++;
277 } else if (method.getName().equals("setSimpleLeafList")) {
278 setSimpleLeafListMethodCount++;
279 } else if (method.getName().equals("getBar")) {
283 } else if (genType.getName().equals("ListChildContainer")) {
284 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
286 } else if (type instanceof GeneratedTransferObject) {
287 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
288 final List<GeneratedProperty> properties = genTO.getProperties();
289 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
290 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
293 listKeyClassPropertyCount = properties.size();
294 listKeyClassPropertyName = properties.get(0).getName();
295 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
296 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
298 hashMethodParameterCount = hashProps.size();
299 hashMethodParameterName = hashProps.get(0).getName();
300 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
302 equalMethodParameterCount = equalProps.size();
303 equalMethodParameterName = equalProps.get(0).getName();
304 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
309 assertEquals(1, listParentContainerMethodsCount);
310 assertEquals(1, listChildContainerMethodsCount);
311 assertEquals(1, getSimpleListKeyMethodCount);
312 assertEquals(1, listKeyClassCount);
314 assertEquals(1, listKeyClassPropertyCount);
315 assertEquals("listKey", listKeyClassPropertyName);
316 assertEquals("Byte", listKeyClassPropertyTypeName);
317 assertEquals(true, listKeyClassPropertyReadOnly);
318 assertEquals(1, hashMethodParameterCount);
319 assertEquals("listKey", hashMethodParameterName);
320 assertEquals("Byte", hashMethodParameterReturnTypeName);
321 assertEquals(1, equalMethodParameterCount);
322 assertEquals("listKey", equalMethodParameterName);
323 assertEquals("Byte", equalMethodParameterReturnTypeName);
325 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
327 assertEquals(1, getListChildContainerMethodCount);
328 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
329 assertEquals(1, getFooMethodCount);
330 assertEquals(0, setFooMethodCount);
331 assertEquals(1, getSimpleLeafListMethodCount);
332 assertEquals(0, setSimpleLeafListMethodCount);
333 assertEquals(1, getBarMethodCount);
335 assertEquals(6, simpleListMethodsCount);
339 public void testListCompositeKeyResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
340 final File testFile = new File(getClass().getResource("/list-composite-key.yang").toURI());
341 final SchemaContext context = YangParserTestUtils.parseYangSources(testFile);
342 assertNotNull(context);
344 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
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() throws URISyntaxException, IOException, SourceException, ReactorException {
392 final File testFile = new File(getClass().getResource("/demo-topology.yang").toURI());
393 final SchemaContext context = YangParserTestUtils.parseYangSources(testFile);
394 assertNotNull(context);
396 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
397 final List<Type> genTypes = bindingGen.generateTypes(context);
399 assertNotNull(genTypes);
400 assertEquals(14, genTypes.size());
402 int genTypesCount = 0;
404 for (final Type type : genTypes) {
405 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
407 } else if (type instanceof GeneratedTransferObject) {
412 assertEquals(11, genTypesCount);
413 assertEquals(3, genTOsCount);