Search in sources :

Example 1 with SubscriptionUsageRecord

use of org.killbill.billing.usage.api.SubscriptionUsageRecord in project killbill by killbill.

the class TestConsumableInArrear method setUsage.

private void setUsage(final UUID subscriptionId, final String unitType, final LocalDate startDate, final Long amount, final CallContext context) throws UsageApiException {
    final List<UsageRecord> usageRecords = new ArrayList<UsageRecord>();
    usageRecords.add(new UsageRecord(startDate, amount));
    final List<UnitUsageRecord> unitUsageRecords = new ArrayList<UnitUsageRecord>();
    unitUsageRecords.add(new UnitUsageRecord(unitType, usageRecords));
    final SubscriptionUsageRecord record = new SubscriptionUsageRecord(subscriptionId, UUID.randomUUID().toString(), unitUsageRecords);
    usageUserApi.recordRolledUpUsage(record, context);
}
Also used : UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) UsageRecord(org.killbill.billing.usage.api.UsageRecord) ArrayList(java.util.ArrayList) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord)

Example 2 with SubscriptionUsageRecord

use of org.killbill.billing.usage.api.SubscriptionUsageRecord in project killbill by killbill.

the class DefaultUsageUserApi method recordRolledUpUsage.

@Override
public void recordRolledUpUsage(final SubscriptionUsageRecord record, final CallContext callContext) throws UsageApiException {
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(record.getSubscriptionId(), ObjectType.SUBSCRIPTION, callContext);
    // check if we have (at least) one row with the supplied tracking id
    if (!Strings.isNullOrEmpty(record.getTrackingId()) && recordsWithTrackingIdExist(record, internalCallContext)) {
        throw new UsageApiException(ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS, record.getTrackingId());
    }
    final List<RolledUpUsageModelDao> usages = new ArrayList<RolledUpUsageModelDao>();
    for (final UnitUsageRecord unitUsageRecord : record.getUnitUsageRecord()) {
        for (final UsageRecord usageRecord : unitUsageRecord.getDailyAmount()) {
            usages.add(new RolledUpUsageModelDao(record.getSubscriptionId(), unitUsageRecord.getUnitType(), usageRecord.getDate(), usageRecord.getAmount(), record.getTrackingId()));
        }
    }
    rolledUpUsageDao.record(usages, internalCallContext);
}
Also used : UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) UsageRecord(org.killbill.billing.usage.api.UsageRecord) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao) UsageApiException(org.killbill.billing.usage.api.UsageApiException)

Example 3 with SubscriptionUsageRecord

use of org.killbill.billing.usage.api.SubscriptionUsageRecord in project killbill by killbill.

the class UsageResource method recordUsage.

@TimedResource
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Record usage for a subscription")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid subscription (e.g. inactive)") })
public Response recordUsage(final SubscriptionUsageRecordJson json, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws EntitlementApiException, AccountApiException, UsageApiException {
    verifyNonNullOrEmpty(json, "SubscriptionUsageRecordJson body should be specified");
    verifyNonNullOrEmpty(json.getSubscriptionId(), "SubscriptionUsageRecordJson subscriptionId needs to be set", json.getUnitUsageRecords(), "SubscriptionUsageRecordJson unitUsageRecords needs to be set");
    Preconditions.checkArgument(!json.getUnitUsageRecords().isEmpty());
    for (final UnitUsageRecordJson unitUsageRecordJson : json.getUnitUsageRecords()) {
        verifyNonNullOrEmpty(unitUsageRecordJson.getUnitType(), "UnitUsageRecordJson unitType need to be set");
        Preconditions.checkArgument(Iterables.size(unitUsageRecordJson.getUsageRecords()) > 0, "UnitUsageRecordJson usageRecords must have at least one element.");
        for (final UsageRecordJson usageRecordJson : unitUsageRecordJson.getUsageRecords()) {
            verifyNonNull(usageRecordJson.getAmount(), "UsageRecordJson amount needs to be set");
            verifyNonNull(usageRecordJson.getRecordDate(), "UsageRecordJson recordDate needs to be set");
        }
    }
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    // Verify subscription exists..
    final Entitlement entitlement = entitlementApi.getEntitlementForId(UUID.fromString(json.getSubscriptionId()), callContext);
    if (entitlement.getState() != EntitlementState.ACTIVE) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    final SubscriptionUsageRecord record = json.toSubscriptionUsageRecord();
    usageUserApi.recordRolledUpUsage(record, callContext);
    return Response.status(Status.CREATED).build();
}
Also used : UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) UsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UsageRecordJson) SubscriptionUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson) Entitlement(org.killbill.billing.entitlement.api.Entitlement) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) CallContext(org.killbill.billing.util.callcontext.CallContext) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with SubscriptionUsageRecord

use of org.killbill.billing.usage.api.SubscriptionUsageRecord in project killbill by killbill.

the class TestSubscriptionUsageRecordJson method testJson.

@Test(groups = "fast")
public void testJson() throws Exception {
    final LocalDate localDate = new LocalDate();
    final String subscriptionId = UUID.randomUUID().toString();
    final String trackingId = UUID.randomUUID().toString();
    final List<UnitUsageRecordJson> unitUsageRecords = new ArrayList<UnitUsageRecordJson>();
    final List<UsageRecordJson> usageRecords = new ArrayList<UsageRecordJson>();
    final UsageRecordJson usageRecordJson = new UsageRecordJson(localDate, 5L);
    usageRecords.add(usageRecordJson);
    final UnitUsageRecordJson unitUsageRecordJson = new UnitUsageRecordJson("foo", usageRecords);
    unitUsageRecords.add(unitUsageRecordJson);
    final SubscriptionUsageRecordJson subscriptionUsageRecordJson = new SubscriptionUsageRecordJson(subscriptionId, trackingId, unitUsageRecords);
    Assert.assertEquals(subscriptionUsageRecordJson.getSubscriptionId(), subscriptionId);
    Assert.assertEquals(subscriptionUsageRecordJson.getTrackingId(), trackingId);
    Assert.assertEquals(subscriptionUsageRecordJson.getUnitUsageRecords().size(), 1);
    Assert.assertEquals(subscriptionUsageRecordJson.getUnitUsageRecords().get(0).getUnitType(), "foo");
    Assert.assertEquals(subscriptionUsageRecordJson.getUnitUsageRecords().get(0).getUsageRecords().size(), 1);
    Assert.assertEquals(subscriptionUsageRecordJson.getUnitUsageRecords().get(0).getUsageRecords().get(0).getAmount(), new Long(5L));
    Assert.assertEquals(subscriptionUsageRecordJson.getUnitUsageRecords().get(0).getUsageRecords().get(0).getRecordDate(), localDate);
    final SubscriptionUsageRecord subscriptionUsageRecord = subscriptionUsageRecordJson.toSubscriptionUsageRecord();
    Assert.assertEquals(subscriptionUsageRecord.getSubscriptionId().toString(), subscriptionId);
    Assert.assertEquals(subscriptionUsageRecord.getTrackingId(), trackingId);
    Assert.assertEquals(subscriptionUsageRecord.getUnitUsageRecord().size(), 1);
    Assert.assertEquals(subscriptionUsageRecord.getUnitUsageRecord().get(0).getUnitType(), "foo");
    Assert.assertEquals(subscriptionUsageRecord.getUnitUsageRecord().get(0).getDailyAmount().size(), 1);
    Assert.assertEquals(subscriptionUsageRecord.getUnitUsageRecord().get(0).getDailyAmount().get(0).getAmount(), new Long(5L));
    Assert.assertEquals(subscriptionUsageRecord.getUnitUsageRecord().get(0).getDailyAmount().get(0).getDate(), localDate);
}
Also used : UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) ArrayList(java.util.ArrayList) UsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UsageRecordJson) UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 5 with SubscriptionUsageRecord

use of org.killbill.billing.usage.api.SubscriptionUsageRecord in project killbill by killbill.

the class SubscriptionUsageRecordJson method toSubscriptionUsageRecord.

public SubscriptionUsageRecord toSubscriptionUsageRecord() {
    final List<UnitUsageRecord> tmp = ImmutableList.copyOf(Iterables.transform(unitUsageRecords, new Function<UnitUsageRecordJson, UnitUsageRecord>() {

        @Override
        public UnitUsageRecord apply(final UnitUsageRecordJson input) {
            return input.toUnitUsageRecord();
        }
    }));
    final SubscriptionUsageRecord result = new SubscriptionUsageRecord(UUID.fromString(subscriptionId), trackingId, tmp);
    return result;
}
Also used : Function(com.google.common.base.Function) UnitUsageRecord(org.killbill.billing.usage.api.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord)

Aggregations

SubscriptionUsageRecord (org.killbill.billing.usage.api.SubscriptionUsageRecord)5 ArrayList (java.util.ArrayList)3 UnitUsageRecord (org.killbill.billing.usage.api.UnitUsageRecord)3 UnitUsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson)2 UsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UsageRecordJson)2 UsageRecord (org.killbill.billing.usage.api.UsageRecord)2 Function (com.google.common.base.Function)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 LocalDate (org.joda.time.LocalDate)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 Entitlement (org.killbill.billing.entitlement.api.Entitlement)1 SubscriptionUsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson)1 UsageApiException (org.killbill.billing.usage.api.UsageApiException)1 RolledUpUsageModelDao (org.killbill.billing.usage.dao.RolledUpUsageModelDao)1 CallContext (org.killbill.billing.util.callcontext.CallContext)1 TimedResource (org.killbill.commons.metrics.TimedResource)1