-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.sal.binding.generator.impl;\r
-\r
-import static org.junit.Assert.*;\r
-\r
-import java.io.File;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.junit.Test;\r
-import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;\r
-import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;\r
-import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
-import org.opendaylight.yangtools.yang.model.api.Module;\r
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;\r
-import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;\r
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;\r
-\r
-public class GeneratedTypesTest {\r
-\r
- private SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) {\r
- final YangModelParser parser = new YangParserImpl();\r
-\r
- final List<File> inputFiles = new ArrayList<File>();\r
- for (int i = 0; i < yangFiles.length; ++i) {\r
- inputFiles.add(new File(yangFiles[i]));\r
- }\r
-\r
- final Set<Module> modules = parser.parseYangModels(inputFiles);\r
- return parser.resolveSchemaContext(modules);\r
- }\r
-\r
- @Test\r
- public void testMultipleModulesResolving() throws URISyntaxException {\r
- final URI topologyPath = getClass().getResource("/abstract-topology.yang").toURI();\r
- final URI typesPath = getClass().getResource("/ietf-inet-types@2010-09-24.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, typesPath);\r
- assertNotNull(context);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(29, genTypes.size());\r
- }\r
-\r
- @Test\r
- public void testLeafrefResolving() throws URISyntaxException {\r
- final URI topologyPath = getClass().getResource("/leafref-test-models/abstract-topology@2013-02-08.yang")\r
- .toURI();\r
- final URI interfacesPath = getClass().getResource("/leafref-test-models/ietf-interfaces@2012-11-15.yang")\r
- .toURI();\r
- final URI inetTypesPath = getClass().getResource("/leafref-test-models/ietf-inet-types@2010-09-24.yang")\r
- .toURI();\r
- final URI yangTypesPath = getClass().getResource("/leafref-test-models/ietf-yang-types@2010-09-24.yang")\r
- .toURI();\r
-\r
- assertNotNull(topologyPath);\r
- assertNotNull(interfacesPath);\r
- // assertTrue(ifTypePath != null);\r
- assertNotNull(inetTypesPath);\r
- assertNotNull(yangTypesPath);\r
-\r
- // final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, ifTypePath,\r
- // inetTypesPath, yangTypesPath);\r
- final SchemaContext context = resolveSchemaContextFromFiles(topologyPath, interfacesPath, inetTypesPath,\r
- yangTypesPath);\r
- assertNotNull(context);\r
- assertEquals(4, context.getModules().size());\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertEquals(54, genTypes.size());\r
- assertNotNull(genTypes);\r
-\r
- GeneratedTransferObject gtIfcKey = null;\r
- GeneratedType gtIfc = null;\r
- GeneratedType gtNetworkLink = null;\r
- GeneratedType gtSource = null;\r
- GeneratedType gtDest = null;\r
- GeneratedType gtTunnel = null;\r
- GeneratedTransferObject gtTunnelKey = null;\r
- GeneratedType gtTopology = null;\r
- for (final Type type : genTypes) {\r
- String name = type.getName();\r
- if ("InterfaceKey".equals(name)) {\r
- gtIfcKey = (GeneratedTransferObject) type;\r
- } else if ("Interface".equals(name)) {\r
- gtIfc = (GeneratedType) type;\r
- } else if ("NetworkLink".equals(name)) {\r
- gtNetworkLink = (GeneratedType) type;\r
- } else if ("SourceNode".equals(name)) {\r
- gtSource = (GeneratedType) type;\r
- } else if ("DestinationNode".equals(name)) {\r
- gtDest = (GeneratedType) type;\r
- } else if ("Tunnel".equals(name)) {\r
- gtTunnel = (GeneratedType) type;\r
- } else if ("TunnelKey".equals(name)) {\r
- gtTunnelKey = (GeneratedTransferObject) type;\r
- } else if ("Topology".equals(name)) {\r
- gtTopology = (GeneratedType) type;\r
- }\r
- }\r
-\r
- assertNotNull(gtIfcKey);\r
- assertNotNull(gtIfc);\r
- assertNotNull(gtNetworkLink);\r
- assertNotNull(gtSource);\r
- assertNotNull(gtDest);\r
- assertNotNull(gtTunnel);\r
- assertNotNull(gtTunnelKey);\r
- assertNotNull(gtTopology);\r
-\r
- // Topology\r
- final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();\r
- assertNotNull(gtTopoMethods);\r
- MethodSignature condLeafref = null;\r
- for (final MethodSignature method : gtTopoMethods) {\r
- if (method.getName().equals("getCondLeafref")) {\r
- condLeafref = method;\r
- }\r
- }\r
- assertNotNull(condLeafref);\r
- Type condLeafRT = condLeafref.getReturnType();\r
- assertNotNull(condLeafRT);\r
- assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());\r
-\r
- // InterfaceId\r
- final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();\r
- assertNotNull(gtIfcKeyProps);\r
- GeneratedProperty ifcIdProp = null;\r
- for (final GeneratedProperty property : gtIfcKeyProps) {\r
- if (property.getName().equals("interfaceId")) {\r
- ifcIdProp = property;\r
- }\r
- }\r
- assertNotNull(ifcIdProp);\r
- Type ifcIdPropType = ifcIdProp.getReturnType();\r
- assertNotNull(ifcIdPropType);\r
- assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());\r
-\r
- // Interface\r
- final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();\r
- assertNotNull(gtIfcMethods);\r
- MethodSignature getIfcKey = null;\r
- MethodSignature getHigherLayerIf = null;\r
- for (final MethodSignature method : gtIfcMethods) {\r
- if (method.getName().equals("getKey")) {\r
- getIfcKey = method;\r
- } else if (method.getName().equals("getHigherLayerIf")) {\r
- getHigherLayerIf = method;\r
- }\r
- }\r
- assertNotNull(getIfcKey);\r
- Type getIfcKeyType = getIfcKey.getReturnType();\r
- assertNotNull(getIfcKeyType);\r
- assertNotSame("java.lang.Void", getIfcKeyType);\r
- assertEquals("InterfaceKey", getIfcKeyType.getName());\r
-\r
- assertNotNull(getHigherLayerIf);\r
- Type getHigherLayerIfType = getHigherLayerIf.getReturnType();\r
- assertNotNull(getHigherLayerIfType);\r
- assertNotSame("java.lang.Void", getHigherLayerIfType);\r
- assertEquals("List", getHigherLayerIfType.getName());\r
-\r
- // NetworkLink\r
- final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();\r
- assertNotNull(gtNetworkLinkMethods);\r
- MethodSignature getIfc = null;\r
- for (MethodSignature method : gtNetworkLinkMethods) {\r
- if (method.getName().equals("getInterface")) {\r
- getIfc = method;\r
- }\r
- }\r
- assertNotNull(getIfc);\r
- Type getIfcType = getIfc.getReturnType();\r
- assertNotNull(getIfcType);\r
- assertNotSame("java.lang.Void", getIfcType);\r
- assertEquals("String", getIfcType.getName());\r
-\r
- // SourceNode\r
- final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();\r
- assertNotNull(gtSourceMethods);\r
- MethodSignature getIdSource = null;\r
- for (MethodSignature method : gtSourceMethods) {\r
- if (method.getName().equals("getId")) {\r
- getIdSource = method;\r
- }\r
- }\r
- assertNotNull(getIdSource);\r
- Type getIdType = getIdSource.getReturnType();\r
- assertNotNull(getIdType);\r
- assertNotSame("java.lang.Void", getIdType);\r
- assertEquals("Uri", getIdType.getName());\r
-\r
- // DestinationNode\r
- final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();\r
- assertNotNull(gtDestMethods);\r
- MethodSignature getIdDest = null;\r
- for (MethodSignature method : gtDestMethods) {\r
- if (method.getName().equals("getId")) {\r
- getIdDest = method;\r
- }\r
- }\r
- assertNotNull(getIdDest);\r
- Type getIdDestType = getIdDest.getReturnType();\r
- assertNotNull(getIdDestType);\r
- assertNotSame("java.lang.Void", getIdDestType);\r
- assertEquals("Uri", getIdDestType.getName());\r
-\r
- // Tunnel\r
- final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();\r
- assertNotNull(gtTunnelMethods);\r
- MethodSignature getTunnelKey = null;\r
- for (MethodSignature method : gtTunnelMethods) {\r
- if (method.getName().equals("getKey")) {\r
- getTunnelKey = method;\r
- }\r
- }\r
- assertNotNull(getTunnelKey);\r
- Type getTunnelKeyType = getTunnelKey.getReturnType();\r
- assertNotNull(getTunnelKeyType);\r
- assertNotSame("java.lang.Void", getTunnelKeyType);\r
- assertEquals("TunnelKey", getTunnelKeyType.getName());\r
-\r
- // TunnelKey\r
- final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();\r
- assertNotNull(gtTunnelKeyProps);\r
- GeneratedProperty tunnelId = null;\r
- for (final GeneratedProperty property : gtTunnelKeyProps) {\r
- if (property.getName().equals("tunnelId")) {\r
- tunnelId = property;\r
- }\r
- }\r
- assertNotNull(tunnelId);\r
- Type tunnelIdType = tunnelId.getReturnType();\r
- assertNotNull(tunnelIdType);\r
- assertNotSame("java.lang.Void", tunnelIdType);\r
- assertEquals("Uri", tunnelIdType.getName());\r
- }\r
-\r
- @Test\r
- public void testContainerResolving() throws URISyntaxException {\r
- final URI filePath = getClass().getResource("/simple-container-demo.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
- assert (context != null);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(3, genTypes.size());\r
-\r
- GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);\r
- GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);\r
- for (Type t : genTypes) {\r
- if ("SimpleContainer".equals(t.getName())) {\r
- simpleContainer = (GeneratedType)t;\r
- } else if ("NestedContainer".equals(t.getName())) {\r
- nestedContainer = (GeneratedType)t;\r
- }\r
- }\r
- assertNotNull(simpleContainer);\r
- assertNotNull(nestedContainer);\r
- assertEquals(3, simpleContainer.getMethodDefinitions().size());\r
- assertEquals(2, nestedContainer.getMethodDefinitions().size());\r
-\r
- int getFooMethodCounter = 0;\r
- int getBarMethodCounter = 0;\r
- int getNestedContainerCounter = 0;\r
-\r
- String getFooMethodReturnTypeName = "";\r
- String getBarMethodReturnTypeName = "";\r
- String getNestedContainerReturnTypeName = "";\r
- for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {\r
- if (method.getName().equals("getFoo")) {\r
- getFooMethodCounter++;\r
- getFooMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getBar")) {\r
- getBarMethodCounter++;\r
- getBarMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getNestedContainer")) {\r
- getNestedContainerCounter++;\r
- getNestedContainerReturnTypeName = method.getReturnType().getName();\r
- }\r
- }\r
-\r
- assertEquals(1, getFooMethodCounter);\r
- assertEquals("Integer", getFooMethodReturnTypeName);\r
-\r
- assertEquals(1, getBarMethodCounter);\r
- assertEquals("String", getBarMethodReturnTypeName);\r
-\r
- assertEquals(1, getNestedContainerCounter);\r
- assertEquals("NestedContainer", getNestedContainerReturnTypeName);\r
-\r
- getFooMethodCounter = 0;\r
- getBarMethodCounter = 0;\r
-\r
- getFooMethodReturnTypeName = "";\r
- getBarMethodReturnTypeName = "";\r
-\r
- for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {\r
-\r
- if (method.getName().equals("getFoo")) {\r
- getFooMethodCounter++;\r
- getFooMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getBar")) {\r
- getBarMethodCounter++;\r
- getBarMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
- }\r
-\r
- assertEquals(1, getFooMethodCounter);\r
- assertEquals("Short", getFooMethodReturnTypeName);\r
-\r
- assertEquals(1, getBarMethodCounter);\r
- assertEquals("String", getBarMethodReturnTypeName);\r
- }\r
-\r
- @Test\r
- public void testLeafListResolving() throws URISyntaxException {\r
- final URI filePath = getClass().getResource("/simple-leaf-list-demo.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
- assertNotNull(context);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(3, genTypes.size());\r
-\r
- GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);\r
- GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);\r
- for (Type t : genTypes) {\r
- if ("SimpleContainer".equals(t.getName())) {\r
- simpleContainer = (GeneratedType)t;\r
- } else if ("NestedContainer".equals(t.getName())) {\r
- nestedContainer = (GeneratedType)t;\r
- }\r
- }\r
- assertNotNull(simpleContainer);\r
- assertNotNull(nestedContainer);\r
- assertEquals(3, simpleContainer.getMethodDefinitions().size());\r
- assertEquals(2, nestedContainer.getMethodDefinitions().size());\r
-\r
- int getFooMethodCounter = 0;\r
- int getBarMethodCounter = 0;\r
- int getNestedContainerCounter = 0;\r
-\r
- String getFooMethodReturnTypeName = "";\r
- String getBarMethodReturnTypeName = "";\r
- String getNestedContainerReturnTypeName = "";\r
- for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {\r
- if (method.getName().equals("getFoo")) {\r
- getFooMethodCounter++;\r
- getFooMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getBar")) {\r
- getBarMethodCounter++;\r
- getBarMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getNestedContainer")) {\r
- getNestedContainerCounter++;\r
- getNestedContainerReturnTypeName = method.getReturnType().getName();\r
- }\r
- }\r
-\r
- assertEquals(1, getFooMethodCounter);\r
- assertEquals("List", getFooMethodReturnTypeName);\r
-\r
- assertEquals(1, getBarMethodCounter);\r
- assertEquals("String", getBarMethodReturnTypeName);\r
-\r
- assertEquals(1, getNestedContainerCounter);\r
- assertEquals("NestedContainer", getNestedContainerReturnTypeName);\r
-\r
- getFooMethodCounter = 0;\r
- getBarMethodCounter = 0;\r
-\r
- getFooMethodReturnTypeName = "";\r
- getBarMethodReturnTypeName = "";\r
-\r
- for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {\r
- if (method.getName().equals("getFoo")) {\r
- getFooMethodCounter++;\r
- getFooMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
-\r
- if (method.getName().equals("getBar")) {\r
- getBarMethodCounter++;\r
- getBarMethodReturnTypeName = method.getReturnType().getName();\r
- }\r
- }\r
-\r
- assertEquals(1, getFooMethodCounter);\r
- assertEquals("Short", getFooMethodReturnTypeName);\r
-\r
- assertEquals(1, getBarMethodCounter);\r
- assertEquals("List", getBarMethodReturnTypeName);\r
- }\r
-\r
- @Test\r
- public void testListResolving() throws URISyntaxException {\r
- final URI filePath = getClass().getResource("/simple-list-demo.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
- assertNotNull(context);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(5, genTypes.size());\r
-\r
- int listParentContainerMethodsCount = 0;\r
- int simpleListMethodsCount = 0;\r
- int listChildContainerMethodsCount = 0;\r
- int listKeyClassCount = 0;\r
-\r
- int getSimpleListKeyMethodCount = 0;\r
- int getListChildContainerMethodCount = 0;\r
- int getFooMethodCount = 0;\r
- int setFooMethodCount = 0;\r
- int getSimpleLeafListMethodCount = 0;\r
- int setSimpleLeafListMethodCount = 0;\r
- int getBarMethodCount = 0;\r
-\r
- String getSimpleListKeyMethodReturnTypeName = "";\r
- String getListChildContainerMethodReturnTypeName = "";\r
-\r
- int listKeyClassPropertyCount = 0;\r
- String listKeyClassPropertyName = "";\r
- String listKeyClassPropertyTypeName = "";\r
- boolean listKeyClassPropertyReadOnly = false;\r
-\r
- int hashMethodParameterCount = 0;\r
- String hashMethodParameterName = "";\r
- String hashMethodParameterReturnTypeName = "";\r
-\r
- int equalMethodParameterCount = 0;\r
- String equalMethodParameterName = "";\r
- String equalMethodParameterReturnTypeName = "";\r
-\r
- for (final Type type : genTypes) {\r
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {\r
- final GeneratedType genType = (GeneratedType) type;\r
- if (genType.getName().equals("ListParentContainer")) {\r
- listParentContainerMethodsCount = genType.getMethodDefinitions().size();\r
- } else if (genType.getName().equals("SimpleList")) {\r
- simpleListMethodsCount = genType.getMethodDefinitions().size();\r
- final List<MethodSignature> methods = genType.getMethodDefinitions();\r
- for (final MethodSignature method : methods) {\r
- if (method.getName().equals("getKey")) {\r
- getSimpleListKeyMethodCount++;\r
- getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();\r
- } else if (method.getName().equals("getListChildContainer")) {\r
- getListChildContainerMethodCount++;\r
- getListChildContainerMethodReturnTypeName = method.getReturnType().getName();\r
- } else if (method.getName().equals("getFoo")) {\r
- getFooMethodCount++;\r
- } else if (method.getName().equals("setFoo")) {\r
- setFooMethodCount++;\r
- } else if (method.getName().equals("getSimpleLeafList")) {\r
- getSimpleLeafListMethodCount++;\r
- } else if (method.getName().equals("setSimpleLeafList")) {\r
- setSimpleLeafListMethodCount++;\r
- } else if (method.getName().equals("getBar")) {\r
- getBarMethodCount++;\r
- }\r
- }\r
- } else if (genType.getName().equals("ListChildContainer")) {\r
- listChildContainerMethodsCount = genType.getMethodDefinitions().size();\r
- }\r
- } else if (type instanceof GeneratedTransferObject) {\r
- final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
- final List<GeneratedProperty> properties = genTO.getProperties();\r
- final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();\r
- final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();\r
-\r
- listKeyClassCount++;\r
- listKeyClassPropertyCount = properties.size();\r
- listKeyClassPropertyName = properties.get(0).getName();\r
- listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();\r
- listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();\r
-\r
- hashMethodParameterCount = hashProps.size();\r
- hashMethodParameterName = hashProps.get(0).getName();\r
- hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();\r
-\r
- equalMethodParameterCount = equalProps.size();\r
- equalMethodParameterName = equalProps.get(0).getName();\r
- equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();\r
-\r
- }\r
- }\r
-\r
- assertEquals(1, listParentContainerMethodsCount);\r
- assertEquals(1, listChildContainerMethodsCount);\r
- assertEquals(1, getSimpleListKeyMethodCount);\r
- assertEquals(1, listKeyClassCount);\r
-\r
- assertEquals(1, listKeyClassPropertyCount);\r
- assertEquals("listKey", listKeyClassPropertyName);\r
- assertEquals("Byte", listKeyClassPropertyTypeName);\r
- assertEquals(true, listKeyClassPropertyReadOnly);\r
- assertEquals(1, hashMethodParameterCount);\r
- assertEquals("listKey", hashMethodParameterName);\r
- assertEquals("Byte", hashMethodParameterReturnTypeName);\r
- assertEquals(1, equalMethodParameterCount);\r
- assertEquals("listKey", equalMethodParameterName);\r
- assertEquals("Byte", equalMethodParameterReturnTypeName);\r
-\r
- assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);\r
-\r
- assertEquals(1, getListChildContainerMethodCount);\r
- assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);\r
- assertEquals(1, getFooMethodCount);\r
- assertEquals(0, setFooMethodCount);\r
- assertEquals(1, getSimpleLeafListMethodCount);\r
- assertEquals(0, setSimpleLeafListMethodCount);\r
- assertEquals(1, getBarMethodCount);\r
-\r
- assertEquals(6, simpleListMethodsCount);\r
- }\r
-\r
- @Test\r
- public void testListCompositeKeyResolving() throws URISyntaxException {\r
- final URI filePath = getClass().getResource("/list-composite-key.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
-\r
- assertNotNull(context);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(7, genTypes.size());\r
-\r
- int genTypesCount = 0;\r
- int genTOsCount = 0;\r
-\r
- int compositeKeyListKeyPropertyCount = 0;\r
- int compositeKeyListKeyCount = 0;\r
- int innerListKeyPropertyCount = 0;\r
-\r
- for (final Type type : genTypes) {\r
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {\r
- genTypesCount++;\r
- } else if (type instanceof GeneratedTransferObject) {\r
- final GeneratedTransferObject genTO = (GeneratedTransferObject) type;\r
-\r
- if (genTO.getName().equals("CompositeKeyListKey")) {\r
- compositeKeyListKeyCount++;\r
- final List<GeneratedProperty> properties = genTO.getProperties();\r
- for (final GeneratedProperty prop : properties) {\r
- if (prop.getName().equals("key1")) {\r
- compositeKeyListKeyPropertyCount++;\r
- } else if (prop.getName().equals("key2")) {\r
- compositeKeyListKeyPropertyCount++;\r
- }\r
- }\r
- genTOsCount++;\r
- } else if (genTO.getName().equals("InnerListKey")) {\r
- final List<GeneratedProperty> properties = genTO.getProperties();\r
- innerListKeyPropertyCount = properties.size();\r
- genTOsCount++;\r
- }\r
- }\r
- }\r
- assertEquals(1, compositeKeyListKeyCount);\r
- assertEquals(2, compositeKeyListKeyPropertyCount);\r
-\r
- assertEquals(1, innerListKeyPropertyCount);\r
-\r
- assertEquals(5, genTypesCount);\r
- assertEquals(2, genTOsCount);\r
- }\r
-\r
- @Test\r
- public void testGeneratedTypes() throws URISyntaxException {\r
- final URI filePath = getClass().getResource("/demo-topology.yang").toURI();\r
- final SchemaContext context = resolveSchemaContextFromFiles(filePath);\r
- assertNotNull(context);\r
-\r
- final BindingGenerator bindingGen = new BindingGeneratorImpl();\r
- final List<Type> genTypes = bindingGen.generateTypes(context);\r
-\r
- assertNotNull(genTypes);\r
- assertEquals(14, genTypes.size());\r
-\r
- int genTypesCount = 0;\r
- int genTOsCount = 0;\r
- for (final Type type : genTypes) {\r
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {\r
- genTypesCount++;\r
- } else if (type instanceof GeneratedTransferObject) {\r
- genTOsCount++;\r
- }\r
- }\r
-\r
- assertEquals(11, genTypesCount);\r
- assertEquals(3, genTOsCount);\r
- }\r
-}\r
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
+import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class GeneratedTypesTest {
+
+ @Test
+ public void testMultipleModulesResolving() throws URISyntaxException, IOException {
+ File abstractTopology = new File(getClass().getResource("/abstract-topology.yang").toURI());
+ File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
+
+ final SchemaContext context = new YangParserImpl().parseFiles(Arrays.asList(abstractTopology, ietfInetTypes));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(29, genTypes.size());
+ }
+
+ @Test
+ public void testContainerResolving() throws URISyntaxException, IOException {
+ final File testFile = new File(getClass().getResource("/simple-container-demo.yang").toURI());
+ final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(3, genTypes.size());
+
+ GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
+ GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
+ for (Type t : genTypes) {
+ if ("SimpleContainer".equals(t.getName())) {
+ simpleContainer = (GeneratedType) t;
+ } else if ("NestedContainer".equals(t.getName())) {
+ nestedContainer = (GeneratedType) t;
+ }
+ }
+ assertNotNull(simpleContainer);
+ assertNotNull(nestedContainer);
+ assertEquals(3, simpleContainer.getMethodDefinitions().size());
+ assertEquals(2, nestedContainer.getMethodDefinitions().size());
+
+ int getFooMethodCounter = 0;
+ int getBarMethodCounter = 0;
+ int getNestedContainerCounter = 0;
+
+ String getFooMethodReturnTypeName = "";
+ String getBarMethodReturnTypeName = "";
+ String getNestedContainerReturnTypeName = "";
+ for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
+ if (method.getName().equals("getFoo")) {
+ getFooMethodCounter++;
+ getFooMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getBar")) {
+ getBarMethodCounter++;
+ getBarMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getNestedContainer")) {
+ getNestedContainerCounter++;
+ getNestedContainerReturnTypeName = method.getReturnType().getName();
+ }
+ }
+
+ assertEquals(1, getFooMethodCounter);
+ assertEquals("Integer", getFooMethodReturnTypeName);
+
+ assertEquals(1, getBarMethodCounter);
+ assertEquals("String", getBarMethodReturnTypeName);
+
+ assertEquals(1, getNestedContainerCounter);
+ assertEquals("NestedContainer", getNestedContainerReturnTypeName);
+
+ getFooMethodCounter = 0;
+ getBarMethodCounter = 0;
+
+ getFooMethodReturnTypeName = "";
+ getBarMethodReturnTypeName = "";
+
+ for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
+
+ if (method.getName().equals("getFoo")) {
+ getFooMethodCounter++;
+ getFooMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getBar")) {
+ getBarMethodCounter++;
+ getBarMethodReturnTypeName = method.getReturnType().getName();
+ }
+ }
+
+ assertEquals(1, getFooMethodCounter);
+ assertEquals("Short", getFooMethodReturnTypeName);
+
+ assertEquals(1, getBarMethodCounter);
+ assertEquals("String", getBarMethodReturnTypeName);
+ }
+
+ @Test
+ public void testLeafListResolving() throws URISyntaxException, IOException {
+ final File testFile = new File(getClass().getResource("/simple-leaf-list-demo.yang").toURI());
+ final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(3, genTypes.size());
+
+ GeneratedType simpleContainer = (GeneratedType) genTypes.get(1);
+ GeneratedType nestedContainer = (GeneratedType) genTypes.get(2);
+ for (Type t : genTypes) {
+ if ("SimpleContainer".equals(t.getName())) {
+ simpleContainer = (GeneratedType) t;
+ } else if ("NestedContainer".equals(t.getName())) {
+ nestedContainer = (GeneratedType) t;
+ }
+ }
+ assertNotNull(simpleContainer);
+ assertNotNull(nestedContainer);
+ assertEquals(3, simpleContainer.getMethodDefinitions().size());
+ assertEquals(2, nestedContainer.getMethodDefinitions().size());
+
+ int getFooMethodCounter = 0;
+ int getBarMethodCounter = 0;
+ int getNestedContainerCounter = 0;
+
+ String getFooMethodReturnTypeName = "";
+ String getBarMethodReturnTypeName = "";
+ String getNestedContainerReturnTypeName = "";
+ for (final MethodSignature method : simpleContainer.getMethodDefinitions()) {
+ if (method.getName().equals("getFoo")) {
+ getFooMethodCounter++;
+ getFooMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getBar")) {
+ getBarMethodCounter++;
+ getBarMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getNestedContainer")) {
+ getNestedContainerCounter++;
+ getNestedContainerReturnTypeName = method.getReturnType().getName();
+ }
+ }
+
+ assertEquals(1, getFooMethodCounter);
+ assertEquals("List", getFooMethodReturnTypeName);
+
+ assertEquals(1, getBarMethodCounter);
+ assertEquals("String", getBarMethodReturnTypeName);
+
+ assertEquals(1, getNestedContainerCounter);
+ assertEquals("NestedContainer", getNestedContainerReturnTypeName);
+
+ getFooMethodCounter = 0;
+ getBarMethodCounter = 0;
+
+ getFooMethodReturnTypeName = "";
+ getBarMethodReturnTypeName = "";
+
+ for (final MethodSignature method : nestedContainer.getMethodDefinitions()) {
+ if (method.getName().equals("getFoo")) {
+ getFooMethodCounter++;
+ getFooMethodReturnTypeName = method.getReturnType().getName();
+ }
+
+ if (method.getName().equals("getBar")) {
+ getBarMethodCounter++;
+ getBarMethodReturnTypeName = method.getReturnType().getName();
+ }
+ }
+
+ assertEquals(1, getFooMethodCounter);
+ assertEquals("Short", getFooMethodReturnTypeName);
+
+ assertEquals(1, getBarMethodCounter);
+ assertEquals("List", getBarMethodReturnTypeName);
+ }
+
+ @Test
+ public void testListResolving() throws URISyntaxException, IOException {
+ final File testFile = new File(getClass().getResource("/simple-list-demo.yang").toURI());
+ final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(5, genTypes.size());
+
+ int listParentContainerMethodsCount = 0;
+ int simpleListMethodsCount = 0;
+ int listChildContainerMethodsCount = 0;
+ int listKeyClassCount = 0;
+
+ int getSimpleListKeyMethodCount = 0;
+ int getListChildContainerMethodCount = 0;
+ int getFooMethodCount = 0;
+ int setFooMethodCount = 0;
+ int getSimpleLeafListMethodCount = 0;
+ int setSimpleLeafListMethodCount = 0;
+ int getBarMethodCount = 0;
+
+ String getSimpleListKeyMethodReturnTypeName = "";
+ String getListChildContainerMethodReturnTypeName = "";
+
+ int listKeyClassPropertyCount = 0;
+ String listKeyClassPropertyName = "";
+ String listKeyClassPropertyTypeName = "";
+ boolean listKeyClassPropertyReadOnly = false;
+
+ int hashMethodParameterCount = 0;
+ String hashMethodParameterName = "";
+ String hashMethodParameterReturnTypeName = "";
+
+ int equalMethodParameterCount = 0;
+ String equalMethodParameterName = "";
+ String equalMethodParameterReturnTypeName = "";
+
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
+ final GeneratedType genType = (GeneratedType) type;
+ if (genType.getName().equals("ListParentContainer")) {
+ listParentContainerMethodsCount = genType.getMethodDefinitions().size();
+ } else if (genType.getName().equals("SimpleList")) {
+ simpleListMethodsCount = genType.getMethodDefinitions().size();
+ final List<MethodSignature> methods = genType.getMethodDefinitions();
+ for (final MethodSignature method : methods) {
+ if (method.getName().equals("getKey")) {
+ getSimpleListKeyMethodCount++;
+ getSimpleListKeyMethodReturnTypeName = method.getReturnType().getName();
+ } else if (method.getName().equals("getListChildContainer")) {
+ getListChildContainerMethodCount++;
+ getListChildContainerMethodReturnTypeName = method.getReturnType().getName();
+ } else if (method.getName().equals("getFoo")) {
+ getFooMethodCount++;
+ } else if (method.getName().equals("setFoo")) {
+ setFooMethodCount++;
+ } else if (method.getName().equals("getSimpleLeafList")) {
+ getSimpleLeafListMethodCount++;
+ } else if (method.getName().equals("setSimpleLeafList")) {
+ setSimpleLeafListMethodCount++;
+ } else if (method.getName().equals("getBar")) {
+ getBarMethodCount++;
+ }
+ }
+ } else if (genType.getName().equals("ListChildContainer")) {
+ listChildContainerMethodsCount = genType.getMethodDefinitions().size();
+ }
+ } else if (type instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+ final List<GeneratedProperty> properties = genTO.getProperties();
+ final List<GeneratedProperty> hashProps = genTO.getHashCodeIdentifiers();
+ final List<GeneratedProperty> equalProps = genTO.getEqualsIdentifiers();
+
+ listKeyClassCount++;
+ listKeyClassPropertyCount = properties.size();
+ listKeyClassPropertyName = properties.get(0).getName();
+ listKeyClassPropertyTypeName = properties.get(0).getReturnType().getName();
+ listKeyClassPropertyReadOnly = properties.get(0).isReadOnly();
+
+ hashMethodParameterCount = hashProps.size();
+ hashMethodParameterName = hashProps.get(0).getName();
+ hashMethodParameterReturnTypeName = hashProps.get(0).getReturnType().getName();
+
+ equalMethodParameterCount = equalProps.size();
+ equalMethodParameterName = equalProps.get(0).getName();
+ equalMethodParameterReturnTypeName = equalProps.get(0).getReturnType().getName();
+
+ }
+ }
+
+ assertEquals(1, listParentContainerMethodsCount);
+ assertEquals(1, listChildContainerMethodsCount);
+ assertEquals(1, getSimpleListKeyMethodCount);
+ assertEquals(1, listKeyClassCount);
+
+ assertEquals(1, listKeyClassPropertyCount);
+ assertEquals("listKey", listKeyClassPropertyName);
+ assertEquals("Byte", listKeyClassPropertyTypeName);
+ assertEquals(true, listKeyClassPropertyReadOnly);
+ assertEquals(1, hashMethodParameterCount);
+ assertEquals("listKey", hashMethodParameterName);
+ assertEquals("Byte", hashMethodParameterReturnTypeName);
+ assertEquals(1, equalMethodParameterCount);
+ assertEquals("listKey", equalMethodParameterName);
+ assertEquals("Byte", equalMethodParameterReturnTypeName);
+
+ assertEquals("SimpleListKey", getSimpleListKeyMethodReturnTypeName);
+
+ assertEquals(1, getListChildContainerMethodCount);
+ assertEquals("ListChildContainer", getListChildContainerMethodReturnTypeName);
+ assertEquals(1, getFooMethodCount);
+ assertEquals(0, setFooMethodCount);
+ assertEquals(1, getSimpleLeafListMethodCount);
+ assertEquals(0, setSimpleLeafListMethodCount);
+ assertEquals(1, getBarMethodCount);
+
+ assertEquals(6, simpleListMethodsCount);
+ }
+
+ @Test
+ public void testListCompositeKeyResolving() throws URISyntaxException, IOException {
+ final File testFile = new File(getClass().getResource("/list-composite-key.yang").toURI());
+ final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(7, genTypes.size());
+
+ int genTypesCount = 0;
+ int genTOsCount = 0;
+
+ int compositeKeyListKeyPropertyCount = 0;
+ int compositeKeyListKeyCount = 0;
+ int innerListKeyPropertyCount = 0;
+
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
+ genTypesCount++;
+ } else if (type instanceof GeneratedTransferObject) {
+ final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+
+ if (genTO.getName().equals("CompositeKeyListKey")) {
+ compositeKeyListKeyCount++;
+ final List<GeneratedProperty> properties = genTO.getProperties();
+ for (final GeneratedProperty prop : properties) {
+ if (prop.getName().equals("key1")) {
+ compositeKeyListKeyPropertyCount++;
+ } else if (prop.getName().equals("key2")) {
+ compositeKeyListKeyPropertyCount++;
+ }
+ }
+ genTOsCount++;
+ } else if (genTO.getName().equals("InnerListKey")) {
+ final List<GeneratedProperty> properties = genTO.getProperties();
+ innerListKeyPropertyCount = properties.size();
+ genTOsCount++;
+ }
+ }
+ }
+ assertEquals(1, compositeKeyListKeyCount);
+ assertEquals(2, compositeKeyListKeyPropertyCount);
+
+ assertEquals(1, innerListKeyPropertyCount);
+
+ assertEquals(5, genTypesCount);
+ assertEquals(2, genTOsCount);
+ }
+
+ @Test
+ public void testGeneratedTypes() throws URISyntaxException, IOException {
+ final File testFile = new File(getClass().getResource("/demo-topology.yang").toURI());
+ final SchemaContext context = new YangParserImpl().parseFiles(Collections.singleton(testFile));
+ assertNotNull(context);
+
+ final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
+ final List<Type> genTypes = bindingGen.generateTypes(context);
+
+ assertNotNull(genTypes);
+ assertEquals(14, genTypes.size());
+
+ int genTypesCount = 0;
+ int genTOsCount = 0;
+ for (final Type type : genTypes) {
+ if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
+ genTypesCount++;
+ } else if (type instanceof GeneratedTransferObject) {
+ genTOsCount++;
+ }
+ }
+
+ assertEquals(11, genTypesCount);
+ assertEquals(3, genTOsCount);
+ }
+}