public BundleJson(final SubscriptionBundle bundle, @Nullable final Currency currency, @Nullable final AccountAuditLogs accountAuditLogs) throws CatalogApiException { super(toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForBundle(bundle.getId()))); this.accountId = bundle.getAccountId(); this.bundleId = bundle.getId(); this.externalKey = bundle.getExternalKey(); this.subscriptions = new LinkedList<SubscriptionJson>(); for (final Subscription subscription : bundle.getSubscriptions()) { this.subscriptions.add(new SubscriptionJson(subscription, currency, accountAuditLogs)); } this.timeline = new BundleTimelineJson(bundle.getTimeline(), accountAuditLogs); }
private String getBundleExternalKey(final Invoice invoice, final List<SubscriptionBundle> bundles) { final Set<UUID> b = new HashSet<UUID>(); for (final InvoiceItem cur : invoice.getInvoiceItems()) { b.add(cur.getBundleId()); } boolean first = true; final StringBuilder tmp = new StringBuilder(); for (final UUID cur : b) { for (final SubscriptionBundle bt : bundles) { if (bt.getId().equals(cur)) { if (!first) { tmp.append(","); } tmp.append(bt.getExternalKey()); first = false; break; } } } return tmp.toString(); } }
private void subscriptionBundleChecker(final List<SubscriptionBundle> bundles, final LocalDate billingStartDate, final Entitlement entitlement, final int idx) { Assert.assertEquals(bundles.get(idx).getId(), entitlement.getBundleId()); Assert.assertEquals(bundles.get(idx).getSubscriptions().size(), 1); Assert.assertEquals(bundles.get(idx).getSubscriptions().get(0).getId(), entitlement.getId()); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().size(), 3); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(0).getEffectiveDate(), entitlement.getEffectiveStartDate()); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(1).getEffectiveDate(), billingStartDate); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getEffectiveDate(), new LocalDate(2013, 9, 6)); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getSubscriptionEventType(), SubscriptionEventType.PHASE); }
assertNotEquals(initialBundle.getId(), newBundle.getId()); assertEquals(initialBundle.getAccountId(), newBundle.getAccountId()); assertEquals(initialBundle.getExternalKey(), newBundle.getExternalKey()); assertNotEquals(initialBundle.getId(), newerBundle.getId()); assertEquals(initialBundle.getAccountId(), newerBundle.getAccountId()); assertEquals(initialBundle.getExternalKey(), newerBundle.getExternalKey());
assertEquals(activeBundle.getId(), entitlement.getBundleId()); assertEquals(firstbundle.getSubscriptions().size(), 1); assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 7)); assertEquals(firstbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 7)); assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 10)); assertEquals(firstbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 10)); assertEquals(secondbundle.getSubscriptions().size(), 1); assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 11)); assertEquals(secondbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 11)); assertNull(secondbundle.getSubscriptions().get(0).getEffectiveEndDate()); assertNull(secondbundle.getSubscriptions().get(0).getBillingEndDate()); assertEquals(secondbundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0); assertEquals(firstbundle.getSubscriptions().size(), 1); assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 7)); assertEquals(firstbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 7)); assertEquals(firstbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 10)); assertEquals(firstbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 10)); assertEquals(secondbundle.getSubscriptions().size(), 1); assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveStartDate(), new LocalDate(2013, 8, 11)); assertEquals(secondbundle.getSubscriptions().get(0).getBillingStartDate(), new LocalDate(2013, 8, 11)); assertEquals(secondbundle.getSubscriptions().get(0).getEffectiveEndDate(), new LocalDate(2013, 8, 14)); assertEquals(secondbundle.getSubscriptions().get(0).getBillingEndDate(), new LocalDate(2013, 8, 14)); assertEquals(secondbundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0); assertEquals(thirdBundle.getSubscriptions().size(), 1);
@Override public BundleJson apply(final SubscriptionBundle bundle) { // Cache audit logs per account if (accountsAuditLogs.get().get(bundle.getAccountId()) == null) { accountsAuditLogs.get().put(bundle.getAccountId(), auditUserApi.getAccountAuditLogs(bundle.getAccountId(), auditMode.getLevel(), tenantContext)); } try { return new BundleJson(bundle, null, accountsAuditLogs.get().get(bundle.getAccountId())); } catch (final CatalogApiException unused) { // Does not happen because we pass a null Currency throw new RuntimeException(unused); } } },
private List<AuditLog> getAuditLogsForBundle(final UUID bundleId, final TenantContext context) { try { final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(bundleId, context); return auditUserApi.getAccountAuditLogs(bundle.getAccountId(), AuditLevel.FULL, context).getAuditLogsForBundle(bundle.getId()); } catch (final SubscriptionApiException e) { Assert.fail(e.toString()); return null; } }
@TimedResource @POST @Path("/{bundleId:" + UUID_PATTERN + "}") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Transfer a bundle to another account", response = BundleJson.class) @ApiResponses(value = {@ApiResponse(code = 201, message = "Bundle transferred successfully"), @ApiResponse(code = 400, message = "Invalid bundle id, requested date or policy supplied"), @ApiResponse(code = 404, message = "Bundle not found")}) public Response transferBundle(@PathParam(ID_PARAM_NAME) final UUID bundleId, final BundleJson json, @QueryParam(QUERY_REQUESTED_DT) final String requestedDate, @QueryParam(QUERY_BILLING_POLICY) @DefaultValue("END_OF_TERM") final BillingActionPolicy billingPolicy, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @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 UriInfo uriInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws EntitlementApiException, SubscriptionApiException, AccountApiException { verifyNonNullOrEmpty(json, "BundleJson body should be specified"); verifyNonNullOrEmpty(json.getAccountId(), "BundleJson accountId needs to be set"); final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request); final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(bundleId, callContext); final LocalDate inputLocalDate = toLocalDate(requestedDate); final UUID newBundleId = entitlementApi.transferEntitlementsOverrideBillingPolicy(bundle.getAccountId(), json.getAccountId(), bundle.getExternalKey(), inputLocalDate, billingPolicy, pluginProperties, callContext); return uriBuilder.buildResponse(uriInfo, BundleResource.class, "getBundle", newBundleId, request); }
private List<SubscriptionBundle> filterBundles(final List<SubscriptionBundle> subscriptionBundlesForAccountId, final List<String> bundlesFilter) { List<SubscriptionBundle> result = new ArrayList<SubscriptionBundle>(); for (SubscriptionBundle subscriptionBundle : subscriptionBundlesForAccountId) { if (bundlesFilter.contains(subscriptionBundle.getId().toString())) { result.add(subscriptionBundle); } } return result; }
@Override public List<SubscriptionEvent> apply(final SubscriptionBundle bundle) { return bundle.getTimeline().getSubscriptionEvents(); } }
@Override public List<Subscription> apply(final SubscriptionBundle input) { return input.getSubscriptions(); } }));
private void subscriptionBundleChecker(final List<SubscriptionBundle> bundles, final LocalDate initialDate, final Entitlement entitlement, final int idx) { Assert.assertEquals(bundles.get(idx).getId(), entitlement.getBundleId()); Assert.assertEquals(bundles.get(idx).getSubscriptions().size(), 1); Assert.assertEquals(bundles.get(idx).getSubscriptions().get(0).getId(), entitlement.getId()); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().size(), 4); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(0).getEffectiveDate(), initialDate); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(0).getSubscriptionEventType(), SubscriptionEventType.START_ENTITLEMENT); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(1).getEffectiveDate(), initialDate); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(1).getSubscriptionEventType(), SubscriptionEventType.START_BILLING); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getEffectiveDate(), initialDate); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getSubscriptionEventType(), SubscriptionEventType.SERVICE_STATE_CHANGE); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getServiceName(), "service"); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(2).getServiceStateName(), "stateName"); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(3).getEffectiveDate(), new LocalDate(2013, 9, 6)); Assert.assertEquals(bundles.get(idx).getTimeline().getSubscriptionEvents().get(3).getSubscriptionEventType(), SubscriptionEventType.PHASE); }
@Override public BundleJson apply(final SubscriptionBundle bundle) { // Cache audit logs per account if (accountsAuditLogs.get().get(bundle.getAccountId()) == null) { accountsAuditLogs.get().put(bundle.getAccountId(), auditUserApi.getAccountAuditLogs(bundle.getAccountId(), auditMode.getLevel(), tenantContext)); } try { return new BundleJson(bundle, null, accountsAuditLogs.get().get(bundle.getAccountId())); } catch (final CatalogApiException unused) { // Does not happen because we pass a null Currency throw new RuntimeException(unused); } } },
@Test(groups = "slow", description = "Test overdue for draft external charge", retryAnalyzer = FlakyRetryAnalyzer.class) public void testShouldNotBeInOverdueAfterDraftExternalCharge() throws Exception { // 2012-05-01T00:03:42.000Z clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0)); setupAccount(); // Create a subscription without failing payments final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE); bundle = subscriptionApi.getSubscriptionBundle(baseEntitlement.getBundleId(), callContext); invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.FIXED, new BigDecimal("0"))); invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 5, 1), callContext); // 2012-05-06 => Create an external charge on a new invoice addDaysAndCheckForCompletion(5); final InvoiceItem externalCharge = new ExternalChargeInvoiceItem(null, account.getId(), bundle.getId(), "For overdue", new LocalDate(2012, 5, 6), new LocalDate(2012, 6, 6), BigDecimal.TEN, Currency.USD, null); invoiceUserApi.insertExternalCharges(account.getId(), clock.getUTCToday(), ImmutableList.<InvoiceItem>of(externalCharge), false, null, callContext).get(0); assertListenerStatus(); invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 6), new LocalDate(2012, 6, 6), InvoiceItemType.EXTERNAL_CHARGE, BigDecimal.TEN)); // 2012-05-31 => DAY 30 have to get out of trial before first payment addDaysAndCheckForCompletion(25, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT); invoiceChecker.checkInvoice(account.getId(), 3, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 31), new LocalDate(2012, 6, 30), InvoiceItemType.RECURRING, new BigDecimal("249.95"))); invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 6, 30), callContext); // Should still be in clear state - the invoice for the bundle has been paid, but not the invoice with the external charge (because it is in draft mode) // We refresh overdue just to be safe, see below checkODState(OverdueWrapper.CLEAR_STATE_NAME); // 2012-06-06 => Past 30 days since the external charge addDaysAndCheckForCompletion(6); // We should still be clear checkODState(OverdueWrapper.CLEAR_STATE_NAME); Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 0); }
final List<SubscriptionEvent> transitions = bundle.getTimeline().getSubscriptionEvents(); assertEquals(transitions.size(), 9); checkSubscriptionEventAuditLog(transitions, 0, SubscriptionEventType.START_ENTITLEMENT);
@TimedResource @GET @Path("/{bundleId:" + UUID_PATTERN + "}/" + TAGS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve bundle tags", response = TagJson.class, responseContainer = "List", nickname = "getBundleTags") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid bundle id supplied"), @ApiResponse(code = 404, message = "Bundle not found")}) public Response getTags(@PathParam(ID_PARAM_NAME) final UUID bundleId, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, SubscriptionApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(bundleId, tenantContext); return super.getTags(bundle.getAccountId(), bundleId, auditMode, includedDeleted, tenantContext); }
@TimedResource @POST @Path("/{bundleId:" + UUID_PATTERN + "}/" + BLOCK) @Consumes(APPLICATION_JSON) @ApiOperation(value = "Block a bundle", response = BlockingStateJson.class, responseContainer = "List") @ApiResponses(value = {@ApiResponse(code = 201, message = "Blocking state created successfully"), @ApiResponse(code = 400, message = "Invalid bundle id supplied"), @ApiResponse(code = 404, message = "Bundle not found")}) public Response addBundleBlockingState(@PathParam(ID_PARAM_NAME) final UUID id, final BlockingStateJson json, @QueryParam(QUERY_REQUESTED_DT) final String requestedDate, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @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 SubscriptionApiException, EntitlementApiException, AccountApiException { final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(id, tenantContext); return addBlockingState(json, bundle.getAccountId(), id, BlockingStateType.SUBSCRIPTION_BUNDLE, requestedDate, pluginPropertiesString, createdBy, reason, comment, request, uriInfo); }
@TimedResource @GET @Path("/{bundleId:" + UUID_PATTERN + "}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a bundle by id", response = BundleJson.class) @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid bundle id supplied"), @ApiResponse(code = 404, message = "Bundle not found")}) public Response getBundle(@PathParam("bundleId") final UUID bundleId, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws SubscriptionApiException, AccountApiException, CatalogApiException { final TenantContext tenantContext = this.context.createTenantContextNoAccountId(request); final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(bundleId, tenantContext); final Account account = accountUserApi.getAccountById(bundle.getAccountId(), tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(bundle.getAccountId(), auditMode.getLevel(), tenantContext); final BundleJson json = new BundleJson(bundle, account.getCurrency(), accountAuditLogs); return Response.status(Status.OK).entity(json).build(); }
@TimedResource @GET @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a bundle by external key", response = BundleJson.class, responseContainer = "List") @ApiResponses(value = {@ApiResponse(code = 404, message = "Bundle not found")}) public Response getBundleByKey(@ApiParam(required=true) @QueryParam(QUERY_EXTERNAL_KEY) final String externalKey, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws SubscriptionApiException, AccountApiException, CatalogApiException { final TenantContext tenantContext = this.context.createTenantContextNoAccountId(request); final List<SubscriptionBundle> bundles; if (includedDeleted) { bundles = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, tenantContext); } else { final SubscriptionBundle activeBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey(externalKey, tenantContext); bundles = ImmutableList.of(activeBundle); } final List<BundleJson> result = new ArrayList<BundleJson>(bundles.size()); for (final SubscriptionBundle bundle : bundles) { final Account account = accountUserApi.getAccountById(bundle.getAccountId(), tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(bundle.getAccountId(), auditMode.getLevel(), tenantContext); final BundleJson json = new BundleJson(bundle, account.getCurrency(), accountAuditLogs); result.add(json); } return Response.status(Status.OK).entity(result).build(); }
private List<AuditLog> getAuditLogsForSubscription(final UUID bundleId, final UUID subscriptionId, final TenantContext context) { try { final SubscriptionBundle bundle = subscriptionApi.getSubscriptionBundle(bundleId, context); return auditUserApi.getAccountAuditLogs(bundle.getAccountId(), AuditLevel.FULL, context).getAuditLogsForSubscription(subscriptionId); } catch (final SubscriptionApiException e) { Assert.fail(e.toString()); return null; } }