44599cd76ad559bfe1f8b1be8c3880d09db8261b
[ovsdb.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / EventDispatcherImplTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10
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;
16
17 import java.lang.reflect.Field;
18 import java.util.Random;
19 import java.util.concurrent.BlockingQueue;
20
21 import org.junit.Before;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
29 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
31 import org.osgi.framework.ServiceReference;
32
33 /**
34  * Unit test for {@link EventDispatcherImpl}
35  */
36 /* TODO SB_MIGRATION */ @Ignore
37 @RunWith(MockitoJUnitRunner.class)
38 public class EventDispatcherImplTest {
39
40     @Mock AbstractHandler handler;
41     @InjectMocks EventDispatcherImpl eventDispatcherImpl;
42
43     private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
44     private ServiceReference ref = mock(ServiceReference.class);
45
46     @Before
47     public void setUp() {
48         Random r = new Random();
49
50         eventDispatcherImpl.init();
51         eventDispatcherImpl.start();
52
53         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
54         when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
55     }
56
57     /**
58      * Test methods {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
59      * and {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
60      */
61     @Test
62     public void testHandlerAddedAndRemoved() throws Exception{
63         AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
64
65         assertNotEquals("Error, handler should be null", handlers[handlerTypeObject.ordinal()], handler);
66
67         eventDispatcherImpl.eventHandlerAdded(ref, handler);
68
69         assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
70
71         eventDispatcherImpl.eventHandlerRemoved(ref);
72
73         assertNull("Error, handler should be null as it has just been removed", handlers[handlerTypeObject.ordinal()]);
74     }
75
76     /**
77      * Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
78      */
79     @Test
80     public void testEnqueueEvent() throws Exception{
81         BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getClassField("events");
82
83         assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
84
85         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
86
87         assertEquals("Error, did not return the expected size", 1, events.size());
88     }
89
90     /**
91      * Get the specified field from EventDispatcherImpl using reflection
92      * @param fieldName - the field to retrieve
93      * @return the desired field
94      */
95     private Object getClassField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
96         Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
97         field.setAccessible(true);
98         return field.get(eventDispatcherImpl);
99     }
100 }