9c2c03e7df717ce930cddb255495a3a4efe45a7e
[mdsal.git] / binding / mdsal-binding-generator / 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 {
26     @Test
27     public void testAugmentationWithRelativeXPath() {
28         final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(
29             YangParserTestUtils.parseYangResourceDirectory("/augment-relative-xpath-models"));
30         assertNotNull("genTypes is null", genTypes);
31         assertEquals(26, genTypes.size());
32
33         GeneratedTransferObject gtInterfaceKey = null;
34         GeneratedType gtInterface = null;
35         GeneratedType gtTunnel = null;
36         GeneratedTransferObject gtTunnelKey = null;
37
38         for (final GeneratedType type : genTypes) {
39             if (!type.getPackageName().contains("augment._abstract.topology")) {
40                 continue;
41             }
42
43             if (type.getName().equals("InterfaceKey")) {
44                 gtInterfaceKey = (GeneratedTransferObject) type;
45
46                 final List<GeneratedProperty> properties = gtInterfaceKey.getProperties();
47                 assertNotNull("InterfaceKey properties are null", properties);
48                 assertEquals(1, properties.size());
49
50                 final GeneratedProperty property = properties.get(0);
51                 assertEquals("interfaceId", property.getName());
52                 assertNotNull("interfaceId return type is null", property.getReturnType());
53                 assertEquals(JavaTypeName.create(String.class), property.getReturnType().getIdentifier());
54             } else if (type.getName().equals("Interface")) {
55                 gtInterface = type;
56
57                 final List<MethodSignature> gtInterfaceMethods = gtInterface.getMethodDefinitions();
58                 assertNotNull("Interface methods are null", gtInterfaceMethods);
59                 assertEquals(9, gtInterfaceMethods.size());
60
61                 MethodSignature getIfcKeyMethod = null;
62                 for (final MethodSignature method : gtInterfaceMethods) {
63                     if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
64                         getIfcKeyMethod = method;
65                         break;
66                     }
67                 }
68                 assertNotNull("getKey method is null", getIfcKeyMethod);
69                 assertNotNull("getKey method return type is null", getIfcKeyMethod.getReturnType());
70                 assertEquals(JavaTypeName.create(
71                     "org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology.rev130503.topology.interfaces",
72                     "InterfaceKey"),
73                     getIfcKeyMethod.getReturnType().getIdentifier());
74             } else if (type.getName().equals("Tunnel")) {
75                 gtTunnel = type;
76
77                 final List<MethodSignature> tunnelMethods = gtTunnel.getMethodDefinitions();
78                 assertNotNull("Tunnel methods are null", tunnelMethods);
79                 assertEquals(7, tunnelMethods.size());
80
81                 MethodSignature getTunnelKeyMethod = null;
82                 for (MethodSignature method : tunnelMethods) {
83                     if (BindingMapping.IDENTIFIABLE_KEY_NAME.equals(method.getName())) {
84                         getTunnelKeyMethod = method;
85                         break;
86                     }
87                 }
88                 assertNotNull("getKey method is null", getTunnelKeyMethod);
89                 assertNotNull("getKey method return type", getTunnelKeyMethod.getReturnType());
90                 assertEquals(JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model.augment._abstract.topology"
91                     + ".rev130503.topology.network.links.network.link.tunnels", "TunnelKey"),
92                     getTunnelKeyMethod.getReturnType().getIdentifier());
93             } else if (type.getName().equals("TunnelKey")) {
94                 assertThat(type, instanceOf(GeneratedTransferObject.class));
95
96                 gtTunnelKey = (GeneratedTransferObject) type;
97
98                 final List<GeneratedProperty> properties = gtTunnelKey.getProperties();
99                 assertNotNull("TunnelKey properties are null", properties);
100                 assertEquals(1, properties.size());
101
102                 final GeneratedProperty property = properties.get(0);
103                 assertEquals("tunnelId", property.getName());
104                 assertNotNull("tunnelId return type is null", property.getReturnType());
105                 assertEquals(
106                     JavaTypeName.create("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208", "Uri"),
107                     property.getReturnType().getIdentifier());
108             }
109         }
110
111         assertNotNull("Interface is null", gtInterface);
112         assertNotNull("InterfaceKey is null", gtInterfaceKey);
113         assertNotNull("Tunnel is null", gtTunnel);
114         assertNotNull("TunnelKey is null", gtTunnelKey);
115     }
116 }