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