Remove raw references to Map in XSQL
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AnnotationsTest.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.config.manager.impl.dynamicmbean;
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 import static org.junit.Assert.fail;
15
16 import java.lang.reflect.Method;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ExecutorService;
23
24 import javax.management.ObjectName;
25
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
28 import org.opendaylight.controller.config.api.annotations.Description;
29 import org.opendaylight.controller.config.api.annotations.RequireInterface;
30 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
31
32 import com.google.common.base.Throwables;
33 import com.google.common.collect.Sets;
34
35 public class AnnotationsTest {
36
37     private final String setSomethingString = "setSomething";
38
39     private void assertRequireInterfaceAnnotationHasCorrectValue(
40             Class<?> clazz, String methodName,
41             Set<Class<?>> inspectedInterfaces,
42             Class<? extends AbstractServiceInterface> expectedValue) {
43         Method setter = findMethod(clazz, methodName);
44         RequireInterface found = AttributeHolder
45                 .findRequireInterfaceAnnotation(setter, inspectedInterfaces);
46         if (expectedValue == null) {
47             assertNull(found);
48         } else {
49             assertNotNull(found);
50             assertEquals(expectedValue, found.value());
51         }
52     }
53
54     private Method findMethod(Class<?> clazz, String methodName) {
55         Method setter;
56         try {
57             setter = clazz.getMethod(methodName,
58                     new Class[] { ObjectName.class });
59         } catch (Exception e) {
60             throw Throwables.propagate(e);
61         }
62         return setter;
63     }
64
65     private void assertDescription(Class<?> clazz, String methodName,
66             Set<Class<?>> exportedInterfaces, String expectedValue) {
67         Method setter = findMethod(clazz, methodName);
68         String found = AttributeHolder.findDescription(setter,
69                 exportedInterfaces);
70         if (expectedValue == null) {
71             assertNull(found);
72         } else {
73             assertNotNull(found);
74             assertEquals(expectedValue, found);
75         }
76     }
77
78     private void assertDescriptionOnClass(Class<?> clazz,
79             Set<Class<?>> jmxInterfaces, String expectedValue) {
80         String found = AbstractDynamicWrapper.findDescription(clazz,
81                 jmxInterfaces);
82         if (expectedValue == null) {
83             assertNull(found);
84         } else {
85             assertNotNull(found);
86             assertEquals(expectedValue, found);
87         }
88     }
89
90     private void assertNoDescriptionOnClass(Class<?> clazz,
91             Set<Class<?>> jmxInterfaces) {
92         String found = AbstractDynamicWrapper.findDescription(clazz,
93                 jmxInterfaces);
94         assertTrue(found.isEmpty());
95     }
96
97     static final String SIMPLE = "simple";
98     static final String SUBCLASS2 = "subclass2";
99
100     @ServiceInterfaceAnnotation(value = SIMPLE, osgiRegistrationType = Executor.class,
101     namespace = "ns", revision = "rev", localName = SIMPLE)
102     static interface SimpleSI extends AbstractServiceInterface {
103
104     }
105
106     @Description("class")
107     public static class SuperClass {
108         @RequireInterface(SimpleSI.class)
109         @Description("descr")
110         public void setSomething(ObjectName objectName) {
111
112         }
113     }
114
115     private static Set<Class<?>> emptySetOfInterfaces() {
116         return Collections.emptySet();
117     }
118
119     @Test
120     public void testFindAnnotation_directly() throws Exception {
121         assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class,
122                 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
123         assertDescription(SuperClass.class, setSomethingString,
124                 emptySetOfInterfaces(), "descr");
125         assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
126                 "class");
127     }
128
129     public static class SubClassWithout extends SuperClass {
130
131     }
132
133     @Test
134     public void testFindAnnotation_subclassWithout() throws Exception {
135         assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithout.class,
136                 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
137         assertDescription(SubClassWithout.class, setSomethingString,
138                 emptySetOfInterfaces(), "descr");
139         assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
140                 "class");
141     }
142
143     public static class SubClassWithEmptyMethod extends SuperClass {
144         @Override
145         public void setSomething(ObjectName objectName) {
146
147         }
148     }
149
150     @Test
151     public void testOverridingWithoutAnnotation() throws Exception {
152         assertRequireInterfaceAnnotationHasCorrectValue(
153                 SubClassWithEmptyMethod.class, setSomethingString,
154                 emptySetOfInterfaces(), SimpleSI.class);
155         assertDescription(SubClassWithEmptyMethod.class, setSomethingString,
156                 emptySetOfInterfaces(), "descr");
157         assertDescriptionOnClass(SubClassWithEmptyMethod.class,
158                 emptySetOfInterfaces(), "class");
159     }
160
161     static interface SubSI extends SimpleSI {
162
163     }
164
165     @ServiceInterfaceAnnotation(value = SUBCLASS2, osgiRegistrationType = ExecutorService.class,
166     namespace = "ns", revision = "rev", localName = SUBCLASS2)
167
168     static interface SubSI2 extends SubSI {
169
170     }
171
172     public static class SubClassWithAnnotation extends SuperClass {
173         @Override
174         @RequireInterface(SubSI2.class)
175         @Description("descr2")
176         public void setSomething(ObjectName objectName) {
177
178         }
179     }
180
181     @Test
182     public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
183         assertDescription(SubClassWithAnnotation.class, setSomethingString,
184                 emptySetOfInterfaces(), "descr2\ndescr");
185         try {
186             assertRequireInterfaceAnnotationHasCorrectValue(
187                     SubClassWithAnnotation.class, setSomethingString,
188                     emptySetOfInterfaces(), SubSI2.class);
189             fail();
190         } catch (IllegalStateException e) {
191             assertTrue(
192                     e.getMessage(),
193                     e.getMessage()
194                             .startsWith("Error finding @RequireInterface. More than one value specified"));
195         }
196     }
197
198     public static interface HasSomeMethod {
199         void setSomething(ObjectName objectName);
200     }
201
202     public static class SubClassWithoutMethodWithInterface extends SuperClass
203             implements HasSomeMethod {
204
205     }
206
207     @Test
208     public void testFindAnnotation_SubClassWithoutMethodWithInterface()
209             throws Exception {
210         assertRequireInterfaceAnnotationHasCorrectValue(
211                 SubClassWithoutMethodWithInterface.class, setSomethingString,
212                 emptySetOfInterfaces(), SimpleSI.class);
213         assertDescription(SubClassWithoutMethodWithInterface.class,
214                 setSomethingString, emptySetOfInterfaces(), "descr");
215     }
216
217     static abstract class SuperClassWithInterface implements HasSomeMethod {
218         @Override
219         @RequireInterface(SubSI2.class)
220         @Description("descr")
221         public void setSomething(ObjectName objectName) {
222
223         }
224     }
225
226     @Description("class")
227     public static class SubClassOfSuperClassWithInterface extends
228             SuperClassWithInterface {
229
230     }
231
232     @Test
233     public void testFindAnnotation_SubClassOfSuperClassWithInterface()
234             throws Exception {
235         assertRequireInterfaceAnnotationHasCorrectValue(
236                 SubClassOfSuperClassWithInterface.class, setSomethingString,
237                 emptySetOfInterfaces(), SubSI2.class);
238         assertDescription(SubClassOfSuperClassWithInterface.class,
239                 setSomethingString, emptySetOfInterfaces(), "descr");
240         assertDescriptionOnClass(SubClassOfSuperClassWithInterface.class,
241                 emptySetOfInterfaces(), "class");
242     }
243
244     @Test
245     public void testFindAnnotation2() throws Exception {
246         assertNoDescriptionOnClass(SuperClassWithInterface.class,
247                 emptySetOfInterfaces());
248     }
249
250     @Description("class")
251     static interface HasSomeMethodWithAnnotations {
252
253         @RequireInterface(SubSI2.class)
254         @Description("descr")
255         void setSomething(ObjectName objectName);
256     }
257
258     static class HasSomeMethodWithAnnotationsImpl implements
259             HasSomeMethodWithAnnotations {
260         @Override
261         public void setSomething(ObjectName objectName) {
262         }
263
264     }
265
266     @Test
267     public void testHasSomeMethodWithAnnotationsImpl() {
268         HashSet<Class<?>> exportedInterfaces = Sets
269                 .<Class<?>> newHashSet(HasSomeMethodWithAnnotations.class);
270         assertRequireInterfaceAnnotationHasCorrectValue(
271                 HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
272                 exportedInterfaces, SubSI2.class);
273
274         assertDescription(HasSomeMethodWithAnnotationsImpl.class,
275                 setSomethingString, exportedInterfaces, "descr");
276
277         assertDescriptionOnClass(
278                 HasSomeMethodWithAnnotationsImpl.class,
279                 new HashSet<Class<?>>(Arrays
280                         .asList(HasSomeMethodWithAnnotations.class)), "class");
281     }
282
283 }