@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(); }
@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); }
subscriptionApi.getSubscriptionBundlesForAccountIdAndExternalKey(accountId, externalKey, tenantContext) : subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext);
@GET @Path("/{subscriptionId:" + UUID_PATTERN + "}/" + TAGS) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve subscription tags", response = TagJson.class, responseContainer = "List", nickname = "getSubscriptionTags") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid subscription id supplied"), @ApiResponse(code = 404, message = "Subscription not found")}) public Response getTags(@PathParam(ID_PARAM_NAME) final UUID subscriptionId, @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 Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, tenantContext); return super.getTags(subscription.getAccountId(), subscriptionId, auditMode, includedDeleted, tenantContext); }
protected Response addBlockingState(final BlockingStateJson json, final UUID accountId, final UUID blockableId, final BlockingStateType type, final String requestedDate, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final HttpServletRequest request, @Nullable final UriInfo uriInfo) throws SubscriptionApiException, EntitlementApiException, AccountApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final CallContext callContext = context.createCallContextNoAccountId(createdBy, reason, comment, request); final boolean isBlockBilling = (json.isBlockBilling() != null && json.isBlockBilling()); final boolean isBlockEntitlement = (json.isBlockEntitlement() != null && json.isBlockEntitlement()); final boolean isBlockChange = (json.isBlockChange() != null && json.isBlockChange()); final LocalDate resolvedRequestedDate = toLocalDate(requestedDate); final BlockingState input = new DefaultBlockingState(blockableId, type, json.getStateName(), json.getService(), isBlockChange, isBlockEntitlement, isBlockBilling, null); subscriptionApi.addBlockingState(input, resolvedRequestedDate, pluginProperties, callContext); return uriInfo != null ? uriBuilder.buildResponse(uriInfo, AccountResource.class, "getBlockingStates", accountId, ImmutableMap.<String, String>of(QUERY_BLOCKING_STATE_TYPES, type.name()) , request) : null; }
assertNull(entitlement.getEffectiveEndDate()); final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext); assertEquals(bundles.size(), 1); final SubscriptionBundle activeBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey(externalKey, callContext); assertEquals(activeBundle.getId(), entitlement.getBundleId()); subscriptionApi.getActiveSubscriptionBundleForExternalKey(externalKey, callContext); Assert.fail("Expected getActiveSubscriptionBundleForExternalKey to fail after cancellation"); } catch (SubscriptionApiException e) { assertEquals(entitlement2.getExternalKey(), externalKey); final List<SubscriptionBundle> bundles2 = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext); assertEquals(bundles2.size(), 2); assertEquals(secondbundle.getOriginalCreatedDate().compareTo(firstbundle.getCreatedDate()), 0); final List<SubscriptionBundle> bundles2Again = subscriptionApi.getSubscriptionBundlesForAccountIdAndExternalKey(account.getId(), externalKey, callContext); assertEquals(bundles2Again.size(), 2); assertListenerStatus(); final List<SubscriptionBundle> bundles3 = subscriptionApi.getSubscriptionBundlesForExternalKey(externalKey, callContext); assertEquals(bundles3.size(), 3);
@Override public List<SubscriptionBundle> call() throws Exception { return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext); } };
bundle = subscriptionApi.getSubscriptionBundle(baseEntitlement.getBundleId(), callContext); final Subscription cancelledBaseSubscription = subscriptionApi.getSubscriptionForEntitlementId(baseEntitlement.getId(), callContext); assertTrue(cancelledBaseSubscription.getState() == EntitlementState.CANCELLED); assertEquals(cancelledBaseSubscription.getEffectiveEndDate(), new LocalDate(2012, 6, 05)); assertEquals(cancelledBaseSubscription.getBillingEndDate(), new LocalDate(2012, 6, 30)); final Subscription cancelledAddon1 = subscriptionApi.getSubscriptionForEntitlementId(addOn1.getId(), callContext); assertTrue(cancelledAddon1.getState() == EntitlementState.CANCELLED); assertEquals(cancelledAddon1.getEffectiveEndDate(), new LocalDate(2012, 6, 05));
protected SubscriptionBundle getLatestSubscriptionBundleForExternalKey(final String bundleExternalKey, final TenantContext context) throws OSGIServiceNotAvailable { final SubscriptionApi subscriptionApi = getSubscriptionApi(); try { final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForExternalKey(bundleExternalKey, context); if (bundles.isEmpty()) { throw new OSGIServiceNotAvailable("Unable to retrieve latest bundle for bundle external key " + bundleExternalKey); } return bundles.get(bundles.size() - 1); } catch (final SubscriptionApiException e) { logService.log(LogService.LOG_WARNING, "Error retrieving bundles for bundle external key " + bundleExternalKey, e); throw new OSGIServiceNotAvailable(e); } }
final Iterable<BlockingState> iterableForCreateState = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), null, OrderingType.ASCENDING, SubscriptionApi.ALL_EVENTS, callContext); assertTrue(iterableForCreateState.iterator().hasNext()); final BlockingState createState = iterableForCreateState.iterator().next(); subscriptionApi.addBlockingState(state1, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); subscriptionApi.addBlockingState(state2, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); subscriptionApi.addBlockingState(state3, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); subscriptionApi.addBlockingState(state4, internalCallContext.toLocalDate(futureEffectiveDate), ImmutableList.<PluginProperty>of(), callContext); final Iterable<BlockingState> blockingStates1 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.ACCOUNT, BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc1", "svc2"), OrderingType.ASCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext); verifyBlockingStates(blockingStates1, ImmutableList.<BlockingState>of(state1, state2, state3)); final Iterable<BlockingState> blockingStates2 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc1", "svc2"), OrderingType.DESCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext); verifyBlockingStates(blockingStates2, ImmutableList.<BlockingState>of(state3, state2)); final Iterable<BlockingState> blockingStates3 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION), ImmutableList.of("svc2"), OrderingType.DESCENDING, SubscriptionApi.PAST_OR_PRESENT_EVENTS, callContext); verifyBlockingStates(blockingStates3, ImmutableList.<BlockingState>of(state2)); final Iterable<BlockingState> blockingStates4 = subscriptionApi.getBlockingStates(account.getId(), null, null, OrderingType.DESCENDING, SubscriptionApi.ALL_EVENTS, callContext); verifyBlockingStates(blockingStates4, ImmutableList.<BlockingState>of(state4, state3, state2, state1, createState)); final Iterable<BlockingState> blockingStates5 = subscriptionApi.getBlockingStates(account.getId(), ImmutableList.of(BlockingStateType.SUBSCRIPTION_BUNDLE), null, OrderingType.ASCENDING, SubscriptionApi.FUTURE_EVENTS, callContext); verifyBlockingStates(blockingStates5, ImmutableList.<BlockingState>of(state4));
@TimedResource @GET @Path("/{accountId:" + UUID_PATTERN + "}/" + BLOCK) @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve blocking states for account", response = BlockingStateJson.class, responseContainer = "List") @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid account id supplied")}) public Response getBlockingStates(@PathParam(ID_PARAM_NAME) final UUID accountId, @QueryParam(QUERY_BLOCKING_STATE_TYPES) final List<BlockingStateType> typeFilter, @QueryParam(QUERY_BLOCKING_STATE_SVCS) final List<String> svcsFilter, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws EntitlementApiException { final TenantContext tenantContext = this.context.createTenantContextWithAccountId(accountId, request); final Iterable<BlockingState> blockingStates = subscriptionApi.getBlockingStates(accountId, typeFilter, svcsFilter, OrderingType.ASCENDING, SubscriptionApi.ALL_EVENTS, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext); final List<BlockingStateJson> result = ImmutableList.copyOf(Iterables.transform(blockingStates, new Function<BlockingState, BlockingStateJson>() { @Override public BlockingStateJson apply(final BlockingState input) { return new BlockingStateJson(input, accountAuditLogs); } })); return Response.status(Status.OK).entity(result).build(); }
@TimedResource @GET @Path("/{subscriptionId:" + UUID_PATTERN + "}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a subscription by id", response = SubscriptionJson.class) @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid subscription id supplied"), @ApiResponse(code = 404, message = "Subscription not found")}) public Response getSubscription(@PathParam("subscriptionId") final UUID subscriptionId, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws SubscriptionApiException, AccountApiException, CatalogApiException { final TenantContext context = this.context.createTenantContextNoAccountId(request); final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(subscriptionId, context); final Account account = accountUserApi.getAccountById(subscription.getAccountId(), context); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(subscription.getAccountId(), auditMode.getLevel(), context); final SubscriptionJson json = new SubscriptionJson(subscription, account.getCurrency(), accountAuditLogs); return Response.status(Status.OK).entity(json).build(); }
@Test(groups = "slow") public void testBlockingStatesV2() throws Exception { final DateTime initialDate = new DateTime(2017, 3, 1, 0, 1, 35, 0, DateTimeZone.UTC); clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis()); final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(0)); assertNotNull(account); final BlockingState blockingState1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state1", "Service", false, false, true, null); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("pistol-monthly-notrial", null); // Unlike the previous scenario, we create the subscription and set the blocking state at the same time busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK); subscriptionApi.addBlockingState(blockingState1, null, ImmutableList.<PluginProperty>of(), callContext); entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), "bundleExternalKey", null, null, false, true, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); clock.addMonths(1); busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); final BlockingState blockingState2 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, "state2", "Service", false, false, false, null); subscriptionApi.addBlockingState(blockingState2, null, ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); clock.addMonths(1); assertListenerStatus(); }
protected List<SubscriptionBundle> getSubscriptionBundlesForAccount(final UUID accountId, final TenantContext context) throws OSGIServiceNotAvailable { final SubscriptionApi subscriptionApi = getSubscriptionApi(); try { return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, context); } catch (final SubscriptionApiException e) { logService.log(LogService.LOG_WARNING, "Error retrieving bundles for account id " + accountId, e); throw new OSGIServiceNotAvailable(e); } }
@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); }
@Override public Response doOperation(final CallContext ctx) throws EntitlementApiException, SubscriptionApiException { final Entitlement current = entitlementApi.getEntitlementForId(subscriptionId, ctx); final LocalDate inputLocalDate = toLocalDate(requestedDate); final Entitlement newEntitlement; if (billingPolicy == null && entitlementPolicy == null) { newEntitlement = current.cancelEntitlementWithDate(inputLocalDate, useRequestedDateForBilling, pluginProperties, ctx); } else if (billingPolicy == null && entitlementPolicy != null) { newEntitlement = current.cancelEntitlementWithPolicy(entitlementPolicy, pluginProperties, ctx); } else if (billingPolicy != null && entitlementPolicy == null) { newEntitlement = current.cancelEntitlementWithDateOverrideBillingPolicy(inputLocalDate, billingPolicy, pluginProperties, ctx); } else { newEntitlement = current.cancelEntitlementWithPolicyOverrideBillingPolicy(entitlementPolicy, billingPolicy, pluginProperties, ctx); } final Subscription subscription = subscriptionApi.getSubscriptionForEntitlementId(newEntitlement.getId(), ctx); final LocalDate nowInAccountTimeZone = new LocalDate(callContext.getCreatedDate(), subscription.getBillingEndDate().getChronology().getZone()); isImmediateOp = subscription.getBillingEndDate() != null && !subscription.getBillingEndDate().isAfter(nowInAccountTimeZone); return Response.status(Status.NO_CONTENT).build(); }
subscriptionApi.addBlockingState(blockingState, new LocalDate(2016, 5, 1), ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus(); subscriptionApi.addBlockingState(unblockingState, new LocalDate(2016, 5, 16), ImmutableList.<PluginProperty>of(), callContext); assertListenerStatus();
final SubscriptionBundle initialBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey("bundleKey", callContext); final DefaultEntitlement newBaseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", newProductName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE); final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForExternalKey("bundleKey", callContext); Assert.assertEquals(bundles.size(), 2); final SubscriptionBundle newBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey("bundleKey", callContext); assertNotEquals(initialBundle.getId(), newBundle.getId()); assertEquals(initialBundle.getAccountId(), newBundle.getAccountId()); assertEquals(newerBaseEntitlement.getState(), EntitlementState.ACTIVE); final List<SubscriptionBundle> bundlesAgain = subscriptionApi.getSubscriptionBundlesForExternalKey("bundleKey", callContext); Assert.assertEquals(bundlesAgain.size(), 3); final SubscriptionBundle newerBundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey("bundleKey", callContext); assertNotEquals(initialBundle.getId(), newerBundle.getId()); assertEquals(initialBundle.getAccountId(), newerBundle.getAccountId());
final List<SubscriptionBundle> bundles = subscriptionApi.getSubscriptionBundlesForAccountId(accountId, callContext); final Iterable<Subscription> subscriptions = Iterables.concat(Iterables.transform(bundles, new Function<SubscriptionBundle, List<Subscription>>() { @Override
@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(); }