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;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import java.lang.reflect.Field;
21 import java.net.HttpURLConnection;
22 import java.util.ArrayList;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
34 import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD;
35 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
36 import org.opendaylight.neutron.spi.INeutronPortCRUD;
37 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
38 import org.opendaylight.neutron.spi.NeutronLoadBalancer;
39 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
40 import org.opendaylight.neutron.spi.NeutronLoadBalancerPoolMember;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
42 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration;
44 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration.LoadBalancerPoolMember;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerProvider;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
47 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.osgi.framework.ServiceReference;
51 import org.powermock.api.mockito.PowerMockito;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
56 * Unit test for {@link LBaaSPoolMemberHandler}
58 @PrepareForTest({NeutronCacheUtils.class, ServiceHelper.class})
59 @RunWith(PowerMockRunner.class)
60 public class LBaaSPoolHandlerTest {
62 @InjectMocks LBaaSPoolHandler lBaaSPoolHandler;
64 @Mock private INeutronLoadBalancerPoolCRUD neutronLBPoolCache;
65 @Mock private INeutronLoadBalancerCRUD neutronLBCache;
66 @Mock private LoadBalancerProvider loadBalancerProvider;
67 @Mock private NodeCacheManager nodeCacheManager;
69 @Mock private NeutronLoadBalancerPool neutronLBPool;
73 when(neutronLBPool.getLoadBalancerPoolProtocol()).thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP);
75 List<NeutronLoadBalancerPoolMember> members = new ArrayList<NeutronLoadBalancerPoolMember>();
76 NeutronLoadBalancerPoolMember neutronLBPoolMember = mock(NeutronLoadBalancerPoolMember.class);
77 when(neutronLBPoolMember.getPoolMemberAdminStateIsUp()).thenReturn(true);
78 when(neutronLBPoolMember.getPoolMemberSubnetID()).thenReturn("subnetID");
79 when(neutronLBPoolMember.getID()).thenReturn("pool_memberID");
80 when(neutronLBPoolMember.getPoolMemberAddress()).thenReturn("pool_member_address");
81 when(neutronLBPoolMember.getPoolMemberProtoPort()).thenReturn(1);
82 members.add(neutronLBPoolMember);
83 when(neutronLBPool.getLoadBalancerPoolMembers()).thenReturn(members);
85 List<NeutronLoadBalancer> list_neutronLB = new ArrayList<NeutronLoadBalancer>();
86 NeutronLoadBalancer neutronLB = mock(NeutronLoadBalancer.class);
87 when(neutronLB.getLoadBalancerName()).thenReturn("load_balancer_name");
88 when(neutronLB.getLoadBalancerVipAddress()).thenReturn("vip_address");
89 when(neutronLB.getLoadBalancerVipSubnetID()).thenReturn("subnetID");
90 list_neutronLB.add(neutronLB);
91 when(neutronLBCache.getAllNeutronLoadBalancers()).thenReturn(list_neutronLB);
93 Map.Entry<String,String> providerInfo = mock(Entry.class);
94 when(providerInfo.getKey()).thenReturn("key");
95 when(providerInfo.getValue()).thenReturn("value");
97 PowerMockito.mockStatic(NeutronCacheUtils.class);
98 when(NeutronCacheUtils.getMacAddress(any(INeutronPortCRUD.class), anyString(), anyString())).thenReturn("mac_address");
99 when(NeutronCacheUtils.getProviderInformation(any(INeutronNetworkCRUD.class), any(INeutronSubnetCRUD.class), anyString())).thenReturn(providerInfo);
103 * Test method {@link LBaaSPoolHandler#canCreateNeutronLoadBalancerPool(NeutronLoadBalancerPool)}
106 public void testCanCreateNeutronLoadBalancerPoolMember() {
107 when(neutronLBPool.getLoadBalancerPoolProtocol())
108 .thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP) // to test HTTP_OK
109 .thenReturn(null) // to test HTTP_BAD_REQUEST
110 .thenReturn("dummy_proto"); // to test HTTP_NOT_ACCEPTABLE
112 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_OK, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
113 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_BAD_REQUEST, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
114 assertEquals("Error, canCreateNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, lBaaSPoolHandler.canCreateNeutronLoadBalancerPool(neutronLBPool));
118 * Test method {@link LBaaSPoolHandler#canUpdateNeutronLoadBalancerPool(NeutronLoadBalancerPool, NeutronLoadBalancerPool)}
120 public void testCanUpdateNeutronLoadBalancerPool() {
121 assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_NOT_IMPLEMENTED, lBaaSPoolHandler.canUpdateNeutronLoadBalancerPool(any(NeutronLoadBalancerPool.class), any(NeutronLoadBalancerPool.class)));
125 * Test method {@link LBaaSPoolHandler#canDeleteNeutronLoadBalancerPool(NeutronLoadBalancerPool)}
128 public void testCanDeleteNeutronLoadBalancerPool() {
129 when(neutronLBPool.getLoadBalancerPoolProtocol())
130 .thenReturn(LoadBalancerConfiguration.PROTOCOL_HTTP) // to test HTTP_OK
131 .thenReturn(null) // to test HTTP_BAD_REQUEST
132 .thenReturn("dummy_proto"); // to test HTTP_NOT_ACCEPTABLE
134 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_OK, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
135 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_BAD_REQUEST, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
136 assertEquals("Error, canDeleteNeutronLoadBalancerPool() didn't return the correct HTTP flag", HttpURLConnection.HTTP_NOT_ACCEPTABLE, lBaaSPoolHandler.canDeleteNeutronLoadBalancerPool(neutronLBPool));
140 * Test method {@link LBaaSPoolHandler#processEvent(AbstractEvent)}
143 public void testProcessEvent() {
144 LBaaSPoolHandler lbaasPoolHandlerSpy = Mockito.spy(lBaaSPoolHandler);
146 NorthboundEvent ev = mock(NorthboundEvent.class);
147 when(ev.getLoadBalancerPool()).thenReturn(neutronLBPool);
149 List<Node> list_node = new ArrayList<Node>();
150 list_node .add(mock(Node.class));
151 when(nodeCacheManager.getBridgeNodes()).thenReturn(list_node);
153 when(ev.getAction()).thenReturn(Action.ADD);
154 lbaasPoolHandlerSpy.processEvent(ev);
155 verify(lbaasPoolHandlerSpy, times(1)).extractLBConfiguration(any(NeutronLoadBalancerPool.class));
157 when(ev.getAction()).thenReturn(Action.DELETE);
158 lbaasPoolHandlerSpy.processEvent(ev);
159 verify(lbaasPoolHandlerSpy, times(2)).extractLBConfiguration(any(NeutronLoadBalancerPool.class)); // 1 + 1 above
161 when(ev.getAction()).thenReturn(Action.UPDATE);
162 lbaasPoolHandlerSpy.processEvent(ev);
163 verify(lbaasPoolHandlerSpy, times(2)).extractLBConfiguration(any(NeutronLoadBalancerPool.class)); // same as before as nothing as been done
167 * Test method {@link LBaaSPoolHandler#extractLBConfiguration(NeutronLoadBalancerPool)}
170 public void testExtractLBConfiguration() {
171 List<LoadBalancerConfiguration> list_lbConfig = lBaaSPoolHandler.extractLBConfiguration(neutronLBPool);
172 LoadBalancerConfiguration lbConfig = list_lbConfig.get(0);
174 verify(neutronLBCache, times(1)).getAllNeutronLoadBalancers();
176 // make sure the load balancer configuration was correctly populated
177 assertEquals("Error, did not return the correct value", "key", lbConfig.getProviderNetworkType());
178 assertEquals("Error, did not return the correct value", "value", lbConfig.getProviderSegmentationId());
179 assertEquals("Error, did not return the correct value", "mac_address", lbConfig.getVmac());
181 // make sure the load balancer pool member was correctly populated
182 LoadBalancerPoolMember member = lbConfig.getMembers().get("pool_memberID");
183 assertEquals("Error, did not return the correct value", "pool_member_address", member.getIP());
184 assertEquals("Error, did not return the correct value", "mac_address", member.getMAC());
185 assertEquals("Error, did not return the correct value", LoadBalancerConfiguration.PROTOCOL_HTTP, member.getProtocol());
186 assertTrue("Error, did not return the correct value", 1 == member.getPort());
190 public void testSetDependencies() throws Exception {
191 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
192 LoadBalancerProvider loadBalancerProvider = mock(LoadBalancerProvider.class);
193 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
195 PowerMockito.mockStatic(ServiceHelper.class);
196 PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, lBaaSPoolHandler)).thenReturn(eventDispatcher);
197 PowerMockito.when(ServiceHelper.getGlobalInstance(LoadBalancerProvider.class, lBaaSPoolHandler)).thenReturn(loadBalancerProvider);
198 PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, lBaaSPoolHandler)).thenReturn(nodeCacheManager);
200 lBaaSPoolHandler.setDependencies(mock(ServiceReference.class));
202 assertEquals("Error, did not return the correct object", lBaaSPoolHandler.eventDispatcher, eventDispatcher);
203 assertEquals("Error, did not return the correct object", getField("loadBalancerProvider"), loadBalancerProvider);
204 assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
208 public void testSetDependenciesObject() throws Exception{
209 INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
210 lBaaSPoolHandler.setDependencies(iNeutronNetworkCRUD);
211 assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
213 INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
214 lBaaSPoolHandler.setDependencies(iNeutronPortCRUD);
215 assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
217 INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
218 lBaaSPoolHandler.setDependencies(iNeutronSubnetCRUD);
219 assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
221 INeutronLoadBalancerCRUD iNeutronLoadBalancerCRUD = mock(INeutronLoadBalancerCRUD.class);
222 lBaaSPoolHandler.setDependencies(iNeutronLoadBalancerCRUD);
223 assertEquals("Error, did not return the correct object", getField("neutronLBCache"), iNeutronLoadBalancerCRUD);
225 LoadBalancerProvider loadBalancerProvider = mock(LoadBalancerProvider.class);
226 lBaaSPoolHandler.setDependencies(loadBalancerProvider);
227 assertEquals("Error, did not return the correct object", getField("loadBalancerProvider"), loadBalancerProvider);
230 private Object getField(String fieldName) throws Exception {
231 Field field = LBaaSPoolHandler.class.getDeclaredField(fieldName);
232 field.setAccessible(true);
233 return field.get(lBaaSPoolHandler);