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