use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.
the class TestSubscriptionHelper method createSubscription.
private DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nullable final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final LocalDate requestedDate) throws SubscriptionBaseApiException {
final VersionedCatalog catalog;
try {
catalog = catalogInternalApi.getFullCatalog(true, true, internalCallContext);
} catch (CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
// Make sure the right account information is used
final InternalCallContext internalCallContext = bundle == null ? this.internalCallContext : internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
boolean bundleExists = false;
if (bundle != null) {
try {
bundleExists = (subscriptionApi.getBundleFromId(bundle.getId(), internalCallContext) != null);
} catch (final SubscriptionBaseApiException ignored) {
}
}
if (!noEvents && (requestedDate == null || requestedDate.compareTo(clock.getUTCToday()) <= 0)) {
testListener.pushExpectedEvent(NextEvent.CREATE);
}
final ImmutableList<EntitlementSpecifier> entitlementSpecifiers = ImmutableList.<EntitlementSpecifier>of(new EntitlementSpecifier() {
@Override
public PlanPhaseSpecifier getPlanPhaseSpecifier() {
return new PlanPhaseSpecifier(productName, term, planSet, phaseType);
}
@Override
public Integer getBillCycleDay() {
return null;
}
@Override
public String getExternalKey() {
return UUID.randomUUID().toString();
}
@Override
public List<PlanPhasePriceOverride> getOverrides() {
return null;
}
});
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle == null || !bundleExists ? null : bundle.getId(), bundle == null ? null : bundle.getExternalKey(), entitlementSpecifiers, requestedDate, false);
final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionApi.createBaseSubscriptionsWithAddOns(catalog, ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext).get(0);
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionBaseWithAddOns.getSubscriptionBaseList().get(0);
assertNotNull(subscription);
testListener.assertListenerStatus();
mockNonEntityDao.addTenantRecordIdMapping(subscription.getId(), internalCallContext);
mockNonEntityDao.addAccountRecordIdMapping(subscription.getId(), internalCallContext);
mockNonEntityDao.addTenantRecordIdMapping(subscription.getBundleId(), internalCallContext);
mockNonEntityDao.addAccountRecordIdMapping(subscription.getBundleId(), internalCallContext);
return subscription;
}
use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.
the class TestUserApiCreate method testCreateSubscriptionWithBCD.
@Test(groups = "slow")
public void testCreateSubscriptionWithBCD() throws SubscriptionBaseApiException {
final DateTime init = clock.getUTCNow();
final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
final Iterable<EntitlementSpecifier> specifiers = ImmutableList.<EntitlementSpecifier>of(new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("shotgun-monthly"), 18, null, null));
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle.getId(), bundle.getExternalKey(), specifiers, init.toLocalDate(), false);
testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BCD_CHANGE);
final List<SubscriptionBaseWithAddOns> subscriptionBaseWithAddOns = subscriptionInternalApi.createBaseSubscriptionsWithAddOns(catalog.getCatalog(), ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext);
testListener.assertListenerStatus();
assertEquals(subscriptionBaseWithAddOns.size(), 1);
assertEquals(subscriptionBaseWithAddOns.get(0).getSubscriptionBaseList().size(), 1);
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionBaseWithAddOns.get(0).getSubscriptionBaseList().get(0);
assertNotNull(subscription);
assertNotNull(subscription.getBillCycleDayLocal());
assertEquals(subscription.getBillCycleDayLocal().intValue(), 18);
}
use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.
the class DefaultEntitlementApi method createBaseEntitlementsWithAddOns.
private List<UUID> createBaseEntitlementsWithAddOns(final OperationType operationType, final UUID accountId, final Iterable<BaseEntitlementWithAddOnsSpecifier> originalBaseEntitlementWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
logCreateEntitlementsWithAOs(log, originalBaseEntitlementWithAddOnsSpecifiers);
final EntitlementContext pluginContext = new DefaultEntitlementContext(operationType, accountId, null, originalBaseEntitlementWithAddOnsSpecifiers, null, properties, callContext);
final WithEntitlementPlugin<List<UUID>> createBaseEntitlementsWithAddOns = new WithEntitlementPlugin<List<UUID>>() {
@Override
public List<UUID> doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(accountId, callContext);
final VersionedCatalog catalog;
try {
catalog = catalogInternalApi.getFullCatalog(true, true, contextWithValidAccountRecordId);
} catch (final CatalogApiException e) {
throw new EntitlementApiException(e);
}
final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle = new HashMap<UUID, Optional<EventsStream>>();
final Map<String, Optional<UUID>> bundleKeyToIdMapping = new HashMap<String, Optional<UUID>>();
final Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifiersAfterPlugins = updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers();
final Collection<SubscriptionBaseWithAddOnsSpecifier> subscriptionBaseWithAddOnsSpecifiers = new LinkedList<SubscriptionBaseWithAddOnsSpecifier>();
DateTime upTo = null;
for (final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier : baseEntitlementWithAddOnsSpecifiersAfterPlugins) {
// Entitlement
final DateTime entitlementRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithValidAccountRecordId);
upTo = upTo == null || upTo.compareTo(entitlementRequestedDate) < 0 ? entitlementRequestedDate : upTo;
final UUID bundleId = populateCaches(baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscriptionPerBundle, bundleKeyToIdMapping, catalog, callContext, contextWithValidAccountRecordId);
if (bundleId != null) {
final Optional<EventsStream> eventsStreamForBaseSubscription = eventsStreamForBaseSubscriptionPerBundle.get(bundleId);
if (eventsStreamForBaseSubscription.isPresent()) {
// Verify if the operation is valid for that bundle
preCheckAddEntitlement(bundleId, entitlementRequestedDate, baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscription.get());
}
}
final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifier.getBundleId(), baseEntitlementWithAddOnsSpecifier.getBundleExternalKey(), baseEntitlementWithAddOnsSpecifier.getEntitlementSpecifier(), baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(), baseEntitlementWithAddOnsSpecifier.isMigrated());
subscriptionBaseWithAddOnsSpecifiers.add(subscriptionBaseWithAddOnsSpecifier);
}
// Verify if operation is allowed by looking for is_block_change on Account
// Note that to fully check for block_change we should also look for BlockingState at the BUNDLE/SUBSCRIPTION level in case some of the input contain a BP that already exists.
checkForAccountBlockingChange(accountId, upTo, contextWithValidAccountRecordId);
final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns;
try {
subscriptionsWithAddOns = subscriptionBaseInternalApi.createBaseSubscriptionsWithAddOns(catalog, subscriptionBaseWithAddOnsSpecifiers, renameCancelledBundleIfExist, contextWithValidAccountRecordId);
} catch (final SubscriptionBaseApiException e) {
throw new EntitlementApiException(e);
}
// Update the context for plugins (assume underlying ordering is respected)
for (int i = 0; i < subscriptionsWithAddOns.size(); i++) {
final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionsWithAddOns.get(i);
updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleId(subscriptionBaseWithAddOns.getBundle().getId());
updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleExternalKey(subscriptionBaseWithAddOns.getBundle().getExternalKey());
}
return createEntitlementEvents(baseEntitlementWithAddOnsSpecifiersAfterPlugins, subscriptionsWithAddOns, updatedPluginContext, contextWithValidAccountRecordId);
}
};
return pluginExecution.executeWithPlugin(createBaseEntitlementsWithAddOns, pluginContext);
}
use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.
the class DefaultSubscriptionBaseCreateApi method createBaseSubscriptionsWithAddOns.
List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final Iterable<SubscriptionBaseWithAddOnsSpecifier> baseAndAddOnEntitlementsSpecifiers, final boolean renameCancelledBundleIfExist, final SubscriptionCatalog catalog, final AddonUtils addonUtils, final CacheController<UUID, UUID> accountIdCacheController, final CacheController<UUID, UUID> bundleIdCacheController, final CallContext callContext, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException {
// Prepare the subscription specifiers from the entitlement specifiers
final Collection<SubscriptionAndAddOnsSpecifier> baseAndAddOnSubscriptionsSpecifiers = new ArrayList<SubscriptionAndAddOnsSpecifier>();
for (final SubscriptionBaseWithAddOnsSpecifier baseAndAddOnEntitlementsSpecifier : baseAndAddOnEntitlementsSpecifiers) {
prepareSubscriptionAndAddOnsSpecifier(baseAndAddOnSubscriptionsSpecifiers, baseAndAddOnEntitlementsSpecifier, renameCancelledBundleIfExist, catalog, addonUtils, accountIdCacheController, callContext, context);
}
// Create the subscriptions
final List<SubscriptionBaseWithAddOns> subscriptionBaseWithAddOns = apiService.createPlansWithAddOns(callContext.getAccountId(), baseAndAddOnSubscriptionsSpecifiers, catalog, callContext);
// Populate the caches
for (final SubscriptionBaseWithAddOns subscriptionBaseWithAO : subscriptionBaseWithAddOns) {
for (final SubscriptionBase subscriptionBase : subscriptionBaseWithAO.getSubscriptionBaseList()) {
bundleIdCacheController.putIfAbsent(subscriptionBase.getId(), subscriptionBaseWithAO.getBundle().getId());
}
}
return subscriptionBaseWithAddOns;
}
Aggregations