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