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