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.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;
30 public class AnnotationBuilderTest {
33 public void generatedTypeAnnotationTest() {
34 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
35 "org.opendaylight.controller", "AnnotInterface");
37 genTypeBuilder.addAnnotation("javax.management", "MXBean");
38 final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
39 "javax.management", "Description");
40 annotDesc.addParameter("description", "some sort of interface");
42 final GeneratedType genType = genTypeBuilder.toInstance();
44 assertNotNull(genType);
45 assertNotNull(genType.getAnnotations());
46 assertEquals(2, genType.getAnnotations().size());
49 for (final AnnotationType annotation : genType.getAnnotations()) {
50 if (annotation.getPackageName().equals("javax.management")
51 && annotation.getName().equals("MXBean")) {
53 assertEquals(0, annotation.getParameters().size());
55 if (annotation.getPackageName().equals("javax.management")
56 && annotation.getName().equals("Description")) {
58 assertEquals(1, annotation.getParameters().size());
59 AnnotationType.Parameter param = annotation
60 .getParameter("description");
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());
69 assertEquals(2, annotCount);
73 public void methodSignatureAnnotationTest() {
74 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
75 "org.opendaylight.controller", "TransferObject");
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",
84 annotManAttr.addParameter("description", "\"The Name Attribute\"");
85 annotManAttr.addParameter("currencyTimeLimit", "20");
86 annotManAttr.addParameter("defaultValue", "\"bar\"");
87 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
89 final AnnotationTypeBuilder annotManProp = methodBuilder
90 .addAnnotation("org.springframework.jmx.export.annotation",
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);
99 final GeneratedType genType = genTypeBuilder.toInstance();
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());
111 for (final AnnotationType annotation : annotations) {
112 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
113 && annotation.getName().equals("ManagedAttribute")) {
115 assertEquals(4, annotation.getParameters().size());
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());
126 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
127 && annotation.getName().equals("ManagedOperation")) {
130 assertEquals(1, annotation.getParameters().size());
131 assertNotNull(annotation.getParameter("types"));
132 assertEquals(3, annotation.getParameter("types").getValues().size());
135 assertEquals(2, annotCount);
139 public void generatedPropertyAnnotationTest() {
140 final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(
141 "org.opendaylight.controller", "AnnotInterface");
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",
150 annotManAttr.addParameter("description", "\"The Name Attribute\"");
151 annotManAttr.addParameter("currencyTimeLimit", "20");
152 annotManAttr.addParameter("defaultValue", "\"bar\"");
153 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
155 final AnnotationTypeBuilder annotManProp = propertyBuilder
156 .addAnnotation("org.springframework.jmx.export.annotation",
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);
165 final GeneratedTransferObject genTransObj = genTOBuilder.toInstance();
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());
177 for (final AnnotationType annotation : annotations) {
178 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
179 && annotation.getName().equals("ManagedAttribute")) {
181 assertEquals(4, annotation.getParameters().size());
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());
192 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
193 && annotation.getName().equals("ManagedOperation")) {
196 assertEquals(1, annotation.getParameters().size());
197 assertNotNull(annotation.getParameter("types"));
198 assertEquals(3, annotation.getParameter("types").getValues().size());
201 assertEquals(2, annotCount);
205 public void generatedTransfeObjectAnnotationTest() {
206 final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
207 "org.opendaylight.controller", "AnnotClassCache");
209 genTypeBuilder.addAnnotation("javax.management", "MBean");
210 final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
211 "javax.management", "NotificationInfo");
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\")");
219 GeneratedTransferObject genTO = genTypeBuilder.toInstance();
221 assertNotNull(genTO);
222 assertNotNull(genTO.getAnnotations());
223 assertEquals(2, genTO.getAnnotations().size());
226 for (final AnnotationType annotation : genTO.getAnnotations()) {
227 if (annotation.getPackageName().equals("javax.management")
228 && annotation.getName().equals("MBean")) {
230 assertEquals(0, annotation.getParameters().size());
232 if (annotation.getPackageName().equals("javax.management")
233 && annotation.getName().equals("NotificationInfo")) {
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));
245 param = annotation.getParameter("description");
246 assertNotNull(param);
247 assertEquals("description", param.getName());
248 assertNotNull(param.getValue());
249 assertEquals("@Description(\"my notification\")",
253 assertEquals(2, annotCount);