public TestCallContext(final String userName) { this(userName, new DefaultClock().getUTCNow(), new DefaultClock().getUTCNow()); }
@Override public DateTime dryRunChangePlan(final DefaultSubscriptionBase subscription, final EntitlementSpecifier spec, @Nullable final DateTime requestedDateWithMs, @Nullable final BillingActionPolicy requestedPolicy, final TenantContext context) throws SubscriptionBaseApiException { final DateTime now = clock.getUTCNow(); BillingActionPolicy policyMaybeNull = requestedPolicy; if (requestedDateWithMs == null && requestedPolicy == null) { final PlanChangeResult planChangeResult = getPlanChangeResult(subscription, spec.getPlanPhaseSpecifier(), now, context); policyMaybeNull = planChangeResult.getPolicy(); } if (policyMaybeNull != null) { return subscription.getPlanChangeEffectiveDate(policyMaybeNull, null, -1, null); } else if (requestedDateWithMs != null) { return DefaultClock.truncateMs(requestedDateWithMs); } else { return now; } }
public DefaultPersistentBus(final DataSource dataSource, final Properties properties) { this(InTransaction.buildDDBI(dataSource), new DefaultClock(), new ConfigurationObjectFactory(properties).buildWithReplacements(PersistentBusConfig.class, ImmutableMap.<String, String>of("instanceName", "main")), new MetricRegistry(), new DatabaseTransactionNotificationApi()); }
@Override public LocalDate getToday(final DateTimeZone timeZone) { return new LocalDate(getUTCNow(), timeZone); }
@Override public DateTime getUTCNow() { return getNow(DateTimeZone.UTC); }
@Override public LocalDate getUTCToday() { return getToday(DateTimeZone.UTC); }
public DefaultNotificationQueueService(final DataSource dataSource, final Properties properties) { this(InTransaction.buildDDBI(dataSource), new DefaultClock(), new ConfigurationObjectFactory(new SimplePropertyConfigSource(properties)).buildWithReplacements(NotificationQueueConfig.class, ImmutableMap.<String, String>of("instanceName", "main")), new MetricRegistry()); }
@Override public boolean cancelWithRequestedDate(final DefaultSubscriptionBase subscription, final DateTime requestedDateWithMs, final CallContext context) throws SubscriptionBaseApiException { final EntitlementState currentState = subscription.getState(); if (currentState == EntitlementState.CANCELLED) { throw new SubscriptionBaseApiException(ErrorCode.SUB_CANCEL_BAD_STATE, subscription.getId(), currentState); } final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context); final Catalog fullCatalog; try { fullCatalog = catalogInternalApi.getFullCatalog(true, true, internalCallContext); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : context.getCreatedDate(); return doCancelPlan(ImmutableMap.<DefaultSubscriptionBase, DateTime>of(subscription, effectiveDate), fullCatalog, internalCallContext); } catch (final CatalogApiException e) { throw new SubscriptionBaseApiException(e); } }
public TestCallContext(final String userName) { this(userName, new DefaultClock().getUTCNow(), new DefaultClock().getUTCNow()); }
@BeforeClass(groups = "fast") protected void beforeClass() throws Exception { if (hasFailed()) { return; } super.beforeClass(); final Clock clock = new DefaultClock(); this.account = new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8)) .firstNameLength(6) .email(UUID.randomUUID().toString().substring(1, 8)) .phone(UUID.randomUUID().toString().substring(1, 8)) .migrated(false) .externalKey(UUID.randomUUID().toString().substring(1, 8)) .billingCycleDayLocal(31) .currency(Currency.USD) .paymentMethodId(UUID.randomUUID()) .timeZone(DateTimeZone.UTC) .build(); }
@Test(groups = "fast") public void testChangeSubscriptionWithPolicy() throws Exception { final SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME); try { final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); subscription.changePlanWithPolicy(new DefaultEntitlementSpecifier(planPhaseSpecifier), BillingActionPolicy.ILLEGAL, callContext); Assert.fail("Call changePlanWithPolicy should have failed"); } catch (final SubscriptionBaseError error) { assertTrue(true); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL); } // Assume the call takes less than a second final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); assertEquals(DefaultClock.truncateMs(subscription.changePlanWithPolicy(new DefaultEntitlementSpecifier(planPhaseSpecifier), BillingActionPolicy.IMMEDIATE, callContext)), DefaultClock.truncateMs(clock.getUTCNow())); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.MONTHLY); }
@Override @BeforeClass(groups = "slow") public void setUpBeforeClass() throws Exception { super.setUpBeforeClass(); this.dao = new TestPluginPaymentDao(embeddedDB.getDataSource()); final Clock clock = new DefaultClock(); final UUID tenantId = UUID.randomUUID(); context = Mockito.mock(CallContext.class); Mockito.when(context.getTenantId()).thenReturn(tenantId); account = TestUtils.buildAccount(Currency.JPY, "JP"); final OSGIKillbillAPI killbillApi = TestUtils.buildOSGIKillbillAPI(account); final Payment payment = TestUtils.buildPayment(account.getId(), account.getPaymentMethodId(), account.getCurrency(), killbillApi); final OSGIKillbillLogService logService = TestUtils.buildLogService(); final OSGIConfigPropertiesService configPropertiesService = Mockito.mock(OSGIConfigPropertiesService.class); api = new TestPaymentPluginApi(killbillApi, configPropertiesService, logService, clock, dao); TestUtils.updateOSGIKillbillAPI(killbillApi, api); }
@Override public DateTime getDryRunChangePlanEffectiveDate(final SubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime requestedDateWithMs, final BillingActionPolicy requestedPolicy, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException { final TenantContext tenantContext = internalCallContextFactory.createTenantContext(context); final CallContext callContext = internalCallContextFactory.createCallContext(context); // verify the number of subscriptions (of the same kind) allowed per bundle final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : null; final DateTime effectiveCatalogDate = effectiveDate != null ? effectiveDate : context.getCreatedDate(); final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), callContext); final Plan plan = catalog.createOrFindPlan(spec.getPlanPhaseSpecifier(), overridesWithContext, effectiveCatalogDate, subscription.getStartDate()); if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(plan.getProduct().getCategory().toString())) { if (plan.getPlansAllowedInBundle() != -1 && plan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(getSubscriptionsForBundle(subscription.getBundleId(), null, catalog, addonUtils, callContext, context), plan.getName()) >= plan.getPlansAllowedInBundle()) { // the plan can be changed to the new value, because it has reached its limit by bundle throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_AO_MAX_PLAN_ALLOWED_BY_BUNDLE, plan.getName()); } } return apiService.dryRunChangePlan((DefaultSubscriptionBase) subscription, spec, effectiveDate, requestedPolicy, tenantContext); }
@Override public DateTime getNow(final DateTimeZone tz) { final DateTime result = new DateTime(tz); return truncateMs(result); }
public static DateTime toUTCDateTime(final DateTime input) { if (input == null) { return null; } final DateTime result = input.toDateTime(DateTimeZone.UTC); return truncateMs(result); }
@Override public DateTime dryRunChangePlan(final DefaultSubscriptionBase subscription, final EntitlementSpecifier spec, @Nullable final DateTime requestedDateWithMs, @Nullable final BillingActionPolicy requestedPolicy, final TenantContext context) throws SubscriptionBaseApiException { final DateTime now = clock.getUTCNow(); BillingActionPolicy policyMaybeNull = requestedPolicy; if (requestedDateWithMs == null && requestedPolicy == null) { final PlanChangeResult planChangeResult = getPlanChangeResult(subscription, spec.getPlanPhaseSpecifier(), now, context); policyMaybeNull = planChangeResult.getPolicy(); } if (policyMaybeNull != null) { return subscription.getPlanChangeEffectiveDate(policyMaybeNull, null, -1, null); } else if (requestedDateWithMs != null) { return DefaultClock.truncateMs(requestedDateWithMs); } else { return now; } }
@Override public boolean cancelWithRequestedDate(final DefaultSubscriptionBase subscription, final DateTime requestedDateWithMs, final CallContext context) throws SubscriptionBaseApiException { final EntitlementState currentState = subscription.getState(); if (currentState == EntitlementState.CANCELLED) { throw new SubscriptionBaseApiException(ErrorCode.SUB_CANCEL_BAD_STATE, subscription.getId(), currentState); } final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context); final Catalog fullCatalog; try { fullCatalog = catalogInternalApi.getFullCatalog(true, true, internalCallContext); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : context.getCreatedDate(); return doCancelPlan(ImmutableMap.<DefaultSubscriptionBase, DateTime>of(subscription, effectiveDate), fullCatalog, internalCallContext); } catch (final CatalogApiException e) { throw new SubscriptionBaseApiException(e); } }
@Test(groups = "fast") public void testChangeSubscriptionWithPolicy() throws Exception { final SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME); try { final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); subscription.changePlanWithPolicy(new DefaultEntitlementSpecifier(planPhaseSpecifier), BillingActionPolicy.ILLEGAL, callContext); Assert.fail("Call changePlanWithPolicy should have failed"); } catch (final SubscriptionBaseError error) { assertTrue(true); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL); } // Assume the call takes less than a second final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); assertEquals(DefaultClock.truncateMs(subscription.changePlanWithPolicy(new DefaultEntitlementSpecifier(planPhaseSpecifier), BillingActionPolicy.IMMEDIATE, callContext)), DefaultClock.truncateMs(clock.getUTCNow())); assertEquals(subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext).getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.MONTHLY); }
@Override public DateTime getDryRunChangePlanEffectiveDate(final SubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime requestedDateWithMs, final BillingActionPolicy requestedPolicy, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException { final TenantContext tenantContext = internalCallContextFactory.createTenantContext(context); final CallContext callContext = internalCallContextFactory.createCallContext(context); // verify the number of subscriptions (of the same kind) allowed per bundle final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : null; final DateTime effectiveCatalogDate = effectiveDate != null ? effectiveDate : context.getCreatedDate(); final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), callContext); final Plan plan = catalog.createOrFindPlan(spec.getPlanPhaseSpecifier(), overridesWithContext, effectiveCatalogDate, subscription.getStartDate()); if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(plan.getProduct().getCategory().toString())) { if (plan.getPlansAllowedInBundle() != -1 && plan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(getSubscriptionsForBundle(subscription.getBundleId(), null, catalog, addonUtils, callContext, context), plan.getName()) >= plan.getPlansAllowedInBundle()) { // the plan can be changed to the new value, because it has reached its limit by bundle throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_AO_MAX_PLAN_ALLOWED_BY_BUNDLE, plan.getName()); } } return apiService.dryRunChangePlan((DefaultSubscriptionBase) subscription, spec, effectiveDate, requestedPolicy, tenantContext); }