killbill-aplcache

Details

diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java b/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
index a624345..dce8c99 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/alignment/PlanAligner.java
@@ -66,7 +66,8 @@ public class PlanAligner  {
     public TimedPhase [] getCurrentAndNextTimedPhaseOnCreate(SubscriptionData subscription,
             Plan plan, PhaseType initialPhase, String priceList, DateTime effectiveDate)
         throws CatalogApiException, EntitlementUserApiException {
-        List<TimedPhase> timedPhases = getTimedPhaseOnCreate(subscription, plan, initialPhase, priceList, effectiveDate);
+        List<TimedPhase> timedPhases = getTimedPhaseOnCreate(subscription.getStartDate(),
+                subscription.getBundleStartDate(), plan, initialPhase, priceList);
         TimedPhase [] result = new TimedPhase[2];
         result[0] = getTimedPhase(timedPhases, effectiveDate, WhichPhase.CURRENT);
         result[1] = getTimedPhase(timedPhases, effectiveDate, WhichPhase.NEXT);
@@ -126,13 +127,15 @@ public class PlanAligner  {
             }
 
             switch(lastPlanTransition.getTransitionType()) {
-            // If we never had any Plan change, this a simple Phase alignement based on initial Phase and startPhaseDate
+            // If we never had any Plan change, borrow the logics for createPlan alignment
             case CREATE:
-                DateTime initialStartPhase = subscription.getStartDate();
-                PhaseType initialPhase = lastPlanTransition.getNextPhase().getPhaseType();
-                List<TimedPhase> timedPhases = getPhaseAlignments(subscription.getCurrentPlan(), initialPhase, initialStartPhase);
+                List<TimedPhase> timedPhases = getTimedPhaseOnCreate(subscription.getStartDate(),
+                        subscription.getBundleStartDate(),
+                        lastPlanTransition.getNextPlan(),
+                        lastPlanTransition.getNextPhase().getPhaseType(),
+                        lastPlanTransition.getNextPriceList());
                 return getTimedPhase(timedPhases, effectiveDate, WhichPhase.NEXT);
-            // If we went through Plan changes, look at previous Plan Transition and borrow changePlan logics to recompute everything
+            // If we went through Plan changes, borrow the logics for chnagePlan alignement
             case CHANGE:
                 return getTimedPhaseOnChange(subscription.getStartDate(),
                         subscription.getBundleStartDate(),
@@ -151,8 +154,9 @@ public class PlanAligner  {
         }
     }
 
-    private List<TimedPhase> getTimedPhaseOnCreate(SubscriptionData subscription,
-            Plan plan, PhaseType initialPhase, String priceList, DateTime effectiveDate)
+    private List<TimedPhase> getTimedPhaseOnCreate(DateTime subscriptionStartDate,
+            DateTime bundleStartDate,
+            Plan plan, PhaseType initialPhase, String priceList)
         throws CatalogApiException, EntitlementUserApiException  {
 
         Catalog catalog = catalogService.getCatalog();
@@ -163,15 +167,14 @@ public class PlanAligner  {
                 priceList);
 
         DateTime planStartDate = null;
-        PlanAlignmentCreate alignement = null;
-        alignement = catalog.planCreateAlignment(planSpecifier);
+        PlanAlignmentCreate alignement = catalog.planCreateAlignment(planSpecifier);
 
         switch(alignement) {
         case START_OF_SUBSCRIPTION:
-            planStartDate = subscription.getStartDate();
+            planStartDate = subscriptionStartDate;
             break;
         case START_OF_BUNDLE:
-            planStartDate = subscription.getBundleStartDate();
+            planStartDate = bundleStartDate;
             break;
         default:
             throw new EntitlementError(String.format("Unknwon PlanAlignmentCreate %s", alignement));
@@ -204,6 +207,7 @@ public class PlanAligner  {
 
         Catalog catalog = catalogService.getCatalog();
         ProductCategory currentCategory = currentPlan.getProduct().getCategory();
+        // STEPH tiered ADDON not implemented yet
         if (currentCategory != ProductCategory.BASE) {
             throw new EntitlementError(String.format("Only implemented changePlan for BasePlan"));
         }
@@ -294,7 +298,7 @@ public class PlanAligner  {
         case NEXT:
             return next;
         default:
-            throw new EntitlementError(String.format("Unepected %s TimedPhase", which));
+            throw new EntitlementError(String.format("Unexpected %s TimedPhase", which));
         }
     }
 }
diff --git a/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiAddOn.java b/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiAddOn.java
index 8cbdc93..a2f3532 100644
--- a/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiAddOn.java
+++ b/entitlement/src/test/java/com/ning/billing/entitlement/api/user/TestUserApiAddOn.java
@@ -18,6 +18,7 @@ package com.ning.billing.entitlement.api.user;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 import org.joda.time.DateTime;
@@ -28,10 +29,13 @@ import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Stage;
 import com.ning.billing.catalog.api.BillingPeriod;
+import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.catalog.api.Duration;
 import com.ning.billing.catalog.api.PhaseType;
 import com.ning.billing.catalog.api.Plan;
+import com.ning.billing.catalog.api.PlanAlignmentCreate;
 import com.ning.billing.catalog.api.PlanPhase;
+import com.ning.billing.catalog.api.PlanSpecifier;
 import com.ning.billing.catalog.api.PriceListSet;
 import com.ning.billing.catalog.api.ProductCategory;
 import com.ning.billing.entitlement.api.TestApiBase;
@@ -45,9 +49,63 @@ public class TestUserApiAddOn extends TestApiBase {
         return Guice.createInjector(Stage.DEVELOPMENT, new MockEngineModuleSql());
     }
 
-    @Test(enabled=false, groups={"sql"})
+    @Test(enabled=true, groups={"sql"})
     public void testAddonCreateWithBundleAlign() {
         try {
+            String aoProduct = "Telescopic-Scope";
+            BillingPeriod aoTerm = BillingPeriod.MONTHLY;
+            String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
+
+            // This is just to double check our test catalog gives us what we want before we start the test
+            PlanSpecifier planSpecifier = new PlanSpecifier(aoProduct,
+                    ProductCategory.ADD_ON,
+                    aoTerm,
+                    aoPriceList);
+            PlanAlignmentCreate alignement = catalog.planCreateAlignment(planSpecifier);
+            assertEquals(alignement, PlanAlignmentCreate.START_OF_BUNDLE);
+
+            testAddonCreateInternal(aoProduct, aoTerm, aoPriceList, alignement);
+
+        } catch (CatalogApiException e) {
+            Assert.fail(e.getMessage());
+        }
+    }
+
+    /*
+     *
+     * STEPH : Can't make the catalog work as i want so disable test until resolved
+     *             <createAlignmentCase>
+                <product>Laser-Scope</product>
+                <alignment>START_OF_SUBSCRIPTION</alignment>
+            </createAlignmentCase>
+
+     */
+    @Test(enabled=false, groups={"sql"})
+    public void testAddonCreateWithSubscriptionAlign() {
+
+        try {
+            String aoProduct = "Laser-Scope";
+            BillingPeriod aoTerm = BillingPeriod.MONTHLY;
+            String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
+
+            // This is just to double check our test catalog gives us what we want before we start the test
+            PlanSpecifier planSpecifier = new PlanSpecifier(aoProduct,
+                    ProductCategory.ADD_ON,
+                    aoTerm,
+                    aoPriceList);
+            PlanAlignmentCreate alignement = catalog.planCreateAlignment(planSpecifier);
+            assertEquals(alignement, PlanAlignmentCreate.START_OF_SUBSCRIPTION);
+
+            testAddonCreateInternal(aoProduct, aoTerm, aoPriceList, alignement);
+
+            } catch (CatalogApiException e) {
+                Assert.fail(e.getMessage());
+            }
+    }
+
+
+    private void testAddonCreateInternal(String aoProduct, BillingPeriod aoTerm, String aoPriceList, PlanAlignmentCreate expAlignement) {
+        try {
 
             String baseProduct = "Shotgun";
             BillingPeriod baseTerm = BillingPeriod.MONTHLY;
@@ -56,10 +114,6 @@ public class TestUserApiAddOn extends TestApiBase {
             // CREATE BP
             SubscriptionData baseSubscription = createSubscription(baseProduct, baseTerm, basePriceList);
 
-            String aoProduct = "Telescopic-Scope";
-            BillingPeriod aoTerm = BillingPeriod.MONTHLY;
-            String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
-
             // MOVE CLOCK 14 DAYS LATER
             Duration someTimeLater = getDurationDay(13);
             clock.setDeltaFromReality(someTimeLater, DAY_IN_MS);
@@ -85,9 +139,12 @@ public class TestUserApiAddOn extends TestApiBase {
 
            // CHECK next AO PHASE EVENT IS INDEED A MONTH AFTER BP STARTED => BUNDLE ALIGNMENT
            SubscriptionTransition aoPendingTranstion = aoSubscription.getPendingTransition();
-           assertEquals(aoPendingTranstion.getEffectiveTransitionTime(), baseSubscription.getStartDate().plusMonths(1));
-
 
+           if (expAlignement == PlanAlignmentCreate.START_OF_BUNDLE) {
+               assertEquals(aoPendingTranstion.getEffectiveTransitionTime(), baseSubscription.getStartDate().plusMonths(1));
+           } else {
+               assertEquals(aoPendingTranstion.getEffectiveTransitionTime(), aoSubscription.getStartDate().plusMonths(1));
+           }
 
            // ADD TWO PHASE EVENTS (BP + AO)
            testListener.reset();
@@ -95,18 +152,11 @@ public class TestUserApiAddOn extends TestApiBase {
            testListener.pushExpectedEvent(NextEvent.PHASE);
 
            // MOVE THROUGH TIME TO GO INTO EVERGREEN
-           someTimeLater = getDurationDay(20);
+           someTimeLater = aoCurrentPhase.getDuration();
            clock.addDeltaFromReality(someTimeLater);
            assertTrue(testListener.isCompleted(5000));
 
 
-           try {
-               Thread.currentThread().sleep(1000 * 1000);
-           } catch (InterruptedException e) {
-
-
-           }
-
            // CHECK EVERYTHING AGAIN
            aoSubscription = (SubscriptionData) entitlementApi.getSubscriptionFromId(aoSubscription.getId());
 
@@ -120,6 +170,11 @@ public class TestUserApiAddOn extends TestApiBase {
            assertNotNull(aoCurrentPhase);
            assertEquals(aoCurrentPhase.getPhaseType(), PhaseType.EVERGREEN);
 
+
+           aoSubscription = (SubscriptionData) entitlementApi.getSubscriptionFromId(aoSubscription.getId());
+           aoPendingTranstion = aoSubscription.getPendingTransition();
+           assertNull(aoPendingTranstion);
+
         } catch (EntitlementUserApiException e) {
             Assert.fail(e.getMessage());
         }
diff --git a/entitlement/src/test/resources/testInput.xml b/entitlement/src/test/resources/testInput.xml
index 531d88f..7174893 100644
--- a/entitlement/src/test/resources/testInput.xml
+++ b/entitlement/src/test/resources/testInput.xml
@@ -135,9 +135,6 @@ Use Cases to do:
 		</changePolicy>
 		<changeAlignment>
 			<changeAlignmentCase>
-				<alignment>START_OF_SUBSCRIPTION</alignment>
-			</changeAlignmentCase>
-			<changeAlignmentCase>
 				<toPriceList>rescue</toPriceList>
 				<alignment>CHANGE_OF_PLAN</alignment>
 			</changeAlignmentCase>
@@ -146,20 +143,23 @@ Use Cases to do:
 				<toPriceList>rescue</toPriceList>
 				<alignment>CHANGE_OF_PRICELIST</alignment>
 			</changeAlignmentCase>
+            <changeAlignmentCase>
+                <alignment>START_OF_SUBSCRIPTION</alignment>
+            </changeAlignmentCase>
 		</changeAlignment>
 		<cancelPolicy>
 			<cancelPolicyCase>
-				<policy>END_OF_TERM</policy>
-			</cancelPolicyCase>
-			<cancelPolicyCase>
 				<phaseType>TRIAL</phaseType>
 				<policy>IMMEDIATE</policy>
 			</cancelPolicyCase>
+            <cancelPolicyCase>
+                <policy>END_OF_TERM</policy>
+            </cancelPolicyCase>
 		</cancelPolicy>
 		<createAlignment>
-			<createAlignmentCase>
-				<alignment>START_OF_BUNDLE</alignment>
-			</createAlignmentCase>
+            <createAlignmentCase>
+                <alignment>START_OF_BUNDLE</alignment>
+            </createAlignmentCase>
 		</createAlignment>
 		<billingAlignment>
 		<billingAlignmentCase>