use of org.apache.cloudstack.api.response.QuotaCreditsResponse in project cloudstack by apache.
the class QuotaCreditsCmd method execute.
@Override
public void execute() {
Long accountId = null;
Account account = _accountService.getActiveAccountByName(accountName, domainId);
if (account != null) {
accountId = account.getAccountId();
}
if (accountId == null) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "The account does not exists or has been removed/disabled");
}
if (getValue() == null) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Please send a valid non-empty quota value");
}
if (getQuotaEnforce() != null) {
_quotaService.setLockAccount(accountId, getQuotaEnforce());
}
if (getMinBalance() != null) {
_quotaService.setMinBalance(accountId, getMinBalance());
} else {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Please set a value for min balance");
}
final QuotaCreditsResponse response = _responseBuilder.addQuotaCredits(accountId, getDomainId(), getValue(), CallContext.current().getCallingUserId(), getQuotaEnforce());
response.setResponseName(getCommandName());
response.setObjectName("quotacredits");
setResponseObject(response);
}
use of org.apache.cloudstack.api.response.QuotaCreditsResponse in project cloudstack by apache.
the class QuotaCreditsCmdTest method testQuotaCreditsCmd.
@Test
public void testQuotaCreditsCmd() throws NoSuchFieldException, IllegalAccessException {
QuotaCreditsCmd cmd = new QuotaCreditsCmd();
cmd.setAccountName("admin");
cmd.setMinBalance(200.0);
Field rbField = QuotaCreditsCmd.class.getDeclaredField("_responseBuilder");
rbField.setAccessible(true);
rbField.set(cmd, responseBuilder);
Field qsbField = QuotaCreditsCmd.class.getDeclaredField("_quotaService");
qsbField.setAccessible(true);
qsbField.set(cmd, quotaService);
Field asField = BaseCmd.class.getDeclaredField("_accountService");
asField.setAccessible(true);
asField.set(cmd, accountService);
AccountVO acc = new AccountVO();
acc.setId(2L);
Mockito.when(accountService.getActiveAccountByName(Mockito.anyString(), Mockito.anyLong())).thenReturn(acc);
Mockito.when(responseBuilder.addQuotaCredits(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyDouble(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(new QuotaCreditsResponse());
// No value provided test
try {
cmd.execute();
} catch (ServerApiException e) {
assertTrue(e.getErrorCode().equals(ApiErrorCode.PARAM_ERROR));
}
// With value provided test
cmd.setValue(11.80);
cmd.execute();
Mockito.verify(quotaService, Mockito.times(0)).setLockAccount(Mockito.anyLong(), Mockito.anyBoolean());
Mockito.verify(quotaService, Mockito.times(1)).setMinBalance(Mockito.anyLong(), Mockito.anyDouble());
Mockito.verify(responseBuilder, Mockito.times(1)).addQuotaCredits(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyDouble(), Mockito.anyLong(), Mockito.anyBoolean());
}
Aggregations