Rework inlined union generation
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedTypesLeafrefTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNotSame;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class GeneratedTypesLeafrefTest {
28
29     @Test
30     public void testLeafrefResolving() {
31         final SchemaContext context = YangParserTestUtils.parseYangResources(GeneratedTypesLeafrefTest.class,
32             "/leafref-test-models/abstract-topology@2013-02-08.yang", "/ietf/ietf-interfaces.yang",
33             "/ietf/ietf-inet-types.yang", "/ietf/ietf-yang-types.yang");
34         assertNotNull(context);
35         assertEquals(4, context.getModules().size());
36
37         final BindingGenerator bindingGen = new BindingGeneratorImpl();
38         final List<Type> genTypes = bindingGen.generateTypes(context);
39
40         assertEquals(54, genTypes.size());
41         assertNotNull(genTypes);
42
43         GeneratedTransferObject gtIfcKey = null;
44         GeneratedType gtIfc = null;
45         GeneratedType gtNetworkLink = null;
46         GeneratedType gtSource = null;
47         GeneratedType gtDest = null;
48         GeneratedType gtTunnel = null;
49         GeneratedTransferObject gtTunnelKey = null;
50         GeneratedType gtTopology = null;
51         for (final Type type : genTypes) {
52             String name = type.getName();
53             if ("InterfaceKey".equals(name)
54                     && "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces".equals(
55                         type.getPackageName())) {
56                 gtIfcKey = (GeneratedTransferObject) type;
57             } else if ("Interface".equals(name)) {
58                 gtIfc = (GeneratedType) type;
59             } else if ("NetworkLink".equals(name)) {
60                 gtNetworkLink = (GeneratedType) type;
61             } else if ("SourceNode".equals(name)) {
62                 gtSource = (GeneratedType) type;
63             } else if ("DestinationNode".equals(name)) {
64                 gtDest = (GeneratedType) type;
65             } else if ("Tunnel".equals(name)) {
66                 gtTunnel = (GeneratedType) type;
67             } else if ("TunnelKey".equals(name)) {
68                 gtTunnelKey = (GeneratedTransferObject) type;
69             } else if ("Topology".equals(name)) {
70                 gtTopology = (GeneratedType) type;
71             }
72         }
73
74         assertNotNull(gtIfcKey);
75         assertNotNull(gtIfc);
76         assertNotNull(gtNetworkLink);
77         assertNotNull(gtSource);
78         assertNotNull(gtDest);
79         assertNotNull(gtTunnel);
80         assertNotNull(gtTunnelKey);
81         assertNotNull(gtTopology);
82
83         // Topology
84         final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
85         assertNotNull(gtTopoMethods);
86         MethodSignature condLeafref = null;
87         for (final MethodSignature method : gtTopoMethods) {
88             if (method.getName().equals("getCondLeafref")) {
89                 condLeafref = method;
90             }
91         }
92         assertNotNull(condLeafref);
93         Type condLeafRT = condLeafref.getReturnType();
94         assertNotNull(condLeafRT);
95         assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
96
97         // InterfaceId
98         final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
99         assertNotNull(gtIfcKeyProps);
100         GeneratedProperty ifcIdProp = null;
101         for (final GeneratedProperty property : gtIfcKeyProps) {
102             if (property.getName().equals("interfaceId")) {
103                 ifcIdProp = property;
104             }
105         }
106         assertNotNull(ifcIdProp);
107         Type ifcIdPropType = ifcIdProp.getReturnType();
108         assertNotNull(ifcIdPropType);
109         assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
110
111         // Interface
112         final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
113         assertNotNull(gtIfcMethods);
114         MethodSignature getIfcKey = null;
115         MethodSignature getHigherLayerIf = null;
116         for (final MethodSignature method : gtIfcMethods) {
117             if (method.getName().equals("getKey")) {
118                 getIfcKey = method;
119             } else if (method.getName().equals("getHigherLayerIf")) {
120                 getHigherLayerIf = method;
121             }
122         }
123         assertNotNull(getIfcKey);
124         Type getIfcKeyType = getIfcKey.getReturnType();
125         assertNotNull(getIfcKeyType);
126         assertNotSame("java.lang.Void", getIfcKeyType);
127         assertEquals("InterfaceKey", getIfcKeyType.getName());
128
129         assertNotNull(getHigherLayerIf);
130         Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
131         assertNotNull(getHigherLayerIfType);
132         assertNotSame("java.lang.Void", getHigherLayerIfType);
133         assertEquals("List", getHigherLayerIfType.getName());
134
135         // NetworkLink
136         final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
137         assertNotNull(gtNetworkLinkMethods);
138         MethodSignature getIfc = null;
139         for (MethodSignature method : gtNetworkLinkMethods) {
140             if (method.getName().equals("getInterface")) {
141                 getIfc = method;
142             }
143         }
144         assertNotNull(getIfc);
145         Type getIfcType = getIfc.getReturnType();
146         assertNotNull(getIfcType);
147         assertNotSame("java.lang.Void", getIfcType);
148         assertEquals("String", getIfcType.getName());
149
150         // SourceNode
151         final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
152         assertNotNull(gtSourceMethods);
153         MethodSignature getIdSource = null;
154         for (MethodSignature method : gtSourceMethods) {
155             if (method.getName().equals("getId")) {
156                 getIdSource = method;
157             }
158         }
159         assertNotNull(getIdSource);
160         Type getIdType = getIdSource.getReturnType();
161         assertNotNull(getIdType);
162         assertNotSame("java.lang.Void", getIdType);
163         assertEquals("Uri", getIdType.getName());
164
165         // DestinationNode
166         final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
167         assertNotNull(gtDestMethods);
168         MethodSignature getIdDest = null;
169         for (MethodSignature method : gtDestMethods) {
170             if (method.getName().equals("getId")) {
171                 getIdDest = method;
172             }
173         }
174         assertNotNull(getIdDest);
175         Type getIdDestType = getIdDest.getReturnType();
176         assertNotNull(getIdDestType);
177         assertNotSame("java.lang.Void", getIdDestType);
178         assertEquals("Uri", getIdDestType.getName());
179
180         // Tunnel
181         final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
182         assertNotNull(gtTunnelMethods);
183         MethodSignature getTunnelKey = null;
184         for (MethodSignature method : gtTunnelMethods) {
185             if (method.getName().equals("getKey")) {
186                 getTunnelKey = method;
187             }
188         }
189         assertNotNull(getTunnelKey);
190         Type getTunnelKeyType = getTunnelKey.getReturnType();
191         assertNotNull(getTunnelKeyType);
192         assertNotSame("java.lang.Void", getTunnelKeyType);
193         assertEquals("TunnelKey", getTunnelKeyType.getName());
194
195         // TunnelKey
196         final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
197         assertNotNull(gtTunnelKeyProps);
198         GeneratedProperty tunnelId = null;
199         for (final GeneratedProperty property : gtTunnelKeyProps) {
200             if (property.getName().equals("tunnelId")) {
201                 tunnelId = property;
202             }
203         }
204         assertNotNull(tunnelId);
205         Type tunnelIdType = tunnelId.getReturnType();
206         assertNotNull(tunnelIdType);
207         assertNotSame("java.lang.Void", tunnelIdType);
208         assertEquals("Uri", tunnelIdType.getName());
209     }
210
211     @Test
212     public void testLeafrefInvalidPathResolving() {
213         final SchemaContext context =  YangParserTestUtils.parseYangResource("/leafref-test-invalid-model/foo.yang");
214         assertNotNull(context);
215         assertEquals(1, context.getModules().size());
216
217         final BindingGenerator bindingGen = new BindingGeneratorImpl();
218         try {
219             bindingGen.generateTypes(context);
220             fail("Expected IllegalArgumentException caused by invalid leafref path");
221         } catch (IllegalArgumentException e) {
222             String expected = "Failed to find leafref target";
223             assertTrue(e.getMessage().contains(expected));
224         }
225     }
226 }