2 * Copyright (c) 2015, 2016 Inocybe and others. All rights reserved.
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
9 package org.opendaylight.netvirt.openstack.netvirt.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import java.lang.reflect.Field;
17 import java.util.HashMap;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.Spy;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.netvirt.openstack.netvirt.NodeCacheManagerEvent;
26 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
27 import org.opendaylight.netvirt.openstack.netvirt.api.EventDispatcher;
28 import org.opendaylight.netvirt.openstack.netvirt.api.NodeCacheListener;
29 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
30 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.osgi.framework.ServiceReference;
38 * Unit test for {@link NodeCacheManagerImpl}
40 @RunWith(MockitoJUnitRunner.class)
41 public class NodeCacheManagerImplTest {
43 @InjectMocks private NodeCacheManagerImpl nodeCacheManagerImpl;
44 @Spy private Map<Long, NodeCacheListener> handlers = new HashMap<>();
46 @Mock private Southbound southbound;
49 public void testProcessEvent() {
50 NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class);
51 Node node = mock(Node.class);
52 when(node.getNodeId()).thenReturn(mock(NodeId.class));
53 when(ev.getNode()).thenReturn(node);
55 when(ev.getAction()).thenReturn(Action.UPDATE);
56 nodeCacheManagerImpl.processEvent(ev);
57 assertEquals("Error, did not delete the event", 1, nodeCacheManagerImpl.getNodes().size());
59 when(ev.getAction()).thenReturn(Action.DELETE);
60 nodeCacheManagerImpl.processEvent(ev);
61 assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size());
65 public void testCacheListenerAddedAndRemoved() {
66 ServiceReference ref = mock(ServiceReference.class);
67 when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(1L);
70 nodeCacheManagerImpl.cacheListenerAdded(ref, mock(NodeCacheListener.class));
71 assertEquals("Error, cacheListenerAdded() did not add any listener", 1, handlers.size());
73 nodeCacheManagerImpl.cacheListenerRemoved(ref);
74 assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size());
78 public void testGetOvsdbNodes() {
81 when(southbound.extractOvsdbNode(any(Node.class))).thenReturn(mock(OvsdbNodeAugmentation.class));
83 assertEquals("Error, getOvsdbNodes() did not return the correct value", 1, nodeCacheManagerImpl.getOvsdbNodes().size());
87 public void testGetBridgeNodes() {
90 when(southbound.getBridge(any(Node.class))).thenReturn(mock(OvsdbBridgeAugmentation.class));
92 assertEquals("Error, getBridgeNodes() did not return the correct value", 1, nodeCacheManagerImpl.getBridgeNodes().size());
96 public void testGetNodes() {
99 assertEquals("Error, getNodes() did not return the correct value", 1, nodeCacheManagerImpl.getNodes().size());
102 private void addItem() {
103 NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class);
104 Node node = mock(Node.class);
105 when(node.getNodeId()).thenReturn(mock(NodeId.class));
106 when(ev.getNode()).thenReturn(node);
108 when(ev.getAction()).thenReturn(Action.UPDATE);
109 nodeCacheManagerImpl.processEvent(ev);
113 public void testSetDependencies() throws Exception {
114 Southbound southbound = mock(Southbound.class);
115 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
117 ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
118 ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
120 nodeCacheManagerImpl.setDependencies(mock(ServiceReference.class));
122 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
123 assertEquals("Error, did not return the correct object", getSuperField("eventDispatcher"), eventDispatcher);
126 private Object getField(String fieldName) throws Exception {
127 Field field = NodeCacheManagerImpl.class.getDeclaredField(fieldName);
128 field.setAccessible(true);
129 return field.get(nodeCacheManagerImpl);
132 private Object getSuperField(String fieldName) throws Exception {
133 Field field = NodeCacheManagerImpl.class.getSuperclass().getDeclaredField(fieldName);
134 field.setAccessible(true);
135 return field.get(nodeCacheManagerImpl);