Adjust to yangtools-2.0.0 changes
[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(true);
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                 gtIfcKey = (GeneratedTransferObject) type;
55             } else if ("Interface".equals(name)) {
56                 gtIfc = (GeneratedType) type;
57             } else if ("NetworkLink".equals(name)) {
58                 gtNetworkLink = (GeneratedType) type;
59             } else if ("SourceNode".equals(name)) {
60                 gtSource = (GeneratedType) type;
61             } else if ("DestinationNode".equals(name)) {
62                 gtDest = (GeneratedType) type;
63             } else if ("Tunnel".equals(name)) {
64                 gtTunnel = (GeneratedType) type;
65             } else if ("TunnelKey".equals(name)) {
66                 gtTunnelKey = (GeneratedTransferObject) type;
67             } else if ("Topology".equals(name)) {
68                 gtTopology = (GeneratedType) type;
69             }
70         }
71
72         assertNotNull(gtIfcKey);
73         assertNotNull(gtIfc);
74         assertNotNull(gtNetworkLink);
75         assertNotNull(gtSource);
76         assertNotNull(gtDest);
77         assertNotNull(gtTunnel);
78         assertNotNull(gtTunnelKey);
79         assertNotNull(gtTopology);
80
81         // Topology
82         final List<MethodSignature> gtTopoMethods = gtTopology.getMethodDefinitions();
83         assertNotNull(gtTopoMethods);
84         MethodSignature condLeafref = null;
85         for (final MethodSignature method : gtTopoMethods) {
86             if (method.getName().equals("getCondLeafref")) {
87                 condLeafref = method;
88             }
89         }
90         assertNotNull(condLeafref);
91         Type condLeafRT = condLeafref.getReturnType();
92         assertNotNull(condLeafRT);
93         assertEquals("java.lang.Object", condLeafRT.getFullyQualifiedName());
94
95         // InterfaceId
96         final List<GeneratedProperty> gtIfcKeyProps = gtIfcKey.getProperties();
97         assertNotNull(gtIfcKeyProps);
98         GeneratedProperty ifcIdProp = null;
99         for (final GeneratedProperty property : gtIfcKeyProps) {
100             if (property.getName().equals("interfaceId")) {
101                 ifcIdProp = property;
102             }
103         }
104         assertNotNull(ifcIdProp);
105         Type ifcIdPropType = ifcIdProp.getReturnType();
106         assertNotNull(ifcIdPropType);
107         assertEquals("java.lang.String", ifcIdPropType.getFullyQualifiedName());
108
109         // Interface
110         final List<MethodSignature> gtIfcMethods = gtIfc.getMethodDefinitions();
111         assertNotNull(gtIfcMethods);
112         MethodSignature getIfcKey = null;
113         MethodSignature getHigherLayerIf = null;
114         for (final MethodSignature method : gtIfcMethods) {
115             if (method.getName().equals("getKey")) {
116                 getIfcKey = method;
117             } else if (method.getName().equals("getHigherLayerIf")) {
118                 getHigherLayerIf = method;
119             }
120         }
121         assertNotNull(getIfcKey);
122         Type getIfcKeyType = getIfcKey.getReturnType();
123         assertNotNull(getIfcKeyType);
124         assertNotSame("java.lang.Void", getIfcKeyType);
125         assertEquals("InterfaceKey", getIfcKeyType.getName());
126
127         assertNotNull(getHigherLayerIf);
128         Type getHigherLayerIfType = getHigherLayerIf.getReturnType();
129         assertNotNull(getHigherLayerIfType);
130         assertNotSame("java.lang.Void", getHigherLayerIfType);
131         assertEquals("List", getHigherLayerIfType.getName());
132
133         // NetworkLink
134         final List<MethodSignature> gtNetworkLinkMethods = gtNetworkLink.getMethodDefinitions();
135         assertNotNull(gtNetworkLinkMethods);
136         MethodSignature getIfc = null;
137         for (MethodSignature method : gtNetworkLinkMethods) {
138             if (method.getName().equals("getInterface")) {
139                 getIfc = method;
140             }
141         }
142         assertNotNull(getIfc);
143         Type getIfcType = getIfc.getReturnType();
144         assertNotNull(getIfcType);
145         assertNotSame("java.lang.Void", getIfcType);
146         assertEquals("String", getIfcType.getName());
147
148         // SourceNode
149         final List<MethodSignature> gtSourceMethods = gtSource.getMethodDefinitions();
150         assertNotNull(gtSourceMethods);
151         MethodSignature getIdSource = null;
152         for (MethodSignature method : gtSourceMethods) {
153             if (method.getName().equals("getId")) {
154                 getIdSource = method;
155             }
156         }
157         assertNotNull(getIdSource);
158         Type getIdType = getIdSource.getReturnType();
159         assertNotNull(getIdType);
160         assertNotSame("java.lang.Void", getIdType);
161         assertEquals("Uri", getIdType.getName());
162
163         // DestinationNode
164         final List<MethodSignature> gtDestMethods = gtDest.getMethodDefinitions();
165         assertNotNull(gtDestMethods);
166         MethodSignature getIdDest = null;
167         for (MethodSignature method : gtDestMethods) {
168             if (method.getName().equals("getId")) {
169                 getIdDest = method;
170             }
171         }
172         assertNotNull(getIdDest);
173         Type getIdDestType = getIdDest.getReturnType();
174         assertNotNull(getIdDestType);
175         assertNotSame("java.lang.Void", getIdDestType);
176         assertEquals("Uri", getIdDestType.getName());
177
178         // Tunnel
179         final List<MethodSignature> gtTunnelMethods = gtTunnel.getMethodDefinitions();
180         assertNotNull(gtTunnelMethods);
181         MethodSignature getTunnelKey = null;
182         for (MethodSignature method : gtTunnelMethods) {
183             if (method.getName().equals("getKey")) {
184                 getTunnelKey = method;
185             }
186         }
187         assertNotNull(getTunnelKey);
188         Type getTunnelKeyType = getTunnelKey.getReturnType();
189         assertNotNull(getTunnelKeyType);
190         assertNotSame("java.lang.Void", getTunnelKeyType);
191         assertEquals("TunnelKey", getTunnelKeyType.getName());
192
193         // TunnelKey
194         final List<GeneratedProperty> gtTunnelKeyProps = gtTunnelKey.getProperties();
195         assertNotNull(gtTunnelKeyProps);
196         GeneratedProperty tunnelId = null;
197         for (final GeneratedProperty property : gtTunnelKeyProps) {
198             if (property.getName().equals("tunnelId")) {
199                 tunnelId = property;
200             }
201         }
202         assertNotNull(tunnelId);
203         Type tunnelIdType = tunnelId.getReturnType();
204         assertNotNull(tunnelIdType);
205         assertNotSame("java.lang.Void", tunnelIdType);
206         assertEquals("Uri", tunnelIdType.getName());
207     }
208
209     @Test
210     public void testLeafrefInvalidPathResolving() {
211         final SchemaContext context =  YangParserTestUtils.parseYangResource("/leafref-test-invalid-model/foo.yang");
212         assertNotNull(context);
213         assertEquals(1, context.getModules().size());
214
215         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
216         try {
217             bindingGen.generateTypes(context);
218             fail("Expected IllegalArgumentException caused by invalid leafref path");
219         } catch (IllegalArgumentException e) {
220             String expected = "Failed to find leafref target";
221             assertTrue(e.getMessage().contains(expected));
222         }
223     }
224 }