2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
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;
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;
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;
32 public class AnnotationsTest {
34 private final String setSomethingString = "setSomething";
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) {
44 assertEquals(expectedValue, found.value());
48 private static Method findMethod(final Class<?> clazz, final String methodName) {
51 setter = clazz.getMethod(methodName, new Class[] { ObjectName.class });
52 } catch (final NoSuchMethodException e) {
53 throw Throwables.propagate(e);
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) {
66 assertEquals(expectedValue, found);
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) {
77 assertEquals(expectedValue, found);
81 private static void assertNoDescriptionOnClass(final Class<?> clazz, final Set<Class<?>> jmxInterfaces) {
82 String found = AbstractDynamicWrapper.findDescription(clazz, jmxInterfaces);
83 assertTrue(found.isEmpty());
86 static final String SIMPLE = "simple";
87 static final String SUBCLASS2 = "subclass2";
89 @ServiceInterfaceAnnotation(value = SIMPLE,
90 osgiRegistrationType = Executor.class,
91 namespace = "ns", revision = "rev", localName = SIMPLE)
92 interface SimpleSI extends AbstractServiceInterface {
96 public static class SuperClass {
97 @RequireInterface(SimpleSI.class)
99 public void setSomething(final ObjectName objectName) {
104 private static Set<Class<?>> emptySetOfInterfaces() {
105 return Collections.emptySet();
109 public void testFindAnnotation_directly() throws Exception {
110 assertRequireInterfaceAnnotationHasCorrectValue(SuperClass.class, setSomethingString, emptySetOfInterfaces(),
112 assertDescription(SuperClass.class, setSomethingString, emptySetOfInterfaces(), "descr");
113 assertDescriptionOnClass(SuperClass.class, emptySetOfInterfaces(), "class");
116 public static class SubClassWithout extends SuperClass {
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");
128 public static class SubClassWithEmptyMethod extends SuperClass {
130 public void setSomething(final ObjectName objectName) {
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");
143 interface SubSI extends SimpleSI {
147 @ServiceInterfaceAnnotation(value = SUBCLASS2,
148 osgiRegistrationType = ExecutorService.class, namespace = "ns", revision = "rev", localName = SUBCLASS2)
149 interface SubSI2 extends SubSI {
152 public static class SubClassWithAnnotation extends SuperClass {
154 @RequireInterface(SubSI2.class)
155 @Description("descr2")
156 public void setSomething(final ObjectName objectName) {
162 public void testFindAnnotation_SubClassWithAnnotation() throws Exception {
163 assertDescription(SubClassWithAnnotation.class, setSomethingString, emptySetOfInterfaces(), "descr2\ndescr");
165 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithAnnotation.class, setSomethingString,
166 emptySetOfInterfaces(), SubSI2.class);
168 } catch (final IllegalStateException e) {
169 assertTrue(e.getMessage(),
170 e.getMessage().startsWith("Error finding @RequireInterface. More than one value specified"));
174 public interface HasSomeMethod {
175 void setSomething(ObjectName objectName);
178 public static class SubClassWithoutMethodWithInterface extends SuperClass implements HasSomeMethod {
183 public void testFindAnnotation_SubClassWithoutMethodWithInterface() throws Exception {
184 assertRequireInterfaceAnnotationHasCorrectValue(SubClassWithoutMethodWithInterface.class, setSomethingString,
185 emptySetOfInterfaces(), SimpleSI.class);
186 assertDescription(SubClassWithoutMethodWithInterface.class, setSomethingString, emptySetOfInterfaces(),
190 abstract static class SuperClassWithInterface implements HasSomeMethod {
192 @RequireInterface(SubSI2.class)
193 @Description("descr")
194 public void setSomething(final ObjectName objectName) {
198 @Description("class")
199 public static class SubClassOfSuperClassWithInterface extends SuperClassWithInterface {
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");
211 public void testFindAnnotation2() throws Exception {
212 assertNoDescriptionOnClass(SuperClassWithInterface.class, emptySetOfInterfaces());
215 @Description("class")
216 interface HasSomeMethodWithAnnotations {
217 @RequireInterface(SubSI2.class)
218 @Description("descr")
219 void setSomething(ObjectName objectName);
222 static class HasSomeMethodWithAnnotationsImpl implements HasSomeMethodWithAnnotations {
224 public void setSomething(final ObjectName objectName) {
229 public void testHasSomeMethodWithAnnotationsImpl() {
230 HashSet<Class<?>> exportedInterfaces = Sets.<Class<?>>newHashSet(HasSomeMethodWithAnnotations.class);
231 assertRequireInterfaceAnnotationHasCorrectValue(HasSomeMethodWithAnnotationsImpl.class, setSomethingString,
232 exportedInterfaces, SubSI2.class);
234 assertDescription(HasSomeMethodWithAnnotationsImpl.class, setSomethingString, exportedInterfaces, "descr");
236 assertDescriptionOnClass(HasSomeMethodWithAnnotationsImpl.class,
237 new HashSet<>(Arrays.asList(HasSomeMethodWithAnnotations.class)), "class");