2 * Copyright (c) 2013 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.stmt.parser.retest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
13 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
14 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 public class GeneratedTypesTest {
31 public void testMultipleModulesResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
32 File abstractTopology = new File(getClass().getResource("/abstract-topology.yang").toURI());
33 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
35 final SchemaContext context = RetestUtils.parseYangSources(abstractTopology, ietfInetTypes);
36 assertNotNull(context);
38 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
39 final List<Type> genTypes = bindingGen.generateTypes(context);
41 assertNotNull(genTypes);
42 assertEquals(29, genTypes.size());
46 public void testContainerResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
47 final File testFile = new File(getClass().getResource("/simple-container-demo.yang").toURI());
48 final SchemaContext context = RetestUtils.parseYangSources(testFile);
49 assertNotNull(context);
51 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
52 final List<Type> genTypes = bindingGen.generateTypes(context);
54 assertNotNull(genTypes);
55 assertEquals(3, genTypes.size());
57 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
58 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
59 for (Type t : genTypes) {
60 if ("SimpleContainer".equals(t.getName())) {
61 simpleContainer = (GeneratedType) t;
62 } else if ("NestedContainer".equals(t.getName())) {
63 nestedContainer = (GeneratedType) t;
66 assertNotNull(simpleContainer);
67 assertNotNull(nestedContainer);
68 assertEquals(3, simpleContainer.getMethodDefinitions().size());
69 assertEquals(2, nestedContainer.getMethodDefinitions().size());
71 int getFooMethodCounter = 0;
72 int getBarMethodCounter = 0;
73 int getNestedContainerCounter = 0;
75 String getFooMethodReturnTypeName = "";
76 String getBarMethodReturnTypeName = "";
77 String getNestedContainerReturnTypeName = "";
78 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
79 if (method.getName().equals("getFoo")) {
80 getFooMethodCounter++;
81 getFooMethodReturnTypeName = method.getReturnType().getName();
84 if (method.getName().equals("getBar")) {
85 getBarMethodCounter++;
86 getBarMethodReturnTypeName = method.getReturnType().getName();
89 if (method.getName().equals("getNestedContainer")) {
90 getNestedContainerCounter++;
91 getNestedContainerReturnTypeName = method.getReturnType().getName();
95 assertEquals(1, getFooMethodCounter);
96 assertEquals("Integer", getFooMethodReturnTypeName);
98 assertEquals(1, getBarMethodCounter);
99 assertEquals("String", getBarMethodReturnTypeName);
101 assertEquals(1, getNestedContainerCounter);
102 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
104 getFooMethodCounter = 0;
105 getBarMethodCounter = 0;
107 getFooMethodReturnTypeName = "";
108 getBarMethodReturnTypeName = "";
110 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
112 if (method.getName().equals("getFoo")) {
113 getFooMethodCounter++;
114 getFooMethodReturnTypeName = method.getReturnType().getName();
117 if (method.getName().equals("getBar")) {
118 getBarMethodCounter++;
119 getBarMethodReturnTypeName = method.getReturnType().getName();
123 assertEquals(1, getFooMethodCounter);
124 assertEquals("Short", getFooMethodReturnTypeName);
126 assertEquals(1, getBarMethodCounter);
127 assertEquals("String", getBarMethodReturnTypeName);
131 public void testLeafListResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
132 final File testFile = new File(getClass().getResource("/simple-leaf-list-demo.yang").toURI());
133 final SchemaContext context = RetestUtils.parseYangSources(testFile);
134 assertNotNull(context);
136 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
137 final List<Type> genTypes = bindingGen.generateTypes(context);
139 assertNotNull(genTypes);
140 assertEquals(3, genTypes.size());
142 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
143 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
144 for (Type t : genTypes) {
145 if ("SimpleContainer".equals(t.getName())) {
146 simpleContainer = (GeneratedType) t;
147 } else if ("NestedContainer".equals(t.getName())) {
148 nestedContainer = (GeneratedType) t;
151 assertNotNull(simpleContainer);
152 assertNotNull(nestedContainer);
153 assertEquals(3, simpleContainer.getMethodDefinitions().size());
154 assertEquals(2, nestedContainer.getMethodDefinitions().size());
156 int getFooMethodCounter = 0;
157 int getBarMethodCounter = 0;
158 int getNestedContainerCounter = 0;
160 String getFooMethodReturnTypeName = "";
161 String getBarMethodReturnTypeName = "";
162 String getNestedContainerReturnTypeName = "";
163 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
164 if (method.getName().equals("getFoo")) {
165 getFooMethodCounter++;
166 getFooMethodReturnTypeName = method.getReturnType().getName();
169 if (method.getName().equals("getBar")) {
170 getBarMethodCounter++;
171 getBarMethodReturnTypeName = method.getReturnType().getName();
174 if (method.getName().equals("getNestedContainer")) {
175 getNestedContainerCounter++;
176 getNestedContainerReturnTypeName = method.getReturnType().getName();
180 assertEquals(1, getFooMethodCounter);
181 assertEquals("List", getFooMethodReturnTypeName);
183 assertEquals(1, getBarMethodCounter);
184 assertEquals("String", getBarMethodReturnTypeName);
186 assertEquals(1, getNestedContainerCounter);
187 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
189 getFooMethodCounter = 0;
190 getBarMethodCounter = 0;
192 getFooMethodReturnTypeName = "";
193 getBarMethodReturnTypeName = "";
195 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
196 if (method.getName().equals("getFoo")) {
197 getFooMethodCounter++;
198 getFooMethodReturnTypeName = method.getReturnType().getName();
201 if (method.getName().equals("getBar")) {
202 getBarMethodCounter++;
203 getBarMethodReturnTypeName = method.getReturnType().getName();
207 assertEquals(1, getFooMethodCounter);
208 assertEquals("Short", getFooMethodReturnTypeName);
210 assertEquals(1, getBarMethodCounter);
211 assertEquals("List", getBarMethodReturnTypeName);
215 public void testListResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
216 final File testFile = new File(getClass().getResource("/simple-list-demo.yang").toURI());
217 final SchemaContext context = RetestUtils.parseYangSources(testFile);
218 assertNotNull(context);
220 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
221 final List<Type> genTypes = bindingGen.generateTypes(context);
223 assertNotNull(genTypes);
224 assertEquals(5, genTypes.size());
226 int listParentContainerMethodsCount = 0;
227 int simpleListMethodsCount = 0;
228 int listChildContainerMethodsCount = 0;
229 int listKeyClassCount = 0;
231 int getSimpleListKeyMethodCount = 0;
232 int getListChildContainerMethodCount = 0;
233 int getFooMethodCount = 0;
234 int setFooMethodCount = 0;
235 int getSimpleLeafListMethodCount = 0;
236 int setSimpleLeafListMethodCount = 0;
237 int getBarMethodCount = 0;
239 String getSimpleListKeyMethodReturnTypeName = "";
240 String getListChildContainerMethodReturnTypeName = "";
242 int listKeyClassPropertyCount = 0;
243 String listKeyClassPropertyName = "";
244 String listKeyClassPropertyTypeName = "";
245 boolean listKeyClassPropertyReadOnly = false;
247 int hashMethodParameterCount = 0;
248 String hashMethodParameterName = "";
249 String hashMethodParameterReturnTypeName = "";
251 int equalMethodParameterCount = 0;
252 String equalMethodParameterName = "";
253 String equalMethodParameterReturnTypeName = "";
255 for (final Type type : genTypes) {
256 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
257 final GeneratedType genType = (GeneratedType) type;
258 if (genType.getName().equals("ListParentContainer")) {
259 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
260 } else if (genType.getName().equals("SimpleList")) {
261 simpleListMethodsCount = genType.getMethodDefinitions().size();
262 final List<MethodSignature> methods = genType.getMethodDefinitions();
263 for (final MethodSignature method : methods) {
264 if (method.getName().equals("getKey")) {
265 getSimpleListKeyMethodCount++;
266 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
267 } else if (method.getName().equals("getListChildContainer")) {
268 getListChildContainerMethodCount++;
269 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
270 } else if (method.getName().equals("getFoo")) {
272 } else if (method.getName().equals("setFoo")) {
274 } else if (method.getName().equals("getSimpleLeafList")) {
275 getSimpleLeafListMethodCount++;
276 } else if (method.getName().equals("setSimpleLeafList")) {
277 setSimpleLeafListMethodCount++;
278 } else if (method.getName().equals("getBar")) {
282 } else if (genType.getName().equals("ListChildContainer")) {
283 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
285 } else if (type instanceof GeneratedTransferObject) {
286 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
287 final List<GeneratedProperty> properties = genTO.getProperties();
288 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
289 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
292 listKeyClassPropertyCount = properties.size();
293 listKeyClassPropertyName = properties.get(0).getName();
294 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
295 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
297 hashMethodParameterCount = hashProps.size();
298 hashMethodParameterName = hashProps.get(0).getName();
299 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
301 equalMethodParameterCount = equalProps.size();
302 equalMethodParameterName = equalProps.get(0).getName();
303 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
308 assertEquals(1, listParentContainerMethodsCount);
309 assertEquals(1, listChildContainerMethodsCount);
310 assertEquals(1, getSimpleListKeyMethodCount);
311 assertEquals(1, listKeyClassCount);
313 assertEquals(1, listKeyClassPropertyCount);
314 assertEquals("listKey", listKeyClassPropertyName);
315 assertEquals("Byte", listKeyClassPropertyTypeName);
316 assertEquals(true, listKeyClassPropertyReadOnly);
317 assertEquals(1, hashMethodParameterCount);
318 assertEquals("listKey", hashMethodParameterName);
319 assertEquals("Byte", hashMethodParameterReturnTypeName);
320 assertEquals(1, equalMethodParameterCount);
321 assertEquals("listKey", equalMethodParameterName);
322 assertEquals("Byte", equalMethodParameterReturnTypeName);
324 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
326 assertEquals(1, getListChildContainerMethodCount);
327 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
328 assertEquals(1, getFooMethodCount);
329 assertEquals(0, setFooMethodCount);
330 assertEquals(1, getSimpleLeafListMethodCount);
331 assertEquals(0, setSimpleLeafListMethodCount);
332 assertEquals(1, getBarMethodCount);
334 assertEquals(6, simpleListMethodsCount);
338 public void testListCompositeKeyResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
339 final File testFile = new File(getClass().getResource("/list-composite-key.yang").toURI());
340 final SchemaContext context = RetestUtils.parseYangSources(testFile);
341 assertNotNull(context);
343 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
344 final List<Type> genTypes = bindingGen.generateTypes(context);
346 assertNotNull(genTypes);
347 assertEquals(7, genTypes.size());
349 int genTypesCount = 0;
352 int compositeKeyListKeyPropertyCount = 0;
353 int compositeKeyListKeyCount = 0;
354 int innerListKeyPropertyCount = 0;
356 for (final Type type : genTypes) {
357 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
359 } else if (type instanceof GeneratedTransferObject) {
360 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
362 if (genTO.getName().equals("CompositeKeyListKey")) {
363 compositeKeyListKeyCount++;
364 final List<GeneratedProperty> properties = genTO.getProperties();
365 for (final GeneratedProperty prop : properties) {
366 if (prop.getName().equals("key1")) {
367 compositeKeyListKeyPropertyCount++;
368 } else if (prop.getName().equals("key2")) {
369 compositeKeyListKeyPropertyCount++;
373 } else if (genTO.getName().equals("InnerListKey")) {
374 final List<GeneratedProperty> properties = genTO.getProperties();
375 innerListKeyPropertyCount = properties.size();
380 assertEquals(1, compositeKeyListKeyCount);
381 assertEquals(2, compositeKeyListKeyPropertyCount);
383 assertEquals(1, innerListKeyPropertyCount);
385 assertEquals(5, genTypesCount);
386 assertEquals(2, genTOsCount);
390 public void testGeneratedTypes() throws URISyntaxException, IOException, SourceException, ReactorException {
391 final File testFile = new File(getClass().getResource("/demo-topology.yang").toURI());
392 final SchemaContext context = RetestUtils.parseYangSources(testFile);
393 assertNotNull(context);
395 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
396 final List<Type> genTypes = bindingGen.generateTypes(context);
398 assertNotNull(genTypes);
399 assertEquals(14, genTypes.size());
401 int genTypesCount = 0;
403 for (final Type type : genTypes) {
404 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
406 } else if (type instanceof GeneratedTransferObject) {
411 assertEquals(11, genTypesCount);
412 assertEquals(3, genTOsCount);