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