2fcdd5286c25af5bee22fc25e8b4230101249195
[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.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;
30
31 import com.google.common.collect.Maps;
32
33 /**
34  * Unit test for {@link NodeCacheManagerImpl}
35  */
36 @RunWith(PowerMockRunner.class)
37 @PrepareForTest(NodeUtils.class)
38 public class NodeCacheManagerImplTest {
39
40     @InjectMocks NodeCacheManagerImpl nodeCacheManagerImpl;
41     @Spy private Map<Long, NodeCacheListener> handlers = Maps.newHashMap();
42
43     @Test
44     public void testProcessEvent() {
45         NodeCacheManagerEvent ev = mock(NodeCacheManagerEvent.class);
46         when(ev.getNodeIdentifier()).thenReturn("node_identifier");
47
48         PowerMockito.mockStatic(NodeUtils.class);
49         when(NodeUtils.getOpenFlowNode(anyString())).thenReturn(mock(Node.class));
50
51         when(ev.getAction()).thenReturn(Action.ADD);
52         nodeCacheManagerImpl.processEvent(ev);
53         assertEquals("Error, did not add the event", 1, nodeCacheManagerImpl.getNodes().size());
54
55         when(ev.getAction()).thenReturn(Action.DELETE);
56         nodeCacheManagerImpl.processEvent(ev);
57         assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size());
58     }
59
60     @Test
61     public void testCacheListenerAddedAndRemoved() {
62         ServiceReference ref = mock(ServiceReference.class);
63         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(Long.valueOf(1));
64
65         // add
66         nodeCacheManagerImpl.cacheListenerAdded(ref, mock(NodeCacheListener.class));
67         assertEquals("Error, cacheListenerAdded() did not add any listener", 1, handlers.size());
68         // remove
69         nodeCacheManagerImpl.cacheListenerRemoved(ref);
70         assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size());
71     }
72 }