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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.