Fix NPE triggered after disabling SG on a port
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / impl / NodeCacheManagerImplTest.java
1 /*
2  * Copyright (c) 2015, 2016 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.netvirt.openstack.netvirt.impl;
10
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;
15
16 import java.lang.reflect.Field;
17 import java.util.HashMap;
18 import java.util.Map;
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;
36
37 /**
38  * Unit test for {@link NodeCacheManagerImpl}
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class NodeCacheManagerImplTest {
42
43     @InjectMocks private NodeCacheManagerImpl nodeCacheManagerImpl;
44     @Spy private Map<Long, NodeCacheListener> handlers = new HashMap<>();
45
46     @Mock private Southbound southbound;
47
48     @Test
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);
54
55         when(ev.getAction()).thenReturn(Action.UPDATE);
56         nodeCacheManagerImpl.processEvent(ev);
57         assertEquals("Error, did not delete the event", 1, nodeCacheManagerImpl.getNodes().size());
58
59         when(ev.getAction()).thenReturn(Action.DELETE);
60         nodeCacheManagerImpl.processEvent(ev);
61         assertEquals("Error, did not delete the event", 0, nodeCacheManagerImpl.getNodes().size());
62     }
63
64     @Test
65     public void testCacheListenerAddedAndRemoved() {
66         ServiceReference ref = mock(ServiceReference.class);
67         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID)).thenReturn(1L);
68
69         // add
70         nodeCacheManagerImpl.cacheListenerAdded(ref, mock(NodeCacheListener.class));
71         assertEquals("Error, cacheListenerAdded() did not add any listener", 1, handlers.size());
72         // remove
73         nodeCacheManagerImpl.cacheListenerRemoved(ref);
74         assertEquals("Error, cacheListenerAdded() did not remove any listener", 0, handlers.size());
75     }
76
77     @Test
78     public void testGetOvsdbNodes() {
79         addItem();
80
81         when(southbound.extractOvsdbNode(any(Node.class))).thenReturn(mock(OvsdbNodeAugmentation.class));
82
83         assertEquals("Error, getOvsdbNodes() did not return the correct value", 1, nodeCacheManagerImpl.getOvsdbNodes().size());
84     }
85
86     @Test
87     public void testGetBridgeNodes() {
88         addItem();
89
90         when(southbound.getBridge(any(Node.class))).thenReturn(mock(OvsdbBridgeAugmentation.class));
91
92         assertEquals("Error, getBridgeNodes() did not return the correct value", 1, nodeCacheManagerImpl.getBridgeNodes().size());
93     }
94
95     @Test
96     public void testGetNodes() {
97         addItem();
98
99         assertEquals("Error, getNodes() did not return the correct value", 1, nodeCacheManagerImpl.getNodes().size());
100     }
101
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);
107
108         when(ev.getAction()).thenReturn(Action.UPDATE);
109         nodeCacheManagerImpl.processEvent(ev);
110     }
111
112     @Test
113     public void testSetDependencies() throws Exception {
114         Southbound southbound = mock(Southbound.class);
115         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
116
117         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
118         ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
119
120         nodeCacheManagerImpl.setDependencies(mock(ServiceReference.class));
121
122         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
123         assertEquals("Error, did not return the correct object", getSuperField("eventDispatcher"), eventDispatcher);
124     }
125
126     private Object getField(String fieldName) throws Exception {
127         Field field = NodeCacheManagerImpl.class.getDeclaredField(fieldName);
128         field.setAccessible(true);
129         return field.get(nodeCacheManagerImpl);
130     }
131
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);
136     }
137 }