Merge "Add JUnit testing for EventDispatcherImpl class."
[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.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;
31
32 /**
33  * Unit test for class EventDispatcherImpl
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class EventDispatcherImplTest {
37
38     @Mock AbstractHandler handler;
39     @InjectMocks EventDispatcherImpl eventDispatcherImpl;
40
41     private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
42     public ServiceReference ref = mock(ServiceReference.class);
43
44     @Before
45     public void setUp() {
46         Random r = new Random();
47
48         eventDispatcherImpl.init();
49         eventDispatcherImpl.start();
50
51         // configure serviceReference
52         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
53         when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
54     }
55
56     /**
57      * Test method {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
58      */
59     @Test
60     public void testeventHandlerAdded() throws Exception{
61         // get handlers from EventDispatcherImpl for test purposes
62         AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
63
64         // test when should be null
65         assertNotEquals(handlers[handlerTypeObject.ordinal()], handler);
66
67         // add handler
68         eventDispatcherImpl.eventHandlerAdded(ref, handler);
69
70         // test when handler added
71         assertEquals(handlers[handlerTypeObject.ordinal()], handler);
72     }
73
74     /**
75      * Test method {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
76      */
77     @Test
78     public void testHandlerRemoved() throws Exception{
79         // get handlers from EventDispatcherImpl for test purposes
80         AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
81
82         // add a handler
83         eventDispatcherImpl.eventHandlerAdded(ref, handler);
84
85         // test when handler added
86         assertEquals(handlers[handlerTypeObject.ordinal()], handler);
87
88         // remove handler
89         eventDispatcherImpl.eventHandlerRemoved(ref);
90
91         // test once handler removed
92         assertNull(handlers[handlerTypeObject.ordinal()]);
93     }
94
95     /**
96      * Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
97      */
98     @Test
99     public void testEnqueueEvent() throws Exception{
100         // get events from EventDispatcherImpl for test purposes
101         BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getClassField("events");
102
103         // test before enqueue event
104         assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
105
106         // enqueue event
107         eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
108
109         // test after enqueue event
110         assertEquals("Error, did not return the expected size", 1, events.size());
111     }
112
113     /**
114      * Get the specified field from EventDispatcherImpl using reflection
115      * @param fieldName - the field to retrieve
116      * @return the desired field
117      */
118     private Object getClassField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
119         Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
120         field.setAccessible(true);
121         return field.get(eventDispatcherImpl);
122     }
123 }