Updated yangtools version to 0.5.6-SNAPSHOT.
[yangtools.git] / code-generator / binding-generator-util / src / test / java / org / opendaylight / yangtools / 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.yangtools.binding.generator.util.generated.type.builder;
9
10 import static org.junit.Assert.*;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
25
26 public class AnnotationBuilderTest {
27
28     @Test
29     public void generatedTypeAnnotationTest() {
30         final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
31                 "org.opendaylight.controller", "AnnotInterface");
32
33         genTypeBuilder.addAnnotation("javax.management", "MXBean");
34         final AnnotationTypeBuilder annotDesc = genTypeBuilder.addAnnotation(
35                 "javax.management", "Description");
36         annotDesc.addParameter("description", "some sort of interface");
37
38         final GeneratedType genType = genTypeBuilder.toInstance();
39
40         assertNotNull(genType);
41         assertNotNull(genType.getAnnotations());
42         assertEquals(2, genType.getAnnotations().size());
43
44         int annotCount = 0;
45         for (final AnnotationType annotation : genType.getAnnotations()) {
46             if (annotation.getPackageName().equals("javax.management")
47                     && annotation.getName().equals("MXBean")) {
48                 annotCount++;
49                 assertEquals(0, annotation.getParameters().size());
50             }
51             if (annotation.getPackageName().equals("javax.management")
52                     && annotation.getName().equals("Description")) {
53                 annotCount++;
54                 assertEquals(1, annotation.getParameters().size());
55                 AnnotationType.Parameter param = annotation
56                         .getParameter("description");
57                 assertNotNull(param);
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());
63             }
64         }
65         assertEquals(2, annotCount);
66     }
67
68     @Test
69     public void methodSignatureAnnotationTest() {
70         final GeneratedTypeBuilder genTypeBuilder = new GeneratedTypeBuilderImpl(
71                 "org.opendaylight.controller", "TransferObject");
72
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",
78                         "ManagedAttribute");
79
80         annotManAttr.addParameter("description", "\"The Name Attribute\"");
81         annotManAttr.addParameter("currencyTimeLimit", "20");
82         annotManAttr.addParameter("defaultValue", "\"bar\"");
83         annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
84
85         final AnnotationTypeBuilder annotManProp = methodBuilder
86                 .addAnnotation("org.springframework.jmx.export.annotation",
87                         "ManagedOperation");
88
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);
94
95         final GeneratedType genType = genTypeBuilder.toInstance();
96
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());
105
106         int annotCount = 0;
107         for (final AnnotationType annotation : annotations) {
108             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
109                     && annotation.getName().equals("ManagedAttribute")) {
110                 annotCount++;
111                 assertEquals(4, annotation.getParameters().size());
112
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());
121             }
122             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
123                     && annotation.getName().equals("ManagedOperation")) {
124                 annotCount++;
125
126                 assertEquals(1, annotation.getParameters().size());
127                 assertNotNull(annotation.getParameter("types"));
128                 assertEquals(3, annotation.getParameter("types").getValues().size());
129             }
130         }
131         assertEquals(2, annotCount);
132     }
133
134     @Test
135     public void generatedPropertyAnnotationTest() {
136         final GeneratedTOBuilder genTOBuilder = new GeneratedTOBuilderImpl(
137                 "org.opendaylight.controller", "AnnotInterface");
138
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",
144                         "ManagedAttribute");
145
146         annotManAttr.addParameter("description", "\"The Name Attribute\"");
147         annotManAttr.addParameter("currencyTimeLimit", "20");
148         annotManAttr.addParameter("defaultValue", "\"bar\"");
149         annotManAttr.addParameter("persistPolicy", "\"OnUpdate\"");
150
151         final AnnotationTypeBuilder annotManProp = propertyBuilder
152                 .addAnnotation("org.springframework.jmx.export.annotation",
153                         "ManagedOperation");
154
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);
160
161         final GeneratedTransferObject genTransObj = genTOBuilder.toInstance();
162
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());
171
172         int annotCount = 0;
173         for (final AnnotationType annotation : annotations) {
174             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
175                     && annotation.getName().equals("ManagedAttribute")) {
176                 annotCount++;
177                 assertEquals(4, annotation.getParameters().size());
178
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());
187             }
188             if (annotation.getPackageName().equals("org.springframework.jmx.export.annotation")
189                     && annotation.getName().equals("ManagedOperation")) {
190                 annotCount++;
191
192                 assertEquals(1, annotation.getParameters().size());
193                 assertNotNull(annotation.getParameter("types"));
194                 assertEquals(3, annotation.getParameter("types").getValues().size());
195             }
196         }
197         assertEquals(2, annotCount);
198     }
199
200     @Test
201     public void generatedTransfeObjectAnnotationTest() {
202         final GeneratedTOBuilder genTypeBuilder = new GeneratedTOBuilderImpl(
203                 "org.opendaylight.controller", "AnnotClassCache");
204
205         genTypeBuilder.addAnnotation("javax.management", "MBean");
206         final AnnotationTypeBuilder annotNotify = genTypeBuilder.addAnnotation(
207                 "javax.management", "NotificationInfo");
208
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\")");
214
215         GeneratedTransferObject genTO = genTypeBuilder.toInstance();
216
217         assertNotNull(genTO);
218         assertNotNull(genTO.getAnnotations());
219         assertEquals(2, genTO.getAnnotations().size());
220
221         int annotCount = 0;
222         for (final AnnotationType annotation : genTO.getAnnotations()) {
223             if (annotation.getPackageName().equals("javax.management")
224                     && annotation.getName().equals("MBean")) {
225                 annotCount++;
226                 assertEquals(0, annotation.getParameters().size());
227             }
228             if (annotation.getPackageName().equals("javax.management")
229                     && annotation.getName().equals("NotificationInfo")) {
230                 annotCount++;
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));
240
241                 param = annotation.getParameter("description");
242                 assertNotNull(param);
243                 assertEquals("description", param.getName());
244                 assertNotNull(param.getValue());
245                 assertEquals("@Description(\"my notification\")",
246                         param.getValue());
247             }
248         }
249         assertEquals(2, annotCount);
250     }
251 }