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.yangtools.binding.generator.util.generated.type.builder;
10 import static org.junit.Assert.*;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.binding.generator.util.Types;
17 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
26 public class AnnotationBuilderTest {
29 public void generatedTypeAnnotationTest() {
30 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
31 "org.opendaylight.controller", "AnnotInterface");
33 genTypeBuilder.addAnnotation("javax.management", "MXBean");
34 final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
35 "javax.management", "Description");
36 annotDesc.addParameter("description", "some sort of interface");
38 final GeneratedType genType = genTypeBuilder.toInstance();
40 assertNotNull(genType);
41 assertNotNull(genType.getAnnotations());
42 assertEquals(2, genType.getAnnotations().size());
45 for (final AnnotationType annotation : genType.getAnnotations()) {
46 if (annotation.getPackageName().equals("javax.management")
47 && annotation.getName().equals("MXBean")) {
49 assertEquals(0, annotation.getParameters().size());
51 if (annotation.getPackageName().equals("javax.management")
52 && annotation.getName().equals("Description")) {
54 assertEquals(1, annotation.getParameters().size());
55 AnnotationType.Parameter param = annotation
56 .getParameter("description");
58 assertEquals("description", param.getName());
59 assertNotNull(param.getValue());
60 assertEquals("some sort of interface", param.getValue());
61 assertNotNull(param.getValues());
62 assertTrue(param.getValues().isEmpty());
65 assertEquals(2, annotCount);
69 public void methodSignatureAnnotationTest() {
70 final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
71 "org.opendaylight.controller", "TransferObject");
73 final MethodSignatureBuilder methodBuilder = genTypeBuilder
74 .addMethod("simpleMethod");
75 methodBuilder.setReturnType(Types.typeForClass(Integer.class));
76 final AnnotationTypeBuilder annotManAttr = methodBuilder
77 .addAnnotation("org.springframework.jmx.export.annotation",
80 annotManAttr.addParameter("description", "\"The Name Attribute\"");
81 annotManAttr.addParameter("currencyTimeLimit", "20");
82 annotManAttr.addParameter("defaultValue", "\"bar\"");
83 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
85 final AnnotationTypeBuilder annotManProp = methodBuilder
86 .addAnnotation("org.springframework.jmx.export.annotation",
89 final List<String> typeValues = new ArrayList<String>();
90 typeValues.add("\"val1\"");
91 typeValues.add("\"val2\"");
92 typeValues.add("\"val3\"");
93 annotManProp.addParameters("types", typeValues);
95 final GeneratedType genType = genTypeBuilder.toInstance();
97 assertNotNull(genType);
98 assertNotNull(genType.getAnnotations());
99 assertNotNull(genType.getMethodDefinitions());
100 assertNotNull(genType.getMethodDefinitions().get(0));
101 assertNotNull(genType.getMethodDefinitions().get(0).getAnnotations());
102 final List<AnnotationType> annotations = genType.getMethodDefinitions()
103 .get(0).getAnnotations();
104 assertEquals(2, annotations.size());
107 for (final AnnotationType annotation : annotations) {
108 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
109 && annotation.getName().equals("ManagedAttribute")) {
111 assertEquals(4, annotation.getParameters().size());
113 assertNotNull(annotation.getParameter("description"));
114 assertNotNull(annotation.getParameter("currencyTimeLimit"));
115 assertNotNull(annotation.getParameter("defaultValue"));
116 assertNotNull(annotation.getParameter("persistPolicy"));
117 assertEquals("\"The Name Attribute\"", annotation.getParameter("description").getValue());
118 assertEquals("20", annotation.getParameter("currencyTimeLimit").getValue());
119 assertEquals("\"bar\"", annotation.getParameter("defaultValue").getValue());
120 assertEquals("\"OnUpdate\"", annotation.getParameter("persistPolicy").getValue());
122 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
123 && annotation.getName().equals("ManagedOperation")) {
126 assertEquals(1, annotation.getParameters().size());
127 assertNotNull(annotation.getParameter("types"));
128 assertEquals(3, annotation.getParameter("types").getValues().size());
131 assertEquals(2, annotCount);
135 public void generatedPropertyAnnotationTest() {
136 final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(
137 "org.opendaylight.controller", "AnnotInterface");
139 final GeneratedPropertyBuilder propertyBuilder = genTOBuilder
140 .addProperty("simpleProperty");
141 propertyBuilder.setReturnType(Types.typeForClass(Integer.class));
142 final AnnotationTypeBuilder annotManAttr = propertyBuilder
143 .addAnnotation("org.springframework.jmx.export.annotation",
146 annotManAttr.addParameter("description", "\"The Name Attribute\"");
147 annotManAttr.addParameter("currencyTimeLimit", "20");
148 annotManAttr.addParameter("defaultValue", "\"bar\"");
149 annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
151 final AnnotationTypeBuilder annotManProp = propertyBuilder
152 .addAnnotation("org.springframework.jmx.export.annotation",
155 final List<String> typeValues = new ArrayList<String>();
156 typeValues.add("\"val1\"");
157 typeValues.add("\"val2\"");
158 typeValues.add("\"val3\"");
159 annotManProp.addParameters("types", typeValues);
161 final GeneratedTransferObject genTransObj = genTOBuilder.toInstance();
163 assertNotNull(genTransObj);
164 assertNotNull(genTransObj.getAnnotations());
165 assertNotNull(genTransObj.getProperties());
166 assertNotNull(genTransObj.getProperties().get(0));
167 assertNotNull(genTransObj.getProperties().get(0).getAnnotations());
168 final List<AnnotationType> annotations = genTransObj.getProperties()
169 .get(0).getAnnotations();
170 assertEquals(2, annotations.size());
173 for (final AnnotationType annotation : annotations) {
174 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
175 && annotation.getName().equals("ManagedAttribute")) {
177 assertEquals(4, annotation.getParameters().size());
179 assertNotNull(annotation.getParameter("description"));
180 assertNotNull(annotation.getParameter("currencyTimeLimit"));
181 assertNotNull(annotation.getParameter("defaultValue"));
182 assertNotNull(annotation.getParameter("persistPolicy"));
183 assertEquals("\"The Name Attribute\"", annotation.getParameter("description").getValue());
184 assertEquals("20", annotation.getParameter("currencyTimeLimit").getValue());
185 assertEquals("\"bar\"", annotation.getParameter("defaultValue").getValue());
186 assertEquals("\"OnUpdate\"", annotation.getParameter("persistPolicy").getValue());
188 if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
189 && annotation.getName().equals("ManagedOperation")) {
192 assertEquals(1, annotation.getParameters().size());
193 assertNotNull(annotation.getParameter("types"));
194 assertEquals(3, annotation.getParameter("types").getValues().size());
197 assertEquals(2, annotCount);
201 public void generatedTransfeObjectAnnotationTest() {
202 final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
203 "org.opendaylight.controller", "AnnotClassCache");
205 genTypeBuilder.addAnnotation("javax.management", "MBean");
206 final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
207 "javax.management", "NotificationInfo");
209 final List<String> notifyList = new ArrayList<String>();
210 notifyList.add("\"my.notif.type\"");
211 annotNotify.addParameters("types", notifyList);
212 annotNotify.addParameter("description",
213 "@Description(\"my notification\")");
215 GeneratedTransferObject genTO = genTypeBuilder.toInstance();
217 assertNotNull(genTO);
218 assertNotNull(genTO.getAnnotations());
219 assertEquals(2, genTO.getAnnotations().size());
222 for (final AnnotationType annotation : genTO.getAnnotations()) {
223 if (annotation.getPackageName().equals("javax.management")
224 && annotation.getName().equals("MBean")) {
226 assertEquals(0, annotation.getParameters().size());
228 if (annotation.getPackageName().equals("javax.management")
229 && annotation.getName().equals("NotificationInfo")) {
231 assertEquals(2, annotation.getParameters().size());
232 AnnotationType.Parameter param = annotation
233 .getParameter("types");
234 assertNotNull(param);
235 assertEquals("types", param.getName());
236 assertNull(param.getValue());
237 assertNotNull(param.getValues());
238 assertEquals(1, param.getValues().size());
239 assertEquals("\"my.notif.type\"", param.getValues().get(0));
241 param = annotation.getParameter("description");
242 assertNotNull(param);
243 assertEquals("description", param.getName());
244 assertNotNull(param.getValue());
245 assertEquals("@Description(\"my notification\")",
249 assertEquals(2, annotCount);