d5a118c44ade8d6d6002886ba608527843919d8d
[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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
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         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(
34             YangParserTestUtils.parseYangFiles(testModels));
35         assertNotNull("genTypes is null", genTypes);
36         assertEquals(26, genTypes.size());
37
38         GeneratedTransferObject gtInterfaceKey = null;
39         GeneratedType gtInterface = null;
40         GeneratedType gtTunnel = null;
41         GeneratedTransferObject gtTunnelKey = null;
42
43         for (final GeneratedType type : genTypes) {
44             if (!type.getPackageName().contains("augment._abstract.topology")) {
45                 continue;
46             }
47
48             if (type.getName().equals("InterfaceKey")) {
49                 gtInterfaceKey = (GeneratedTransferObject) type;
50
51                 final List<GeneratedProperty> properties = gtInterfaceKey.getProperties();
52                 assertNotNull("InterfaceKey properties are null", properties);
53                 assertEquals(1, properties.size());
54
55                 final GeneratedProperty property = properties.get(0);
56                 assertEquals("interfaceId", property.getName());
57                 assertNotNull("interfaceId return type is null", property.getReturnType());
58                 assertEquals(JavaTypeName.create(String.class), property.getReturnType().getIdentifier());
59             } else if (type.getName().equals("Interface")) {
60                 gtInterface = type;
61
62                 final List<MethodSignature> gtInterfaceMethods = gtInterface.getMethodDefinitions();
63                 assertNotNull("Interface methods are null", gtInterfaceMethods);
64                 assertEquals(7, gtInterfaceMethods.size());
65
66                 MethodSignature getIfcKeyMethod = null;
67                 for (final MethodSignature method : gtInterfaceMethods) {
68                     if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
69                         getIfcKeyMethod = method;
70                         break;
71                     }
72                 }
73                 assertNotNull("getKey method is null", getIfcKeyMethod);
74                 assertNotNull("getKey method return type is null", getIfcKeyMethod.getReturnType());
75                 assertEquals(JavaTypeName.create(
76                     "org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503.topology.interfaces",
77                     "InterfaceKey"),
78                     getIfcKeyMethod.getReturnType().getIdentifier());
79             } else if (type.getName().equals("Tunnel")) {
80                 gtTunnel = type;
81
82                 final List<MethodSignature> tunnelMethods = gtTunnel.getMethodDefinitions();
83                 assertNotNull("Tunnel methods are null", tunnelMethods);
84                 assertEquals(6, tunnelMethods.size());
85
86                 MethodSignature getTunnelKeyMethod = null;
87                 for (MethodSignature method : tunnelMethods) {
88                     if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
89                         getTunnelKeyMethod = method;
90                         break;
91                     }
92                 }
93                 assertNotNull("getKey method is null", getTunnelKeyMethod);
94                 assertNotNull("getKey method return type", getTunnelKeyMethod.getReturnType());
95                 assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology"
96                     + ".rev130503.topology.network.links.network.link.tunnels", "TunnelKey"),
97                     getTunnelKeyMethod.getReturnType().getIdentifier());
98             } else if (type.getName().equals("TunnelKey")) {
99                 assertThat(type, instanceOf(GeneratedTransferObject.class));
100
101                 gtTunnelKey = (GeneratedTransferObject) type;
102
103                 final List<GeneratedProperty> properties = gtTunnelKey.getProperties();
104                 assertNotNull("TunnelKey properties are null", properties);
105                 assertEquals(1, properties.size());
106
107                 final GeneratedProperty property = properties.get(0);
108                 assertEquals("tunnelId", property.getName());
109                 assertNotNull("tunnelId return type is null", property.getReturnType());
110                 assertEquals(
111                     JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208", "Uri"),
112                     property.getReturnType().getIdentifier());
113             }
114         }
115
116         assertNotNull("Interface is null", gtInterface);
117         assertNotNull("InterfaceKey is null", gtInterfaceKey);
118         assertNotNull("Tunnel is null", gtTunnel);
119         assertNotNull("TunnelKey is null", gtTunnelKey);
120     }
121
122 }