Initial code drop of yang model driven configuration system
[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     static interface SimpleSI extends AbstractServiceInterface {
102
103     }
104
105     @Description("class")
106     public static class SuperClass {
107         @RequireInterface(SimpleSI.class)
108         @Description("descr")
109         public void setSomething(ObjectName objectName) {
110
111         }
112     }
113
114     private static Set<Class<?>> emptySetOfInterfaces() {
115         return Collections.emptySet();
116     }
117
118     @Test
119     public void testFindAnnotation_directly() throws Exception {
120         assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class,
121                 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
122         assertDescription(SuperClass.class, setSomethingString,
123                 emptySetOfInterfaces(), "descr");
124         assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
125                 "class");
126     }
127
128     public static class SubClassWithout extends SuperClass {
129
130     }
131
132     @Test
133     public void testFindAnnotation_subclassWithout() throws Exception {
134         assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithout.class,
135                 setSomethingString, emptySetOfInterfaces(), SimpleSI.class);
136         assertDescription(SubClassWithout.class, setSomethingString,
137                 emptySetOfInterfaces(), "descr");
138         assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(),
139                 "class");
140     }
141
142     public static class SubClassWithEmptyMethod extends SuperClass {
143         @Override
144         public void setSomething(ObjectName objectName) {
145
146         }
147     }
148
149     @Test
150     public void testOverridingWithoutAnnotation() throws Exception {
151         assertRequireInterfaceAnnotationHasCorrectValue(
152                 SubClassWithEmptyMethod.class, setSomethingString,
153                 emptySetOfInterfaces(), SimpleSI.class);
154         assertDescription(SubClassWithEmptyMethod.class, setSomethingString,
155                 emptySetOfInterfaces(), "descr");
156         assertDescriptionOnClass(SubClassWithEmptyMethod.class,
157                 emptySetOfInterfaces(), "class");
158     }
159
160     static interface SubSI extends SimpleSI {
161
162     }
163
164     @ServiceInterfaceAnnotation(value = SUBCLASS2, osgiRegistrationType = ExecutorService.class)
165     static interface SubSI2 extends SubSI {
166
167     }
168
169     public static class SubClassWithAnnotation extends SuperClass {
170         @Override
171         @RequireInterface(SubSI2.class)
172         @Description("descr2")
173         public void setSomething(ObjectName objectName) {
174
175         }
176     }
177
178     @Test
179     public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
180         assertDescription(SubClassWithAnnotation.class, setSomethingString,
181                 emptySetOfInterfaces(), "descr2\ndescr");
182         try {
183             assertRequireInterfaceAnnotationHasCorrectValue(
184                     SubClassWithAnnotation.class, setSomethingString,
185                     emptySetOfInterfaces(), SubSI2.class);
186             fail();
187         } catch (IllegalStateException e) {
188             assertTrue(
189                     e.getMessage(),
190                     e.getMessage()
191                             .startsWith("Error finding @RequireInterface. More than one value specified"));
192         }
193     }
194
195     public static interface HasSomeMethod {
196         void setSomething(ObjectName objectName);
197     }
198
199     public static class SubClassWithoutMethodWithInterface extends SuperClass
200             implements HasSomeMethod {
201
202     }
203
204     @Test
205     public void testFindAnnotation_SubClassWithoutMethodWithInterface()
206             throws Exception {
207         assertRequireInterfaceAnnotationHasCorrectValue(
208                 SubClassWithoutMethodWithInterface.class, setSomethingString,
209                 emptySetOfInterfaces(), SimpleSI.class);
210         assertDescription(SubClassWithoutMethodWithInterface.class,
211                 setSomethingString, emptySetOfInterfaces(), "descr");
212     }
213
214     static abstract class SuperClassWithInterface implements HasSomeMethod {
215         @Override
216         @RequireInterface(SubSI2.class)
217         @Description("descr")
218         public void setSomething(ObjectName objectName) {
219
220         }
221     }
222
223     @Description("class")
224     public static class SubClassOfSuperClassWithInterface extends
225             SuperClassWithInterface {
226
227     }
228
229     @Test
230     public void testFindAnnotation_SubClassOfSuperClassWithInterface()
231             throws Exception {
232         assertRequireInterfaceAnnotationHasCorrectValue(
233                 SubClassOfSuperClassWithInterface.class, setSomethingString,
234                 emptySetOfInterfaces(), SubSI2.class);
235         assertDescription(SubClassOfSuperClassWithInterface.class,
236                 setSomethingString, emptySetOfInterfaces(), "descr");
237         assertDescriptionOnClass(SubClassOfSuperClassWithInterface.class,
238                 emptySetOfInterfaces(), "class");
239     }
240
241     @Test
242     public void testFindAnnotation2() throws Exception {
243         assertNoDescriptionOnClass(SuperClassWithInterface.class,
244                 emptySetOfInterfaces());
245     }
246
247     @Description("class")
248     static interface HasSomeMethodWithAnnotations {
249
250         @RequireInterface(SubSI2.class)
251         @Description("descr")
252         void setSomething(ObjectName objectName);
253     }
254
255     static class HasSomeMethodWithAnnotationsImpl implements
256             HasSomeMethodWithAnnotations {
257         @Override
258         public void setSomething(ObjectName objectName) {
259         }
260
261     }
262
263     @Test
264     public void testHasSomeMethodWithAnnotationsImpl() {
265         HashSet<Class<?>> exportedInterfaces = Sets
266                 .<Class<?>> newHashSet(HasSomeMethodWithAnnotations.class);
267         assertRequireInterfaceAnnotationHasCorrectValue(
268                 HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
269                 exportedInterfaces, SubSI2.class);
270
271         assertDescription(HasSomeMethodWithAnnotationsImpl.class,
272                 setSomethingString, exportedInterfaces, "descr");
273
274         assertDescriptionOnClass(
275                 HasSomeMethodWithAnnotationsImpl.class,
276                 new HashSet<Class<?>>(Arrays
277                         .asList(HasSomeMethodWithAnnotations.class)), "class");
278     }
279
280 }