killbill-memoizeit

junction: cleanup TestBillingApi * Refactor tests to avoid

8/27/2012 8:21:29 PM

Details

diff --git a/junction/src/test/java/com/ning/billing/junction/plumbing/billing/TestBillingApi.java b/junction/src/test/java/com/ning/billing/junction/plumbing/billing/TestBillingApi.java
index 661f5ca..20dd293 100644
--- a/junction/src/test/java/com/ning/billing/junction/plumbing/billing/TestBillingApi.java
+++ b/junction/src/test/java/com/ning/billing/junction/plumbing/billing/TestBillingApi.java
@@ -16,7 +16,6 @@
 
 package com.ning.billing.junction.plumbing.billing;
 
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -24,7 +23,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedSet;
-import java.util.TreeSet;
 import java.util.UUID;
 
 import org.joda.time.DateTime;
@@ -41,15 +39,12 @@ import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.catalog.MockCatalog;
 import com.ning.billing.catalog.MockCatalogService;
 import com.ning.billing.catalog.api.BillingAlignment;
-import com.ning.billing.catalog.api.BillingPeriod;
 import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.catalog.api.CatalogService;
 import com.ning.billing.catalog.api.Currency;
-import com.ning.billing.catalog.api.CurrencyValueNull;
 import com.ning.billing.catalog.api.InternationalPrice;
 import com.ning.billing.catalog.api.Plan;
 import com.ning.billing.catalog.api.PlanPhase;
-import com.ning.billing.catalog.api.Price;
 import com.ning.billing.catalog.api.PriceList;
 import com.ning.billing.catalog.api.PriceListSet;
 import com.ning.billing.entitlement.api.SubscriptionTransitionType;
@@ -85,7 +80,6 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
-import static org.testng.Assert.assertTrue;
 
 public class TestBillingApi extends JunctionTestSuite {
 
@@ -108,9 +102,12 @@ public class TestBillingApi extends JunctionTestSuite {
     };
 
     private Clock clock;
+
+    private AccountUserApi accountApi;
+    private BillCycleDayCalculator bcdCalculator;
+    private CallContextFactory factory;
+    private BillingApi api;
     private Subscription subscription;
-    private DateTime subscriptionStartDate;
-    private Plan subscriptionPlan;
     private TagUserApi tagApi;
 
     @BeforeSuite(groups = "fast")
@@ -121,18 +118,19 @@ public class TestBillingApi extends JunctionTestSuite {
 
     @BeforeMethod(groups = "fast")
     public void setupEveryTime() throws EntitlementUserApiException {
+        accountApi = Mockito.mock(AccountUserApi.class);
+
         final List<SubscriptionBundle> bundles = new ArrayList<SubscriptionBundle>();
         final SubscriptionBundle bundle = Mockito.mock(SubscriptionBundle.class);
         Mockito.when(bundle.getId()).thenReturn(bunId);
 
-        //new SubscriptionBundleData( eventId,"TestKey", subId,  clock.getUTCNow().minusDays(4), null);
         bundles.add(bundle);
 
         effectiveSubscriptionTransitions = new LinkedList<EffectiveSubscriptionEvent>();
         final List<Subscription> subscriptions = new LinkedList<Subscription>();
 
-        subscriptionStartDate = clock.getUTCNow().minusDays(3);
-        subscription = new MockSubscription(subId, bunId, subscriptionPlan, subscriptionStartDate, effectiveSubscriptionTransitions);
+        final DateTime subscriptionStartDate = clock.getUTCNow().minusDays(3);
+        subscription = new MockSubscription(subId, bunId, null, subscriptionStartDate, effectiveSubscriptionTransitions);
 
         subscriptions.add(subscription);
 
@@ -145,193 +143,84 @@ public class TestBillingApi extends JunctionTestSuite {
 
         tagApi = mock(TagUserApi.class);
 
-        assertTrue(true);
+        bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
+        factory = new DefaultCallContextFactory(clock);
+        api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
+
+        // Set a default alignment
+        ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.ACCOUNT);
     }
 
     @Test(groups = "fast")
     public void testBillingEventsEmpty() throws AccountApiException {
-        final UUID accountId = UUID.randomUUID();
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getId()).thenReturn(accountId);
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
-
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-
         final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
         Assert.assertEquals(events.size(), 0);
     }
 
     @Test(groups = "fast")
     public void testBillingEventsNoBillingPeriod() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
-        final PlanPhase nextPhase = nextPlan.getAllPhases()[0]; // The trial has no billing period
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
-
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(32));
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
+        // The trial has no billing period
+        final PlanPhase nextPhase = nextPlan.getAllPhases()[0];
+        final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
+        final Account account = createAccount(10);
 
-        checkFirstEvent(events, nextPlan, 32, subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
+        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
+        checkFirstEvent(events, nextPlan, account.getBillCycleDay().getDayOfMonthUTC(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
     }
 
-    @Test(enabled = false, groups = "fast")
-    public void testBillingEventsAnnual() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
+    @Test(groups = "fast")
+    public void testBillingEventsSubscriptionAligned() throws CatalogApiException, AccountApiException {
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
+        final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        effectiveSubscriptionTransitions.add(t);
-
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(1));
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
+        final Account account = createAccount(1);
 
         ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.SUBSCRIPTION);
 
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
-
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
-
-        checkFirstEvent(events, nextPlan, subscription.getStartDate().plusDays(30).getDayOfMonth(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
+        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
+        // The expected BCD is when the subscription started since we skip the trial phase
+        checkFirstEvent(events, nextPlan, subscription.getStartDate().getDayOfMonth(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
     }
 
     @Test(groups = "fast")
-    public void testBillingEventsMonthly() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
+    public void testBillingEventsAccountAligned() throws CatalogApiException, AccountApiException {
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
-
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(32));
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.ACCOUNT);
-
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-
-        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
+        final Account account = createAccount(32);
 
+        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
+        // The expected BCD is the account BCD (account aligned by default)
         checkFirstEvent(events, nextPlan, 32, subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
     }
 
-    @Test(enabled = false, groups = "fast")
-    public void testBillingEventsAddOn() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("Horn1USD", now);
+    @Test(groups = "fast")
+    public void testBillingEventsBundleAligned() throws CatalogApiException, AccountApiException {
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("Horn1USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[0];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
+        final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(1));
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        final Account account = createAccount(1);
 
         ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.BUNDLE);
+        ((MockSubscription) subscription).setPlan(catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now));
 
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-        subscriptionPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
-
-        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
-
-        checkFirstEvent(events, nextPlan, subscription.getStartDate().plusDays(30).getDayOfMonth(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
+        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
+        // The expected BCD is when the subscription started
+        checkFirstEvent(events, nextPlan, subscription.getStartDate().getDayOfMonth(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString());
     }
 
     @Test(groups = "fast")
     public void testBillingEventsWithBlock() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
-
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(32));
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
+        final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.ACCOUNT);
+        final Account account = createAccount(32);
 
         final List<BlockingState> blockingStates = new ArrayList<BlockingState>();
         blockingStates.add(new DefaultBlockingState(bunId, DISABLED_BUNDLE, Blockable.Type.SUBSCRIPTION_BUNDLE, "test", true, true, true, now.plusDays(1)));
@@ -366,41 +255,24 @@ public class TestBillingApi extends JunctionTestSuite {
             }
         });
 
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
         final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockingCal, catalogService, tagApi);
-        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(new UUID(0L, 0L));
+        final SortedSet<BillingEvent> events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
 
         Assert.assertEquals(events.size(), 3);
         final Iterator<BillingEvent> it = events.iterator();
 
-        checkEvent(it.next(), nextPlan, 32, subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
-        checkEvent(it.next(), nextPlan, 32, subId, now.plusDays(1), nextPhase, SubscriptionTransitionType.START_BILLING_DISABLED.toString(), null, null);
-        checkEvent(it.next(), nextPlan, 32, subId, now.plusDays(2), nextPhase, SubscriptionTransitionType.END_BILLING_DISABLED.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
+        checkEvent(it.next(), nextPlan, account.getBillCycleDay().getDayOfMonthUTC(), subId, now, nextPhase, SubscriptionTransitionType.CREATE.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
+        checkEvent(it.next(), nextPlan, account.getBillCycleDay().getDayOfMonthUTC(), subId, now.plusDays(1), nextPhase, SubscriptionTransitionType.START_BILLING_DISABLED.toString(), null, null);
+        checkEvent(it.next(), nextPlan, account.getBillCycleDay().getDayOfMonthUTC(), subId, now.plusDays(2), nextPhase, SubscriptionTransitionType.END_BILLING_DISABLED.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
     }
 
     @Test(groups = "fast")
     public void testBillingEventsAutoInvoicingOffAccount() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
+        createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(32));
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        final Account account = createAccount(32);
 
         final Map<String, Tag> tags = new HashMap<String, Tag>();
         final Tag aioTag = mock(Tag.class);
@@ -409,12 +281,6 @@ public class TestBillingApi extends JunctionTestSuite {
         when(tagApi.getTags(account.getId(), ObjectType.ACCOUNT)).thenReturn(tags);
         assertEquals(tagApi.getTags(account.getId(), ObjectType.ACCOUNT), tags);
 
-        ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.ACCOUNT);
-
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-
         final BillingEventSet events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
 
         assertEquals(events.isAccountAutoInvoiceOff(), true);
@@ -423,26 +289,11 @@ public class TestBillingApi extends JunctionTestSuite {
 
     @Test(groups = "fast")
     public void testBillingEventsAutoInvoicingOffBundle() throws CatalogApiException, AccountApiException {
-        final DateTime now = clock.getUTCNow();
-        final DateTime then = now.minusDays(1);
-        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", now);
+        final Plan nextPlan = catalogService.getFullCatalog().findPlan("PickupTrialEvergreen10USD", clock.getUTCNow());
         final PlanPhase nextPhase = nextPlan.getAllPhases()[1];
-        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
+        createSubscriptionCreationEvent(nextPlan, nextPhase);
 
-        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
-                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
-                nextPlan.getName(), nextPhase.getName(),
-                nextPriceList.getName(), 1L, null,
-                SubscriptionTransitionType.CREATE, 0, null);
-
-        effectiveSubscriptionTransitions.add(t);
-
-        final AccountUserApi accountApi = Mockito.mock(AccountUserApi.class);
-        final Account account = Mockito.mock(Account.class);
-        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(32));
-        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
-        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
-        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        final Account account = createAccount(32);
 
         final Map<String, Tag> tags = new HashMap<String, Tag>();
         final Tag aioTag = mock(Tag.class);
@@ -450,12 +301,6 @@ public class TestBillingApi extends JunctionTestSuite {
         tags.put(ControlTagType.AUTO_INVOICING_OFF.name(), aioTag);
         when(tagApi.getTags(bunId, ObjectType.BUNDLE)).thenReturn(tags);
 
-        ((MockCatalog) catalogService.getFullCatalog()).setBillingAlignment(BillingAlignment.ACCOUNT);
-
-        final BillCycleDayCalculator bcdCalculator = new BillCycleDayCalculator(catalogService, entitlementApi);
-        final CallContextFactory factory = new DefaultCallContextFactory(clock);
-        final BillingApi api = new DefaultBillingApi(null, factory, accountApi, bcdCalculator, entitlementApi, blockCalculator, catalogService, tagApi);
-
         final BillingEventSet events = api.getBillingEventsForAccountAndUpdateAccountBCD(account.getId());
 
         assertEquals(events.getSubscriptionIdsWithAutoInvoiceOff().size(), 1);
@@ -466,12 +311,11 @@ public class TestBillingApi extends JunctionTestSuite {
     private void checkFirstEvent(final SortedSet<BillingEvent> events, final Plan nextPlan,
                                  final int BCD, final UUID id, final DateTime time, final PlanPhase nextPhase, final String desc) throws CatalogApiException {
         Assert.assertEquals(events.size(), 1);
-        checkEvent(events.first(), nextPlan,
-                   BCD, id, time, nextPhase, desc, nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
+        checkEvent(events.first(), nextPlan, BCD, id, time, nextPhase, desc, nextPhase.getFixedPrice(), nextPhase.getRecurringPrice());
     }
 
-    private void checkEvent(final BillingEvent event, final Plan nextPlan,
-                            final int BCD, final UUID id, final DateTime time, final PlanPhase nextPhase, final String desc, final InternationalPrice fixedPrice, final InternationalPrice recurringPrice) throws CatalogApiException {
+    private void checkEvent(final BillingEvent event, final Plan nextPlan, final int BCD, final UUID id, final DateTime time,
+                            final PlanPhase nextPhase, final String desc, final InternationalPrice fixedPrice, final InternationalPrice recurringPrice) throws CatalogApiException {
         if (fixedPrice != null) {
             Assert.assertEquals(fixedPrice.getPrice(Currency.USD), event.getFixedPrice());
         } else {
@@ -495,4 +339,29 @@ public class TestBillingApi extends JunctionTestSuite {
         Assert.assertEquals(BillingModeType.IN_ADVANCE, event.getBillingMode());
         Assert.assertEquals(desc, event.getTransitionType().toString());
     }
+
+    private Account createAccount(final int billCycleDay) throws AccountApiException {
+        final Account account = Mockito.mock(Account.class);
+        Mockito.when(account.getBillCycleDay()).thenReturn(new MockBillCycleDay(billCycleDay));
+        Mockito.when(account.getCurrency()).thenReturn(Currency.USD);
+        Mockito.when(account.getId()).thenReturn(UUID.randomUUID());
+        Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
+        Mockito.when(accountApi.getAccountById(Mockito.<UUID>any())).thenReturn(account);
+        return account;
+    }
+
+    private DateTime createSubscriptionCreationEvent(final Plan nextPlan, final PlanPhase nextPhase) throws CatalogApiException {
+        final DateTime now = clock.getUTCNow();
+        final DateTime then = now.minusDays(1);
+        final PriceList nextPriceList = catalogService.getFullCatalog().findPriceList(PriceListSet.DEFAULT_PRICELIST_NAME, now);
+
+        final EffectiveSubscriptionEvent t = new MockEffectiveSubscriptionEvent(
+                eventId, subId, bunId, then, now, null, null, null, null, SubscriptionState.ACTIVE,
+                nextPlan.getName(), nextPhase.getName(),
+                nextPriceList.getName(), 1L, null,
+                SubscriptionTransitionType.CREATE, 0, null);
+
+        effectiveSubscriptionTransitions.add(t);
+        return now;
+    }
 }
diff --git a/util/src/test/java/com/ning/billing/mock/MockSubscription.java b/util/src/test/java/com/ning/billing/mock/MockSubscription.java
index fee4237..1425500 100644
--- a/util/src/test/java/com/ning/billing/mock/MockSubscription.java
+++ b/util/src/test/java/com/ning/billing/mock/MockSubscription.java
@@ -42,7 +42,7 @@ public class MockSubscription implements Subscription {
     private final UUID id;
     private final UUID bundleId;
     private final SubscriptionState state;
-    private final Plan plan;
+    private Plan plan;
     private final PlanPhase phase;
     private final DateTime startDate;
     private final List<EffectiveSubscriptionEvent> transitions;
@@ -210,4 +210,8 @@ public class MockSubscription implements Subscription {
         // TODO Auto-generated method stub
         return null;
     }
+
+    public void setPlan(final Plan plan) {
+        this.plan = plan;
+    }
 }