Added support for resolving augmentations.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / generator / impl / AugmentedTypeTest.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.controller.sal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
24 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
25 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
26 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
27 import org.opendaylight.controller.sal.binding.model.api.Type;
28 import org.opendaylight.controller.yang.model.api.Module;
29 import org.opendaylight.controller.yang.model.api.SchemaContext;
30 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
31 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
32
33 public class AugmentedTypeTest {
34
35     private final static List<File> augmentModels = new ArrayList<>();
36     private final static String augmentFolderPath = AugmentedTypeTest.class
37             .getResource("/augment-test-models").getPath();
38
39     @BeforeClass
40     public static void loadTestResources() {
41         final File augFolder = new File(augmentFolderPath);
42
43         for (final File fileEntry : augFolder.listFiles()) {
44             if (fileEntry.isFile()) {
45                 augmentModels.add(fileEntry);
46             }
47         }
48     }
49
50     @Test
51     public void augmentedAbstractTopologyTest() {
52         final YangModelParser parser = new YangParserImpl();
53         final Set<Module> modules = parser.parseYangModels(augmentModels);
54         final SchemaContext context = parser.resolveSchemaContext(modules);
55
56         assertNotNull(context);
57         final BindingGenerator bindingGen = new BindingGeneratorImpl();
58         final List<Type> genTypes = bindingGen.generateTypes(context);
59
60         assertNotNull(genTypes);
61         assertTrue(!genTypes.isEmpty());
62
63         int resolvedAugmentsCount = 0;
64         for (final Type type : genTypes) {
65             assertNotNull(type);
66             if (type.getName().equals("Topology")) {
67                 final GeneratedType absTopologyType = (GeneratedType) type;
68                 final List<MethodSignature> methods = absTopologyType
69                         .getMethodDefinitions();
70                 assertNotNull(methods);
71                 assertEquals(4, methods.size());
72             } else if (type.getName().equals("InterfaceKey")
73                     && type instanceof GeneratedTransferObject) {
74                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
75                 final List<GeneratedProperty> properties = genTO
76                         .getProperties();
77
78                 assertTrue(properties != null);
79                 for (final GeneratedProperty property : properties) {
80                     if (property.getName().equals("InterfaceId")) {
81                         assertTrue(property.getReturnType() != null);
82                         assertFalse(property.getReturnType().equals(
83                                 "java.lang.Void"));
84                         assertTrue(property.getReturnType().getName()
85                                 .equals("String"));
86                         resolvedAugmentsCount++;
87                     }
88                 }
89             } else if (type.getName().equals("Interface")
90                     && type instanceof GeneratedType) {
91                 final GeneratedType genType = (GeneratedType) type;
92                 final List<MethodSignature> methods = genType
93                         .getMethodDefinitions();
94
95                 assertTrue(methods != null);
96                 for (final MethodSignature method : methods) {
97                     if (method.getName().equals("getInterfaceKey")) {
98                         assertTrue(method.getReturnType() != null);
99                         assertFalse(method.getReturnType().equals(
100                                 "java.lang.Void"));
101                         assertTrue(method.getReturnType().getName()
102                                 .equals("InterfaceKey"));
103                         resolvedAugmentsCount++;
104                     } else if (method.getName().equals("getHigherLayerIf")) {
105                         assertTrue(method.getReturnType() != null);
106                         assertFalse(method.getReturnType().equals(
107                                 "java.lang.Void"));
108                         assertTrue(method.getReturnType().getName()
109                                 .equals("List"));
110                         resolvedAugmentsCount++;
111                     }
112                 }
113             } else if (type.getName().equals("Tunnel")
114                     && type instanceof GeneratedType) {
115                 final GeneratedType genType = (GeneratedType) type;
116                 final List<MethodSignature> methods = genType
117                         .getMethodDefinitions();
118                 assertTrue(methods != null);
119                 for (MethodSignature method : methods) {
120                     if (method.getName().equals("getTunnelKey")) {
121                         assertTrue(method.getReturnType() != null);
122                         assertFalse(method.getReturnType().equals(
123                                 "java.lang.Void"));
124                         assertTrue(method.getReturnType().getName()
125                                 .equals("TunnelKey"));
126                         resolvedAugmentsCount++;
127                     }
128                 }
129             } else if (type.getName().equals("TunnelKey")
130                     && type instanceof GeneratedTransferObject) {
131                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
132                 final List<GeneratedProperty> properties = genTO
133                         .getProperties();
134
135                 assertTrue(properties != null);
136                 for (final GeneratedProperty property : properties) {
137                     if (property.getName().equals("TunnelId")) {
138                         assertTrue(property.getReturnType() != null);
139                         assertFalse(property.getReturnType().equals(
140                                 "java.lang.Void"));
141                         assertTrue(property.getReturnType().getName()
142                                 .equals("Uri"));
143                         resolvedAugmentsCount++;
144                     }
145                 }
146             } else if (type.getName().equals("NetworkLink2")
147                     && type instanceof GeneratedType) {
148                 final GeneratedType genType = (GeneratedType) type;
149                 final List<MethodSignature> methods = genType
150                         .getMethodDefinitions();
151                 assertTrue(methods != null);
152                 for (MethodSignature method : methods) {
153                     if (method.getName().equals("getInterface")) {
154                         assertTrue(method.getReturnType() != null);
155                         assertFalse(method.getReturnType().equals(
156                                 "java.lang.Void"));
157                         assertTrue(method.getReturnType().getName()
158                                 .equals("String"));
159                         resolvedAugmentsCount++;
160                     }
161                 }
162             }
163         }
164         assertEquals(6, resolvedAugmentsCount);
165     }
166
167     @Test
168     public void augmentedNetworkLinkTest() {
169
170     }
171
172     @Test
173     public void augmentedTopologyTunnelsTest() {
174
175     }
176 }