use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.
the class SubscriptionTestSuiteWithEmbeddedDB method beforeMethod.
@Override
@BeforeMethod(groups = "slow")
public void beforeMethod() throws Exception {
if (hasFailed()) {
return;
}
super.beforeMethod();
subscriptionTestInitializer.startTestFramework(testListener, clock, busService, subscriptionBaseService);
this.catalog = DefaultSubscriptionCatalogApi.wrapCatalog(subscriptionTestInitializer.initCatalog(catalogService, internalCallContext), clock);
this.accountData = subscriptionTestInitializer.initAccountData(clock);
final Account account = createAccount(accountData);
final SubscriptionBaseBundle model = subscriptionTestInitializer.initBundle(account.getId(), subscriptionInternalApi, clock, internalCallContext);
this.bundle = subscriptionInternalApi.createBundleForAccount(model.getAccountId(), model.getExternalKey(), false, internalCallContext);
}
use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.
the class TestBillCycleDayCalculator method testCalculateBCDForAOWithBPCancelledBundleAligned.
@Test(groups = "fast")
public void testCalculateBCDForAOWithBPCancelledBundleAligned() throws Exception {
final DateTimeZone accountTimeZone = DateTimeZone.UTC;
final DateTime bpStartDateUTC = new DateTime(2012, 7, 16, 21, 0, 0, DateTimeZone.UTC);
final int expectedBCDUTC = 16;
// Create a Bundle associated with a subscription
final SubscriptionBaseBundle bundle = Mockito.mock(SubscriptionBaseBundle.class);
final SubscriptionBase subscription = Mockito.mock(SubscriptionBase.class);
Mockito.when(subscription.getStartDate()).thenReturn(bpStartDateUTC);
// Create a the base plan associated with that subscription
final Plan plan = Mockito.mock(Plan.class);
Mockito.when(subscription.getLastActivePlan()).thenReturn(plan);
Mockito.when(subscription.getDateOfFirstRecurringNonZeroCharge()).thenReturn(bpStartDateUTC);
final ImmutableAccountData account = Mockito.mock(ImmutableAccountData.class);
Mockito.when(account.getTimeZone()).thenReturn(accountTimeZone);
final Integer billCycleDayLocal = BillCycleDayCalculator.calculateBcdForAlignment(new HashMap<UUID, Integer>(), subscription, subscription, BillingAlignment.BUNDLE, internalCallContext, 0);
Assert.assertEquals(billCycleDayLocal, (Integer) expectedBCDUTC);
}
use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.
the class TestSubscriptionDao method afterMethod.
// to ignore events
@Override
@AfterMethod(groups = "slow")
public void afterMethod() throws Exception {
if (hasFailed()) {
final String externalKey = "12345";
final DateTime startDate = clock.getUTCNow();
final DateTime createdDate = startDate.plusSeconds(10);
final DefaultSubscriptionBaseBundle bundleDef = new DefaultSubscriptionBaseBundle(externalKey, accountId, startDate, startDate, createdDate, createdDate);
final SubscriptionBaseBundle bundle = dao.createSubscriptionBundle(bundleDef, catalog, true, internalCallContext);
final List<SubscriptionBaseBundle> result = dao.getSubscriptionBundlesForKey(externalKey, internalCallContext);
assertEquals(result.size(), 1);
assertEquals(result.get(0).getExternalKey(), bundle.getExternalKey());
// Operation succeeds but nothing new got created because bundle is empty
dao.createSubscriptionBundle(bundleDef, catalog, true, internalCallContext);
final List<SubscriptionBaseBundle> result2 = dao.getSubscriptionBundlesForKey(externalKey, internalCallContext);
assertEquals(result2.size(), 1);
// Create a subscription and this time operation should fail
final SubscriptionBuilder builder = new SubscriptionBuilder().setId(UUIDs.randomUUID()).setBundleId(bundle.getId()).setBundleExternalKey(bundle.getExternalKey()).setCategory(ProductCategory.BASE).setBundleStartDate(startDate).setAlignStartDate(startDate).setMigrated(false);
final ApiEventBuilder createBuilder = new ApiEventBuilder().setSubscriptionId(builder.getId()).setEventPlan("shotgun-monthly").setEventPlanPhase("shotgun-monthly-trial").setEventPriceList(DefaultPriceListSet.DEFAULT_PRICELIST_NAME).setEffectiveDate(startDate).setFromDisk(true);
final SubscriptionBaseEvent creationEvent = new ApiEventCreate(createBuilder);
final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(builder);
testListener.pushExpectedEvents(NextEvent.CREATE);
final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = new DefaultSubscriptionBaseWithAddOns(bundle, ImmutableList.<SubscriptionBase>of(subscription));
dao.createSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOns>of(subscriptionBaseWithAddOns), ImmutableMap.<UUID, List<SubscriptionBaseEvent>>of(subscription.getId(), ImmutableList.<SubscriptionBaseEvent>of(creationEvent)), catalog, internalCallContext);
assertListenerStatus();
// Operation Should now fail
try {
dao.createSubscriptionBundle(bundleDef, catalog, true, internalCallContext);
Assert.fail("Should fail to create new subscription bundle with existing key");
} catch (SubscriptionBaseApiException e) {
assertEquals(ErrorCode.SUB_CREATE_ACTIVE_BUNDLE_KEY_EXISTS.getCode(), e.getCode());
}
return;
}
subscriptionTestInitializer.stopTestFramework(testListener, busService, subscriptionBaseService);
}
use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.
the class TestSubscriptionDao method testDirtyFlag.
@Test(groups = "slow")
public void testDirtyFlag() throws Throwable {
final IDBI dbiSpy = Mockito.spy(dbi);
final IDBI roDbiSpy = Mockito.spy(roDbi);
final SubscriptionDao subscriptionDao = new DefaultSubscriptionDao(dbiSpy, roDbiSpy, clock, addonUtils, notificationQueueService, bus, controlCacheDispatcher, nonEntityDao, auditDao, internalCallContextFactory);
Mockito.verify(dbiSpy, Mockito.times(0)).open();
Mockito.verify(roDbiSpy, Mockito.times(0)).open();
// @BeforeMethod created the account
DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() {
@Override
public Object execute() throws Throwable {
Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 0);
Mockito.verify(dbiSpy, Mockito.times(0)).open();
Mockito.verify(roDbiSpy, Mockito.times(1)).open();
Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 0);
Mockito.verify(dbiSpy, Mockito.times(0)).open();
Mockito.verify(roDbiSpy, Mockito.times(2)).open();
final String externalKey = UUID.randomUUID().toString();
final DateTime startDate = clock.getUTCNow();
final DateTime createdDate = startDate.plusSeconds(10);
final DefaultSubscriptionBaseBundle bundleDef = new DefaultSubscriptionBaseBundle(externalKey, accountId, startDate, startDate, createdDate, createdDate);
final SubscriptionBaseBundle bundle = subscriptionDao.createSubscriptionBundle(bundleDef, catalog, false, internalCallContext);
Mockito.verify(dbiSpy, Mockito.times(1)).open();
Mockito.verify(roDbiSpy, Mockito.times(2)).open();
Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 1);
Mockito.verify(dbiSpy, Mockito.times(2)).open();
Mockito.verify(roDbiSpy, Mockito.times(2)).open();
Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 1);
Mockito.verify(dbiSpy, Mockito.times(3)).open();
Mockito.verify(roDbiSpy, Mockito.times(2)).open();
return null;
}
});
DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() {
@Override
public Object execute() {
Assert.assertEquals(subscriptionDao.getSubscriptionBundleForAccount(accountId, internalCallContext).size(), 1);
Mockito.verify(dbiSpy, Mockito.times(3)).open();
Mockito.verify(roDbiSpy, Mockito.times(3)).open();
return null;
}
});
}
use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.
the class TestTransfer method testTransferBPNoTrialWithCTD.
@Test(groups = "slow")
public void testTransferBPNoTrialWithCTD() throws Exception {
final String baseProduct = "Shotgun";
final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
// CREATE BP
final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
// MOVE AFTER TRIAL
testListener.pushExpectedEvent(NextEvent.PHASE);
clock.addDays(40);
assertListenerStatus();
// SET CTD
final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
final DateTime transferRequestedDate = clock.getUTCNow();
testListener.pushExpectedEvent(NextEvent.TRANSFER);
transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, false, false, callContext);
assertListenerStatus();
// CHECK OLD BASE IS CANCEL AT THE TRANSFER DATE
final SubscriptionBase oldBaseSubscription = subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
assertNotNull(oldBaseSubscription.getFutureEndDate());
assertTrue(oldBaseSubscription.getFutureEndDate().compareTo(ctd) == 0);
// CHECK NEW BUNDLE EXIST, WITH ONE SUBSCRIPTION STARTING ON TRANSFER_DATE
final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
assertEquals(bundlesForAccountAndKey.size(), 1);
final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
assertEquals(subscriptions.size(), 1);
final SubscriptionBase newBaseSubscription = subscriptions.get(0);
assertTrue(((DefaultSubscriptionBase) newBaseSubscription).getAlignStartDate().compareTo(((DefaultSubscriptionBase) baseSubscription).getAlignStartDate()) == 0);
// CHECK ONLY ONE PHASE EXISTS
assertEquals(subscriptionInternalApi.getAllTransitions(newBaseSubscription, internalCallContext).size(), 1);
Plan newPlan = newBaseSubscription.getCurrentPlan();
assertEquals(newPlan.getProduct().getName(), baseProduct);
assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
// MAKE A PLAN CHANGE IMM
clock.addDays(5);
final String newBaseProduct1 = "Assault-Rifle";
final BillingPeriod newBaseTerm1 = BillingPeriod.ANNUAL;
testListener.pushExpectedEvent(NextEvent.CHANGE);
final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(newBaseProduct1, newBaseTerm1, basePriceList);
newBaseSubscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier, null, null, null), callContext);
assertListenerStatus();
newPlan = newBaseSubscription.getCurrentPlan();
assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
assertEquals(newBaseSubscription.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
// SET CTD AND MAKE CHANGE EOT
clock.addDays(2);
final DateTime newCtd = newBaseSubscription.getStartDate().plusYears(1);
setChargedThroughDate(newBaseSubscription.getId(), newCtd, internalCallContext);
final SubscriptionBase newBaseSubscriptionWithCtd = subscriptionInternalApi.getSubscriptionFromId(newBaseSubscription.getId(), internalCallContext);
final String newBaseProduct2 = "Pistol";
final BillingPeriod newBaseTerm2 = BillingPeriod.ANNUAL;
final PlanPhaseSpecifier planPhaseSpecifier1 = new PlanPhaseSpecifier(newBaseProduct2, newBaseTerm2, basePriceList);
newBaseSubscriptionWithCtd.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier1, null, null, null), callContext);
newPlan = newBaseSubscriptionWithCtd.getCurrentPlan();
assertEquals(newPlan.getProduct().getName(), newBaseProduct1);
assertEquals(newBaseSubscriptionWithCtd.getCurrentPhase().getPhaseType(), PhaseType.EVERGREEN);
assertNotNull(newBaseSubscriptionWithCtd.getPendingTransition());
assertEquals(newBaseSubscriptionWithCtd.getPendingTransition().getEffectiveTransitionTime(), newCtd);
}
Aggregations