import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
import org.osgi.framework.ServiceReference;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
/**
- * Unit test for class EventDispatcherImpl
+ * Unit test for {@link EventDispatcherImpl}
*/
-@RunWith(MockitoJUnitRunner.class)
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ServiceHelper.class)
public class EventDispatcherImplTest {
- @Mock AbstractHandler handler;
- @InjectMocks EventDispatcherImpl eventDispatcherImpl;
+ @InjectMocks private EventDispatcherImpl eventDispatcherImpl;
+
+ @Mock private AbstractHandler handler;
+ @Mock private ServiceReference<?> ref;
private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
- public ServiceReference ref = mock(ServiceReference.class);
@Before
public void setUp() {
Random r = new Random();
- eventDispatcherImpl.init();
eventDispatcherImpl.start();
- // configure serviceReference
when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
}
/**
- * Test method {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
+ * Test methods {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
+ * and {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
*/
@Test
- public void testeventHandlerAdded() throws Exception{
- // get handlers from EventDispatcherImpl for test purposes
- AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
-
- // test when should be null
- assertNotEquals(handlers[handlerTypeObject.ordinal()], handler);
-
- // add handler
- eventDispatcherImpl.eventHandlerAdded(ref, handler);
+ public void testHandlerAddedAndRemoved() throws Exception{
+ AbstractHandler[] handlers = ( AbstractHandler[]) getField("handlers");
- // test when handler added
- assertEquals(handlers[handlerTypeObject.ordinal()], handler);
- }
-
- /**
- * Test method {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
- */
- @Test
- public void testHandlerRemoved() throws Exception{
- // get handlers from EventDispatcherImpl for test purposes
- AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
+ assertNotEquals("Error, handler should be null", handlers[handlerTypeObject.ordinal()], handler);
- // add a handler
eventDispatcherImpl.eventHandlerAdded(ref, handler);
- // test when handler added
- assertEquals(handlers[handlerTypeObject.ordinal()], handler);
+ assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
- // remove handler
eventDispatcherImpl.eventHandlerRemoved(ref);
- // test once handler removed
- assertNull(handlers[handlerTypeObject.ordinal()]);
+ assertNull("Error, handler should be null as it has just been removed", handlers[handlerTypeObject.ordinal()]);
}
/**
* Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
*/
+ @SuppressWarnings("unchecked")
@Test
public void testEnqueueEvent() throws Exception{
- // get events from EventDispatcherImpl for test purposes
- BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getClassField("events");
+ BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getField("events");
- // test before enqueue event
assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
- // enqueue event
+ eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
+ eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
+ eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
- // test after enqueue event
- assertEquals("Error, did not return the expected size", 1, events.size());
+ assertEquals("Error, did not return the expected size", 3, events.size());
}
- /**
- * Get the specified field from EventDispatcherImpl using reflection
- * @param fieldName - the field to retrieve
- * @return the desired field
- */
- private Object getClassField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
+ private Object getField(String fieldName) throws Exception {
Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(eventDispatcherImpl);