Merge "OF plugin classes must have a strict dependency on Connection Service"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / test / java / org / opendaylight / controller / binding / generator / util / generated / type / builder / AnnotationBuilderTest.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.binding.generator.util.generated.type.builder;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Test;
19 import org.opendaylight.controller.binding.generator.util.Types;
20 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
25 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
26 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
27 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
28
29 public class AnnotationBuilderTest {
30
31     @Test
32     public void generatedTypeAnnotationTest() {
33         final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
34                 "org.opendaylight.controller", "AnnotInterface");
35
36         genTypeBuilder.addAnnotation("javax.management", "MXBean");
37         final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
38                 "javax.management", "Description");
39         annotDesc.addParameter("description", "some sort of interface");
40
41         final GeneratedType genType = genTypeBuilder.toInstance();
42
43         assertNotNull(genType);
44         assertNotNull(genType.getAnnotations());
45         assertEquals(2, genType.getAnnotations().size());
46
47         int annotCount = 0;
48         for (final AnnotationType annotation : genType.getAnnotations()) {
49             if (annotation.getPackageName().equals("javax.management")
50                     && annotation.getName().equals("MXBean")) {
51                 annotCount++;
52                 assertEquals(0, annotation.getParameters().size());
53             }
54             if (annotation.getPackageName().equals("javax.management")
55                     && annotation.getName().equals("Description")) {
56                 annotCount++;
57                 assertEquals(1, annotation.getParameters().size());
58                 AnnotationType.Parameter param = annotation
59                         .getParameter("description");
60                 assertNotNull(param);
61                 assertEquals("description", param.getName());
62                 assertNotNull(param.getValue());
63                 assertEquals("some sort of interface", param.getValue());
64                 assertNotNull(param.getValues());
65                 assertTrue(param.getValues().isEmpty());
66             }
67         }
68         assertEquals(2, annotCount);
69     }
70
71     @Test
72     public void methodSignatureAnnotationTest() {
73         final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
74                 "org.opendaylight.controller", "TransferObject");
75
76         final MethodSignatureBuilder methodBuilder = genTypeBuilder
77                 .addMethod("simpleMethod");
78         methodBuilder.setReturnType(Types.typeForClass(Integer.class));
79         final AnnotationTypeBuilder annotManAttr = methodBuilder
80                 .addAnnotation("org.springframework.jmx.export.annotation",
81                         "ManagedAttribute");
82
83         annotManAttr.addParameter("description", "\"The Name Attribute\"");
84         annotManAttr.addParameter("currencyTimeLimit", "20");
85         annotManAttr.addParameter("defaultValue", "\"bar\"");
86         annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
87
88         final AnnotationTypeBuilder annotManProp = methodBuilder
89                 .addAnnotation("org.springframework.jmx.export.annotation",
90                         "ManagedOperation");
91
92         final List<String> typeValues = new ArrayList<String>();
93         typeValues.add("\"val1\"");
94         typeValues.add("\"val2\"");
95         typeValues.add("\"val3\"");
96         annotManProp.addParameters("types", typeValues);
97
98         final GeneratedType genType = genTypeBuilder.toInstance();
99
100         assertNotNull(genType);
101         assertNotNull(genType.getAnnotations());
102         assertNotNull(genType.getMethodDefinitions());
103         assertNotNull(genType.getMethodDefinitions().get(0));
104         assertNotNull(genType.getMethodDefinitions().get(0).getAnnotations());
105         final List<AnnotationType> annotations = genType.getMethodDefinitions()
106                 .get(0).getAnnotations();
107         assertEquals(2, annotations.size());
108         
109         int annotCount = 0;
110         for (final AnnotationType annotation : annotations) {
111             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
112                     && annotation.getName().equals("ManagedAttribute")) {
113                 annotCount++;
114                 assertEquals(4, annotation.getParameters().size());
115                 
116                 assertNotNull(annotation.getParameter("description"));
117                 assertNotNull(annotation.getParameter("currencyTimeLimit"));
118                 assertNotNull(annotation.getParameter("defaultValue"));
119                 assertNotNull(annotation.getParameter("persistPolicy"));
120                 assertEquals("\"The Name Attribute\"", annotation.getParameter("description").getValue());
121                 assertEquals("20", annotation.getParameter("currencyTimeLimit").getValue());
122                 assertEquals("\"bar\"", annotation.getParameter("defaultValue").getValue());
123                 assertEquals("\"OnUpdate\"", annotation.getParameter("persistPolicy").getValue());
124             }
125             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
126                     && annotation.getName().equals("ManagedOperation")) {
127                 annotCount++;
128                 
129                 assertEquals(1, annotation.getParameters().size());
130                 assertNotNull(annotation.getParameter("types"));
131                 assertEquals(3, annotation.getParameter("types").getValues().size());
132             }
133         }
134         assertEquals(2, annotCount);
135     }
136
137     @Test
138     public void generatedPropertyAnnotationTest() {
139         final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(
140                 "org.opendaylight.controller", "AnnotInterface");
141
142         final GeneratedPropertyBuilder propertyBuilder = genTOBuilder
143                 .addProperty("simpleProperty");
144         propertyBuilder.setReturnType(Types.typeForClass(Integer.class));
145         final AnnotationTypeBuilder annotManAttr = propertyBuilder
146                 .addAnnotation("org.springframework.jmx.export.annotation",
147                         "ManagedAttribute");
148
149         annotManAttr.addParameter("description", "\"The Name Attribute\"");
150         annotManAttr.addParameter("currencyTimeLimit", "20");
151         annotManAttr.addParameter("defaultValue", "\"bar\"");
152         annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
153
154         final AnnotationTypeBuilder annotManProp = propertyBuilder
155                 .addAnnotation("org.springframework.jmx.export.annotation",
156                         "ManagedOperation");
157
158         final List<String> typeValues = new ArrayList<String>();
159         typeValues.add("\"val1\"");
160         typeValues.add("\"val2\"");
161         typeValues.add("\"val3\"");
162         annotManProp.addParameters("types", typeValues);
163
164         final GeneratedTransferObject genTransObj = genTOBuilder.toInstance();
165
166         assertNotNull(genTransObj);
167         assertNotNull(genTransObj.getAnnotations());
168         assertNotNull(genTransObj.getProperties());
169         assertNotNull(genTransObj.getProperties().get(0));
170         assertNotNull(genTransObj.getProperties().get(0).getAnnotations());
171         final List<AnnotationType> annotations = genTransObj.getProperties()
172                 .get(0).getAnnotations();
173         assertEquals(2, annotations.size());
174         
175         int annotCount = 0;
176         for (final AnnotationType annotation : annotations) {
177             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
178                     && annotation.getName().equals("ManagedAttribute")) {
179                 annotCount++;
180                 assertEquals(4, annotation.getParameters().size());
181                 
182                 assertNotNull(annotation.getParameter("description"));
183                 assertNotNull(annotation.getParameter("currencyTimeLimit"));
184                 assertNotNull(annotation.getParameter("defaultValue"));
185                 assertNotNull(annotation.getParameter("persistPolicy"));
186                 assertEquals("\"The Name Attribute\"", annotation.getParameter("description").getValue());
187                 assertEquals("20", annotation.getParameter("currencyTimeLimit").getValue());
188                 assertEquals("\"bar\"", annotation.getParameter("defaultValue").getValue());
189                 assertEquals("\"OnUpdate\"", annotation.getParameter("persistPolicy").getValue());
190             }
191             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
192                     && annotation.getName().equals("ManagedOperation")) {
193                 annotCount++;
194                 
195                 assertEquals(1, annotation.getParameters().size());
196                 assertNotNull(annotation.getParameter("types"));
197                 assertEquals(3, annotation.getParameter("types").getValues().size());
198             }
199         }
200         assertEquals(2, annotCount);
201     }
202
203     @Test
204     public void generatedTransfeObjectAnnotationTest() {
205         final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
206                 "org.opendaylight.controller", "AnnotClassCache");
207
208         genTypeBuilder.addAnnotation("javax.management", "MBean");
209         final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
210                 "javax.management", "NotificationInfo");
211
212         final List<String> notifyList = new ArrayList<String>();
213         notifyList.add("\"my.notif.type\"");
214         annotNotify.addParameters("types", notifyList);
215         annotNotify.addParameter("description",
216                 "@Description(\"my notification\")");
217
218         GeneratedTransferObject genTO = genTypeBuilder.toInstance();
219
220         assertNotNull(genTO);
221         assertNotNull(genTO.getAnnotations());
222         assertEquals(2, genTO.getAnnotations().size());
223
224         int annotCount = 0;
225         for (final AnnotationType annotation : genTO.getAnnotations()) {
226             if (annotation.getPackageName().equals("javax.management")
227                     && annotation.getName().equals("MBean")) {
228                 annotCount++;
229                 assertEquals(0, annotation.getParameters().size());
230             }
231             if (annotation.getPackageName().equals("javax.management")
232                     && annotation.getName().equals("NotificationInfo")) {
233                 annotCount++;
234                 assertEquals(2, annotation.getParameters().size());
235                 AnnotationType.Parameter param = annotation
236                         .getParameter("types");
237                 assertNotNull(param);
238                 assertEquals("types", param.getName());
239                 assertNull(param.getValue());
240                 assertNotNull(param.getValues());
241                 assertEquals(1, param.getValues().size());
242                 assertEquals("\"my.notif.type\"", param.getValues().get(0));
243
244                 param = annotation.getParameter("description");
245                 assertNotNull(param);
246                 assertEquals("description", param.getName());
247                 assertNotNull(param.getValue());
248                 assertEquals("@Description(\"my notification\")",
249                         param.getValue());
250             }
251         }
252         assertEquals(2, annotCount);
253     }
254 }