use of org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson in project killbill by killbill.
the class TestJaxRsResourceBase method testGetHighestRecordDate.
@Test(groups = "fast")
public void testGetHighestRecordDate() throws Exception {
final UsageResourceTest usageResource = new UsageResourceTest();
final List<UsageRecordJson> fooRecords = ImmutableList.<UsageRecordJson>builder().add(new UsageRecordJson(new LocalDate(2018, 03, 04), 28L)).add(new UsageRecordJson(new LocalDate(2018, 03, 05), 2L)).add(new UsageRecordJson(new LocalDate(2018, 03, 01), 1L)).add(new UsageRecordJson(new LocalDate(2018, 04, 06), 24L)).build();
final UnitUsageRecordJson unitRecordFoo = new UnitUsageRecordJson("foo", fooRecords);
final List<UsageRecordJson> barRecords = ImmutableList.<UsageRecordJson>builder().add(new UsageRecordJson(new LocalDate(2018, 02, 04), 28L)).add(new UsageRecordJson(new LocalDate(2018, 03, 06), 2L)).add(// Highest date point
new UsageRecordJson(new LocalDate(2018, 04, 18), 1L)).add(new UsageRecordJson(new LocalDate(2018, 04, 13), 24L)).build();
final UnitUsageRecordJson unitRecordBar = new UnitUsageRecordJson("bar", barRecords);
final List<UsageRecordJson> zooRecords = ImmutableList.<UsageRecordJson>builder().add(new UsageRecordJson(new LocalDate(2018, 02, 04), 28L)).add(new UsageRecordJson(new LocalDate(2018, 03, 06), 2L)).add(new UsageRecordJson(new LocalDate(2018, 04, 17), 1L)).add(new UsageRecordJson(new LocalDate(2018, 04, 12), 24L)).build();
final UnitUsageRecordJson unitRecordZoo = new UnitUsageRecordJson("zoo", zooRecords);
final List<UnitUsageRecordJson> input = ImmutableList.<UnitUsageRecordJson>builder().add(unitRecordFoo).add(unitRecordBar).add(unitRecordZoo).build();
final LocalDate result = usageResource.getHighestRecordDate(input);
Assert.assertTrue(result.compareTo(new LocalDate(2018, 04, 18)) == 0);
}
use of org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson 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 = 200, message = "Successfully recorded usage data change"), @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.createCallContextNoAccountId(createdBy, reason, comment, request);
// Verify subscription exists..
final Entitlement entitlement = entitlementApi.getEntitlementForId(json.getSubscriptionId(), callContext);
if (entitlement.getEffectiveEndDate() != null) {
final LocalDate highestRecordDate = getHighestRecordDate(json.getUnitUsageRecords());
if (entitlement.getEffectiveEndDate().compareTo(highestRecordDate) < 0) {
return Response.status(Status.BAD_REQUEST).build();
}
}
final SubscriptionUsageRecord record = json.toSubscriptionUsageRecord();
usageUserApi.recordRolledUpUsage(record, callContext);
return Response.status(Status.CREATED).build();
}
use of org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson in project killbill by killbill.
the class TestSubscriptionUsageRecordJson method testJson.
@Test(groups = "fast")
public void testJson() throws Exception {
final LocalDate localDate = new LocalDate();
final UUID subscriptionId = UUID.randomUUID();
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(), 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);
}
Aggregations