Search in sources :

Example 6 with QuotaTariffVO

use of org.apache.cloudstack.quota.vo.QuotaTariffVO in project cloudstack by apache.

the class QuotaManagerImplTest method testUpdateQuotaRecords.

@Test
public void testUpdateQuotaRecords() {
    UsageVO usageVO = new UsageVO();
    usageVO.setId(100L);
    usageVO.setQuotaCalculated(0);
    usageVO.setUsageType(UsageTypes.NETWORK_BYTES_SENT);
    usageVO.setRawUsage(9000000000.0);
    usageVO.setSize(1010101010L);
    QuotaTariffVO tariffVO = new QuotaTariffVO();
    tariffVO.setCurrencyValue(new BigDecimal(1));
    Mockito.when(quotaTariffDao.findTariffPlanByUsageType(Mockito.anyInt(), Mockito.any(Date.class))).thenReturn(tariffVO);
    QuotaUsageVO qu = quotaManager.updateQuotaNetwork(usageVO, UsageTypes.NETWORK_BYTES_SENT);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaAllocatedVMUsage(usageVO, new BigDecimal(0.5));
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaDiskUsage(usageVO, new BigDecimal(0.5), UsageTypes.VOLUME);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    qu = quotaManager.updateQuotaRaw(usageVO, new BigDecimal(0.5), UsageTypes.VPN_USERS);
    assertTrue(qu.getQuotaUsed().compareTo(BigDecimal.ZERO) > 0);
    Mockito.verify(quotaUsageDao, Mockito.times(4)).persistQuotaUsage(Mockito.any(QuotaUsageVO.class));
    Mockito.verify(usageDao, Mockito.times(4)).persistUsage(Mockito.any(UsageVO.class));
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) UsageVO(com.cloud.usage.UsageVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) QuotaUsageVO(org.apache.cloudstack.quota.vo.QuotaUsageVO) Test(org.junit.Test)

Example 7 with QuotaTariffVO

use of org.apache.cloudstack.quota.vo.QuotaTariffVO in project cloudstack by apache.

the class QuotaResponseBuilderImpl method updateQuotaTariffPlan.

@Override
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
    final int quotaType = cmd.getUsageType();
    final BigDecimal quotaCost = new BigDecimal(cmd.getValue());
    final Date effectiveDate = _quotaService.computeAdjustedTime(cmd.getStartDate());
    final Date now = _quotaService.computeAdjustedTime(new Date());
    // if effective date is in the past return error
    if (effectiveDate.compareTo(now) < 0) {
        throw new InvalidParameterValueException("Incorrect effective date for tariff " + effectiveDate + " is less than now " + now);
    }
    QuotaTypes quotaConstant = QuotaTypes.listQuotaTypes().get(quotaType);
    if (quotaConstant == null) {
        throw new InvalidParameterValueException("Quota type does not exists " + quotaType);
    }
    QuotaTariffVO result = null;
    result = new QuotaTariffVO(quotaType);
    result.setUsageName(quotaConstant.getQuotaName());
    result.setUsageUnit(quotaConstant.getQuotaUnit());
    result.setUsageDiscriminator(quotaConstant.getDiscriminator());
    result.setCurrencyValue(quotaCost);
    result.setEffectiveOn(effectiveDate);
    result.setUpdatedOn(now);
    result.setUpdatedBy(cmd.getEntityOwnerId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(String.format("Updating Quota Tariff Plan: New value=%s for resource type=%d effective on date=%s", quotaCost, quotaType, effectiveDate));
    }
    _quotaTariffDao.addQuotaTariff(result);
    return result;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaTypes(org.apache.cloudstack.quota.constant.QuotaTypes) QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 8 with QuotaTariffVO

use of org.apache.cloudstack.quota.vo.QuotaTariffVO in project cloudstack by apache.

the class QuotaResponseBuilderImpl method listQuotaTariffPlans.

@Override
public List<QuotaTariffVO> listQuotaTariffPlans(final QuotaTariffListCmd cmd) {
    List<QuotaTariffVO> result = new ArrayList<QuotaTariffVO>();
    Date effectiveDate = cmd.getEffectiveDate() == null ? new Date() : cmd.getEffectiveDate();
    Date adjustedEffectiveDate = _quotaService.computeAdjustedTime(effectiveDate);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Effective datec=" + effectiveDate + " quotatype=" + cmd.getUsageType() + " Adjusted date=" + adjustedEffectiveDate);
    }
    if (cmd.getUsageType() != null) {
        QuotaTariffVO tariffPlan = _quotaTariffDao.findTariffPlanByUsageType(cmd.getUsageType(), adjustedEffectiveDate);
        if (tariffPlan != null) {
            result.add(tariffPlan);
        }
    } else {
        result = _quotaTariffDao.listAllTariffPlans(adjustedEffectiveDate);
    }
    return result;
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 9 with QuotaTariffVO

use of org.apache.cloudstack.quota.vo.QuotaTariffVO in project cloudstack by apache.

the class QuotaTariffUpdateCmdTest method testQuotaTariffUpdateCmd.

@Test
public void testQuotaTariffUpdateCmd() throws NoSuchFieldException, IllegalAccessException {
    QuotaTariffUpdateCmd cmd = new QuotaTariffUpdateCmd();
    Field rbField = QuotaTariffUpdateCmd.class.getDeclaredField("_responseBuilder");
    rbField.setAccessible(true);
    rbField.set(cmd, responseBuilder);
    QuotaTariffVO tariff = new QuotaTariffVO();
    tariff.setEffectiveOn(new Date());
    tariff.setCurrencyValue(new BigDecimal(100));
    tariff.setUsageType(QuotaTypes.MEMORY);
    Mockito.when(responseBuilder.updateQuotaTariffPlan(Mockito.eq(cmd))).thenReturn(null);
    try {
        cmd.execute();
    } catch (ServerApiException e) {
        assertTrue(e.getErrorCode().equals(ApiErrorCode.INTERNAL_ERROR));
    }
    Mockito.when(responseBuilder.updateQuotaTariffPlan(Mockito.eq(cmd))).thenReturn(tariff);
    Mockito.when(responseBuilder.createQuotaTariffResponse(Mockito.eq(tariff))).thenReturn(new QuotaTariffResponse());
    cmd.execute();
    Mockito.verify(responseBuilder, Mockito.times(1)).createQuotaTariffResponse(Mockito.eq(tariff));
}
Also used : Field(java.lang.reflect.Field) ServerApiException(org.apache.cloudstack.api.ServerApiException) QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) QuotaTariffResponse(org.apache.cloudstack.api.response.QuotaTariffResponse) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 10 with QuotaTariffVO

use of org.apache.cloudstack.quota.vo.QuotaTariffVO in project cloudstack by apache.

the class QuotaResponseBuilderImplTest method makeTariffTestData.

private QuotaTariffVO makeTariffTestData() {
    QuotaTariffVO tariffVO = new QuotaTariffVO();
    tariffVO.setUsageType(QuotaTypes.IP_ADDRESS);
    tariffVO.setUsageName("ip address");
    tariffVO.setUsageUnit("IP-Month");
    tariffVO.setCurrencyValue(BigDecimal.valueOf(100.19));
    tariffVO.setEffectiveOn(new Date());
    tariffVO.setUsageDiscriminator("");
    return tariffVO;
}
Also used : QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) Date(java.util.Date)

Aggregations

QuotaTariffVO (org.apache.cloudstack.quota.vo.QuotaTariffVO)14 BigDecimal (java.math.BigDecimal)9 Date (java.util.Date)6 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)6 ArrayList (java.util.ArrayList)4 QuotaTariffResponse (org.apache.cloudstack.api.response.QuotaTariffResponse)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)2 ServerApiException (org.apache.cloudstack.api.ServerApiException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 UsageVO (com.cloud.usage.UsageVO)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 QuotaTypes (org.apache.cloudstack.quota.constant.QuotaTypes)1 ServiceOfferingVO (org.apache.cloudstack.quota.vo.ServiceOfferingVO)1