Updated Branches: refs/heads/gslb-wip 70e2eeaea -> fd18fb619
- unit tests for inerface methods exposed by GlobalLoadBalancingRulesService - implements deleteGlobalLoadBalancerRule() method Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/fd18fb61 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/fd18fb61 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/fd18fb61 Branch: refs/heads/gslb-wip Commit: fd18fb6190e4f3c5272a5d9a7bf6221570ba1e29 Parents: 54bff97 Author: Murali Reddy <[email protected]> Authored: Tue Mar 5 14:47:27 2013 +0530 Committer: Murali Reddy <[email protected]> Committed: Tue Mar 5 14:47:27 2013 +0530 ---------------------------------------------------------------------- .../gslb/GlobalLoadBalancingRulesServiceImpl.java | 54 +- .../GlobalLoadBalancingRulesServiceImplTest.java | 942 +++++++++++++++ 2 files changed, 990 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fd18fb61/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java ---------------------------------------------------------------------- diff --git a/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java b/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java index b55ccd7..c8bca8e 100644 --- a/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java +++ b/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java @@ -82,7 +82,8 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR @Override @DB - @ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, eventDescription = "creating global load balancer") + @ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, eventDescription = + "creating global load balancer rule") public GlobalLoadBalancerRule createGlobalLoadBalancerRule(CreateGlobalLoadBalancerRuleCmd newGslb) throws InvalidParameterSpecException { @@ -204,9 +205,12 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } Network network = _networkDao.findById(loadBalancer.getNetworkId()); - if (oldZones.contains(network.getDataCenterId())) { + + if (oldZones != null && oldZones.contains(network.getDataCenterId()) || + newZones != null && newZones.contains(network.getDataCenterId())) { throw new InvalidParameterValueException("Each load balancer rule specified should be in unique zone"); } + newZones.add(network.getDataCenterId()); } @@ -298,10 +302,6 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR _accountMgr.checkAccess(caller, null, true, loadBalancer); - if (loadBalancer.getState() == LoadBalancer.State.Revoke) { - throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is in revoke state"); - } - if (oldLbRuleIds != null && !oldLbRuleIds.contains(loadBalancer.getId())) { throw new InvalidParameterValueException("Load balancer ID " + loadBalancer.getUuid() + " is not assigned" + " to global load balancer rule: " + gslbRule.getUuid()); @@ -347,8 +347,50 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } @Override + @DB + @ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_DELETE, eventDescription = + "Delete global load balancer rule") public boolean deleteGlobalLoadBalancerRule(DeleteGlobalLoadBalancerRuleCmd deleteGslbCmd) { + UserContext ctx = UserContext.current(); + Account caller = ctx.getCaller(); + + long gslbRuleId = deleteGslbCmd.getGlobalLoadBalancerId(); + GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); + if (gslbRule == null) { + throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId); + } + + _accountMgr.checkAccess(caller, SecurityChecker.AccessType.ModifyEntry, true, gslbRule); + + if (gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke) { + throw new InvalidParameterValueException("global load balancer rule id: " + gslbRuleId + " is already in revoked state"); + } + + Transaction txn = Transaction.currentTxn(); + txn.start(); + + List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId); + if (gslbLbMapVos != null) { + //mark all the GSLB-LB mapping to be in revoke state + for (GlobalLoadBalancerLbRuleMapVO gslbLbMap : gslbLbMapVos) { + gslbLbMap.setRevoke(true); + } + } + + //mark the GSlb rule to be in revoke state + gslbRule.setState(GlobalLoadBalancerRule.State.Revoke); + _gslbRuleDao.update(gslbRuleId, gslbRule); + + txn.commit(); + + // send the new configuration to back end + try { + applyGlobalLoadBalancerRuleConfig(gslbRuleId, true); + } catch (Exception e) { + + } + return false; } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fd18fb61/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java ---------------------------------------------------------------------- diff --git a/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java new file mode 100644 index 0000000..d74688e --- /dev/null +++ b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java @@ -0,0 +1,942 @@ +package org.apache.cloudstack.region.gslb; + +import com.cloud.agent.AgentManager; +import com.cloud.configuration.dao.ConfigurationDao; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.network.dao.*; +import com.cloud.network.rules.FirewallRule; +import com.cloud.network.rules.RulesManager; +import com.cloud.region.ha.GlobalLoadBalancerRule; +import com.cloud.user.Account; +import com.cloud.user.AccountManager; +import com.cloud.user.AccountVO; +import com.cloud.user.UserContext; +import com.cloud.utils.db.Transaction; +import junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.cloudstack.api.command.user.region.ha.gslb.AssignToGlobalLoadBalancerRuleCmd; +import org.apache.cloudstack.api.command.user.region.ha.gslb.CreateGlobalLoadBalancerRuleCmd; +import org.apache.cloudstack.api.command.user.region.ha.gslb.DeleteGlobalLoadBalancerRuleCmd; +import org.apache.cloudstack.api.command.user.region.ha.gslb.RemoveFromGlobalLoadBalancerRuleCmd; +import org.apache.cloudstack.region.RegionVO; +import org.apache.cloudstack.region.dao.RegionDao; +import org.apache.log4j.Logger; +import org.junit.Test; +import org.mockito.Mockito; + +import java.lang.reflect.Field; +import java.security.spec.InvalidParameterSpecException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.when; + +public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { + + private static final Logger s_logger = Logger.getLogger( GlobalLoadBalancingRulesServiceImplTest.class); + + @Test + public void testCreateGlobalLoadBalancerRule() throws Exception { + + s_logger.info("Running tests for CreateGlobalLoadBalancerRule() service API"); + + /* + * TEST 1: given valid parameters CreateGlobalLoadBalancerRule should succeed + */ + runCreateGlobalLoadBalancerRulePostiveTest(); + + /* + * TEST 2: given invalid algorithm CreateGlobalLoadBalancerRule should fail + */ + runCreateGlobalLoadBalancerRuleInvalidAlgorithm(); + + /* + * TEST 3: given invalid persistence method CreateGlobalLoadBalancerRule should fail + */ + runCreateGlobalLoadBalancerRuleInvalidStickyMethod(); + + /* + * TEST 4: given invalid service type CreateGlobalLoadBalancerRule should fail + */ + runCreateGlobalLoadBalancerRuleInvalidServiceType(); + + /* + * TEST 5: given 'domain name' that is already used by a different GSLB rule CreateGlobalLoadBalancerRule should fail + */ + runCreateGlobalLoadBalancerRuleInvalidDomainName(); + } + + @Test + public void testAssignToGlobalLoadBalancerRule() throws Exception { + + s_logger.info("Running tests for AssignToGlobalLoadBalancerRule() service API"); + + /* + * TEST 1: given valid gslb rule id, valid lb rule id, and caller has access to both the rules + * assignToGlobalLoadBalancerRule service API should succeed + */ + runAssignToGlobalLoadBalancerRuleTest(); + + /* + * TEST 2: given valid gslb rule id, two valid Lb rules but both belong to same zone then + * assignToGlobalLoadBalancerRule service API should fail + */ + runAssignToGlobalLoadBalancerRuleTestSameZoneLb(); + + /* + * TEST 3: if gslb rule is in revoke state assignToGlobalLoadBalancerRule service API should fail + */ + runAssignToGlobalLoadBalancerRuleTestRevokedState(); + } + + @Test + public void testRemoveFromGlobalLoadBalancerRule() throws Exception { + + s_logger.info("Running tests for RemoveFromGlobalLoadBalancerRule() service API"); + + /* + * TEST 1: given valid gslb rule id, valid lb rule id and is assigned to given gslb rule id + * then RemoveFromGlobalLoadBalancerRule service API should succeed + */ + runRemoveFromGlobalLoadBalancerRuleTest(); + + /* + * TEST 2: given valid gslb rule id, valid lb rule id but NOT assigned to given gslb rule id + * then RemoveFromGlobalLoadBalancerRule service API should fail + */ + runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb(); + + /* + * TEST 3: given valid gslb rule id, INVALID lb rule id then RemoveFromGlobalLoadBalancerRule + * service API should fail + */ + runRemoveFromGlobalLoadBalancerRuleTestInvalidLb(); + } + + @Test + public void testDeleteGlobalLoadBalancerRule() throws Exception { + + s_logger.info("Running tests for DeleteGlobalLoadBalancerRule() service API"); + + /* + * TEST 1: given valid gslb rule id with assigned Lb rules, DeleteGlobalLoadBalancerRule() + * call should succeed, and Gslb rule should be set to revoke state + */ + runDeleteGlobalLoadBalancerRuleTestWithNoLbRules(); + + /* + * TEST 2: given valid gslb rule id with assigned Lb rules, DeleteGlobalLoadBalancerRule() + * call should succeed, and Gslb rule should be set to revoke state + */ + runDeleteGlobalLoadBalancerRuleTestWithLbRules(); + + + } + + void runCreateGlobalLoadBalancerRulePostiveTest() throws Exception { + + Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + Account account = (Account) new AccountVO("testaccount", 1, + "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + when(gslbServiceImpl._gslbRuleDao.persist(any(GlobalLoadBalancerRuleVO.class))).thenReturn(new GlobalLoadBalancerRuleVO()); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + RegionVO region = new RegionVO(); + region.setGslbEnabled(true); + when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region); + + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = createCmd.getClass().getSuperclass(); + + Field regionIdField = _class.getDeclaredField("regionId"); + regionIdField.setAccessible(true); + regionIdField.set(createCmd, new Integer(1)); + + Field algoField = _class.getDeclaredField("algorithm"); + algoField.setAccessible(true); + algoField.set(createCmd, "roundrobin"); + + Field stickyField = _class.getDeclaredField("stickyMethod"); + stickyField.setAccessible(true); + stickyField.set(createCmd, "sourceip"); + + Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName"); + nameField.setAccessible(true); + nameField.set(createCmd, "gslb-rule"); + + Field descriptionField = _class.getDeclaredField("description"); + descriptionField.setAccessible(true); + descriptionField.set(createCmd, "testing create gslb-rule"); + + Field serviceDomainField = _class.getDeclaredField("serviceDomainName"); + serviceDomainField.setAccessible(true); + serviceDomainField.set(createCmd, "gslb-rule-domain"); + + Field serviceTypeField = _class.getDeclaredField("serviceType"); + serviceTypeField.setAccessible(true); + serviceTypeField.set(createCmd, "tcp"); + + try { + gslbServiceImpl.createGlobalLoadBalancerRule(createCmd); + } catch (Exception e) { + s_logger.info("exception in testing runCreateGlobalLoadBalancerRulePostiveTest message: " + e.toString()); + } + } + + void runCreateGlobalLoadBalancerRuleInvalidAlgorithm() throws Exception { + + Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + Account account = (Account) new AccountVO("testaccount", 1, + "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + when(gslbServiceImpl._gslbRuleDao.persist(any(GlobalLoadBalancerRuleVO.class))).thenReturn(new GlobalLoadBalancerRuleVO()); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + RegionVO region = new RegionVO(); + region.setGslbEnabled(true); + when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region); + + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = createCmd.getClass().getSuperclass(); + + Field regionIdField = _class.getDeclaredField("regionId"); + regionIdField.setAccessible(true); + regionIdField.set(createCmd, new Integer(1)); + + Field algoField = _class.getDeclaredField("algorithm"); + algoField.setAccessible(true); + algoField.set(createCmd, "invalidalgo"); + + Field stickyField = _class.getDeclaredField("stickyMethod"); + stickyField.setAccessible(true); + stickyField.set(createCmd, "sourceip"); + + Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName"); + nameField.setAccessible(true); + nameField.set(createCmd, "gslb-rule"); + + Field descriptionField = _class.getDeclaredField("description"); + descriptionField.setAccessible(true); + descriptionField.set(createCmd, "testing create gslb-rule"); + + Field serviceDomainField = _class.getDeclaredField("serviceDomainName"); + serviceDomainField.setAccessible(true); + serviceDomainField.set(createCmd, "gslb-rule-domain"); + + Field serviceTypeField = _class.getDeclaredField("serviceType"); + serviceTypeField.setAccessible(true); + serviceTypeField.set(createCmd, "tcp"); + + try { + gslbServiceImpl.createGlobalLoadBalancerRule(createCmd); + } catch (InvalidParameterSpecException e) { + Assert.assertTrue(e.getMessage().contains("Invalid Algorithm")); + } + } + + void runCreateGlobalLoadBalancerRuleInvalidStickyMethod() throws Exception { + + Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + Account account = (Account) new AccountVO("testaccount", 1, + "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + when(gslbServiceImpl._gslbRuleDao.persist(any(GlobalLoadBalancerRuleVO.class))).thenReturn(new GlobalLoadBalancerRuleVO()); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + RegionVO region = new RegionVO(); + region.setGslbEnabled(true); + when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region); + + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = createCmd.getClass().getSuperclass(); + + Field regionIdField = _class.getDeclaredField("regionId"); + regionIdField.setAccessible(true); + regionIdField.set(createCmd, new Integer(1)); + + Field algoField = _class.getDeclaredField("algorithm"); + algoField.setAccessible(true); + algoField.set(createCmd, "roundrobin"); + + Field stickyField = _class.getDeclaredField("stickyMethod"); + stickyField.setAccessible(true); + stickyField.set(createCmd, "ivalidstickymethod"); + + Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName"); + nameField.setAccessible(true); + nameField.set(createCmd, "gslb-rule"); + + Field descriptionField = _class.getDeclaredField("description"); + descriptionField.setAccessible(true); + descriptionField.set(createCmd, "testing create gslb-rule"); + + Field serviceDomainField = _class.getDeclaredField("serviceDomainName"); + serviceDomainField.setAccessible(true); + serviceDomainField.set(createCmd, "gslb-rule-domain"); + + Field serviceTypeField = _class.getDeclaredField("serviceType"); + serviceTypeField.setAccessible(true); + serviceTypeField.set(createCmd, "tcp"); + + try { + gslbServiceImpl.createGlobalLoadBalancerRule(createCmd); + } catch (InvalidParameterSpecException e) { + Assert.assertTrue(e.getMessage().contains("Invalid persistence")); + } + } + + void runCreateGlobalLoadBalancerRuleInvalidServiceType() throws Exception { + + Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + Account account = (Account) new AccountVO("testaccount", 1, + "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + when(gslbServiceImpl._gslbRuleDao.persist(any(GlobalLoadBalancerRuleVO.class))).thenReturn(new GlobalLoadBalancerRuleVO()); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + RegionVO region = new RegionVO(); + region.setGslbEnabled(true); + when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region); + + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = createCmd.getClass().getSuperclass(); + + Field regionIdField = _class.getDeclaredField("regionId"); + regionIdField.setAccessible(true); + regionIdField.set(createCmd, new Integer(1)); + + Field algoField = _class.getDeclaredField("algorithm"); + algoField.setAccessible(true); + algoField.set(createCmd, "roundrobin"); + + Field stickyField = _class.getDeclaredField("stickyMethod"); + stickyField.setAccessible(true); + stickyField.set(createCmd, "sourceip"); + + Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName"); + nameField.setAccessible(true); + nameField.set(createCmd, "gslb-rule"); + + Field descriptionField = _class.getDeclaredField("description"); + descriptionField.setAccessible(true); + descriptionField.set(createCmd, "testing create gslb-rule"); + + Field serviceDomainField = _class.getDeclaredField("serviceDomainName"); + serviceDomainField.setAccessible(true); + serviceDomainField.set(createCmd, "gslb-rule-domain"); + + Field serviceTypeField = _class.getDeclaredField("serviceType"); + serviceTypeField.setAccessible(true); + serviceTypeField.set(createCmd, "invalidtcp"); + + try { + gslbServiceImpl.createGlobalLoadBalancerRule(createCmd); + } catch (InvalidParameterSpecException e) { + Assert.assertTrue(e.getMessage().contains("Invalid service type")); + } + } + + void runCreateGlobalLoadBalancerRuleInvalidDomainName() throws Exception { + + Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + Account account = (Account) new AccountVO("testaccount", 1, + "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + RegionVO region = new RegionVO(); + region.setGslbEnabled(true); + when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region); + + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = createCmd.getClass().getSuperclass(); + + Field regionIdField = _class.getDeclaredField("regionId"); + regionIdField.setAccessible(true); + regionIdField.set(createCmd, new Integer(1)); + + Field algoField = _class.getDeclaredField("algorithm"); + algoField.setAccessible(true); + algoField.set(createCmd, "roundrobin"); + + Field stickyField = _class.getDeclaredField("stickyMethod"); + stickyField.setAccessible(true); + stickyField.set(createCmd, "sourceip"); + + Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName"); + nameField.setAccessible(true); + nameField.set(createCmd, "gslb-rule"); + + Field descriptionField = _class.getDeclaredField("description"); + descriptionField.setAccessible(true); + descriptionField.set(createCmd, "testing create gslb-rule"); + + Field serviceDomainField = _class.getDeclaredField("serviceDomainName"); + serviceDomainField.setAccessible(true); + serviceDomainField.set(createCmd, "gslb-rule-domain"); + List<GlobalLoadBalancerRuleVO> gslbRules = new ArrayList<GlobalLoadBalancerRuleVO>(); + gslbRules.add(new GlobalLoadBalancerRuleVO()); + when(gslbServiceImpl._gslbRuleDao.listByDomainName("gslb-rule-domain")).thenReturn(gslbRules); + + Field serviceTypeField = _class.getDeclaredField("serviceType"); + serviceTypeField.setAccessible(true); + serviceTypeField.set(createCmd, "tcp"); + + try { + gslbServiceImpl.createGlobalLoadBalancerRule(createCmd); + } catch (InvalidParameterSpecException e) { + Assert.assertTrue(e.getMessage().contains("There exist a GSLB rule")); + } + } + + void runAssignToGlobalLoadBalancerRuleTest() throws Exception { + + Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = assignCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(assignCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + LoadBalancerVO lbRule = new LoadBalancerVO(); + lbRule.setState(FirewallRule.State.Active); + Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField.setAccessible(true); + networkIdField.set(lbRule, new Long(1)); + + when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule); + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRules.set(assignCmd, lbRuleIds); + + NetworkVO networkVo = new NetworkVO(); + Field dcID = NetworkVO.class.getDeclaredField("dataCenterId"); + dcID.setAccessible(true); + dcID.set(networkVo, new Long(1)); + when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo); + + try { + gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd); + } catch (Exception e) { + s_logger.info("exception in testing runAssignToGlobalLoadBalancerRuleTest message: " + e.toString()); + } + } + + void runAssignToGlobalLoadBalancerRuleTestSameZoneLb() throws Exception { + + Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = assignCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 3, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(assignCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 3, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + LoadBalancerVO lbRule1 = new LoadBalancerVO(); + lbRule1.setState(FirewallRule.State.Active); + Field networkIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField1.setAccessible(true); + networkIdField1.set(lbRule1, new Long(1)); + Field idField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id"); + idField1.setAccessible(true); + idField1.set(lbRule1, new Long(1)); + + LoadBalancerVO lbRule2 = new LoadBalancerVO(); + lbRule2.setState(FirewallRule.State.Active); + Field networkIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField2.setAccessible(true); + networkIdField2.set(lbRule2, new Long(1)); + Field idField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id"); + idField2.setAccessible(true); + idField2.set(lbRule2, new Long(2)); + + when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule1); + when(gslbServiceImpl._lbDao.findById(new Long(2))).thenReturn(lbRule2); + + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRuleIds.add(new Long(2)); + lbRules.set(assignCmd, lbRuleIds); + + NetworkVO networkVo = new NetworkVO(); + Field dcID = NetworkVO.class.getDeclaredField("dataCenterId"); + dcID.setAccessible(true); + dcID.set(networkVo, new Long(1)); + when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo); + + try { + gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd); + } catch (InvalidParameterValueException e) { + Assert.assertTrue(e.getMessage().contains("Each load balancer rule specified should be in unique zone")); + } + } + + void runAssignToGlobalLoadBalancerRuleTestRevokedState() throws Exception { + + Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTestRevokedState"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = assignCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(assignCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Revoke); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + LoadBalancerVO lbRule = new LoadBalancerVO(); + lbRule.setState(FirewallRule.State.Active); + Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField.setAccessible(true); + networkIdField.set(lbRule, new Long(1)); + + when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule); + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRules.set(assignCmd, lbRuleIds); + + NetworkVO networkVo = new NetworkVO(); + Field dcID = NetworkVO.class.getDeclaredField("dataCenterId"); + dcID.setAccessible(true); + dcID.set(networkVo, new Long(1)); + when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo); + + try { + gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd); + } catch (InvalidParameterValueException e) { + Assert.assertTrue(e.getMessage().contains("revoked state")); + } + } + + void runRemoveFromGlobalLoadBalancerRuleTest() throws Exception { + + Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTest"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd = new RemoveFromGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = removeFromGslbCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(removeFromGslbCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + LoadBalancerVO lbRule = new LoadBalancerVO(); + lbRule.setState(FirewallRule.State.Active); + Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField.setAccessible(true); + networkIdField.set(lbRule, new Long(1)); + Field idField = LoadBalancerVO.class.getSuperclass().getDeclaredField("id"); + idField.setAccessible(true); + idField.set(lbRule, new Long(1)); + + when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule); + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRules.set(removeFromGslbCmd, lbRuleIds); + + NetworkVO networkVo = new NetworkVO(); + Field dcID = NetworkVO.class.getDeclaredField("dataCenterId"); + dcID.setAccessible(true); + dcID.set(networkVo, new Long(1)); + when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo); + + GlobalLoadBalancerLbRuleMapVO gslbLbMap = new GlobalLoadBalancerLbRuleMapVO(1, 1); + List<GlobalLoadBalancerLbRuleMapVO> listSslbLbMap = new ArrayList<GlobalLoadBalancerLbRuleMapVO>(); + listSslbLbMap.add(gslbLbMap); + when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(listSslbLbMap); + + when(gslbServiceImpl._gslbLbMapDao.findByGslbRuleIdAndLbRuleId(new Long(1), new Long(1))).thenReturn(gslbLbMap); + + gslbServiceImpl.removeFromGlobalLoadBalancerRule(removeFromGslbCmd); + } + + void runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb() throws Exception { + + Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd = new RemoveFromGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = removeFromGslbCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(removeFromGslbCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + LoadBalancerVO lbRule = new LoadBalancerVO(); + lbRule.setState(FirewallRule.State.Active); + Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId"); + networkIdField.setAccessible(true); + networkIdField.set(lbRule, new Long(1)); + Field idField = LoadBalancerVO.class.getSuperclass().getDeclaredField("id"); + idField.setAccessible(true); + idField.set(lbRule, new Long(1)); + + when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule); + + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRules.set(removeFromGslbCmd, lbRuleIds); + + NetworkVO networkVo = new NetworkVO(); + Field dcID = NetworkVO.class.getDeclaredField("dataCenterId"); + dcID.setAccessible(true); + dcID.set(networkVo, new Long(1)); + when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo); + + try { + gslbServiceImpl.removeFromGlobalLoadBalancerRule(removeFromGslbCmd); + } catch (InvalidParameterValueException e) { + Assert.assertTrue(e.getMessage().contains("not assigned to global load balancer rule")); + } + } + + void runRemoveFromGlobalLoadBalancerRuleTestInvalidLb() throws Exception { + + Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTestInvalidLb"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd = new RemoveFromGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = removeFromGslbCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(removeFromGslbCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + Field lbRules = _class.getDeclaredField("loadBalancerRulesIds"); + lbRules.setAccessible(true); + List<Long> lbRuleIds = new ArrayList<Long>(); + lbRuleIds.add(new Long(1)); + lbRules.set(removeFromGslbCmd, lbRuleIds); + + try { + gslbServiceImpl.removeFromGlobalLoadBalancerRule(removeFromGslbCmd); + } catch (InvalidParameterValueException e) { + Assert.assertTrue(e.getMessage().contains("load balancer rule ID does not exist")); + } + } + + void runDeleteGlobalLoadBalancerRuleTestWithNoLbRules() throws Exception { + + Transaction txn = Transaction.open("runDeleteGlobalLoadBalancerRuleTestWithNoLbRules"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + DeleteGlobalLoadBalancerRuleCmd deleteCmd = new DeleteGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = deleteCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(deleteCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(null); + + try { + gslbServiceImpl.deleteGlobalLoadBalancerRule(deleteCmd); + Assert.assertTrue(gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke); + } catch (Exception e) { + s_logger.info("exception in testing runDeleteGlobalLoadBalancerRuleTestWithNoLbRules. " + e.toString()); + } + } + + void runDeleteGlobalLoadBalancerRuleTestWithLbRules() throws Exception { + + Transaction txn = Transaction.open("runDeleteGlobalLoadBalancerRuleTestWithLbRules"); + + GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); + + gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class); + gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class); + gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class); + gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class); + gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class); + gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class); + gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class); + gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class); + gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class); + gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class); + + DeleteGlobalLoadBalancerRuleCmd deleteCmd = new DeleteGlobalLoadBalancerRuleCmdExtn(); + Class<?> _class = deleteCmd.getClass().getSuperclass(); + + Account account = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString(), 1); + when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account); + + UserContext.registerContext(1, account, null, true); + + Field gslbRuleId = _class.getDeclaredField("id"); + gslbRuleId.setAccessible(true); + gslbRuleId.set(deleteCmd, new Long(1)); + + GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", + "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active); + when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule); + + + GlobalLoadBalancerLbRuleMapVO gslbLmMap = new GlobalLoadBalancerLbRuleMapVO(1,1); + List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = new ArrayList<GlobalLoadBalancerLbRuleMapVO>(); + gslbLbMapVos.add(gslbLmMap); + when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(gslbLbMapVos); + + try { + gslbServiceImpl.deleteGlobalLoadBalancerRule(deleteCmd); + Assert.assertTrue(gslbRule.getState() == GlobalLoadBalancerRule.State.Revoke); + Assert.assertTrue(gslbLmMap.isRevoke() == true); + } catch (Exception e) { + s_logger.info("exception in testing runDeleteGlobalLoadBalancerRuleTestWithLbRules. " + e.toString()); + } + } + + public class CreateGlobalLoadBalancerRuleCmdExtn extends CreateGlobalLoadBalancerRuleCmd { + public long getEntityOwnerId() { + return 1; + } + } + + public class AssignToGlobalLoadBalancerRuleCmdExtn extends AssignToGlobalLoadBalancerRuleCmd { + public long getEntityOwnerId() { + return 1; + } + } + + public class RemoveFromGlobalLoadBalancerRuleCmdExtn extends RemoveFromGlobalLoadBalancerRuleCmd { + public long getEntityOwnerId() { + return 1; + } + } + + public class DeleteGlobalLoadBalancerRuleCmdExtn extends DeleteGlobalLoadBalancerRuleCmd { + public long getEntityOwnerId() { + return 1; + } + } +}
