X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openstack%2Fnet-virt%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fopenstack%2Fnetvirt%2Fimpl%2FEventDispatcherImplTest.java;h=878a32148c17a937a088545f13302e62cd44e50f;hb=3492b5d9c2c619c1085e9ccc5a534b650f37dc20;hp=888d0c4cba8c91f3879de0c3543de426efbc359a;hpb=14eee0353df762660cc4ed97fde8e501ba1fdc48;p=ovsdb.git diff --git a/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java b/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java index 888d0c4cb..878a32148 100644 --- a/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java +++ b/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java @@ -23,99 +23,76 @@ import org.junit.Test; 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 events = (BlockingQueue) getClassField("events"); + BlockingQueue events = (BlockingQueue) 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);