killbill-aplcache

Details

diff --git a/api/src/main/java/com/ning/billing/junction/api/BlockingState.java b/api/src/main/java/com/ning/billing/junction/api/BlockingState.java
index eb68096..5ae1665 100644
--- a/api/src/main/java/com/ning/billing/junction/api/BlockingState.java
+++ b/api/src/main/java/com/ning/billing/junction/api/BlockingState.java
@@ -99,6 +99,67 @@ public class BlockingState implements Comparable<BlockingState>{
 
     @Override
     public int compareTo(BlockingState arg0) {
-        return timestamp.compareTo(arg0.getTimestamp());
+        if (timestamp.compareTo(arg0.getTimestamp()) != 0) {
+            return timestamp.compareTo(arg0.getTimestamp());
+        } else {
+            return hashCode() - arg0.hashCode();
+        }
     }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + (blockBilling ? 1231 : 1237);
+        result = prime * result + (blockChange ? 1231 : 1237);
+        result = prime * result + (blockEntitlement ? 1231 : 1237);
+        result = prime * result + ((blockingId == null) ? 0 : blockingId.hashCode());
+        result = prime * result + ((service == null) ? 0 : service.hashCode());
+        result = prime * result + ((stateName == null) ? 0 : stateName.hashCode());
+        result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
+        result = prime * result + ((type == null) ? 0 : type.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        BlockingState other = (BlockingState) obj;
+        if (blockBilling != other.blockBilling)
+            return false;
+        if (blockChange != other.blockChange)
+            return false;
+        if (blockEntitlement != other.blockEntitlement)
+            return false;
+        if (blockingId == null) {
+            if (other.blockingId != null)
+                return false;
+        } else if (!blockingId.equals(other.blockingId))
+            return false;
+        if (service == null) {
+            if (other.service != null)
+                return false;
+        } else if (!service.equals(other.service))
+            return false;
+        if (stateName == null) {
+            if (other.stateName != null)
+                return false;
+        } else if (!stateName.equals(other.stateName))
+            return false;
+        if (timestamp == null) {
+            if (other.timestamp != null)
+                return false;
+        } else if (!timestamp.equals(other.timestamp))
+            return false;
+        if (type != other.type)
+            return false;
+        return true;
+    }
+    
+    
 }
diff --git a/junction/src/test/java/com/ning/billing/junction/api/blocking/TestBlockingApi.java b/junction/src/test/java/com/ning/billing/junction/api/blocking/TestBlockingApi.java
new file mode 100644
index 0000000..2d27a3e
--- /dev/null
+++ b/junction/src/test/java/com/ning/billing/junction/api/blocking/TestBlockingApi.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.junction.api.blocking;
+
+import java.io.IOException;
+import java.util.SortedSet;
+import java.util.UUID;
+
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import com.google.inject.Inject;
+import com.ning.billing.catalog.api.CatalogApiException;
+import com.ning.billing.dbi.MysqlTestingHelper;
+import com.ning.billing.entitlement.api.user.SubscriptionBundle;
+import com.ning.billing.junction.MockModule;
+import com.ning.billing.junction.api.Blockable;
+import com.ning.billing.junction.api.BlockingApi;
+import com.ning.billing.junction.api.BlockingState;
+import com.ning.billing.junction.dao.TestBlockingDao;
+import com.ning.billing.mock.BrainDeadProxyFactory;
+import com.ning.billing.mock.BrainDeadProxyFactory.ZombieControl;
+import com.ning.billing.util.clock.ClockMock;
+
+@Guice(modules = { MockModule.class })
+public class TestBlockingApi {
+    private Logger log = LoggerFactory.getLogger(TestBlockingDao.class);
+    
+    @Inject
+    private MysqlTestingHelper helper;
+    
+    @Inject 
+    private BlockingApi api;
+    
+    @Inject
+    private ClockMock clock;
+
+    @BeforeClass(groups={"slow"})
+    public void setup() throws IOException {
+        log.info("Starting set up TestBlockingApi");
+
+        final String utilDdl = IOUtils.toString(TestBlockingDao.class.getResourceAsStream("/com/ning/billing/junction/ddl.sql"));
+
+        helper.startMysql();
+        helper.initDb(utilDdl);
+     
+    }
+    
+    @BeforeMethod(groups={"slow"})
+    public void clean() {       
+        helper.cleanupTable("blocking_states");
+        clock.resetDeltaFromReality();
+    }
+    
+    @AfterClass(groups = "slow")
+    public void stopMysql()
+    {
+        helper.stopMysql();
+    }
+
+    @Test(groups={"slow"}, enabled=true)
+    public void testApi() { 
+
+        UUID uuid = UUID.randomUUID();
+        String overdueStateName = "WayPassedItMan";
+        String service = "TEST";
+        
+        boolean blockChange = true;
+        boolean blockEntitlement = false;
+        boolean blockBilling = false;
+
+        BlockingState state1 = new BlockingState(uuid, overdueStateName, Blockable.Type.SUBSCRIPTION_BUNDLE, service, blockChange, blockEntitlement,blockBilling);
+        api.setBlockingState(state1);
+        clock.setDeltaFromReality(1000 * 3600 * 24);
+        
+        String overdueStateName2 = "NoReallyThisCantGoOn";
+        BlockingState state2 = new BlockingState(uuid, overdueStateName2, Blockable.Type.SUBSCRIPTION_BUNDLE, service, blockChange, blockEntitlement,blockBilling);
+        api.setBlockingState(state2);
+        
+        SubscriptionBundle bundle = BrainDeadProxyFactory.createBrainDeadProxyFor(SubscriptionBundle.class);
+        ((ZombieControl)bundle).addResult("getId", uuid);
+        
+        Assert.assertEquals(api.getBlockingStateNameFor(bundle), overdueStateName2);
+        Assert.assertEquals(api.getBlockingStateNameFor(bundle.getId(), Blockable.Type.SUBSCRIPTION_BUNDLE), overdueStateName2);
+        
+    }
+    
+    @Test(groups={"slow"}, enabled=true)
+    public void testApiHistory() throws CatalogApiException { 
+        UUID uuid = UUID.randomUUID();
+        String overdueStateName = "WayPassedItMan";
+        String service = "TEST";
+        
+        boolean blockChange = true;
+        boolean blockEntitlement = false;
+        boolean blockBilling = false;
+
+        BlockingState state1 = new BlockingState(uuid, overdueStateName, Blockable.Type.SUBSCRIPTION_BUNDLE, service, blockChange, blockEntitlement,blockBilling);
+        api.setBlockingState(state1);
+        
+        clock.setDeltaFromReality(1000 * 3600 * 24);
+
+        String overdueStateName2 = "NoReallyThisCantGoOn";
+        BlockingState state2 = new BlockingState(uuid, overdueStateName2, Blockable.Type.SUBSCRIPTION_BUNDLE, service, blockChange, blockEntitlement,blockBilling);
+        api.setBlockingState(state2);
+        
+        SubscriptionBundle bundle = BrainDeadProxyFactory.createBrainDeadProxyFor(SubscriptionBundle.class);
+        ((ZombieControl)bundle).addResult("getId", uuid);
+        
+     
+        SortedSet<BlockingState> history1 = api.getBlockingHistory(bundle);
+        SortedSet<BlockingState> history2 = api.getBlockingHistory(bundle.getId(), Blockable.Type.get(bundle));
+        
+        Assert.assertEquals(history1.size(), 2);
+        Assert.assertEquals(history1.first().getStateName(), overdueStateName);
+        Assert.assertEquals(history1.last().getStateName(), overdueStateName2);
+        
+        Assert.assertEquals(history2.size(), 2);
+        Assert.assertEquals(history2.first().getStateName(), overdueStateName);
+        Assert.assertEquals(history2.last().getStateName(), overdueStateName2);
+       
+    }
+    
+}
diff --git a/junction/src/test/java/com/ning/billing/junction/dao/TestBlockingDao.java b/junction/src/test/java/com/ning/billing/junction/dao/TestBlockingDao.java
index 6392722..961e0d9 100644
--- a/junction/src/test/java/com/ning/billing/junction/dao/TestBlockingDao.java
+++ b/junction/src/test/java/com/ning/billing/junction/dao/TestBlockingDao.java
@@ -24,6 +24,7 @@ import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
@@ -32,15 +33,15 @@ import com.google.inject.Inject;
 import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.dbi.MysqlTestingHelper;
 import com.ning.billing.entitlement.api.user.SubscriptionBundle;
+import com.ning.billing.junction.MockModule;
 import com.ning.billing.junction.api.Blockable;
 import com.ning.billing.junction.api.BlockingState;
-import com.ning.billing.junction.dao.BlockingStateDao;
-import com.ning.billing.junction.glue.NEModule;
+import com.ning.billing.junction.glue.JunctionModule;
 import com.ning.billing.mock.BrainDeadProxyFactory;
 import com.ning.billing.mock.BrainDeadProxyFactory.ZombieControl;
 import com.ning.billing.util.clock.ClockMock;
 
-@Guice(modules = {MockModule.class,  NEModule.class})
+@Guice(modules = {MockModule.class,  JunctionModule.class})
 public class TestBlockingDao {
     private Logger log = LoggerFactory.getLogger(TestBlockingDao.class);
     
@@ -50,12 +51,9 @@ public class TestBlockingDao {
     @Inject
     private BlockingStateDao dao;
 
-    @Inject
-    private BlockingStateDao accessDao;
-
     @BeforeClass(groups={"slow"})
     public void setup() throws IOException {
-        log.info("Starting set up");
+        log.info("Starting set up TestBlockingDao");
 
         final String utilDdl = IOUtils.toString(TestBlockingDao.class.getResourceAsStream("/com/ning/billing/junction/ddl.sql"));
 
@@ -64,6 +62,12 @@ public class TestBlockingDao {
 
     }
     
+    @AfterClass(groups = "slow")
+    public void stopMysql()
+    {
+        helper.stopMysql();
+    }
+    
     
     @Test(groups={"slow"}, enabled=true)
     public void testDao() { 
@@ -87,8 +91,8 @@ public class TestBlockingDao {
         SubscriptionBundle bundle = BrainDeadProxyFactory.createBrainDeadProxyFor(SubscriptionBundle.class);
         ((ZombieControl)bundle).addResult("getId", uuid);
         
-        Assert.assertEquals(accessDao.getBlockingStateFor(bundle), overdueStateName2);
-        Assert.assertEquals(accessDao.getBlockingStateForIdAndType(bundle.getId(), Blockable.Type.SUBSCRIPTION_BUNDLE), overdueStateName2);
+        Assert.assertEquals(dao.getBlockingStateFor(bundle), overdueStateName2);
+        Assert.assertEquals(dao.getBlockingStateForIdAndType(bundle.getId(), Blockable.Type.SUBSCRIPTION_BUNDLE), overdueStateName2);
         
     }
     
@@ -115,8 +119,8 @@ public class TestBlockingDao {
         ((ZombieControl)bundle).addResult("getId", uuid);
         
      
-        SortedSet<BlockingState> history1 = accessDao.getBlockingHistoryFor(bundle);
-        SortedSet<BlockingState> history2 = accessDao.getBlockingHistoryForIdAndType(bundle.getId(), Blockable.Type.get(bundle));
+        SortedSet<BlockingState> history1 = dao.getBlockingHistoryFor(bundle);
+        SortedSet<BlockingState> history2 = dao.getBlockingHistoryForIdAndType(bundle.getId(), Blockable.Type.get(bundle));
         
         Assert.assertEquals(history1.size(), 2);
         Assert.assertEquals(history1.first().getStateName(), overdueStateName);