2 * Copyright (c) 2015 Inocybe 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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import java.lang.reflect.Field;
18 import java.util.Random;
19 import java.util.concurrent.BlockingQueue;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
28 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
30 import org.osgi.framework.ServiceReference;
33 * Unit test for class EventDispatcherImpl
35 @RunWith(MockitoJUnitRunner.class)
36 public class EventDispatcherImplTest {
38 @Mock AbstractHandler handler;
39 @InjectMocks EventDispatcherImpl eventDispatcherImpl;
41 private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
42 public ServiceReference ref = mock(ServiceReference.class);
46 Random r = new Random();
48 eventDispatcherImpl.init();
49 eventDispatcherImpl.start();
51 when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
52 when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
56 * Test method {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
59 public void testeventHandlerAdded() throws Exception{
60 AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
62 assertNotEquals("Error, handler should be null", handlers[handlerTypeObject.ordinal()], handler);
64 eventDispatcherImpl.eventHandlerAdded(ref, handler);
66 assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
70 * Test method {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
73 public void testHandlerRemoved() throws Exception{
74 AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
76 eventDispatcherImpl.eventHandlerAdded(ref, handler);
78 assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
80 eventDispatcherImpl.eventHandlerRemoved(ref);
82 assertNull("Error, handler should be null as it has just been removed", handlers[handlerTypeObject.ordinal()]);
86 * Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
89 public void testEnqueueEvent() throws Exception{
90 BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getClassField("events");
92 assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
94 eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
96 assertEquals("Error, did not return the expected size", 1, events.size());
100 * Get the specified field from EventDispatcherImpl using reflection
101 * @param fieldName - the field to retrieve
102 * @return the desired field
104 private Object getClassField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
105 Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
106 field.setAccessible(true);
107 return field.get(eventDispatcherImpl);