Updated Branches: refs/heads/vmware-datamodel b77facffa -> dc4aef9b7
Added tests for removal of VMware dc from cloudstack zone. Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/dc4aef9b Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/dc4aef9b Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/dc4aef9b Branch: refs/heads/vmware-datamodel Commit: dc4aef9b7eceb1e3b7c8ca4dc94b8f8a20fee361 Parents: b77facf Author: Sateesh Chodapuneedi <[email protected]> Authored: Wed May 22 13:03:31 2013 +0530 Committer: Sateesh Chodapuneedi <[email protected]> Committed: Wed May 22 13:03:31 2013 +0530 ---------------------------------------------------------------------- .../vmware/manager/VmwareManagerImpl.java | 1 - .../vmware/VmwareDatacenterApiUnitTest.java | 80 ++++++++++----- 2 files changed, 52 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/dc4aef9b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java index 21c6cda..4462037 100755 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java @@ -1102,7 +1102,6 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw if (zone == null) { InvalidParameterValueException ex = new InvalidParameterValueException( "Can't find zone by the id specified"); - ex.addProxyObject(zone, zoneId, "dcId"); throw ex; } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/dc4aef9b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java index 6ff4e47..ff98556 100644 --- a/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java +++ b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -103,6 +104,9 @@ public class VmwareDatacenterApiUnitTest { @Inject ClusterDetailsDao _clusterDetailsDao; + @Inject + ConfigurationDao _configDao; + @Inject AccountDao _accountDao; @@ -110,6 +114,7 @@ public class VmwareDatacenterApiUnitTest { AccountManager _acctMgr; long zoneId; + long podId; long clusterId; long vmwareDcId; private static long domainId = 5L; @@ -120,8 +125,7 @@ public class VmwareDatacenterApiUnitTest { private static String user = "administrator"; private static String password = "password"; private static String guid = vmwareDcName + "@" + vCenterHost; - private static AddVmwareDcCmd addCmd; - private static RemoveVmwareDcCmd removeCmd; + private static VmwareDatacenterVO dc; private static List<VmwareDatacenterVO> vmwareDcs; private static ClusterVO cluster; @@ -129,21 +133,27 @@ public class VmwareDatacenterApiUnitTest { private static List<ClusterVO> clusterList; private static ClusterDetailsVO clusterDetails; + @Mock + private static AddVmwareDcCmd addCmd; + @Mock + private static RemoveVmwareDcCmd removeCmd; + @BeforeClass public static void setUp() throws ConfigurationException { } @Before public void testSetUp() { + Mockito.when(_configDao.isPremium()).thenReturn(true); ComponentContext.initComponentsLifeCycle(); + MockitoAnnotations.initMocks(this); DataCenterVO zone = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Basic, null, null, true, true, null, null); - zone = _dcDao.persist(zone); - zoneId = zone.getId(); + zoneId = 1L; HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), zoneId, "192.168.56.1", "192.168.56.0/24", 8, "test"); - pod = _podDao.persist(pod); + podId = 1L; AccountVO acct = new AccountVO(200L); acct.setType(Account.ACCOUNT_TYPE_ADMIN); @@ -154,6 +164,7 @@ public class VmwareDatacenterApiUnitTest { when(_accountDao.findByIdIncludingRemoved(0L)).thenReturn(acct); dc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, user, password); + vmwareDcs = new ArrayList<VmwareDatacenterVO>(); vmwareDcs.add(dc); vmwareDcId = dc.getId(); @@ -161,8 +172,7 @@ public class VmwareDatacenterApiUnitTest { cluster.setHypervisorType(HypervisorType.VMware.toString()); cluster.setClusterType(ClusterType.ExternalManaged); cluster.setManagedState(ManagedState.Managed); - cluster = _clusterDao.persist(cluster); - clusterId = cluster.getId(); + clusterId = 1L; clusterList = new ArrayList<ClusterVO>(); clusterList.add(cluster); @@ -170,11 +180,6 @@ public class VmwareDatacenterApiUnitTest { dcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDcId); - addCmd = new AddVmwareDcCmd(); - removeCmd = new RemoveVmwareDcCmd(); - addCmd._vmwareDatacenterService = _vmwareDatacenterService; - removeCmd._vmwareDatacenterService = _vmwareDatacenterService; - Mockito.when(_dcDao.persist(Mockito.any(DataCenterVO.class))).thenReturn(zone); Mockito.when(_dcDao.findById(1L)).thenReturn(zone); Mockito.when(_podDao.persist(Mockito.any(HostPodVO.class))).thenReturn(pod); @@ -197,62 +202,73 @@ public class VmwareDatacenterApiUnitTest { Mockito.when(addCmd.getUsername()).thenReturn(user); Mockito.when(addCmd.getPassword()).thenReturn(password); Mockito.when(addCmd.getName()).thenReturn(vmwareDcName); + Mockito.when(removeCmd.getZoneId()).thenReturn(1L); } @Test(expected = InvalidParameterValueException.class) public void testAddVmwareDcToInvalidZone() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(addCmd.getZoneId()).thenReturn(2L); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone as no zone exists with specified id.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = ResourceInUseException.class) public void testAddVmwareDcToZoneWithClusters() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(_clusterDao.listByZoneId(1L)).thenReturn(clusterList); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' as the zone already has some clusters of type VMware.", dc); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); + } + + @Test(expected = InvalidParameterValueException.class) + public void testRemoveVmwareDcToInvalidZone() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { + Mockito.when(removeCmd.getZoneId()).thenReturn(2L); + _vmwareDatacenterService.removeVmwareDatacenter(removeCmd); + } + + @Test(expected = ResourceInUseException.class) + public void testRemoveVmwareDcToZoneWithClusters() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { + Mockito.when(_clusterDao.listByZoneId(1L)).thenReturn(clusterList); + _vmwareDatacenterService.removeVmwareDatacenter(removeCmd); } @Test(expected = ResourceInUseException.class) public void testAddVmwareDcToZoneWithVmwareDc() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(_vmwareDcDao.getVmwareDatacenterByNameAndVcenter(vmwareDcName, vCenterHost)).thenReturn(vmwareDcs); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' as the zone already has some clusters of type VMware.", dc); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = InvalidParameterValueException.class) public void testAddVmwareDcWithNullUser() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(addCmd.getUsername()).thenReturn(null); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone as username is not specified.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = InvalidParameterValueException.class) public void testAddVmwareDcWithNullPassword() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(addCmd.getPassword()).thenReturn(null); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone as password is not specified.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = InvalidParameterValueException.class) public void testAddVmwareDcWithNullUrl() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(addCmd.getUrl()).thenReturn(null); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone as vcenter url is not specified.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = InvalidParameterValueException.class) public void testAddVmwareDcWithNullDcName() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(addCmd.getName()).thenReturn(null); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone as name of datacenter is not specified.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); } @Test(expected = CloudRuntimeException.class) public void testReAddVmwareDc() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(dcZoneMap); - VmwareDatacenter dc = _vmwareDatacenterService.addVmwareDatacenter(addCmd); - assertNotNull("Failed to add Vmware datacenter 'dc' to zone.", dc.getVmwareDatacenterName()); + _vmwareDatacenterService.addVmwareDatacenter(addCmd); + } + + @Test(expected = CloudRuntimeException.class) + public void testRemoveNonexistingVmwareDc() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception { + Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(null); + _vmwareDatacenterService.removeVmwareDatacenter(removeCmd); } @Configuration @@ -369,6 +385,14 @@ public class VmwareDatacenterApiUnitTest { return Mockito.mock(UserVmDao.class); } + public AddVmwareDcCmd addVmwareDatacenterCmd() { + return Mockito.mock(AddVmwareDcCmd.class); + } + + public RemoveVmwareDcCmd removeVmwareDcCmd() { + return Mockito.mock(RemoveVmwareDcCmd.class); + } + public static class Library implements TypeFilter { @Override
