Add JUnit testing for EventDispatcherImpl class. 99/17199/1
authorAlexis de Talhouët <adetalhouet@inocybe.com>
Thu, 26 Mar 2015 19:51:11 +0000 (15:51 -0400)
committerAlexis de Talhouët <adetalhouet@inocybe.com>
Thu, 26 Mar 2015 19:51:11 +0000 (15:51 -0400)
Change-Id: I4b382bb681af4e6d805094666657d81688166b9d
Signed-off-by: Alexis de Talhouët <adetalhouet@inocybe.com>
openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java [new file with mode: 0644]

diff --git a/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java b/openstack/net-virt/src/test/java/org/opendaylight/ovsdb/openstack/netvirt/impl/EventDispatcherImplTest.java
new file mode 100644 (file)
index 0000000..888d0c4
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2015 Inocybe and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.ovsdb.openstack.netvirt.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.Field;
+import java.util.Random;
+import java.util.concurrent.BlockingQueue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
+import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Unit test for class EventDispatcherImpl
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class EventDispatcherImplTest {
+
+    @Mock AbstractHandler handler;
+    @InjectMocks EventDispatcherImpl eventDispatcherImpl;
+
+    private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
+    public ServiceReference ref = mock(ServiceReference.class);
+
+    @Before
+    public void setUp() {
+        Random r = new Random();
+
+        eventDispatcherImpl.init();
+        eventDispatcherImpl.start();
+
+        // configure serviceReference
+        when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(r.nextLong());
+        when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY)).thenReturn(handlerTypeObject);
+    }
+
+    /**
+     * Test method {@link EventDispatcherImpl#eventHandlerAdded(ServiceReference, AbstractHandler)}
+     */
+    @Test
+    public void testeventHandlerAdded() throws Exception{
+        // get handlers from EventDispatcherImpl for test purposes
+        AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
+
+        // test when should be null
+        assertNotEquals(handlers[handlerTypeObject.ordinal()], handler);
+
+        // add handler
+        eventDispatcherImpl.eventHandlerAdded(ref, handler);
+
+        // test when handler added
+        assertEquals(handlers[handlerTypeObject.ordinal()], handler);
+    }
+
+    /**
+     * Test method {@link EventDispatcherImpl#eventHandlerRemoved(ServiceReference)}
+     */
+    @Test
+    public void testHandlerRemoved() throws Exception{
+        // get handlers from EventDispatcherImpl for test purposes
+        AbstractHandler[] handlers = ( AbstractHandler[]) getClassField("handlers");
+
+        // add a handler
+        eventDispatcherImpl.eventHandlerAdded(ref, handler);
+
+        // test when handler added
+        assertEquals(handlers[handlerTypeObject.ordinal()], handler);
+
+        // remove handler
+        eventDispatcherImpl.eventHandlerRemoved(ref);
+
+        // test once handler removed
+        assertNull(handlers[handlerTypeObject.ordinal()]);
+    }
+
+    /**
+     * Test method {@link EventDispatcherImpl#enqueueEvent(AbstractEvent)}
+     */
+    @Test
+    public void testEnqueueEvent() throws Exception{
+        // get events from EventDispatcherImpl for test purposes
+        BlockingQueue<AbstractEvent> events = (BlockingQueue<AbstractEvent>) getClassField("events");
+
+        // test before enqueue event
+        assertEquals("Error, did not return the expected size, nothing has been added yet", 0, events.size());
+
+        // enqueue event
+        eventDispatcherImpl.enqueueEvent(mock(AbstractEvent.class));
+
+        // test after enqueue event
+        assertEquals("Error, did not return the expected size", 1, events.size());
+    }
+
+    /**
+     * Get the specified field from EventDispatcherImpl using reflection
+     * @param fieldName - the field to retrieve
+     * @return the desired field
+     */
+    private Object getClassField(String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
+        Field field = EventDispatcherImpl.class.getDeclaredField(fieldName);
+        field.setAccessible(true);
+        return field.get(eventDispatcherImpl);
+    }
+}