killbill-memoizeit

analytics: populate missing fields in BAC We weren't populating

8/6/2012 4:39:38 PM

Details

diff --git a/analytics/src/main/java/com/ning/billing/analytics/BusinessAccountRecorder.java b/analytics/src/main/java/com/ning/billing/analytics/BusinessAccountRecorder.java
index 688644f..b4fd225 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/BusinessAccountRecorder.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/BusinessAccountRecorder.java
@@ -25,20 +25,24 @@ import org.joda.time.LocalDate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.inject.Inject;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.account.api.AccountApiException;
 import com.ning.billing.account.api.AccountData;
 import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.analytics.dao.BusinessAccountSqlDao;
 import com.ning.billing.analytics.model.BusinessAccount;
+import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoiceUserApi;
 import com.ning.billing.payment.api.Payment;
 import com.ning.billing.payment.api.PaymentApi;
 import com.ning.billing.payment.api.PaymentApiException;
+import com.ning.billing.payment.api.PaymentMethod;
+
+import com.google.inject.Inject;
 
 public class BusinessAccountRecorder {
+
     private static final Logger log = LoggerFactory.getLogger(BusinessAccountRecorder.class);
 
     private final BusinessAccountSqlDao sqlDao;
@@ -59,11 +63,7 @@ public class BusinessAccountRecorder {
         final Account account;
         try {
             account = accountApi.getAccountByKey(data.getExternalKey());
-            final BusinessAccount bac = new BusinessAccount(account.getId());
-            updateBusinessAccountFromAccount(account, bac);
-
-            log.info("ACCOUNT CREATION " + bac);
-            sqlDao.createAccount(bac);
+            accountUpdated(account.getId());
         } catch (AccountApiException e) {
             log.warn("Error encountered creating BusinessAccount", e);
         }
@@ -103,12 +103,14 @@ public class BusinessAccountRecorder {
     private void updateBusinessAccountFromAccount(final Account account, final BusinessAccount bac) {
         bac.setName(account.getName());
         bac.setKey(account.getExternalKey());
+        final Currency currency = account.getCurrency();
+        bac.setCurrency(currency != null ? currency.toString() : bac.getCurrency());
 
         try {
             LocalDate lastInvoiceDate = bac.getLastInvoiceDate();
             BigDecimal totalInvoiceBalance = bac.getTotalInvoiceBalance();
             String lastPaymentStatus = bac.getLastPaymentStatus();
-            String paymentMethod = bac.getPaymentMethod();
+            String paymentMethodType = bac.getPaymentMethod();
             String creditCardType = bac.getCreditCardType();
             String billingAddressCountry = bac.getBillingAddressCountry();
 
@@ -134,17 +136,23 @@ public class BusinessAccountRecorder {
                         if (lastPaymentDate == null || cur.getEffectiveDate().isAfter(lastPaymentDate)) {
                             lastPaymentDate = cur.getEffectiveDate();
                             lastPaymentStatus = cur.getPaymentStatus().toString();
-                            // TODO STEPH talk to Pierre
-                            paymentMethod = null;
-                            creditCardType = null;
-                            billingAddressCountry = null;
                         }
                     }
                 }
             }
 
+            // Retrieve payment methods
+            for (final PaymentMethod paymentMethod : paymentApi.getPaymentMethods(account, true)) {
+                if (paymentMethod.getId().equals(account.getPaymentMethodId()) && paymentMethod.getPluginDetail() != null) {
+                    paymentMethodType = PaymentMethodUtils.getPaymentMethodType(paymentMethod.getPluginDetail());
+                    creditCardType = PaymentMethodUtils.getCardType(paymentMethod.getPluginDetail());
+                    billingAddressCountry = PaymentMethodUtils.getCardCountry(paymentMethod.getPluginDetail());
+                    break;
+                }
+            }
+
             bac.setLastPaymentStatus(lastPaymentStatus);
-            bac.setPaymentMethod(paymentMethod);
+            bac.setPaymentMethod(paymentMethodType);
             bac.setCreditCardType(creditCardType);
             bac.setBillingAddressCountry(billingAddressCountry);
             bac.setLastInvoiceDate(lastInvoiceDate);
diff --git a/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentRecorder.java b/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentRecorder.java
index 2548ac8..aa5bb1b 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentRecorder.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentRecorder.java
@@ -16,9 +16,10 @@
 
 package com.ning.billing.analytics;
 
+import java.util.UUID;
+
 import javax.annotation.Nullable;
 import javax.inject.Inject;
-import java.util.UUID;
 
 import org.skife.jdbi.v2.Transaction;
 import org.skife.jdbi.v2.TransactionStatus;
@@ -42,6 +43,7 @@ import com.ning.billing.payment.api.PaymentMethodPlugin;
 import com.ning.billing.util.clock.Clock;
 
 public class BusinessInvoicePaymentRecorder {
+
     private static final Logger log = LoggerFactory.getLogger(BusinessInvoicePaymentRecorder.class);
 
     private final BusinessInvoicePaymentSqlDao invoicePaymentSqlDao;
@@ -98,11 +100,9 @@ public class BusinessInvoicePaymentRecorder {
     private void createPayment(final Account account, @Nullable final InvoicePayment invoicePayment, final Payment payment,
                                final PaymentMethod paymentMethod, final String extFirstPaymentRefId, final String extSecondPaymentRefId, final String message) {
         final PaymentMethodPlugin pluginDetail = paymentMethod.getPluginDetail();
-        // TODO - make it generic
-        final String cardCountry = pluginDetail != null ? pluginDetail.getValueString("country") : null;
-        final String cardType = pluginDetail != null ? pluginDetail.getValueString("cardType") : null;
-        // TODO support CreditCard, DebitCard, WireTransfer, BankTransfer, Check, ACH, Cash, Paypal
-        final String paymentMethodString = cardType != null ? "CreditCard" : "Other";
+        final String cardCountry = PaymentMethodUtils.getCardCountry(pluginDetail);
+        final String cardType = PaymentMethodUtils.getCardType(pluginDetail);
+        final String paymentMethodString = PaymentMethodUtils.getPaymentMethodType(pluginDetail);
 
         invoicePaymentSqlDao.inTransaction(new Transaction<Void, BusinessInvoicePaymentSqlDao>() {
             @Override
diff --git a/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountBinder.java b/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountBinder.java
index 50b857b..7aff8db 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountBinder.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountBinder.java
@@ -63,6 +63,7 @@ public @interface BusinessAccountBinder {
                     q.bind("payment_method", account.getPaymentMethod());
                     q.bind("credit_card_type", account.getCreditCardType());
                     q.bind("billing_address_country", account.getBillingAddressCountry());
+                    q.bind("currency", account.getCurrency());
                 }
             };
         }
diff --git a/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountMapper.java b/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountMapper.java
index ca245a0..b9eaa54 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountMapper.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/dao/BusinessAccountMapper.java
@@ -42,7 +42,8 @@ public class BusinessAccountMapper extends MapperBase implements ResultSetMapper
                 r.getString(9),
                 r.getString(10),
                 r.getString(11),
-                r.getString(12)
+                r.getString(12),
+                r.getString(13)
         );
         account.setCreatedDt(new DateTime(r.getLong(3), DateTimeZone.UTC));
         account.setUpdatedDt(new DateTime(r.getLong(4), DateTimeZone.UTC));
diff --git a/analytics/src/main/java/com/ning/billing/analytics/model/BusinessAccount.java b/analytics/src/main/java/com/ning/billing/analytics/model/BusinessAccount.java
index 116597e..66459f1 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/model/BusinessAccount.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/model/BusinessAccount.java
@@ -27,6 +27,7 @@ import com.ning.billing.analytics.utils.Rounder;
 import com.google.common.base.Objects;
 
 public class BusinessAccount {
+
     // Populated by the database
     private DateTime createdDt = null;
     private DateTime updatedDt = null;
@@ -41,6 +42,7 @@ public class BusinessAccount {
     private String paymentMethod;
     private String creditCardType;
     private String billingAddressCountry;
+    private String currency;
 
     public BusinessAccount(final UUID accountId) {
         this.accountId = accountId;
@@ -48,7 +50,8 @@ public class BusinessAccount {
 
     public BusinessAccount(final UUID accountId, final String key, final String name, final BigDecimal balance,
                            final LocalDate lastInvoiceDate, final BigDecimal totalInvoiceBalance, final String lastPaymentStatus,
-                           final String paymentMethod, final String creditCardType, final String billingAddressCountry) {
+                           final String paymentMethod, final String creditCardType, final String billingAddressCountry,
+                           final String currency) {
         this.accountId = accountId;
         this.key = key;
         this.balance = balance;
@@ -59,6 +62,7 @@ public class BusinessAccount {
         this.name = name;
         this.paymentMethod = paymentMethod;
         this.totalInvoiceBalance = totalInvoiceBalance;
+        this.currency = currency;
     }
 
     public UUID getAccountId() {
@@ -109,6 +113,14 @@ public class BusinessAccount {
         this.creditCardType = creditCardType;
     }
 
+    public String getCurrency() {
+        return currency;
+    }
+
+    public void setCurrency(final String currency) {
+        this.currency = currency;
+    }
+
     public LocalDate getLastInvoiceDate() {
         return lastInvoiceDate;
     }
@@ -177,6 +189,7 @@ public class BusinessAccount {
         sb.append(", paymentMethod='").append(paymentMethod).append('\'');
         sb.append(", creditCardType='").append(creditCardType).append('\'');
         sb.append(", billingAddressCountry='").append(billingAddressCountry).append('\'');
+        sb.append(", currency='").append(currency).append('\'');
         sb.append('}');
         return sb.toString();
     }
@@ -204,6 +217,9 @@ public class BusinessAccount {
         if (creditCardType != null ? !creditCardType.equals(that.creditCardType) : that.creditCardType != null) {
             return false;
         }
+        if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
+            return false;
+        }
         if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) {
             return false;
         }
@@ -246,6 +262,7 @@ public class BusinessAccount {
         result = 31 * result + (paymentMethod != null ? paymentMethod.hashCode() : 0);
         result = 31 * result + (creditCardType != null ? creditCardType.hashCode() : 0);
         result = 31 * result + (billingAddressCountry != null ? billingAddressCountry.hashCode() : 0);
+        result = 31 * result + (currency != null ? currency.hashCode() : 0);
         return result;
     }
 }
diff --git a/analytics/src/main/java/com/ning/billing/analytics/PaymentMethodUtils.java b/analytics/src/main/java/com/ning/billing/analytics/PaymentMethodUtils.java
new file mode 100644
index 0000000..eb69b7c
--- /dev/null
+++ b/analytics/src/main/java/com/ning/billing/analytics/PaymentMethodUtils.java
@@ -0,0 +1,60 @@
+/*
+ * 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.analytics;
+
+import javax.annotation.Nullable;
+
+import com.ning.billing.payment.api.PaymentMethodPlugin;
+
+import com.google.common.annotations.VisibleForTesting;
+
+// TODO - make it generic
+public class PaymentMethodUtils {
+
+    @VisibleForTesting
+    static final String COUNTRY_KEY = "country";
+    @VisibleForTesting
+    static final String CARD_TYPE_KEY = "cardType";
+    @VisibleForTesting
+    static final String TYPE_KEY = "type";
+
+    private PaymentMethodUtils() {}
+
+    public static String getCardCountry(@Nullable final PaymentMethodPlugin pluginDetail) {
+        if (pluginDetail == null) {
+            return null;
+        }
+
+        return pluginDetail.getValueString(COUNTRY_KEY);
+    }
+
+    public static String getCardType(@Nullable final PaymentMethodPlugin pluginDetail) {
+        if (pluginDetail == null) {
+            return null;
+        }
+
+        return pluginDetail.getValueString(CARD_TYPE_KEY);
+    }
+
+    public static String getPaymentMethodType(@Nullable final PaymentMethodPlugin pluginDetail) {
+        if (pluginDetail == null) {
+            return null;
+        }
+
+        return pluginDetail.getValueString(TYPE_KEY);
+    }
+}
diff --git a/analytics/src/main/resources/com/ning/billing/analytics/dao/BusinessAccountSqlDao.sql.stg b/analytics/src/main/resources/com/ning/billing/analytics/dao/BusinessAccountSqlDao.sql.stg
index 66052ec..0fa4317 100644
--- a/analytics/src/main/resources/com/ning/billing/analytics/dao/BusinessAccountSqlDao.sql.stg
+++ b/analytics/src/main/resources/com/ning/billing/analytics/dao/BusinessAccountSqlDao.sql.stg
@@ -14,6 +14,7 @@ getAccount(account_id) ::= <<
   , payment_method
   , credit_card_type
   , billing_address_country
+  , currency
   from bac
   where account_id=:account_id
   limit 1
@@ -34,6 +35,7 @@ getAccountByKey(account_key) ::= <<
   , payment_method
   , credit_card_type
   , billing_address_country
+  , currency
   from bac
   where account_key=:account_key
   limit 1
@@ -54,6 +56,7 @@ createAccount() ::= <<
   , payment_method
   , credit_card_type
   , billing_address_country
+  , currency
   ) values (
     :account_id
   , :account_key
@@ -67,6 +70,7 @@ createAccount() ::= <<
   , :payment_method
   , :credit_card_type
   , :billing_address_country
+  , :currency
   );
 >>
 
@@ -81,6 +85,7 @@ saveAccount() ::= <<
   , payment_method=:payment_method
   , credit_card_type=:credit_card_type
   , billing_address_country=:billing_address_country
+  , currency=:currency
   where account_id=:account_id
   ;
 >>
diff --git a/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java b/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java
index 8c3041a..5419a0d 100644
--- a/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java
+++ b/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java
@@ -74,8 +74,11 @@ import com.ning.billing.mock.MockAccountBuilder;
 import com.ning.billing.mock.MockPlan;
 import com.ning.billing.payment.api.DefaultPaymentInfoEvent;
 import com.ning.billing.payment.api.PaymentInfoEvent;
+import com.ning.billing.payment.api.PaymentMethod;
 import com.ning.billing.payment.api.PaymentStatus;
+import com.ning.billing.payment.dao.PaymentAttemptModelDao;
 import com.ning.billing.payment.dao.PaymentDao;
+import com.ning.billing.payment.dao.PaymentModelDao;
 import com.ning.billing.util.bus.Bus;
 import com.ning.billing.util.callcontext.CallContext;
 import com.ning.billing.util.callcontext.CallOrigin;
@@ -96,9 +99,9 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
     final PlanPhase phase = new MockPhase(PhaseType.EVERGREEN, plan, MockDuration.UNLIMITED(), 25.95);
 
     private static final Long TOTAL_ORDERING = 11L;
-    private static final String EXTERNAL_KEY = "12345";
+    private static final String BUNDLE_EXTERNAL_KEY = UUID.randomUUID().toString();
     private static final UUID ACCOUNT_ID = UUID.randomUUID();
-    private static final String ACCOUNT_KEY = "pierre-12345";
+    private static final String ACCOUNT_KEY = UUID.randomUUID().toString();
     private static final Currency ACCOUNT_CURRENCY = Currency.EUR;
     private static final BigDecimal INVOICE_AMOUNT = BigDecimal.valueOf(1243.11);
 
@@ -153,9 +156,13 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
 
     @BeforeMethod(groups = "slow")
     public void createMocks() {
+        final PaymentMethod paymentMethod = Mockito.mock(PaymentMethod.class);
+        final UUID paymentMethodId = UUID.randomUUID();
+        Mockito.when(paymentMethod.getId()).thenReturn(paymentMethodId);
         final Account account = new MockAccountBuilder(UUID.randomUUID())
                 .externalKey(ACCOUNT_KEY)
                 .currency(ACCOUNT_CURRENCY)
+                .paymentMethodId(paymentMethodId)
                 .build();
 
         try {
@@ -171,11 +178,11 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
     }
 
     private void createSubscriptionTransitionEvent(final Account account) throws EntitlementUserApiException {
-        final SubscriptionBundle bundle = entitlementApi.createBundleForAccount(account.getId(), EXTERNAL_KEY, context);
+        final SubscriptionBundle bundle = entitlementApi.createBundleForAccount(account.getId(), BUNDLE_EXTERNAL_KEY, context);
 
         // Verify we correctly initialized the account subsystem
         Assert.assertNotNull(bundle);
-        Assert.assertEquals(bundle.getKey(), EXTERNAL_KEY);
+        Assert.assertEquals(bundle.getKey(), BUNDLE_EXTERNAL_KEY);
 
         // Create a subscription transition event
         final UUID subscriptionId = UUID.randomUUID();
@@ -205,7 +212,7 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
         expectedTransition = new BusinessSubscriptionTransition(
                 TOTAL_ORDERING,
                 transition.getBundleId(),
-                EXTERNAL_KEY,
+                BUNDLE_EXTERNAL_KEY,
                 ACCOUNT_ID,
                 ACCOUNT_KEY,
                 transition.getSubscriptionId(),
@@ -236,21 +243,18 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
         invoiceCreationNotification = new DefaultInvoiceCreationEvent(invoice.getId(), account.getId(),
                                                                       INVOICE_AMOUNT, ACCOUNT_CURRENCY, null);
 
-        paymentInfoNotification = new DefaultPaymentInfoEvent(account.getId(), invoices.get(0).getId(), null, invoices.get(0).getBalance(), -1, PaymentStatus.UNKNOWN, null, null, null, clock.getUTCNow());
-
-        //STEPH talk to Pierre
-        /*
-        paymentInfoNotification = new DefaultPaymentInfoEvent.Builder().setId(UUID.randomUUID()).setExternalPaymentId("12345abcdef").setPaymentMethod(PAYMENT_METHOD).setCardCountry(CARD_COUNTRY).build();
-        final PaymentAttempt2 paymentAttempt = new DefaultPaymentAttempt2(UUID.randomUUID(), invoice.getId(), account.getId(), BigDecimal.TEN,
-                ACCOUNT_CURRENCY, clock.getUTCNow(), clock.getUTCNow(), paymentInfoNotification.getId(), 1, new DateTime(), new DateTime(), PaymentAttemptStatus.COMPLETED_SUCCESS);
-        paymentDao.createPaymentAttempt(paymentAttempt, PaymentAttemptStatus.COMPLETED_SUCCESS, context);
-        paymentDao.insertPaymentInfoWithPaymentAttemptUpdate(paymentInfoNotification, paymentAttempt.getId(), context);
-        Assert.assertEquals(paymentDao.getPaymentInfoList(Arrays.asList(invoice.getId())).size(), 1);
-    */
+        paymentInfoNotification = new DefaultPaymentInfoEvent(account.getId(), invoices.get(0).getId(), null, invoices.get(0).getBalance(), -1,
+                                                              PaymentStatus.UNKNOWN, null, null, null, clock.getUTCNow());
+
+        final PaymentModelDao paymentInfo = new PaymentModelDao(account.getId(), invoice.getId(), account.getPaymentMethodId(),
+                                                                BigDecimal.ONE, Currency.USD, clock.getUTCNow(), PaymentStatus.SUCCESS);
+        final PaymentAttemptModelDao paymentAttempt = new PaymentAttemptModelDao(account.getId(), invoice.getId(), paymentInfo.getId(),
+                                                                                 clock.getUTCNow(), BigDecimal.ONE);
+        paymentDao.insertPaymentWithAttempt(paymentInfo, paymentAttempt, context);
+        Assert.assertEquals(paymentDao.getPaymentsForAccount(account.getId()).size(), 1);
     }
 
-    // STEPH talk to Pierre -- see previous remark hence disable test
-    @Test(groups = "slow", enabled = false)
+    @Test(groups = "slow")
     public void testRegisterForNotifications() throws Exception {
         // Make sure the service has been instantiated
         Assert.assertEquals(service.getName(), "analytics-service");
@@ -265,27 +269,26 @@ public class TestAnalyticsService extends AnalyticsTestSuiteWithEmbeddedDB {
         Assert.assertNull(accountSqlDao.getAccountByKey(ACCOUNT_KEY));
 
         // Send events and wait for the async part...
-        bus.post(transition);
         bus.post(accountCreationNotification);
         Thread.sleep(5000);
+        Assert.assertNotNull(accountSqlDao.getAccountByKey(ACCOUNT_KEY));
 
-        Assert.assertEquals(subscriptionSqlDao.getTransitionsByKey(EXTERNAL_KEY).size(), 1);
-        Assert.assertEquals(subscriptionSqlDao.getTransitionsByKey(EXTERNAL_KEY).get(0), expectedTransition);
+        // Test subscriptions integration - this is just to exercise the code. It's hard to test the actual subscriptions
+        // as we would need to mock a bunch of APIs (see integration tests in Beatrix instead)
+        bus.post(transition);
 
         // Test invoice integration - the account creation notification has triggered a BAC update
-        Assert.assertTrue(accountSqlDao.getAccountByKey(ACCOUNT_KEY).getTotalInvoiceBalance().compareTo(INVOICE_AMOUNT) == 0);
+        Assert.assertEquals(accountSqlDao.getAccountByKey(ACCOUNT_KEY).getTotalInvoiceBalance().compareTo(INVOICE_AMOUNT), 1);
 
         // Post the same invoice event again - the invoice balance shouldn't change
         bus.post(invoiceCreationNotification);
         Thread.sleep(5000);
-        Assert.assertTrue(accountSqlDao.getAccountByKey(ACCOUNT_KEY).getTotalInvoiceBalance().compareTo(INVOICE_AMOUNT) == 0);
+        Assert.assertEquals(accountSqlDao.getAccountByKey(ACCOUNT_KEY).getTotalInvoiceBalance().compareTo(INVOICE_AMOUNT), 1);
 
         // Test payment integration - the fields have already been populated, just make sure the code is exercised
+        // It's hard to test the actual payments fields though in bac, since we should mock the plugin
         bus.post(paymentInfoNotification);
         Thread.sleep(5000);
-        // STEPH talk to Pierre
-        //Assert.assertEquals(accountDao.getAccount(ACCOUNT_KEY).getPaymentMethod(), PAYMENT_METHOD);
-        //Assert.assertEquals(accountDao.getAccount(ACCOUNT_KEY).getBillingAddressCountry(), CARD_COUNTRY);
 
         // Test the shutdown sequence
         try {
diff --git a/analytics/src/test/java/com/ning/billing/analytics/dao/TestAnalyticsDao.java b/analytics/src/test/java/com/ning/billing/analytics/dao/TestAnalyticsDao.java
index 6d10461..ca8b024 100644
--- a/analytics/src/test/java/com/ning/billing/analytics/dao/TestAnalyticsDao.java
+++ b/analytics/src/test/java/com/ning/billing/analytics/dao/TestAnalyticsDao.java
@@ -59,6 +59,7 @@ public class TestAnalyticsDao extends AnalyticsTestSuiteWithEmbeddedDB {
     private static final String EXTERNAL_KEY = "23456";
     private static final UUID ACCOUNT_ID = UUID.randomUUID();
     private static final String ACCOUNT_KEY = "pierre-143343-vcc";
+    private static final String CURRENCY = UUID.randomUUID().toString();
 
     private final Clock clock = new DefaultClock();
     private final Product product = new MockProduct("platinium", "subscription", ProductCategory.BASE);
@@ -105,7 +106,8 @@ public class TestAnalyticsDao extends AnalyticsTestSuiteWithEmbeddedDB {
     }
 
     private void setupBusinessAccount() {
-        account = new BusinessAccount(UUID.randomUUID(), ACCOUNT_KEY, UUID.randomUUID().toString(), BigDecimal.ONE, clock.getUTCToday(), BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa", "FRANCE");
+        account = new BusinessAccount(UUID.randomUUID(), ACCOUNT_KEY, UUID.randomUUID().toString(), BigDecimal.ONE, clock.getUTCToday(),
+                                      BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa", "FRANCE", CURRENCY);
 
         final IDBI dbi = helper.getDBI();
         businessAccountSqlDao = dbi.onDemand(BusinessAccountSqlDao.class);
@@ -287,11 +289,13 @@ public class TestAnalyticsDao extends AnalyticsTestSuiteWithEmbeddedDB {
         final DateTime previousUpdatedDt = account.getUpdatedDt();
         account.setBalance(BigDecimal.TEN);
         account.setPaymentMethod("PayPal");
+        account.setCurrency("CAD");
         businessAccountSqlDao.saveAccount(account);
         // Verify the save worked as expected
         account = businessAccountSqlDao.getAccountByKey(ACCOUNT_KEY);
         Assert.assertEquals(Rounder.round(BigDecimal.TEN), account.getRoundedBalance());
         Assert.assertEquals("PayPal", account.getPaymentMethod());
+        Assert.assertEquals("CAD", account.getCurrency());
         Assert.assertTrue(account.getUpdatedDt().compareTo(previousUpdatedDt) > 0);
 
         // ACCOUNT not found
diff --git a/analytics/src/test/java/com/ning/billing/analytics/model/TestBusinessAccount.java b/analytics/src/test/java/com/ning/billing/analytics/model/TestBusinessAccount.java
index 251072d..8629bce 100644
--- a/analytics/src/test/java/com/ning/billing/analytics/model/TestBusinessAccount.java
+++ b/analytics/src/test/java/com/ning/billing/analytics/model/TestBusinessAccount.java
@@ -19,7 +19,6 @@ package com.ning.billing.analytics.model;
 import java.math.BigDecimal;
 import java.util.UUID;
 
-import org.joda.time.LocalDate;
 import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
@@ -37,7 +36,7 @@ public class TestBusinessAccount extends AnalyticsTestSuite {
     @BeforeMethod(groups = "fast")
     public void setUp() throws Exception {
         account = new BusinessAccount(UUID.randomUUID(), "pierre", UUID.randomUUID().toString(), BigDecimal.ONE, clock.getUTCToday(),
-                                      BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa", "");
+                                      BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa", "", UUID.randomUUID().toString());
     }
 
     @Test(groups = "fast")
@@ -46,8 +45,10 @@ public class TestBusinessAccount extends AnalyticsTestSuite {
         Assert.assertEquals(account, account);
         Assert.assertTrue(account.equals(account));
 
-        final BusinessAccount otherAccount = new BusinessAccount(UUID.randomUUID(), "pierre cardin", UUID.randomUUID().toString(), BigDecimal.ONE, clock.getUTCToday(),
-                                                                 BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa", "");
+        final BusinessAccount otherAccount = new BusinessAccount(UUID.randomUUID(), "pierre cardin", UUID.randomUUID().toString(),
+                                                                 BigDecimal.ONE, clock.getUTCToday(),
+                                                                 BigDecimal.TEN, "ERROR_NOT_ENOUGH_FUNDS", "CreditCard", "Visa",
+                                                                 "", UUID.randomUUID().toString());
         Assert.assertFalse(account.equals(otherAccount));
     }
 
diff --git a/analytics/src/test/java/com/ning/billing/analytics/TestPaymentMethodUtils.java b/analytics/src/test/java/com/ning/billing/analytics/TestPaymentMethodUtils.java
new file mode 100644
index 0000000..1ee75ec
--- /dev/null
+++ b/analytics/src/test/java/com/ning/billing/analytics/TestPaymentMethodUtils.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2010-2012 NingInc.
+ *
+ * Ning licenses this file to you under the Apache Licenseversion 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 writingsoftware
+ * distributed under the License is distributed on an "AS IS" BASISWITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KINDeither express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.analytics;
+
+import java.util.UUID;
+
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.payment.api.PaymentMethodPlugin;
+
+public class TestPaymentMethodUtils {
+
+    @Test(groups = "fast")
+    public void testUnknowns() throws Exception {
+        Assert.assertNull(PaymentMethodUtils.getCardCountry(null));
+        Assert.assertNull(PaymentMethodUtils.getCardType(null));
+        Assert.assertNull(PaymentMethodUtils.getPaymentMethodType(null));
+
+        final PaymentMethodPlugin paymentMethodPlugin = Mockito.mock(PaymentMethodPlugin.class);
+        Assert.assertNull(PaymentMethodUtils.getCardCountry(paymentMethodPlugin));
+        Assert.assertNull(PaymentMethodUtils.getCardType(paymentMethodPlugin));
+        Assert.assertNull(PaymentMethodUtils.getPaymentMethodType(paymentMethodPlugin));
+    }
+
+    @Test(groups = "fast")
+    public void testCardCountry() throws Exception {
+        final String country = UUID.randomUUID().toString();
+        final String cardType = UUID.randomUUID().toString();
+        final String type = UUID.randomUUID().toString();
+
+        final PaymentMethodPlugin paymentMethodPlugin = Mockito.mock(PaymentMethodPlugin.class);
+        Mockito.when(paymentMethodPlugin.getValueString(PaymentMethodUtils.COUNTRY_KEY)).thenReturn(country);
+        Mockito.when(paymentMethodPlugin.getValueString(PaymentMethodUtils.CARD_TYPE_KEY)).thenReturn(cardType);
+        Mockito.when(paymentMethodPlugin.getValueString(PaymentMethodUtils.TYPE_KEY)).thenReturn(type);
+
+        Assert.assertEquals(PaymentMethodUtils.getCardCountry(paymentMethodPlugin), country);
+        Assert.assertEquals(PaymentMethodUtils.getCardType(paymentMethodPlugin), cardType);
+        Assert.assertEquals(PaymentMethodUtils.getPaymentMethodType(paymentMethodPlugin), type);
+    }
+}