Skip to content

Commit

Permalink
Add static methods to avoid duplicate creation of new
Browse files Browse the repository at this point in the history
  • Loading branch information
gzhao9 committed Nov 2, 2023
1 parent 66f5737 commit 5deb5dd
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ public class MockTungstenAnswerFactory {
TungstenAnswer get() {
return tungstenAnswer;
}

public static TungstenAnswer get(boolean returnResult){
TungstenAnswer tungstenAnswers = new MockTungstenAnswerFactory(returnResult).get();
return tungstenAnswers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void applyStaticNatsAddRuleSuccessTest() {
StaticNatImpl staticNat = mock(StaticNatImpl.class);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
TungstenAnswer assignFloatingIpAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer assignFloatingIpAnswer = MockTungstenAnswerFactory.get(true);
Nic nic = mock(Nic.class);
Network publicNetwork = mock(Network.class);
List<StaticNatImpl> staticNatList = List.of(staticNat);
Expand All @@ -277,7 +277,7 @@ public void applyStaticNatsAddRuleFailTest() {
StaticNatImpl staticNat = mock(StaticNatImpl.class);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
TungstenAnswer assignFloatingIpAnswer = new MockTungstenAnswerFactory(false).get();
TungstenAnswer assignFloatingIpAnswer = MockTungstenAnswerFactory.get(false);
Nic nic = mock(Nic.class);
Network publicNetwork = mock(Network.class);
List<StaticNatImpl> staticNatList = List.of(staticNat);
Expand All @@ -298,7 +298,7 @@ public void applyStaticNatsRevokeRuleSuccessTest() {
StaticNatImpl staticNat = mock(StaticNatImpl.class);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
TungstenAnswer releaseFloatingIpAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer releaseFloatingIpAnswer = MockTungstenAnswerFactory.get(true);
Nic nic = mock(Nic.class);
Network publicNetwork = mock(Network.class);
List<StaticNatImpl> staticNatList = List.of(staticNat);
Expand All @@ -319,7 +319,7 @@ public void applyStaticNatsRevokeRuleFailTest() {
StaticNatImpl staticNat = mock(StaticNatImpl.class);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
TungstenAnswer releaseFloatingIpAnswer = new MockTungstenAnswerFactory(false).get();
TungstenAnswer releaseFloatingIpAnswer = MockTungstenAnswerFactory.get(false);
Nic nic = mock(Nic.class);
Network publicNetwork = mock(Network.class);
List<StaticNatImpl> staticNatList = List.of(staticNat);
Expand Down Expand Up @@ -351,11 +351,11 @@ public void applyLBRulesAddRuleSuccessTest() {
TungstenFabricLBHealthMonitorVO tungstenFabricLBHealthMonitorVO = mock(TungstenFabricLBHealthMonitorVO.class);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class);
TungstenAnswer createTungstenNetworkLoadbalancerAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenLoadBalancerPoolAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenLoadBalancerMemberAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenHealthMonitorAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createTungstenNetworkLoadbalancerAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenLoadBalancerPoolAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenLoadBalancerMemberAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenLoadBalancerListenerAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenHealthMonitorAnswer = MockTungstenAnswerFactory.get(true);
LoadBalancingRule.LbSslCert lbSslCert = mock(LoadBalancingRule.LbSslCert.class);
when(lbStickinessPolicy.getMethodName()).thenReturn("AppCookie");
List<Pair<String, String>> pairList = List.of(new Pair<>("cookieName", "cookieValue"));
Expand Down Expand Up @@ -402,10 +402,10 @@ public void applyLBRulesAddRuleFailTest() {
List<LoadBalancerVO> loadBalancerVOList = List.of(loadBalancerVO);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class);
TungstenAnswer createTungstenNetworkLoadbalancerAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenLoadBalancerPoolAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenLoadBalancerMemberAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer updateTungstenHealthMonitorAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createTungstenNetworkLoadbalancerAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenLoadBalancerPoolAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenLoadBalancerMemberAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer updateTungstenHealthMonitorAnswer = MockTungstenAnswerFactory.get(true);
List<Pair<String, String>> pairList = List.of(new Pair<>("cookieName", "cookieValue"));
TungstenFabricLBHealthMonitorVO tungstenFabricLBHealthMonitorVO = mock(TungstenFabricLBHealthMonitorVO.class);

Expand Down Expand Up @@ -445,8 +445,8 @@ public void applyLBRulesRevokeRuleFailTest() {
List<LoadBalancingRule> loadBalancingRuleList1 = List.of(loadBalancingRule1);
List<LoadBalancerVO> loadBalancerVOList1 = List.of(loadBalancerVO1);
TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class);
TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer deleteTungstenLoadBalancerCommand = new MockTungstenAnswerFactory(true).get();
TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer deleteTungstenLoadBalancerCommand = MockTungstenAnswerFactory.get(true);

when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork);
when(loadBalancingRule1.getSourceIp()).thenReturn(ip1);
Expand All @@ -472,8 +472,8 @@ public void applyLBRulesRevokeRuleSuccessTest() {
LoadBalancerVO loadBalancerVO1 = mock(LoadBalancerVO.class);
LoadBalancerVO loadBalancerVO2 = mock(LoadBalancerVO.class);
TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class);
TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer deleteTungstenLoadBalancerCommand = new MockTungstenAnswerFactory(true).get();
TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer deleteTungstenLoadBalancerCommand = MockTungstenAnswerFactory.get(true);
List<LoadBalancingRule> loadBalancingRuleList = List.of(loadBalancingRule);
List<LoadBalancerVO> loadBalancerVOList = Arrays.asList(loadBalancerVO1, loadBalancerVO2);

Expand All @@ -497,7 +497,7 @@ public void applyPFRulesAddRuleSuccessTest() {
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
UserVm userVm = mock(UserVm.class);
Nic nic = mock(Nic.class);
TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer applyTungstenPortForwardingAnswer = MockTungstenAnswerFactory.get(true);
List<PortForwardingRule> portForwardingRuleList = List.of(portForwardingRule);

when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Add);
Expand All @@ -518,7 +518,7 @@ public void applyPFRulesAddRuleFailTest() {
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
UserVm userVm = mock(UserVm.class);
Nic nic = mock(Nic.class);
TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(false).get();
TungstenAnswer applyTungstenPortForwardingAnswer = MockTungstenAnswerFactory.get(false);
List<PortForwardingRule> portForwardingRuleList = List.of(portForwardingRule);

when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Add);
Expand All @@ -539,7 +539,7 @@ public void applyPFRulesRevokeRuleSuccessTest() {
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
UserVm userVm = mock(UserVm.class);
Nic nic = mock(Nic.class);
TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer applyTungstenPortForwardingAnswer = MockTungstenAnswerFactory.get(true);
List<PortForwardingRule> portForwardingRuleList = List.of(portForwardingRule);

when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Revoke);
Expand All @@ -560,7 +560,7 @@ public void applyPFRulesRevokeRuleFailTest() {
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
UserVm userVm = mock(UserVm.class);
Nic nic = mock(Nic.class);
TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(false).get();
TungstenAnswer applyTungstenPortForwardingAnswer = MockTungstenAnswerFactory.get(false);
List<PortForwardingRule> portForwardingRuleList = List.of(portForwardingRule);

when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Revoke);
Expand All @@ -582,9 +582,9 @@ public void preparePublicNetworkTest() throws ConcurrentOperationException {
ReservationContext reservationContext = mock(ReservationContext.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
HostVO host = mock(HostVO.class);
TungstenAnswer createTungstenVMAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createTungstenVMAnswer = MockTungstenAnswerFactory.get(true);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
TungstenAnswer createTungstenNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createTungstenNetworkPolicyAnswer = MockTungstenAnswerFactory.get(true);

nicProfile.setIPv4Address("192.168.100.100");
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public);
Expand All @@ -611,7 +611,7 @@ public void prepareManagementNetworkTest() throws ConcurrentOperationException {
ReservationContext reservationContext = mock(ReservationContext.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
HostVO host = mock(HostVO.class);
TungstenAnswer createTungstenVMAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createTungstenVMAnswer = MockTungstenAnswerFactory.get(true);

nicProfile.setIPv4Address("192.168.100.100");
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Management);
Expand Down Expand Up @@ -728,8 +728,8 @@ public void destroyTest() throws ConcurrentOperationException, ResourceUnavailab
Network network = mock(Network.class);
ReservationContext reservationContext = mock(ReservationContext.class);
List<IPAddressVO> ipAddressVOList = Arrays.asList(ipAddressVO1, ipAddressVO2);
TungstenAnswer tungstenDeleteFIPAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer tungstenDeleteNPAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer tungstenDeleteFIPAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer tungstenDeleteNPAnswer = MockTungstenAnswerFactory.get(true);

when(ipAddressDao.listByAssociatedNetwork(anyLong(), anyBoolean())).thenReturn(ipAddressVOList);
when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFloatingIpCommand.class), anyLong())).thenReturn(tungstenDeleteFIPAnswer);
Expand Down Expand Up @@ -875,8 +875,8 @@ public void applyFWRulesWithAddEgressRuleTest() throws ResourceUnavailableExcept
Network network = mock(Network.class);
FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class);
Network publicNetwork = mock(Network.class);
TungstenAnswer createNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer applyNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createNetworkPolicyAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer applyNetworkPolicyAnswer = MockTungstenAnswerFactory.get(true);

when(firewallRuleVO.getState()).thenReturn(FirewallRule.State.Add);
when(firewallRuleVO.getSourceCidrList()).thenReturn(List.of("192.168.100.0/24"));
Expand All @@ -895,8 +895,8 @@ public void applyFWRulesWithAddIngressRuleTest() throws ResourceUnavailableExcep
Network network = mock(Network.class);
FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class);
Network publicNetwork = mock(Network.class);
TungstenAnswer createNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer applyNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer createNetworkPolicyAnswer = MockTungstenAnswerFactory.get(true);
TungstenAnswer applyNetworkPolicyAnswer = MockTungstenAnswerFactory.get(true);
IPAddressVO ipAddressVO = mock(IPAddressVO.class);
Ip ip = mock(Ip.class);

Expand Down Expand Up @@ -965,7 +965,7 @@ public void prepareMigrationTest() {
ReservationContext context = mock(ReservationContext.class);
VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
HostVO hostVO = mock(HostVO.class);
TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer tungstenAnswer = MockTungstenAnswerFactory.get(true);

when(vm.getType()).thenReturn(VirtualMachine.Type.ConsoleProxy);
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public);
Expand Down Expand Up @@ -1011,4 +1011,4 @@ public void commitMigration() {
tungstenElement.commitMigration(nic, network, vm, src, dest);
verify(tungstenFabricUtils, times(1)).sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void tearDown() throws Exception {
@Test
public void sendTungstenCommandSuccessTest() {
TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class);
TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get();
TungstenAnswer tungstenAnswer = MockTungstenAnswerFactory.get(true);
TungstenCommand tungstenCommand = mock(TungstenCommand.class);

when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO);
Expand Down Expand Up @@ -92,12 +92,12 @@ public void sendTungstenCommandWithNullAnswer() {
@Test(expected = InvalidParameterValueException.class)
public void sendTungstenCommandWithFalseAnswer() {
TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class);
TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(false).get();
TungstenAnswer tungstenAnswer = MockTungstenAnswerFactory.get(false);
TungstenCommand tungstenCommand = mock(TungstenCommand.class);

when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO);
when(agentMgr.easySend(anyLong(), any(TungstenCommand.class))).thenReturn(tungstenAnswer);

tungstenFabricUtils.sendTungstenCommand(tungstenCommand, anyLong());
}
}
}
Loading

0 comments on commit 5deb5dd

Please sign in to comment.