2 * Copyright (c) 2015 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.ovsdb.openstack.netvirt.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.InjectMocks;
20 import org.mockito.Spy;
21 import org.opendaylight.ovsdb.openstack.netvirt.NodeCacheManagerEvent;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
24 import org.opendaylight.ovsdb.utils.mdsal.node.NodeUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.osgi.framework.ServiceReference;
27 import org.powermock.api.mockito.PowerMockito;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.modules.junit4.PowerMockRunner;
31 import com.google.common.collect.Maps;
34 * Unit test for {@link NodeCacheManagerImpl}
36 @RunWith(PowerMockRunner.class)
37 @PrepareForTest(NodeUtils.class)
38 public class NodeCacheManagerImplTest {
40 @InjectMocks NodeCacheManagerImpl nodeCacheManagerImpl;
41 @Spy private Map<Long, NodeCacheListener> handlers = Maps.newHashMap();
44 public void testProcessEvent() {
45 NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class);
46 when(ev.getNodeIdentifier()).thenReturn("node_identifier");
48 PowerMockito.mockStatic(NodeUtils.class);
49 when(NodeUtils.getOpenFlowNode(anyString())).thenReturn(mock(Node.class));
51 when(ev.getAction()).thenReturn(Action.ADD);
52 nodeCacheManagerImpl.processEvent(ev);
53 assertEquals("Error, did not add the event", 1, nodeCacheManagerImpl.getNodes().size());
55 when(ev.getAction()).thenReturn(Action.DELETE);
56 nodeCacheManagerImpl.processEvent(ev);
57 assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size());
61 public void testCacheListenerAddedAndRemoved() {
62 ServiceReference ref = mock(ServiceReference.class);
63 when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(Long.valueOf(1));
66 nodeCacheManagerImpl.cacheListenerAdded(ref, mock(NodeCacheListener.class));
67 assertEquals("Error, cacheListenerAdded() did not add any listener", 1, handlers.size());
69 nodeCacheManagerImpl.cacheListenerRemoved(ref);
70 assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size());