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;
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.Arrays;
17 import java.util.Collections;
18 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class GeneratedTypesTest {
33 public void testMultipleModulesResolving() throws URISyntaxException, IOException {
34 File abstractTopology = new File(getClass().getResource("/abstract-topology.yang").toURI());
35 File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
37 final SchemaContext context = new YangParserImpl().parseFiles(Arrays.asList(abstractTopology, ietfInetTypes));
38 assertNotNull(context);
40 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
41 final List<Type> genTypes = bindingGen.generateTypes(context);
43 assertNotNull(genTypes);
44 assertEquals(29, genTypes.size());
48 public void testContainerResolving() throws URISyntaxException, IOException {
49 final File testFile = new File(getClass().getResource("/simple-container-demo.yang").toURI());
50 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
51 assertNotNull(context);
53 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
54 final List<Type> genTypes = bindingGen.generateTypes(context);
56 assertNotNull(genTypes);
57 assertEquals(3, genTypes.size());
59 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
60 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
61 for (Type t : genTypes) {
62 if ("SimpleContainer".equals(t.getName())) {
63 simpleContainer = (GeneratedType) t;
64 } else if ("NestedContainer".equals(t.getName())) {
65 nestedContainer = (GeneratedType) t;
68 assertNotNull(simpleContainer);
69 assertNotNull(nestedContainer);
70 assertEquals(3, simpleContainer.getMethodDefinitions().size());
71 assertEquals(2, nestedContainer.getMethodDefinitions().size());
73 int getFooMethodCounter = 0;
74 int getBarMethodCounter = 0;
75 int getNestedContainerCounter = 0;
77 String getFooMethodReturnTypeName = "";
78 String getBarMethodReturnTypeName = "";
79 String getNestedContainerReturnTypeName = "";
80 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
81 if (method.getName().equals("getFoo")) {
82 getFooMethodCounter++;
83 getFooMethodReturnTypeName = method.getReturnType().getName();
86 if (method.getName().equals("getBar")) {
87 getBarMethodCounter++;
88 getBarMethodReturnTypeName = method.getReturnType().getName();
91 if (method.getName().equals("getNestedContainer")) {
92 getNestedContainerCounter++;
93 getNestedContainerReturnTypeName = method.getReturnType().getName();
97 assertEquals(1, getFooMethodCounter);
98 assertEquals("Integer", getFooMethodReturnTypeName);
100 assertEquals(1, getBarMethodCounter);
101 assertEquals("String", getBarMethodReturnTypeName);
103 assertEquals(1, getNestedContainerCounter);
104 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
106 getFooMethodCounter = 0;
107 getBarMethodCounter = 0;
109 getFooMethodReturnTypeName = "";
110 getBarMethodReturnTypeName = "";
112 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
114 if (method.getName().equals("getFoo")) {
115 getFooMethodCounter++;
116 getFooMethodReturnTypeName = method.getReturnType().getName();
119 if (method.getName().equals("getBar")) {
120 getBarMethodCounter++;
121 getBarMethodReturnTypeName = method.getReturnType().getName();
125 assertEquals(1, getFooMethodCounter);
126 assertEquals("Short", getFooMethodReturnTypeName);
128 assertEquals(1, getBarMethodCounter);
129 assertEquals("String", getBarMethodReturnTypeName);
133 public void testLeafListResolving() throws URISyntaxException, IOException {
134 final File testFile = new File(getClass().getResource("/simple-leaf-list-demo.yang").toURI());
135 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
136 assertNotNull(context);
138 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
139 final List<Type> genTypes = bindingGen.generateTypes(context);
141 assertNotNull(genTypes);
142 assertEquals(3, genTypes.size());
144 GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
145 GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
146 for (Type t : genTypes) {
147 if ("SimpleContainer".equals(t.getName())) {
148 simpleContainer = (GeneratedType) t;
149 } else if ("NestedContainer".equals(t.getName())) {
150 nestedContainer = (GeneratedType) t;
153 assertNotNull(simpleContainer);
154 assertNotNull(nestedContainer);
155 assertEquals(3, simpleContainer.getMethodDefinitions().size());
156 assertEquals(2, nestedContainer.getMethodDefinitions().size());
158 int getFooMethodCounter = 0;
159 int getBarMethodCounter = 0;
160 int getNestedContainerCounter = 0;
162 String getFooMethodReturnTypeName = "";
163 String getBarMethodReturnTypeName = "";
164 String getNestedContainerReturnTypeName = "";
165 for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
166 if (method.getName().equals("getFoo")) {
167 getFooMethodCounter++;
168 getFooMethodReturnTypeName = method.getReturnType().getName();
171 if (method.getName().equals("getBar")) {
172 getBarMethodCounter++;
173 getBarMethodReturnTypeName = method.getReturnType().getName();
176 if (method.getName().equals("getNestedContainer")) {
177 getNestedContainerCounter++;
178 getNestedContainerReturnTypeName = method.getReturnType().getName();
182 assertEquals(1, getFooMethodCounter);
183 assertEquals("List", getFooMethodReturnTypeName);
185 assertEquals(1, getBarMethodCounter);
186 assertEquals("String", getBarMethodReturnTypeName);
188 assertEquals(1, getNestedContainerCounter);
189 assertEquals("NestedContainer", getNestedContainerReturnTypeName);
191 getFooMethodCounter = 0;
192 getBarMethodCounter = 0;
194 getFooMethodReturnTypeName = "";
195 getBarMethodReturnTypeName = "";
197 for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
198 if (method.getName().equals("getFoo")) {
199 getFooMethodCounter++;
200 getFooMethodReturnTypeName = method.getReturnType().getName();
203 if (method.getName().equals("getBar")) {
204 getBarMethodCounter++;
205 getBarMethodReturnTypeName = method.getReturnType().getName();
209 assertEquals(1, getFooMethodCounter);
210 assertEquals("Short", getFooMethodReturnTypeName);
212 assertEquals(1, getBarMethodCounter);
213 assertEquals("List", getBarMethodReturnTypeName);
217 public void testListResolving() throws URISyntaxException, IOException {
218 final File testFile = new File(getClass().getResource("/simple-list-demo.yang").toURI());
219 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
220 assertNotNull(context);
222 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
223 final List<Type> genTypes = bindingGen.generateTypes(context);
225 assertNotNull(genTypes);
226 assertEquals(5, genTypes.size());
228 int listParentContainerMethodsCount = 0;
229 int simpleListMethodsCount = 0;
230 int listChildContainerMethodsCount = 0;
231 int listKeyClassCount = 0;
233 int getSimpleListKeyMethodCount = 0;
234 int getListChildContainerMethodCount = 0;
235 int getFooMethodCount = 0;
236 int setFooMethodCount = 0;
237 int getSimpleLeafListMethodCount = 0;
238 int setSimpleLeafListMethodCount = 0;
239 int getBarMethodCount = 0;
241 String getSimpleListKeyMethodReturnTypeName = "";
242 String getListChildContainerMethodReturnTypeName = "";
244 int listKeyClassPropertyCount = 0;
245 String listKeyClassPropertyName = "";
246 String listKeyClassPropertyTypeName = "";
247 boolean listKeyClassPropertyReadOnly = false;
249 int hashMethodParameterCount = 0;
250 String hashMethodParameterName = "";
251 String hashMethodParameterReturnTypeName = "";
253 int equalMethodParameterCount = 0;
254 String equalMethodParameterName = "";
255 String equalMethodParameterReturnTypeName = "";
257 for (final Type type : genTypes) {
258 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
259 final GeneratedType genType = (GeneratedType) type;
260 if (genType.getName().equals("ListParentContainer")) {
261 listParentContainerMethodsCount = genType.getMethodDefinitions().size();
262 } else if (genType.getName().equals("SimpleList")) {
263 simpleListMethodsCount = genType.getMethodDefinitions().size();
264 final List<MethodSignature> methods = genType.getMethodDefinitions();
265 for (final MethodSignature method : methods) {
266 if (method.getName().equals("getKey")) {
267 getSimpleListKeyMethodCount++;
268 getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
269 } else if (method.getName().equals("getListChildContainer")) {
270 getListChildContainerMethodCount++;
271 getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
272 } else if (method.getName().equals("getFoo")) {
274 } else if (method.getName().equals("setFoo")) {
276 } else if (method.getName().equals("getSimpleLeafList")) {
277 getSimpleLeafListMethodCount++;
278 } else if (method.getName().equals("setSimpleLeafList")) {
279 setSimpleLeafListMethodCount++;
280 } else if (method.getName().equals("getBar")) {
284 } else if (genType.getName().equals("ListChildContainer")) {
285 listChildContainerMethodsCount = genType.getMethodDefinitions().size();
287 } else if (type instanceof GeneratedTransferObject) {
288 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
289 final List<GeneratedProperty> properties = genTO.getProperties();
290 final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
291 final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
294 listKeyClassPropertyCount = properties.size();
295 listKeyClassPropertyName = properties.get(0).getName();
296 listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
297 listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
299 hashMethodParameterCount = hashProps.size();
300 hashMethodParameterName = hashProps.get(0).getName();
301 hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
303 equalMethodParameterCount = equalProps.size();
304 equalMethodParameterName = equalProps.get(0).getName();
305 equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
310 assertEquals(1, listParentContainerMethodsCount);
311 assertEquals(1, listChildContainerMethodsCount);
312 assertEquals(1, getSimpleListKeyMethodCount);
313 assertEquals(1, listKeyClassCount);
315 assertEquals(1, listKeyClassPropertyCount);
316 assertEquals("listKey", listKeyClassPropertyName);
317 assertEquals("Byte", listKeyClassPropertyTypeName);
318 assertEquals(true, listKeyClassPropertyReadOnly);
319 assertEquals(1, hashMethodParameterCount);
320 assertEquals("listKey", hashMethodParameterName);
321 assertEquals("Byte", hashMethodParameterReturnTypeName);
322 assertEquals(1, equalMethodParameterCount);
323 assertEquals("listKey", equalMethodParameterName);
324 assertEquals("Byte", equalMethodParameterReturnTypeName);
326 assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
328 assertEquals(1, getListChildContainerMethodCount);
329 assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
330 assertEquals(1, getFooMethodCount);
331 assertEquals(0, setFooMethodCount);
332 assertEquals(1, getSimpleLeafListMethodCount);
333 assertEquals(0, setSimpleLeafListMethodCount);
334 assertEquals(1, getBarMethodCount);
336 assertEquals(6, simpleListMethodsCount);
340 public void testListCompositeKeyResolving() throws URISyntaxException, IOException {
341 final File testFile = new File(getClass().getResource("/list-composite-key.yang").toURI());
342 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
343 assertNotNull(context);
345 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
346 final List<Type> genTypes = bindingGen.generateTypes(context);
348 assertNotNull(genTypes);
349 assertEquals(7, genTypes.size());
351 int genTypesCount = 0;
354 int compositeKeyListKeyPropertyCount = 0;
355 int compositeKeyListKeyCount = 0;
356 int innerListKeyPropertyCount = 0;
358 for (final Type type : genTypes) {
359 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
361 } else if (type instanceof GeneratedTransferObject) {
362 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
364 if (genTO.getName().equals("CompositeKeyListKey")) {
365 compositeKeyListKeyCount++;
366 final List<GeneratedProperty> properties = genTO.getProperties();
367 for (final GeneratedProperty prop : properties) {
368 if (prop.getName().equals("key1")) {
369 compositeKeyListKeyPropertyCount++;
370 } else if (prop.getName().equals("key2")) {
371 compositeKeyListKeyPropertyCount++;
375 } else if (genTO.getName().equals("InnerListKey")) {
376 final List<GeneratedProperty> properties = genTO.getProperties();
377 innerListKeyPropertyCount = properties.size();
382 assertEquals(1, compositeKeyListKeyCount);
383 assertEquals(2, compositeKeyListKeyPropertyCount);
385 assertEquals(1, innerListKeyPropertyCount);
387 assertEquals(5, genTypesCount);
388 assertEquals(2, genTOsCount);
392 public void testGeneratedTypes() throws URISyntaxException, IOException {
393 final File testFile = new File(getClass().getResource("/demo-topology.yang").toURI());
394 final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
395 assertNotNull(context);
397 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
398 final List<Type> genTypes = bindingGen.generateTypes(context);
400 assertNotNull(genTypes);
401 assertEquals(14, genTypes.size());
403 int genTypesCount = 0;
405 for (final Type type : genTypes) {
406 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
408 } else if (type instanceof GeneratedTransferObject) {
413 assertEquals(11, genTypesCount);
414 assertEquals(3, genTOsCount);