killbill-aplcache

Details

beatrix/pom.xml 1(+1 -0)

diff --git a/beatrix/pom.xml b/beatrix/pom.xml
index a81c264..547db04 100644
--- a/beatrix/pom.xml
+++ b/beatrix/pom.xml
@@ -111,6 +111,7 @@
             <artifactId>jdbi</artifactId>
             <scope>test</scope>
         </dependency>
+
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationWithAutoInvoiceOffTag.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationWithAutoInvoiceOffTag.java
index 39a9968..bc4f5a9 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationWithAutoInvoiceOffTag.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationWithAutoInvoiceOffTag.java
@@ -16,13 +16,13 @@
 
 package com.ning.billing.beatrix.integration;
 
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.assertEquals;
 
 import java.util.Collection;
-
-import junit.framework.Assert;
+import java.util.Map;
+import java.util.UUID;
 
 import org.joda.time.DateTime;
 import org.testng.annotations.BeforeMethod;
@@ -40,6 +40,12 @@ import com.ning.billing.entitlement.api.user.SubscriptionBundle;
 import com.ning.billing.entitlement.api.user.SubscriptionData;
 import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoiceUserApi;
+import com.ning.billing.util.api.TagDefinitionApiException;
+import com.ning.billing.util.api.TagUserApi;
+import com.ning.billing.util.dao.ObjectType;
+import com.ning.billing.util.tag.ControlTagType;
+import com.ning.billing.util.tag.Tag;
+import com.ning.billing.util.tag.TagDefinition;
 
 @Guice(modules = {BeatrixModule.class})
 public class TestIntegrationWithAutoInvoiceOffTag extends TestIntegrationBase {
@@ -47,7 +53,9 @@ public class TestIntegrationWithAutoInvoiceOffTag extends TestIntegrationBase {
     @Inject
     private InvoiceUserApi invoiceApi;
 
-    //TODO MDW write this test TestIntegrationWithAutoInvoiceOffTag
+    @Inject
+    private TagUserApi tagApi;
+
     private Account account;
     private SubscriptionBundle bundle;
     private String productName;
@@ -70,21 +78,98 @@ public class TestIntegrationWithAutoInvoiceOffTag extends TestIntegrationBase {
     @Test(groups={"slow"}, enabled = true)
     public void testAutoInvoiceOffAccount() throws Exception {
         clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
-        
+        addTag(account.getId(), ObjectType.ACCOUNT);
+       
         // set next invoice to fail and create network 
-        busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.INVOICE);
+        busHandler.pushExpectedEvents(NextEvent.CREATE);
         SubscriptionData baseSubscription = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle.getId(),
                 new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null), null, context));
         assertNotNull(baseSubscription);
         assertTrue(busHandler.isCompleted(DELAY));
 
-        busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR);
+ 
+        Collection<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 0);
+ 
         clock.addDays(10); // DAY 10 still in trial
         assertTrue(busHandler.isCompleted(DELAY));
 
-        Collection<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId());
-        
+        invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 0);
+
+        busHandler.pushExpectedEvents(NextEvent.PHASE);
+        clock.addDays(30); // DAY 40 out of trial
+        assertTrue(busHandler.isCompleted(DELAY));
+
+        invoices = invoiceApi.getInvoicesByAccount(account.getId());       
         assertEquals(invoices.size(), 0);
 
     }
+    
+    @Test(groups={"slow"}, enabled = true)
+    public void testAutoInvoiceOffSingleSubscription() throws Exception {
+        clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
+       
+        // set next invoice to fail and create network 
+        busHandler.pushExpectedEvents(NextEvent.CREATE,NextEvent.INVOICE);
+        SubscriptionData baseSubscription = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle.getId(),
+                new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null), null, context));
+        assertNotNull(baseSubscription);
+        assertTrue(busHandler.isCompleted(DELAY));
+        
+        Collection<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 1); // first invoice is generated immediately after creation can't reliably stop it
+ 
+
+        addTag(baseSubscription.getBundleId(), ObjectType.BUNDLE);
+
+        busHandler.pushExpectedEvents(NextEvent.PHASE);
+        clock.addDays(40); // DAY 40 out of trial
+        assertTrue(busHandler.isCompleted(DELAY));
+
+        invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 1); //No additional invoices generated
+
+    }
+
+    
+    @Test(groups={"slow"}, enabled = true)
+    public void testAutoInvoiceOffMultipleSubscriptions() throws Exception {
+        clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
+       
+        // set next invoice to fail and create network 
+        busHandler.pushExpectedEvents(NextEvent.CREATE,NextEvent.INVOICE);
+        SubscriptionData baseSubscription = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle.getId(),
+                new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null), null, context));
+        assertNotNull(baseSubscription);
+        assertTrue(busHandler.isCompleted(DELAY));
+ 
+        SubscriptionBundle bundle2 = entitlementUserApi.createBundleForAccount(account.getId(), "whatever", context);
+
+        busHandler.pushExpectedEvents(NextEvent.CREATE,NextEvent.INVOICE);
+        SubscriptionData baseSubscription2 = subscriptionDataFromSubscription(entitlementUserApi.createSubscription(bundle2.getId(),
+                new PlanPhaseSpecifier(productName, ProductCategory.BASE, term, planSetName, null), null, context));
+        assertNotNull(baseSubscription2);
+        assertTrue(busHandler.isCompleted(DELAY));
+
+        Collection<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 2); // first invoice is generated immediately after creation can't reliably stop it
+ 
+        addTag(baseSubscription.getBundleId(), ObjectType.BUNDLE);
+
+        busHandler.pushExpectedEvents(NextEvent.PHASE,NextEvent.PHASE,NextEvent.INVOICE);
+        clock.addDays(40); // DAY 40 out of trial
+        assertTrue(busHandler.isCompleted(DELAY));
+
+        invoices = invoiceApi.getInvoicesByAccount(account.getId());       
+        assertEquals(invoices.size(), 3); // Only one additional invoice generated
+    }
+
+
+    private void addTag(UUID id, ObjectType type) throws TagDefinitionApiException {
+        TagDefinition def = tagApi.getTagDefinition(ControlTagType.AUTO_INVOICING_OFF.name());
+        tagApi.addTag(id, type, def, context);
+        Map<String,Tag> tags = tagApi.getTags(id, type);
+        assertNotNull(tags.get(ControlTagType.AUTO_INVOICING_OFF.name()));
+    }
 }
diff --git a/beatrix/src/test/resources/log4j.xml b/beatrix/src/test/resources/log4j.xml
index ac530a1..79e0ec2 100644
--- a/beatrix/src/test/resources/log4j.xml
+++ b/beatrix/src/test/resources/log4j.xml
@@ -33,6 +33,10 @@
         <level value="info"/>
     </logger>
 
+    <logger name="com.ning.billing.junction">
+        <level value="debug"/>
+    </logger>
+
     <root>
         <priority value="info"/>
         <appender-ref ref="stdout"/>
diff --git a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingApi.java b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingApi.java
index cee92dc..6d91e7c 100644
--- a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingApi.java
+++ b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingApi.java
@@ -16,8 +16,10 @@
 
 package com.ning.billing.junction.plumbing.billing;
 
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.SortedSet;
 import java.util.UUID;
 
 import org.joda.time.DateTime;
@@ -99,9 +101,23 @@ public class DefaultBillingApi implements BillingApi {
         } catch (AccountApiException e) {
             log.warn("Failed while getting BillingEvent", e);
         }
+        
+        debugLog(result, "********* Billing Events Raw");
         blockCalculator.insertBlockingEvents(result);
+        debugLog(result,"*********  Billing Events After Blocking");
+        
         return result;
     }
+    
+
+    private void debugLog(SortedSet<BillingEvent> result, String title) {
+        log.debug(title);
+        Iterator<BillingEvent> i = result.iterator();
+        while(i.hasNext()) {
+            log.debug(i.next().toString());
+        }
+        
+    }
 
     private void addBillingEventsForBundles(List<SubscriptionBundle> bundles, Account account, CallContext context,
             DefaultBillingEventSet result) {
diff --git a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingEventSet.java b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingEventSet.java
index 0962a73..d26f70b 100644
--- a/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingEventSet.java
+++ b/junction/src/main/java/com/ning/billing/junction/plumbing/billing/DefaultBillingEventSet.java
@@ -58,5 +58,14 @@ public class DefaultBillingEventSet extends TreeSet<BillingEvent> implements Sor
     public boolean isLast(final BillingEvent event) {
         return last() == event;
     }
+
+    @Override
+    public String toString() {
+        return "DefaultBillingEventSet [accountAutoInvoiceOff=" + accountAutoInvoiceOff
+                + ", subscriptionIdsWithAutoInvoiceOff=" + subscriptionIdsWithAutoInvoiceOff + ", Events="
+                + super.toString() + "]";
+    }
+    
+    
     
 }
diff --git a/util/src/main/java/com/ning/billing/util/tag/dao/DefaultTagDefinitionDao.java b/util/src/main/java/com/ning/billing/util/tag/dao/DefaultTagDefinitionDao.java
index 1b6ce2a..39970da 100644
--- a/util/src/main/java/com/ning/billing/util/tag/dao/DefaultTagDefinitionDao.java
+++ b/util/src/main/java/com/ning/billing/util/tag/dao/DefaultTagDefinitionDao.java
@@ -52,6 +52,12 @@ public class DefaultTagDefinitionDao implements TagDefinitionDao {
 
     @Override
     public TagDefinition getByName(final String definitionName) {
+        // add control tag definitions
+        for (ControlTagType controlTag : ControlTagType.values()) {
+            if(definitionName.equals(controlTag.name())) {
+                return new DefaultTagDefinition(controlTag.toString(), controlTag.getDescription(), true);
+            }
+        }
         return dao.getByName(definitionName);
     }