2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.binding.generator.util.generated.type.builder;
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;
15 import java.util.ArrayList;
16 import java.util.List;
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;
29 public class AnnotationBuilderTest {
32 public void generatedTypeAnnotationTest() {
33 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
34 "org.opendaylight.controller", "AnnotInterface");
36 genTypeBuilder.addAnnotation("javax.management", "MXBean");
37 final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
38 "javax.management", "Description");
39 annotDesc.addParameter("description", "some sort of interface");
41 final GeneratedType genType = genTypeBuilder.toInstance();
43 assertNotNull(genType);
44 assertNotNull(genType.getAnnotations());
45 assertEquals(2, genType.getAnnotations().size());
48 for (final AnnotationType annotation : genType.getAnnotations()) {
49 if (annotation.getPackageName().equals("javax.management")
50 && annotation.getName().equals("MXBean")) {
52 assertEquals(0, annotation.getParameters().size());
54 if (annotation.getPackageName().equals("javax.management")
55 && annotation.getName().equals("Description")) {
57 assertEquals(1, annotation.getParameters().size());
58 AnnotationType.Parameter param = annotation
59 .getParameter("description");
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());
68 assertEquals(2, annotCount);
72 public void methodSignatureAnnotationTest() {
73 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
74 "org.opendaylight.controller", "TransferObject");
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",
83 annotManAttr.addParameter("description", "\"The Name Attribute\"");
84 annotManAttr.addParameter("currencyTimeLimit", "20");
85 annotManAttr.addParameter("defaultValue", "\"bar\"");
86 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
88 final AnnotationTypeBuilder annotManProp = methodBuilder
89 .addAnnotation("org.springframework.jmx.export.annotation",
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);
98 final GeneratedType genType = genTypeBuilder.toInstance();
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());
110 for (final AnnotationType annotation : annotations) {
111 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
112 && annotation.getName().equals("ManagedAttribute")) {
114 assertEquals(4, annotation.getParameters().size());
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());
125 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
126 && annotation.getName().equals("ManagedOperation")) {
129 assertEquals(1, annotation.getParameters().size());
130 assertNotNull(annotation.getParameter("types"));
131 assertEquals(3, annotation.getParameter("types").getValues().size());
134 assertEquals(2, annotCount);
138 public void generatedPropertyAnnotationTest() {
139 final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(
140 "org.opendaylight.controller", "AnnotInterface");
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",
149 annotManAttr.addParameter("description", "\"The Name Attribute\"");
150 annotManAttr.addParameter("currencyTimeLimit", "20");
151 annotManAttr.addParameter("defaultValue", "\"bar\"");
152 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
154 final AnnotationTypeBuilder annotManProp = propertyBuilder
155 .addAnnotation("org.springframework.jmx.export.annotation",
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);
164 final GeneratedTransferObject genTransObj = genTOBuilder.toInstance();
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());
176 for (final AnnotationType annotation : annotations) {
177 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
178 && annotation.getName().equals("ManagedAttribute")) {
180 assertEquals(4, annotation.getParameters().size());
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());
191 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
192 && annotation.getName().equals("ManagedOperation")) {
195 assertEquals(1, annotation.getParameters().size());
196 assertNotNull(annotation.getParameter("types"));
197 assertEquals(3, annotation.getParameter("types").getValues().size());
200 assertEquals(2, annotCount);
204 public void generatedTransfeObjectAnnotationTest() {
205 final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
206 "org.opendaylight.controller", "AnnotClassCache");
208 genTypeBuilder.addAnnotation("javax.management", "MBean");
209 final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
210 "javax.management", "NotificationInfo");
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\")");
218 GeneratedTransferObject genTO = genTypeBuilder.toInstance();
220 assertNotNull(genTO);
221 assertNotNull(genTO.getAnnotations());
222 assertEquals(2, genTO.getAnnotations().size());
225 for (final AnnotationType annotation : genTO.getAnnotations()) {
226 if (annotation.getPackageName().equals("javax.management")
227 && annotation.getName().equals("MBean")) {
229 assertEquals(0, annotation.getParameters().size());
231 if (annotation.getPackageName().equals("javax.management")
232 && annotation.getName().equals("NotificationInfo")) {
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));
244 param = annotation.getParameter("description");
245 assertNotNull(param);
246 assertEquals("description", param.getName());
247 assertNotNull(param.getValue());
248 assertEquals("@Description(\"my notification\")",
252 assertEquals(2, annotCount);