ResourceChecker { public void checkIfDataCenterIsUsable(final DataCenter dataCenter, final Account account) { final long dataCenterId = dataCenter.getId(); logger.debug("Checking if data center " + dataCenterId + " is usable."); if (Grouping.AllocationState.Disabled == dataCenter.getAllocationState() && !accountManager.isRootAdmin(account.getId())) { final PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation, data center with specified id is currently disabled"); ex.addProxyObject(dataCenter.getUuid(), "dcId"); throw ex; } logger.debug("Data center " + dataCenterId + "is usable"); } private  ResourceChecker(final Builder builder); static Builder builder(); DataCenterVO checkIfDataCenterExists(final Long dataCenterId); void checkIfDataCenterIsUsable(final DataCenter dataCenter, final Account account); HostPodVO checkIfPodExists(final Long hostPodId); void checkIfPodIsUsable(final DataCenter dataCenter, final HostPodVO hostPod);  }
@Test(expected = PermissionDeniedException.class) public void test_checkIfDataCenterIsUsable_whenDataCenterIsDisabledAndAccountIsNotRoot() throws Exception { final DataCenterVO dataCenter = new DataCenterVO(); dataCenter.setAllocationState(Grouping.AllocationState.Disabled); final AccountVO account = new AccountVO(1L); when(accountManager.isRootAdmin(1L)).thenReturn(false); final ResourceChecker resourceChecker = buildResourceChecker(); resourceChecker.checkIfDataCenterIsUsable(dataCenter, account); }
@Test public void test_checkIfDataCenterIsUsable_whenDataCenterIsDisabledAndAccountIsRoot() throws Exception { final DataCenterVO dataCenter = new DataCenterVO(); dataCenter.setAllocationState(Grouping.AllocationState.Disabled); final AccountVO account = new AccountVO(1L); when(accountManager.isRootAdmin(1L)).thenReturn(true); final ResourceChecker resourceChecker = buildResourceChecker(); try { resourceChecker.checkIfDataCenterIsUsable(dataCenter, account); } catch (final PermissionDeniedException e) { fail("No PermissionDeniedException should have be generated"); } }
@Test public void test_checkIfDataCenterIsUsable_whenDataCenterIsEnabledAndAccountIsNotRoot() throws Exception { final DataCenterVO dataCenter = new DataCenterVO(); dataCenter.setAllocationState(Grouping.AllocationState.Enabled); final AccountVO account = new AccountVO(1L); when(accountManager.isRootAdmin(1L)).thenReturn(false); final ResourceChecker resourceChecker = buildResourceChecker(); try { resourceChecker.checkIfDataCenterIsUsable(dataCenter, account); } catch (final PermissionDeniedException e) { fail("No PermissionDeniedException should have be generated"); } }
@Test public void test_checkIfDataCenterIsUsable_whenDataCenterIsEnabledAndAccountIsRoot() throws Exception { final DataCenterVO dataCenter = new DataCenterVO(); dataCenter.setAllocationState(Grouping.AllocationState.Enabled); final AccountVO account = new AccountVO(1L); when(accountManager.isRootAdmin(1L)).thenReturn(true); final ResourceChecker resourceChecker = buildResourceChecker(); try { resourceChecker.checkIfDataCenterIsUsable(dataCenter, account); } catch (final PermissionDeniedException e) { fail("No PermissionDeniedException should have be generated"); } }