killbill-uncached

junction: populate account_record_id in DefaultBlockingApi Signed-off-by:

10/5/2012 5:36:58 PM

Details

diff --git a/api/src/main/java/com/ning/billing/junction/api/Blockable.java b/api/src/main/java/com/ning/billing/junction/api/Blockable.java
index 240e70f..daab418 100644
--- a/api/src/main/java/com/ning/billing/junction/api/Blockable.java
+++ b/api/src/main/java/com/ning/billing/junction/api/Blockable.java
@@ -54,6 +54,10 @@ public interface Blockable {
 
         public static ObjectType getObjectType(final Blockable o) {
             final Type type = get(o);
+            return getObjectType(type);
+        }
+
+        public static ObjectType getObjectType(final Type type) {
             switch (type) {
                 case ACCOUNT:
                     return ObjectType.ACCOUNT;
diff --git a/api/src/main/java/com/ning/billing/junction/api/BlockingApi.java b/api/src/main/java/com/ning/billing/junction/api/BlockingApi.java
index adb92bd..7041086 100644
--- a/api/src/main/java/com/ning/billing/junction/api/BlockingApi.java
+++ b/api/src/main/java/com/ning/billing/junction/api/BlockingApi.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2010-2011 Ning, Inc.
+ * Copyright 2010-2012 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
diff --git a/api/src/main/java/com/ning/billing/junction/api/DefaultBlockingState.java b/api/src/main/java/com/ning/billing/junction/api/DefaultBlockingState.java
index 5a179c6..23e6ec6 100644
--- a/api/src/main/java/com/ning/billing/junction/api/DefaultBlockingState.java
+++ b/api/src/main/java/com/ning/billing/junction/api/DefaultBlockingState.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2010-2011 Ning, Inc.
+ * Copyright 2010-2012 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
diff --git a/junction/src/main/java/com/ning/billing/junction/api/blocking/DefaultBlockingApi.java b/junction/src/main/java/com/ning/billing/junction/api/blocking/DefaultBlockingApi.java
index 2e8af01..97f9866 100644
--- a/junction/src/main/java/com/ning/billing/junction/api/blocking/DefaultBlockingApi.java
+++ b/junction/src/main/java/com/ning/billing/junction/api/blocking/DefaultBlockingApi.java
@@ -20,14 +20,17 @@ import java.util.List;
 import java.util.UUID;
 
 import com.ning.billing.junction.api.Blockable;
+import com.ning.billing.junction.api.Blockable.Type;
 import com.ning.billing.junction.api.BlockingApi;
 import com.ning.billing.junction.api.BlockingState;
 import com.ning.billing.junction.api.DefaultBlockingState;
 import com.ning.billing.junction.dao.BlockingStateDao;
 import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.InternalCallContext;
 import com.ning.billing.util.callcontext.InternalCallContextFactory;
 import com.ning.billing.util.callcontext.TenantContext;
 import com.ning.billing.util.clock.Clock;
+import com.ning.billing.util.dao.ObjectType;
 
 import com.google.inject.Inject;
 
@@ -70,7 +73,12 @@ public class DefaultBlockingApi implements BlockingApi {
 
     @Override
     public <T extends Blockable> void setBlockingState(final BlockingState state, final CallContext context) {
-        // TODO accountId?
-        dao.setBlockingState(state, clock, internalCallContextFactory.createInternalCallContext(context));
+        // TODO remove the cast (needed because the id is not exposed in BlockingState)
+        dao.setBlockingState(state, clock, createInternalCallContext((DefaultBlockingState) state, context));
+    }
+
+    private InternalCallContext createInternalCallContext(final DefaultBlockingState blockingState, final CallContext context) {
+        final ObjectType objectType = Type.getObjectType(blockingState.getType());
+        return internalCallContextFactory.createInternalCallContext(blockingState.getBlockedId(), objectType, context);
     }
 }
diff --git a/junction/src/test/java/com/ning/billing/junction/api/blocking/TestDefaultBlockingApi.java b/junction/src/test/java/com/ning/billing/junction/api/blocking/TestDefaultBlockingApi.java
new file mode 100644
index 0000000..f2552c6
--- /dev/null
+++ b/junction/src/test/java/com/ning/billing/junction/api/blocking/TestDefaultBlockingApi.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2010-2012 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.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.tweak.HandleCallback;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.ning.billing.junction.JunctionTestSuiteWithEmbeddedDB;
+import com.ning.billing.junction.api.Blockable.Type;
+import com.ning.billing.junction.api.BlockingState;
+import com.ning.billing.junction.api.DefaultBlockingState;
+import com.ning.billing.junction.dao.BlockingStateDao;
+import com.ning.billing.junction.dao.BlockingStateSqlDao;
+import com.ning.billing.util.callcontext.InternalCallContextFactory;
+import com.ning.billing.util.clock.ClockMock;
+
+public class TestDefaultBlockingApi extends JunctionTestSuiteWithEmbeddedDB {
+
+    private final ClockMock clock = new ClockMock();
+
+    private DefaultBlockingApi blockingApi;
+
+    @BeforeMethod(groups = "slow")
+    public void setUp() throws Exception {
+        final BlockingStateDao blockingStateDao = getMysqlTestingHelper().getDBI().onDemand(BlockingStateSqlDao.class);
+        blockingApi = new DefaultBlockingApi(blockingStateDao, clock, new InternalCallContextFactory(getMysqlTestingHelper().getDBI(), clock));
+    }
+
+    @Test(groups = "slow")
+    public void testSetBlockingStateOnBundle() throws Exception {
+        final UUID bundleId = UUID.randomUUID();
+        final Long accountRecordId = 123049714L;
+        getMysqlTestingHelper().getDBI().withHandle(new HandleCallback<Void>() {
+            @Override
+            public Void withHandle(final Handle handle) throws Exception {
+                handle.execute("DROP TABLE IF EXISTS bundles;\n" +
+                               "CREATE TABLE bundles (\n" +
+                               "    record_id int(11) unsigned NOT NULL AUTO_INCREMENT,\n" +
+                               "    id char(36) NOT NULL,\n" +
+                               "    external_key varchar(64) NOT NULL,\n" +
+                               "    account_id char(36) NOT NULL,\n" +
+                               "    last_sys_update_date datetime,\n" +
+                               "    account_record_id int(11) unsigned default null,\n" +
+                               "    tenant_record_id int(11) unsigned default null,\n" +
+                               "    PRIMARY KEY(record_id)\n" +
+                               ") ENGINE=innodb;");
+                handle.execute("insert into bundles (id, external_key, account_id, account_record_id) values (?, 'foo', ?, ?)",
+                               bundleId.toString(), UUID.randomUUID().toString(), accountRecordId);
+                return null;
+            }
+        });
+
+        final BlockingState blockingState = new DefaultBlockingState(bundleId, "BLOCKED", Type.SUBSCRIPTION_BUNDLE, "myService", true, true, true, clock.getUTCToday().toDateTimeAtStartOfDay());
+        blockingApi.setBlockingState(blockingState, callContext);
+
+        // Verify the blocking state was applied
+        Assert.assertEquals(blockingApi.getBlockingStateFor(bundleId, callContext), blockingState);
+        // Verify the account_record_id was populated
+        getMysqlTestingHelper().getDBI().withHandle(new HandleCallback<Void>() {
+            @Override
+            public Void withHandle(final Handle handle) throws Exception {
+                final List<Map<String, Object>> values = handle.select("select account_record_id from blocking_states where id = ?", bundleId.toString());
+                Assert.assertEquals(values.size(), 1);
+                Assert.assertEquals(values.get(0).keySet().size(), 1);
+                Assert.assertEquals(values.get(0).get("account_record_id"), accountRecordId);
+                return null;
+            }
+        });
+    }
+}
diff --git a/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java b/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
index 432e491..821fb57 100644
--- a/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
+++ b/overdue/src/test/java/com/ning/billing/overdue/applicator/TestOverdueStateApplicator.java
@@ -41,6 +41,7 @@ import static com.jayway.awaitility.Awaitility.await;
 import static java.util.concurrent.TimeUnit.SECONDS;
 
 public class TestOverdueStateApplicator extends OverdueTestBase {
+
     @Inject
     OverdueStateApplicator<SubscriptionBundle> applicator;
 
@@ -69,7 +70,7 @@ public class TestOverdueStateApplicator extends OverdueTestBase {
         checkBussEvent("OD1");
 
         state = config.getBundleStateSet().findState("OD2");
-        applicator.apply(null, null,bundle, BlockingApi.CLEAR_STATE_NAME, state, internalCallContext);
+        applicator.apply(null, null, bundle, BlockingApi.CLEAR_STATE_NAME, state, internalCallContext);
         checkStateApplied(state);
         checkBussEvent("OD2");