killbill-memoizeit

Details

diff --git a/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentDao.java b/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentDao.java
index d77f9b3..2758f3f 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentDao.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/BusinessInvoicePaymentDao.java
@@ -44,8 +44,8 @@ import com.ning.billing.payment.api.PaymentMethodPlugin;
 import com.ning.billing.util.callcontext.InternalCallContext;
 import com.ning.billing.util.clock.Clock;
 import com.ning.billing.util.svcapi.account.AccountInternalApi;
-import com.ning.billing.util.svcapi.payment.PaymentInternalApi;
 import com.ning.billing.util.svcapi.invoice.InvoiceInternalApi;
+import com.ning.billing.util.svcapi.payment.PaymentInternalApi;
 
 public class BusinessInvoicePaymentDao {
 
@@ -95,12 +95,11 @@ public class BusinessInvoicePaymentDao {
             return;
         }
 
-        final PaymentMethod paymentMethod;
+        PaymentMethod paymentMethod = null;
         try {
             paymentMethod = paymentApi.getPaymentMethod(account, payment.getPaymentMethodId(), true, context);
         } catch (PaymentApiException e) {
-            log.warn("Ignoring payment {}: payment method {} does not exist", paymentId, payment.getPaymentMethodId());
-            return;
+            log.info("For payment {}: payment method {} does not exist", paymentId, payment.getPaymentMethodId());
         }
 
         Invoice invoice = null;
@@ -119,12 +118,22 @@ public class BusinessInvoicePaymentDao {
     }
 
     private void createPayment(final Account account, @Nullable final Invoice invoice, @Nullable final InvoicePayment invoicePayment, final Payment payment,
-                               final PaymentMethod paymentMethod, final String extFirstPaymentRefId, final String extSecondPaymentRefId,
+                               @Nullable final PaymentMethod paymentMethod, final String extFirstPaymentRefId, final String extSecondPaymentRefId,
                                final String message, final InternalCallContext context) {
-        final PaymentMethodPlugin pluginDetail = paymentMethod.getPluginDetail();
-        final String cardCountry = PaymentMethodUtils.getCardCountry(pluginDetail);
-        final String cardType = PaymentMethodUtils.getCardType(pluginDetail);
-        final String paymentMethodString = PaymentMethodUtils.getPaymentMethodType(pluginDetail);
+        // paymentMethod may be null if the payment method has been deleted
+        final String cardCountry;
+        final String cardType;
+        final String paymentMethodString;
+        if (paymentMethod != null) {
+            final PaymentMethodPlugin pluginDetail = paymentMethod.getPluginDetail();
+            cardCountry = PaymentMethodUtils.getCardCountry(pluginDetail);
+            cardType = PaymentMethodUtils.getCardType(pluginDetail);
+            paymentMethodString = PaymentMethodUtils.getPaymentMethodType(pluginDetail);
+        } else {
+            cardCountry = null;
+            cardType = null;
+            paymentMethodString = null;
+        }
 
         // invoicePayment may be null on payment failures
         final String invoicePaymentType;
diff --git a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
index 1a8dbde..65563d7 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
@@ -78,6 +78,7 @@ import com.google.common.collect.ImmutableMap;
 
 import static com.ning.billing.jaxrs.resources.JaxrsResource.ACCOUNTS;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.BUNDLES;
+import static com.ning.billing.jaxrs.resources.JaxrsResource.QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.QUERY_PAYMENT_METHOD_PLUGIN_INFO;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.SUBSCRIPTIONS;
 import static org.testng.Assert.assertEquals;
@@ -580,6 +581,13 @@ public abstract class KillbillClient extends ServerTestSuiteWithEmbeddedDB {
         return paymentMethodJson;
     }
 
+    protected void deletePaymentMethod(final String paymentMethodId, final Boolean deleteDefault) throws IOException {
+        final String paymentMethodURI = JaxrsResource.PAYMENT_METHODS_PATH + "/" + paymentMethodId;
+
+        final Response response = doDelete(paymentMethodURI, ImmutableMap.<String, String>of(QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF, deleteDefault.toString()), DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
+    }
+
     protected List<PaymentJsonSimple> getPaymentsForAccount(final String accountId) throws IOException {
         final String paymentsURI = JaxrsResource.ACCOUNTS_PATH + "/" + accountId + "/" + JaxrsResource.PAYMENTS;
         final Response paymentsResponse = doGet(paymentsURI, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
index 569bc3b..e22d58a 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
@@ -45,6 +45,7 @@ import com.google.common.collect.ImmutableMap;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 public class TestChargeback extends TestJaxrsBase {
@@ -52,30 +53,9 @@ public class TestChargeback extends TestJaxrsBase {
     @Test(groups = "slow")
     public void testAddChargeback() throws Exception {
         final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
-        final ChargebackJson input = new ChargebackJson(null, null, BigDecimal.TEN, payment.getPaymentId(), null, null);
-        final String jsonInput = mapper.writeValueAsString(input);
-
-        // Create the chargeback
-        Response response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode(), response.getResponseBody());
-
-        // Find the chargeback by location
-        final String location = response.getHeader("Location");
-        assertNotNull(location);
-        response = doGetWithUrl(location, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifySingleChargebackResponse(response, input);
-
-
-        // Find the chargeback by account
-        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifyCollectionChargebackResponse(response, input);
-
-        // Find the chargeback by payment
-        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + payment.getPaymentId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifyCollectionChargebackResponse(response, input);
+        createAndVerifyChargeback(payment);
     }
 
-
     @Test(groups = "slow")
     public void testMultipleChargeback() throws Exception {
         final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
@@ -86,7 +66,7 @@ public class TestChargeback extends TestJaxrsBase {
 
         //
         int count = 4;
-        Response response = null;
+        Response response;
         while (count-- > 0) {
             response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
             assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.CREATED.getStatusCode(), response.getResponseBody());
@@ -96,14 +76,13 @@ public class TestChargeback extends TestJaxrsBase {
         response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), response.getResponseBody());
 
-
         // Find the chargeback by account
         response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
         ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
         assertEquals(objFromJson.getChargebacks().size(), 4);
         for (int i = 0; i < objFromJson.getChargebacks().size(); i++) {
-            ChargebackJson chargeBack = objFromJson.getChargebacks().get(i);
+            final ChargebackJson chargeBack = objFromJson.getChargebacks().get(i);
             assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
             assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
         }
@@ -113,23 +92,23 @@ public class TestChargeback extends TestJaxrsBase {
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
         objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
         assertEquals(objFromJson.getChargebacks().size(), 4);
-
     }
 
+    @Test(groups = "slow")
+    public void testAddChargebackForDeletedPaymentMethod() throws Exception {
+        final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
 
-    private void verifyCollectionChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
-        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
-        final ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
-        assertEquals(objFromJson.getChargebacks().size(), 1);
-        ChargebackJson chargeBack = objFromJson.getChargebacks().get(0);
-        assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
-        assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
-    }
+        // Check the payment method exists
+        assertEquals(getAccountById(payment.getAccountId()).getPaymentMethodId(), payment.getPaymentMethodId());
+        assertEquals(getPaymentMethod(payment.getPaymentMethodId()).getAccountId(), payment.getAccountId());
 
-    private void verifySingleChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
-        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
-        final ChargebackJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackJson.class);
-        assertTrue(objFromJson.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+        // Delete the payment method
+        deletePaymentMethod(payment.getPaymentMethodId(), true);
+
+        // Check the payment method was deleted
+        assertNull(getAccountById(payment.getAccountId()).getPaymentMethodId());
+
+        createAndVerifyChargeback(payment);
     }
 
     @Test(groups = "slow")
@@ -173,6 +152,44 @@ public class TestChargeback extends TestJaxrsBase {
         //assertEquals(response.getStatusCode(),Status.NO_CONTENT.getStatusCode(), response.getResponseBody());
     }
 
+    private void createAndVerifyChargeback(final PaymentJsonSimple payment) throws IOException {
+        final ChargebackJson input = new ChargebackJson(null, null, BigDecimal.TEN, payment.getPaymentId(), null, null);
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Create the chargeback
+        Response response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode(), response.getResponseBody());
+
+        // Find the chargeback by location
+        final String location = response.getHeader("Location");
+        assertNotNull(location);
+        response = doGetWithUrl(location, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifySingleChargebackResponse(response, input);
+
+        // Find the chargeback by account
+        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifyCollectionChargebackResponse(response, input);
+
+        // Find the chargeback by payment
+        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + payment.getPaymentId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifyCollectionChargebackResponse(response, input);
+    }
+
+    private void verifyCollectionChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
+        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
+        final ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
+        assertEquals(objFromJson.getChargebacks().size(), 1);
+        final ChargebackJson chargeBack = objFromJson.getChargebacks().get(0);
+        assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+        assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
+    }
+
+    private void verifySingleChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
+        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
+        final ChargebackJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackJson.class);
+        assertTrue(objFromJson.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+    }
+
     private PaymentJsonSimple createAccountWithInvoiceAndPayment() throws Exception {
         final InvoiceJsonSimple invoice = createAccountWithInvoice();
         return getPayment(invoice);