Skip to content

Commit 802ca0a

Browse files
Give the update load balancer test command a rule id
updateLoadBalancerRule now passes the command's id to the connection settings, which take a long. UpdateLoadBalancerTest built its command without an id, so the null unboxed and testValidateRuleBeforeUpdateLB threw. The API marks id as required, so only the test could get there. Set the id on the command and stub the DAO calls with it, and mock the firewall rule details DAO the settings are read from. testRuleNotValidated only passed before because the null id missed the lookup and raised the same exception type. It now finds the rule and fails on validation, which is what it means to test. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent 0484f2c commit 802ca0a

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎server/src/test/java/com/cloud/network/lb/UpdateLoadBalancerTest.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@
1919
import static org.mockito.ArgumentMatchers.any;
2020
import static org.mockito.ArgumentMatchers.anyLong;
2121
import static org.mockito.ArgumentMatchers.eq;
22-
import static org.mockito.ArgumentMatchers.isNull;
2322
import static org.mockito.ArgumentMatchers.nullable;
2423
import static org.mockito.Mockito.when;
2524

2625
import java.util.ArrayList;
2726
import java.util.UUID;
2827

28+
import org.apache.cloudstack.api.ApiConstants;
2929
import org.apache.cloudstack.api.command.user.loadbalancer.UpdateLoadBalancerRuleCmd;
3030
import org.apache.cloudstack.context.CallContext;
31+
import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
3132
import org.junit.After;
3233
import org.junit.Before;
3334
import org.junit.Test;
3435
import org.mockito.InOrder;
3536
import org.mockito.Mockito;
37+
import org.springframework.test.util.ReflectionTestUtils;
3638

3739
import com.cloud.exception.InsufficientCapacityException;
3840
import com.cloud.exception.InvalidParameterValueException;
@@ -70,6 +72,7 @@ public class UpdateLoadBalancerTest {
7072
private LoadBalancingServiceProvider lbServiceProvider= Mockito.mock(LoadBalancingServiceProvider.class);
7173

7274
private static long domainId = 5L;
75+
private static long lbRuleId = 10L;
7376
private static String accountName = "admin";
7477

7578
@Before
@@ -83,11 +86,13 @@ public void setUp() {
8386
_lbMgr._lb2stickinesspoliciesDao = Mockito.mock(LBStickinessPolicyDao.class);
8487
_lbMgr._lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
8588
_lbMgr._lbCertMapDao = Mockito.mock(LoadBalancerCertMapDao.class);
89+
_lbMgr._firewallRuleDetailsDao = Mockito.mock(FirewallRuleDetailsDao.class);
8690
_lbMgr._lbDao = lbDao;
8791
_lbMgr._lbProviders = new ArrayList<LoadBalancingServiceProvider>();
8892
_lbMgr._lbProviders.add(lbServiceProvider);
8993

9094
updateLbRuleCmd = new UpdateLoadBalancerRuleCmd();
95+
ReflectionTestUtils.setField(updateLbRuleCmd, ApiConstants.ID, lbRuleId);
9196

9297
AccountVO account = new AccountVO(accountName, domainId, "networkDomain", Account.Type.NORMAL, "uuid");
9398
UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
@@ -99,26 +104,26 @@ public void testValidateRuleBeforeUpdateLB() throws ResourceAllocationException,
99104

100105
LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null, null);
101106

102-
when(lbDao.findById(isNull())).thenReturn(lb);
107+
when(lbDao.findById(lbRuleId)).thenReturn(lb);
103108
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
104109
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
105110
when(lbServiceProvider.validateLBRule(any(Network.class), any(LoadBalancingRule.class))).thenReturn(true);
106-
when(lbDao.update(isNull(), eq(lb))).thenReturn(true);
111+
when(lbDao.update(eq(lbRuleId), eq(lb))).thenReturn(true);
107112
when(netDao.findById(nullable(Long.class))).thenReturn(Mockito.mock(NetworkVO.class));
108113
when(ntwkOffServiceMapDao.canProviderSupportServiceInNetworkOffering(nullable(Long.class), any(Network.Service.class), any(Network.Provider.class))).thenReturn(false);
109114
_lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
110115

111116
InOrder inOrder = Mockito.inOrder(lbServiceProvider, lbDao);
112117
inOrder.verify(lbServiceProvider).validateLBRule(any(Network.class), any(LoadBalancingRule.class));
113-
inOrder.verify(lbDao).update(isNull(),eq(lb));
118+
inOrder.verify(lbDao).update(eq(lbRuleId), eq(lb));
114119
}
115120

116121
@Test(expected = InvalidParameterValueException.class)
117122
public void testRuleNotValidated() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
118123

119124
LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null, null);
120125

121-
when(lbDao.findById(anyLong())).thenReturn(lb);
126+
when(lbDao.findById(lbRuleId)).thenReturn(lb);
122127
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
123128
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
124129
when(lbServiceProvider.validateLBRule(any(Network.class), any(LoadBalancingRule.class))).thenReturn(false);

0 commit comments

Comments
 (0)