2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
\r
10 import static org.junit.Assert.*;
\r
12 import java.io.File;
\r
13 import java.net.URI;
\r
14 import java.net.URISyntaxException;
\r
15 import java.util.ArrayList;
\r
16 import java.util.List;
\r
17 import java.util.Set;
\r
19 import org.junit.Test;
\r
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
\r
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
\r
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
\r
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
\r
24 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
\r
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
\r
26 import org.opendaylight.yangtools.yang.model.api.Module;
\r
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
\r
28 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
\r
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
\r
31 public class GeneratedTypesTest {
\r
33 private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) {
\r
34 final YangModelParser parser = new YangParserImpl();
\r
36 final List<File> inputFiles = new ArrayList<File>();
\r
37 for (int i = 0; i < yangFiles.length; ++i) {
\r
38 inputFiles.add(new File(yangFiles[i]));
\r
41 final Set<Module> modules = parser.parseYangModels(inputFiles);
\r
42 return parser.resolveSchemaContext(modules);
\r
46 public void testMultipleModulesResolving() throws URISyntaxException {
\r
47 final URI topologyPath = getClass().getResource("/abstract-topology.yang").toURI();
\r
48 final URI typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").toURI();
\r
49 final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath);
\r
50 assertNotNull(context);
\r
52 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
53 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
55 assertNotNull(genTypes);
\r
56 assertEquals(29, genTypes.size());
\r
60 public void testContainerResolving() throws URISyntaxException {
\r
61 final URI filePath = getClass().getResource("/simple-container-demo.yang").toURI();
\r
62 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
\r
63 assert (context != null);
\r
65 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
66 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
68 assertNotNull(genTypes);
\r
69 assertEquals(3, genTypes.size());
\r
71 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
\r
72 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
\r
73 for (Type t : genTypes) {
\r
74 if ("SimpleContainer".equals(t.getName())) {
\r
75 simpleContainer = (GeneratedType)t;
\r
76 } else if ("NestedContainer".equals(t.getName())) {
\r
77 nestedContainer = (GeneratedType)t;
\r
80 assertNotNull(simpleContainer);
\r
81 assertNotNull(nestedContainer);
\r
82 assertEquals(3, simpleContainer.getMethodDefinitions().size());
\r
83 assertEquals(2, nestedContainer.getMethodDefinitions().size());
\r
85 int getFooMethodCounter = 0;
\r
86 int getBarMethodCounter = 0;
\r
87 int getNestedContainerCounter = 0;
\r
89 String getFooMethodReturnTypeName = "";
\r
90 String getBarMethodReturnTypeName = "";
\r
91 String getNestedContainerReturnTypeName = "";
\r
92 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
\r
93 if (method.getName().equals("getFoo")) {
\r
94 getFooMethodCounter++;
\r
95 getFooMethodReturnTypeName = method.getReturnType().getName();
\r
98 if (method.getName().equals("getBar")) {
\r
99 getBarMethodCounter++;
\r
100 getBarMethodReturnTypeName = method.getReturnType().getName();
\r
103 if (method.getName().equals("getNestedContainer")) {
\r
104 getNestedContainerCounter++;
\r
105 getNestedContainerReturnTypeName = method.getReturnType().getName();
\r
109 assertEquals(1, getFooMethodCounter);
\r
110 assertEquals("Integer", getFooMethodReturnTypeName);
\r
112 assertEquals(1, getBarMethodCounter);
\r
113 assertEquals("String", getBarMethodReturnTypeName);
\r
115 assertEquals(1, getNestedContainerCounter);
\r
116 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
\r
118 getFooMethodCounter = 0;
\r
119 getBarMethodCounter = 0;
\r
121 getFooMethodReturnTypeName = "";
\r
122 getBarMethodReturnTypeName = "";
\r
124 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
\r
126 if (method.getName().equals("getFoo")) {
\r
127 getFooMethodCounter++;
\r
128 getFooMethodReturnTypeName = method.getReturnType().getName();
\r
131 if (method.getName().equals("getBar")) {
\r
132 getBarMethodCounter++;
\r
133 getBarMethodReturnTypeName = method.getReturnType().getName();
\r
137 assertEquals(1, getFooMethodCounter);
\r
138 assertEquals("Short", getFooMethodReturnTypeName);
\r
140 assertEquals(1, getBarMethodCounter);
\r
141 assertEquals("String", getBarMethodReturnTypeName);
\r
145 public void testLeafListResolving() throws URISyntaxException {
\r
146 final URI filePath = getClass().getResource("/simple-leaf-list-demo.yang").toURI();
\r
147 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
\r
148 assertNotNull(context);
\r
150 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
151 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
153 assertNotNull(genTypes);
\r
154 assertEquals(3, genTypes.size());
\r
156 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
\r
157 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
\r
158 for (Type t : genTypes) {
\r
159 if ("SimpleContainer".equals(t.getName())) {
\r
160 simpleContainer = (GeneratedType)t;
\r
161 } else if ("NestedContainer".equals(t.getName())) {
\r
162 nestedContainer = (GeneratedType)t;
\r
165 assertNotNull(simpleContainer);
\r
166 assertNotNull(nestedContainer);
\r
167 assertEquals(3, simpleContainer.getMethodDefinitions().size());
\r
168 assertEquals(2, nestedContainer.getMethodDefinitions().size());
\r
170 int getFooMethodCounter = 0;
\r
171 int getBarMethodCounter = 0;
\r
172 int getNestedContainerCounter = 0;
\r
174 String getFooMethodReturnTypeName = "";
\r
175 String getBarMethodReturnTypeName = "";
\r
176 String getNestedContainerReturnTypeName = "";
\r
177 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
\r
178 if (method.getName().equals("getFoo")) {
\r
179 getFooMethodCounter++;
\r
180 getFooMethodReturnTypeName = method.getReturnType().getName();
\r
183 if (method.getName().equals("getBar")) {
\r
184 getBarMethodCounter++;
\r
185 getBarMethodReturnTypeName = method.getReturnType().getName();
\r
188 if (method.getName().equals("getNestedContainer")) {
\r
189 getNestedContainerCounter++;
\r
190 getNestedContainerReturnTypeName = method.getReturnType().getName();
\r
194 assertEquals(1, getFooMethodCounter);
\r
195 assertEquals("List", getFooMethodReturnTypeName);
\r
197 assertEquals(1, getBarMethodCounter);
\r
198 assertEquals("String", getBarMethodReturnTypeName);
\r
200 assertEquals(1, getNestedContainerCounter);
\r
201 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
\r
203 getFooMethodCounter = 0;
\r
204 getBarMethodCounter = 0;
\r
206 getFooMethodReturnTypeName = "";
\r
207 getBarMethodReturnTypeName = "";
\r
209 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
\r
210 if (method.getName().equals("getFoo")) {
\r
211 getFooMethodCounter++;
\r
212 getFooMethodReturnTypeName = method.getReturnType().getName();
\r
215 if (method.getName().equals("getBar")) {
\r
216 getBarMethodCounter++;
\r
217 getBarMethodReturnTypeName = method.getReturnType().getName();
\r
221 assertEquals(1, getFooMethodCounter);
\r
222 assertEquals("Short", getFooMethodReturnTypeName);
\r
224 assertEquals(1, getBarMethodCounter);
\r
225 assertEquals("List", getBarMethodReturnTypeName);
\r
229 public void testListResolving() throws URISyntaxException {
\r
230 final URI filePath = getClass().getResource("/simple-list-demo.yang").toURI();
\r
231 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
\r
232 assertNotNull(context);
\r
234 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
235 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
237 assertNotNull(genTypes);
\r
238 assertEquals(5, genTypes.size());
\r
240 int listParentContainerMethodsCount = 0;
\r
241 int simpleListMethodsCount = 0;
\r
242 int listChildContainerMethodsCount = 0;
\r
243 int listKeyClassCount = 0;
\r
245 int getSimpleListKeyMethodCount = 0;
\r
246 int getListChildContainerMethodCount = 0;
\r
247 int getFooMethodCount = 0;
\r
248 int setFooMethodCount = 0;
\r
249 int getSimpleLeafListMethodCount = 0;
\r
250 int setSimpleLeafListMethodCount = 0;
\r
251 int getBarMethodCount = 0;
\r
253 String getSimpleListKeyMethodReturnTypeName = "";
\r
254 String getListChildContainerMethodReturnTypeName = "";
\r
256 int listKeyClassPropertyCount = 0;
\r
257 String listKeyClassPropertyName = "";
\r
258 String listKeyClassPropertyTypeName = "";
\r
259 boolean listKeyClassPropertyReadOnly = false;
\r
261 int hashMethodParameterCount = 0;
\r
262 String hashMethodParameterName = "";
\r
263 String hashMethodParameterReturnTypeName = "";
\r
265 int equalMethodParameterCount = 0;
\r
266 String equalMethodParameterName = "";
\r
267 String equalMethodParameterReturnTypeName = "";
\r
269 for (final Type type : genTypes) {
\r
270 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
\r
271 final GeneratedType genType = (GeneratedType) type;
\r
272 if (genType.getName().equals("ListParentContainer")) {
\r
273 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
\r
274 } else if (genType.getName().equals("SimpleList")) {
\r
275 simpleListMethodsCount = genType.getMethodDefinitions().size();
\r
276 final List<MethodSignature> methods = genType.getMethodDefinitions();
\r
277 for (final MethodSignature method : methods) {
\r
278 if (method.getName().equals("getKey")) {
\r
279 getSimpleListKeyMethodCount++;
\r
280 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
\r
281 } else if (method.getName().equals("getListChildContainer")) {
\r
282 getListChildContainerMethodCount++;
\r
283 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
\r
284 } else if (method.getName().equals("getFoo")) {
\r
285 getFooMethodCount++;
\r
286 } else if (method.getName().equals("setFoo")) {
\r
287 setFooMethodCount++;
\r
288 } else if (method.getName().equals("getSimpleLeafList")) {
\r
289 getSimpleLeafListMethodCount++;
\r
290 } else if (method.getName().equals("setSimpleLeafList")) {
\r
291 setSimpleLeafListMethodCount++;
\r
292 } else if (method.getName().equals("getBar")) {
\r
293 getBarMethodCount++;
\r
296 } else if (genType.getName().equals("ListChildContainer")) {
\r
297 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
\r
299 } else if (type instanceof GeneratedTransferObject) {
\r
300 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
\r
301 final List<GeneratedProperty> properties = genTO.getProperties();
\r
302 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
\r
303 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
\r
305 listKeyClassCount++;
\r
306 listKeyClassPropertyCount = properties.size();
\r
307 listKeyClassPropertyName = properties.get(0).getName();
\r
308 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
\r
309 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
\r
311 hashMethodParameterCount = hashProps.size();
\r
312 hashMethodParameterName = hashProps.get(0).getName();
\r
313 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
\r
315 equalMethodParameterCount = equalProps.size();
\r
316 equalMethodParameterName = equalProps.get(0).getName();
\r
317 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
\r
322 assertEquals(1, listParentContainerMethodsCount);
\r
323 assertEquals(1, listChildContainerMethodsCount);
\r
324 assertEquals(1, getSimpleListKeyMethodCount);
\r
325 assertEquals(1, listKeyClassCount);
\r
327 assertEquals(1, listKeyClassPropertyCount);
\r
328 assertEquals("listKey", listKeyClassPropertyName);
\r
329 assertEquals("Byte", listKeyClassPropertyTypeName);
\r
330 assertEquals(true, listKeyClassPropertyReadOnly);
\r
331 assertEquals(1, hashMethodParameterCount);
\r
332 assertEquals("listKey", hashMethodParameterName);
\r
333 assertEquals("Byte", hashMethodParameterReturnTypeName);
\r
334 assertEquals(1, equalMethodParameterCount);
\r
335 assertEquals("listKey", equalMethodParameterName);
\r
336 assertEquals("Byte", equalMethodParameterReturnTypeName);
\r
338 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
\r
340 assertEquals(1, getListChildContainerMethodCount);
\r
341 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
\r
342 assertEquals(1, getFooMethodCount);
\r
343 assertEquals(0, setFooMethodCount);
\r
344 assertEquals(1, getSimpleLeafListMethodCount);
\r
345 assertEquals(0, setSimpleLeafListMethodCount);
\r
346 assertEquals(1, getBarMethodCount);
\r
348 assertEquals(6, simpleListMethodsCount);
\r
352 public void testListCompositeKeyResolving() throws URISyntaxException {
\r
353 final URI filePath = getClass().getResource("/list-composite-key.yang").toURI();
\r
354 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
\r
356 assertNotNull(context);
\r
358 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
359 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
361 assertNotNull(genTypes);
\r
362 assertEquals(7, genTypes.size());
\r
364 int genTypesCount = 0;
\r
365 int genTOsCount = 0;
\r
367 int compositeKeyListKeyPropertyCount = 0;
\r
368 int compositeKeyListKeyCount = 0;
\r
369 int innerListKeyPropertyCount = 0;
\r
371 for (final Type type : genTypes) {
\r
372 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
\r
374 } else if (type instanceof GeneratedTransferObject) {
\r
375 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
\r
377 if (genTO.getName().equals("CompositeKeyListKey")) {
\r
378 compositeKeyListKeyCount++;
\r
379 final List<GeneratedProperty> properties = genTO.getProperties();
\r
380 for (final GeneratedProperty prop : properties) {
\r
381 if (prop.getName().equals("key1")) {
\r
382 compositeKeyListKeyPropertyCount++;
\r
383 } else if (prop.getName().equals("key2")) {
\r
384 compositeKeyListKeyPropertyCount++;
\r
388 } else if (genTO.getName().equals("InnerListKey")) {
\r
389 final List<GeneratedProperty> properties = genTO.getProperties();
\r
390 innerListKeyPropertyCount = properties.size();
\r
395 assertEquals(1, compositeKeyListKeyCount);
\r
396 assertEquals(2, compositeKeyListKeyPropertyCount);
\r
398 assertEquals(1, innerListKeyPropertyCount);
\r
400 assertEquals(5, genTypesCount);
\r
401 assertEquals(2, genTOsCount);
\r
405 public void testGeneratedTypes() throws URISyntaxException {
\r
406 final URI filePath = getClass().getResource("/demo-topology.yang").toURI();
\r
407 final SchemaContext context = resolveSchemaContextFromFiles(filePath);
\r
408 assertNotNull(context);
\r
410 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
411 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
413 assertNotNull(genTypes);
\r
414 assertEquals(14, genTypes.size());
\r
416 int genTypesCount = 0;
\r
417 int genTOsCount = 0;
\r
418 for (final Type type : genTypes) {
\r
419 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
\r
421 } else if (type instanceof GeneratedTransferObject) {
\r
426 assertEquals(11, genTypesCount);
\r
427 assertEquals(3, genTOsCount);
\r