Fix Bug 3663: Update netvirt.impl UT
[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.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
27 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
28 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
29 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
30 import org.osgi.framework.ServiceReference;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 /**
35  * Unit test for {@link EventDispatcherImpl}
36  */
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest(ServiceHelper.class)
39 public class EventDispatcherImplTest {
40
41     @InjectMocks private EventDispatcherImpl eventDispatcherImpl;
42
43     @Mock private AbstractHandler handler;
44     @Mock private ServiceReference<?> ref;
45
46     private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
47
48     @Before
49     public void setUp() {
50         Random r = new Random();
51
52         eventDispatcherImpl.start();
53
54         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
55         when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
56     }
57
58     /**
59      * Test methods {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
60      * and {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
61      */
62     @Test
63     public void testHandlerAddedAndRemoved() throws Exception{
64         AbstractHandler[] handlers = ( AbstractHandler[]) getField("handlers");
65
66         assertNotEquals("Error, handler should be null", handlers[handlerTypeObject.ordinal()], handler);
67
68         eventDispatcherImpl.eventHandlerAdded(ref, handler);
69
70         assertEquals("Error, did not return the added handler", handlers[handlerTypeObject.ordinal()], handler);
71
72         eventDispatcherImpl.eventHandlerRemoved(ref);
73
74         assertNull("Error, handler should be null as it has just been removed", handlers[handlerTypeObject.ordinal()]);
75     }
76
77     /**
78      * Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
79      */
80     @SuppressWarnings("unchecked")
81     @Test
82     public void testEnqueueEvent() throws Exception{
83         BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getField("events");
84
85         assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
86
87         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
88         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
89         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
90         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
91
92         assertEquals("Error, did not return the expected size", 3, events.size());
93     }
94
95     private Object getField(String fieldName) throws Exception {
96         Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
97         field.setAccessible(true);
98         return field.get(eventDispatcherImpl);
99     }
100 }