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