b50a57799c8852ed2b2a7979ae9d60203ccc7ce6
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / AugmentedTypeTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
22 import org.opendaylight.mdsal.binding.model.ri.Types;
23 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class AugmentedTypeTest {
28     @Test
29     public void augmentedAbstractTopologyTest() {
30         final EffectiveModelContext context = YangParserTestUtils.parseYangResources(AugmentedTypeTest.class,
31             "/augment-test-models/abstract-topology@2013-02-08.yang",
32             "/augment-test-models/augment-abstract-topology@2013-05-03.yang",
33             "/augment-test-models/augment-network-link-attributes@2013-05-03.yang",
34             "/augment-test-models/augment-topology-tunnels@2013-05-03.yang",
35             "/augment-test-models/ietf-interfaces@2012-11-15.yang");
36         assertNotNull("Schema Context is null", context);
37
38         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(context);
39         assertEquals(28, genTypes.size());
40
41         GeneratedTransferObject gtInterfaceKey = null;
42         GeneratedType gtInterface = null;
43         GeneratedType gtTunnel = null;
44         GeneratedTransferObject gtTunnelKey = null;
45         GeneratedType gtNetworkLink2 = null;
46
47         for (final GeneratedType type : genTypes) {
48             if (!type.getPackageName().contains("augment._abstract.topology")) {
49                 continue;
50             }
51
52             if (type.getName().equals("InterfaceKey")) {
53                 gtInterfaceKey = (GeneratedTransferObject) type;
54             } else if (type.getName().equals("Interface")) {
55                 gtInterface = type;
56             } else if (type.getName().equals("Tunnel")) {
57                 gtTunnel = type;
58             } else if (type.getName().equals("TunnelKey")) {
59                 gtTunnelKey = (GeneratedTransferObject) type;
60             } else if (type.getName().equals("NetworkLink2")) {
61                 gtNetworkLink2 = type;
62             }
63         }
64
65         // 'Interface
66         assertNotNull("gtInterface is null", gtInterface);
67         final List<MethodSignature> gtInterfaceMethods = gtInterface.getMethodDefinitions();
68         assertNotNull("gtInterfaceMethods is null", gtInterfaceMethods);
69         MethodSignature getIfcKeyMethod = null;
70         for (final MethodSignature method : gtInterfaceMethods) {
71             if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
72                 getIfcKeyMethod = method;
73                 break;
74             }
75         }
76         assertNotNull("getIfcKeyMethod is null", getIfcKeyMethod);
77         assertThat(getIfcKeyMethod.getReturnType(), instanceOf(GeneratedTransferObject.class));
78         assertEquals(JavaTypeName.create(
79             "org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503.topology.interfaces",
80             "InterfaceKey"), getIfcKeyMethod.getReturnType().getIdentifier());
81
82         MethodSignature getHigherLayerIfMethod = null;
83         for (final MethodSignature method : gtInterfaceMethods) {
84             if (method.getName().equals("getHigherLayerIf")) {
85                 getHigherLayerIfMethod = method;
86                 break;
87             }
88         }
89         assertNotNull("getHigherLayerIf method is null", getHigherLayerIfMethod);
90         assertEquals(Types.setTypeFor(Types.STRING), getHigherLayerIfMethod.getReturnType());
91
92         // 'InterfaceKey'
93         assertNotNull("InterfaceKey is null", gtInterfaceKey);
94         final List<GeneratedProperty> properties = gtInterfaceKey.getProperties();
95         assertNotNull("properties is null", properties);
96         GeneratedProperty gtInterfaceId = null;
97         for (final GeneratedProperty property : properties) {
98             if (property.getName().equals("interfaceId")) {
99                 gtInterfaceId = property;
100                 break;
101             }
102         }
103         assertNotNull("interfaceId is null", gtInterfaceId);
104         assertEquals(Types.STRING, gtInterfaceId.getReturnType());
105
106         // 'Tunnel'
107         assertNotNull("Tunnel is null", gtTunnel);
108         final List<MethodSignature> tunnelMethods = gtTunnel.getMethodDefinitions();
109         assertNotNull("Tunnel methods are null", tunnelMethods);
110         MethodSignature getTunnelKeyMethod = null;
111         for (MethodSignature method : tunnelMethods) {
112             if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
113                 getTunnelKeyMethod = method;
114                 break;
115             }
116         }
117         assertNotNull("getKey method of Tunnel is null", getTunnelKeyMethod);
118
119         var retType = getTunnelKeyMethod.getReturnType();
120         assertThat(retType, instanceOf(GeneratedTransferObject.class));
121         assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503"
122             + ".topology.network.links.network.link.tunnels", "TunnelKey"), retType.getIdentifier());
123
124         // 'TunnelKey'
125         assertNotNull("TunnelKey is null", gtTunnelKey);
126         final List<GeneratedProperty> tunnelKeyProperties = gtTunnelKey.getProperties();
127         assertNotNull("TunnelKey properties are null", tunnelKeyProperties);
128
129         GeneratedProperty gtTunnelId = null;
130         for (final GeneratedProperty property : tunnelKeyProperties) {
131             if (property.getName().equals("tunnelId")) {
132                 gtTunnelId = property;
133             }
134         }
135         assertNotNull("tunnelId is null", gtTunnelId);
136         assertEquals(Types.typeForClass(Integer.class), gtTunnelId.getReturnType());
137
138         // 'NetworkLink2'
139         assertNotNull("NetworkLink2 is null", gtNetworkLink2);
140
141         final List<MethodSignature> networkLink2Methods = gtNetworkLink2.getMethodDefinitions();
142         assertNotNull("NetworkLink2 methods are null", networkLink2Methods);
143
144         MethodSignature getIfcMethod = null;
145         for (MethodSignature method : networkLink2Methods) {
146             if (method.getName().equals("getInterface")) {
147                 getIfcMethod = method;
148                 break;
149             }
150         }
151
152         assertNotNull("getInterface method is null", getIfcMethod);
153         assertEquals(Types.STRING, getIfcMethod.getReturnType());
154     }
155 }