812b0f2e92fa77f8aa5f334ac35c65b6dbe75925
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / AugmentRelativeXPathTest.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.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class AugmentRelativeXPathTest extends AbstractTypesTest {
26
27     public AugmentRelativeXPathTest() {
28         super(AugmentRelativeXPathTest.class.getResource("/augment-relative-xpath-models"));
29     }
30
31     @Test
32     public void testAugmentationWithRelativeXPath() {
33
34         final SchemaContext context = YangParserTestUtils.parseYangFiles(testModels);
35
36         assertNotNull("context is null", context);
37         final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
38
39         assertNotNull("genTypes is null", genTypes);
40         assertFalse("genTypes is empty", genTypes.isEmpty());
41
42         GeneratedTransferObject gtInterfaceKey = null;
43         GeneratedType gtInterface = null;
44         GeneratedType gtTunnel = null;
45         GeneratedTransferObject gtTunnelKey = null;
46
47         for (final Type type : genTypes) {
48             if (!type.getPackageName().contains("augment._abstract.topology")) {
49                 continue;
50             }
51
52             if (type.getName().equals("InterfaceKey")) {
53                 gtInterfaceKey = (GeneratedTransferObject) type;
54             } else if (type.getName().equals("Interface")) {
55                 gtInterface = (GeneratedType) type;
56             } else if (type.getName().equals("Tunnel")) {
57                 gtTunnel = (GeneratedType) type;
58             } else if (type.getName().equals("TunnelKey")) {
59                 gtTunnelKey = (GeneratedTransferObject) type;
60             }
61         }
62
63         // 'Interface
64         assertNotNull("Interface is null", gtInterface);
65         final List<MethodSignature> gtInterfaceMethods = gtInterface.getMethodDefinitions();
66         assertNotNull("Interface methods are null", gtInterfaceMethods);
67         MethodSignature getIfcKeyMethod = null;
68         for (final MethodSignature method : gtInterfaceMethods) {
69             if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
70                 getIfcKeyMethod = method;
71                 break;
72             }
73         }
74         assertNotNull("getKey method is null", getIfcKeyMethod);
75         assertNotNull("getKey method return type is null", getIfcKeyMethod.getReturnType());
76         assertTrue("getKey method return type name must be InterfaceKey", getIfcKeyMethod.getReturnType().getName()
77                 .equals("InterfaceKey"));
78
79         // 'InterfaceKey'
80         assertNotNull("InterfaceKey is null", gtInterfaceKey);
81         final List<GeneratedProperty> properties = gtInterfaceKey.getProperties();
82         assertNotNull("InterfaceKey properties are null", properties);
83         GeneratedProperty gtInterfaceId = null;
84         for (final GeneratedProperty property : properties) {
85             if (property.getName().equals("interfaceId")) {
86                 gtInterfaceId = property;
87                 break;
88             }
89         }
90         assertNotNull("interfaceId is null", gtInterfaceId);
91         assertNotNull("interfaceId return type is null", gtInterfaceId.getReturnType());
92         assertTrue("interfaceId return type name must be String",
93                 gtInterfaceId.getReturnType().getName().equals("String"));
94
95         // 'Tunnel'
96         assertNotNull("Tunnel is null", gtTunnel);
97         final List<MethodSignature> tunnelMethods = gtTunnel.getMethodDefinitions();
98         assertNotNull("Tunnel methods are null", tunnelMethods);
99         MethodSignature getTunnelKeyMethod = null;
100         for (MethodSignature method : tunnelMethods) {
101             if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
102                 getTunnelKeyMethod = method;
103                 break;
104             }
105         }
106         assertNotNull("getKey method is null", getTunnelKeyMethod);
107         assertNotNull("getKey method return type", getTunnelKeyMethod.getReturnType());
108         assertTrue("getKey method return type name must be TunnelKey", getTunnelKeyMethod.getReturnType().getName()
109                 .equals("TunnelKey"));
110
111         // 'TunnelKey'
112         assertNotNull("TunnelKey is null", gtTunnelKey);
113         final List<GeneratedProperty> tunnelKeyProperties = gtTunnelKey.getProperties();
114         assertNotNull("TunnelKey properties are null", tunnelKeyProperties);
115
116         GeneratedProperty gtTunnelId = null;
117         for (final GeneratedProperty property : tunnelKeyProperties) {
118             if (property.getName().equals("tunnelId")) {
119                 gtTunnelId = property;
120             }
121         }
122         assertNotNull("tunnelId is null", gtTunnelId);
123         assertNotNull("tunnelId return type is null", gtTunnelId.getReturnType());
124         assertTrue("tunnelId return type name must be Uri", gtTunnelId.getReturnType().getName().equals("Uri"));
125     }
126
127 }