Add support for openflow node callbacks
[ovsdb.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / NodeCacheManagerImplTest.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 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;
14
15 import java.util.Map;
16
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.InjectMocks;
21 import org.mockito.Spy;
22 import org.opendaylight.ovsdb.openstack.netvirt.NodeCacheManagerEvent;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
25 import org.opendaylight.ovsdb.utils.mdsal.node.NodeUtils;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.osgi.framework.ServiceReference;
28 import org.powermock.api.mockito.PowerMockito;
29 import org.powermock.core.classloader.annotations.PrepareForTest;
30 import org.powermock.modules.junit4.PowerMockRunner;
31
32 import com.google.common.collect.Maps;
33
34 /**
35  * Unit test for {@link NodeCacheManagerImpl}
36  */
37 /* TODO SB_MIGRATION */ @Ignore
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(NodeUtils.class)
40 public class NodeCacheManagerImplTest {
41
42     @InjectMocks NodeCacheManagerImpl nodeCacheManagerImpl;
43     @Spy private Map<Long, NodeCacheListener> handlers = Maps.newHashMap();
44
45     @Test
46     public void testProcessEvent() {
47         NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class);
48         when(ev.getNodeIdentifier()).thenReturn("node_identifier");
49
50         PowerMockito.mockStatic(NodeUtils.class);
51         //when(NodeUtils.getOpenFlowNode(anyString())).thenReturn(mock(Node.class));
52
53         when(ev.getAction()).thenReturn(Action.ADD);
54         nodeCacheManagerImpl.processEvent(ev);
55         assertEquals("Error, did not add the event", 1, nodeCacheManagerImpl.getNodes().size());
56
57         when(ev.getAction()).thenReturn(Action.DELETE);
58         nodeCacheManagerImpl.processEvent(ev);
59         assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size());
60     }
61
62     @Test
63     public void testCacheListenerAddedAndRemoved() {
64         ServiceReference ref = mock(ServiceReference.class);
65         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(Long.valueOf(1));
66
67         // add
68         nodeCacheManagerImpl.cacheListenerAdded(ref, mock(NodeCacheListener.class));
69         assertEquals("Error, cacheListenerAdded() did not add any listener", 1, handlers.size());
70         // remove
71         nodeCacheManagerImpl.cacheListenerRemoved(ref);
72         assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size());
73     }
74 }