Search in sources :

Example 1 with QuotaCreditsResponse

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);
}
Also used : Account(com.cloud.user.Account) ServerApiException(org.apache.cloudstack.api.ServerApiException) QuotaCreditsResponse(org.apache.cloudstack.api.response.QuotaCreditsResponse)

Example 2 with QuotaCreditsResponse

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());
}
Also used : Field(java.lang.reflect.Field) ServerApiException(org.apache.cloudstack.api.ServerApiException) AccountVO(com.cloud.user.AccountVO) QuotaCreditsResponse(org.apache.cloudstack.api.response.QuotaCreditsResponse) Test(org.junit.Test)

Aggregations

ServerApiException (org.apache.cloudstack.api.ServerApiException)2 QuotaCreditsResponse (org.apache.cloudstack.api.response.QuotaCreditsResponse)2 Account (com.cloud.user.Account)1 AccountVO (com.cloud.user.AccountVO)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1