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