killbill-memoizeit

Details

diff --git a/api/src/main/java/com/ning/billing/payment/api/PaymentError.java b/api/src/main/java/com/ning/billing/payment/api/PaymentError.java
index f1474c8..d9b8c49 100644
--- a/api/src/main/java/com/ning/billing/payment/api/PaymentError.java
+++ b/api/src/main/java/com/ning/billing/payment/api/PaymentError.java
@@ -15,6 +15,8 @@
  */
 
 package com.ning.billing.payment.api;
+import java.util.UUID;
+
 import org.codehaus.jackson.annotate.JsonTypeInfo;
 import org.codehaus.jackson.annotate.JsonTypeInfo.Id;
 
@@ -24,15 +26,21 @@ import com.ning.billing.util.bus.BusEvent;
 public class PaymentError implements BusEvent {
     private final String type;
     private final String message;
+    private final UUID accountId;
+    private final UUID invoiceId;
 
-    public PaymentError(PaymentError src) {
+    public PaymentError(PaymentError src, UUID accountId, UUID invoiceId) {
         this.type = src.type;
         this.message = src.message;
+        this.accountId = accountId;
+        this.invoiceId = invoiceId;
     }
 
-    public PaymentError(String type, String message) {
+    public PaymentError(String type, String message, UUID accountId, UUID invoiceId) {
         this.type = type;
         this.message = message;
+        this.accountId = accountId;
+        this.invoiceId = invoiceId;
     }
 
     public String getType() {
@@ -43,10 +51,20 @@ public class PaymentError implements BusEvent {
         return message;
     }
 
+    public UUID getInvoiceId() {
+        return invoiceId;
+    }
+
+    public UUID getAccountId() {
+        return accountId;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
+        result = prime * result + ((accountId == null) ? 0 : accountId.hashCode());
+        result = prime * result + ((invoiceId == null) ? 0 : invoiceId.hashCode());
         result = prime * result + ((message == null) ? 0 : message.hashCode());
         result = prime * result + ((type == null) ? 0 : type.hashCode());
         return result;
@@ -61,6 +79,18 @@ public class PaymentError implements BusEvent {
         if (getClass() != obj.getClass())
             return false;
         PaymentError other = (PaymentError) obj;
+        if (accountId == null) {
+            if (other.accountId != null)
+                return false;
+        }
+        else if (!accountId.equals(other.accountId))
+            return false;
+        if (invoiceId == null) {
+            if (other.invoiceId != null)
+                return false;
+        }
+        else if (!invoiceId.equals(other.invoiceId))
+            return false;
         if (message == null) {
             if (other.message != null)
                 return false;
@@ -78,6 +108,7 @@ public class PaymentError implements BusEvent {
 
     @Override
     public String toString() {
-        return "PaymentError [type=" + type + ", message=" + message + "]";
+        return "PaymentError [type=" + type + ", message=" + message + ", accountId=" + accountId + ", invoiceId=" + invoiceId + "]";
     }
+
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
index 7f40a5c..4dd5040 100644
--- a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
+++ b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentApi.java
@@ -148,14 +148,20 @@ public class DefaultPaymentApi implements PaymentApi {
                 if (invoice.getBalance().compareTo(BigDecimal.ZERO) <= 0 ) {
                     // TODO: send a notification that invoice was ignored?
                     log.info("Received invoice for payment with outstanding amount of 0 {} ", invoice);
-                    return Either.left(new PaymentError("invoice_balance_0", "Invoice balance was 0 or less"));
+                    return Either.left(new PaymentError("invoice_balance_0",
+                                                        "Invoice balance was 0 or less",
+                                                        paymentAttempt.getAccountId(),
+                                                        paymentAttempt.getInvoiceId()));
                 }
                 else {
                     return processPayment(getPaymentProviderPlugin(account), account, invoice, paymentAttempt);
                 }
             }
         }
-        return Either.left(new PaymentError("retry_payment_error", "Could not load payment attempt, invoice or account for id " + paymentAttemptId));
+        return Either.left(new PaymentError("retry_payment_error",
+                                            "Could not load payment attempt, invoice or account for id " + paymentAttemptId,
+                                            paymentAttempt.getAccountId(),
+                                            paymentAttempt.getInvoiceId()));
     }
 
     @Override
@@ -170,7 +176,10 @@ public class DefaultPaymentApi implements PaymentApi {
             if (invoice.getBalance().compareTo(BigDecimal.ZERO) <= 0 ) {
                 // TODO: send a notification that invoice was ignored?
                 log.info("Received invoice for payment with balance of 0 {} ", invoice);
-                Either<PaymentError, PaymentInfo> result = Either.left(new PaymentError("invoice_balance_0", "Invoice balance was 0 or less"));
+                Either<PaymentError, PaymentInfo> result = Either.left(new PaymentError("invoice_balance_0",
+                                                                                        "Invoice balance was 0 or less",
+                                                                                        account.getId(),
+                                                                                        UUID.fromString(invoiceId)));
                 processedPaymentsOrErrors.add(result);
             }
             else {
diff --git a/payment/src/main/java/com/ning/billing/payment/provider/NoOpPaymentProviderPlugin.java b/payment/src/main/java/com/ning/billing/payment/provider/NoOpPaymentProviderPlugin.java
index 71c7e41..280d1e0 100644
--- a/payment/src/main/java/com/ning/billing/payment/provider/NoOpPaymentProviderPlugin.java
+++ b/payment/src/main/java/com/ning/billing/payment/provider/NoOpPaymentProviderPlugin.java
@@ -53,7 +53,10 @@ public class NoOpPaymentProviderPlugin implements PaymentProviderPlugin {
 
     @Override
     public Either<PaymentError, String> createPaymentProviderAccount(Account account) {
-        return Either.left(new PaymentError("unsupported", "Account creation not supported in this plugin"));
+        return Either.left(new PaymentError("unsupported",
+                                            "Account creation not supported in this plugin",
+                                            account.getId(),
+                                            null));
     }
 
     @Override
diff --git a/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java b/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
index 7155d63..dee2a28 100644
--- a/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
+++ b/payment/src/test/java/com/ning/billing/payment/provider/MockPaymentProviderPlugin.java
@@ -52,7 +52,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
     @Override
     public Either<PaymentError, PaymentInfo> processInvoice(Account account, Invoice invoice) {
         if (makeNextInvoiceFail.getAndSet(false)) {
-            return Either.left(new PaymentError("unknown", "test error"));
+            return Either.left(new PaymentError("unknown", "test error", account.getId(), invoice.getId()));
         }
         else {
             PaymentInfo payment = new PaymentInfo.Builder().setPaymentId(UUID.randomUUID().toString())
@@ -75,7 +75,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
         PaymentInfo payment = payments.get(paymentId);
 
         if (payment == null) {
-            return Either.left(new PaymentError("notfound", "No payment found for id " + paymentId));
+            return Either.left(new PaymentError("notfound", "No payment found for id " + paymentId, null, null));
         }
         else {
             return Either.right(payment);
@@ -94,7 +94,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
             return Either.right(id);
         }
         else {
-            return Either.left(new PaymentError("unknown", "Did not get account to create payment provider account"));
+            return Either.left(new PaymentError("unknown", "Did not get account to create payment provider account", null, null));
         }
     }
 
@@ -104,7 +104,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
             return Either.right(accounts.get(accountKey));
         }
         else {
-            return Either.left(new PaymentError("unknown", "Did not get account for accountKey " + accountKey));
+            return Either.left(new PaymentError("unknown", "Did not get account for accountKey " + accountKey, null, null));
         }
     }
 
@@ -136,7 +136,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
                     realPaymentMethod = new CreditCardPaymentMethodInfo.Builder(ccPaymentMethod).setId(paymentMethodId).build();
                 }
                 if (realPaymentMethod == null) {
-                    return Either.left(new PaymentError("unsupported", "Payment method " + paymentMethod.getType() + " not supported by the plugin"));
+                    return Either.left(new PaymentError("unsupported", "Payment method " + paymentMethod.getType() + " not supported by the plugin", null, null));
                 }
                 else {
                     if (shouldBeDefault) {
@@ -147,11 +147,11 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
                 }
             }
                 else {
-                    return Either.left(new PaymentError("noaccount", "Could not retrieve account for accountKey " + accountKey));
+                    return Either.left(new PaymentError("noaccount", "Could not retrieve account for accountKey " + accountKey, null, null));
                 }
         }
         else {
-            return Either.left(new PaymentError("unknown", "Could not create add payment method " + paymentMethod + " for " + accountKey));
+            return Either.left(new PaymentError("unknown", "Could not create add payment method " + paymentMethod + " for " + accountKey, null, null));
         }
     }
 
@@ -195,7 +195,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
                 realPaymentMethod = new CreditCardPaymentMethodInfo.Builder(ccPaymentMethod).build();
             }
             if (realPaymentMethod == null) {
-                return Either.left(new PaymentError("unsupported", "Payment method " + paymentMethod.getType() + " not supported by the plugin"));
+                return Either.left(new PaymentError("unsupported", "Payment method " + paymentMethod.getType() + " not supported by the plugin", null, null));
             }
             else {
                 paymentMethods.put(paymentMethod.getId(), paymentMethod);
@@ -203,7 +203,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
             }
         }
         else {
-            return Either.left(new PaymentError("unknown", "Could not create add payment method " + paymentMethod + " for " + accountKey));
+            return Either.left(new PaymentError("unknown", "Could not create add payment method " + paymentMethod + " for " + accountKey, null, null));
         }
     }
 
@@ -213,11 +213,11 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
         if (paymentMethodInfo != null) {
             if (Boolean.FALSE.equals(paymentMethodInfo.getDefaultMethod()) || paymentMethodInfo.getDefaultMethod() == null) {
                 if (paymentMethods.remove(paymentMethodId) == null) {
-                    return Either.left(new PaymentError("unknown", "Did not get any result back"));
+                    return Either.left(new PaymentError("unknown", "Did not get any result back", null, null));
                 }
             }
             else {
-                return Either.left(new PaymentError("error", "Cannot delete default payment method"));
+                return Either.left(new PaymentError("error", "Cannot delete default payment method", null, null));
             }
         }
         return Either.right(null);
@@ -226,7 +226,7 @@ public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
     @Override
     public Either<PaymentError, PaymentMethodInfo> getPaymentMethodInfo(String paymentMethodId) {
         if (paymentMethodId == null) {
-            return Either.left(new PaymentError("unknown", "Could not retrieve payment method for paymentMethodId " + paymentMethodId));
+            return Either.left(new PaymentError("unknown", "Could not retrieve payment method for paymentMethodId " + paymentMethodId, null, null));
         }
 
         return Either.right(paymentMethods.get(paymentMethodId));
diff --git a/payment/src/test/java/com/ning/billing/payment/TestRetryService.java b/payment/src/test/java/com/ning/billing/payment/TestRetryService.java
index 9930a3f..6a08eb5 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestRetryService.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestRetryService.java
@@ -1,3 +1,19 @@
+/*
+ * 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.payment;
 
 import static org.testng.Assert.assertEquals;
@@ -98,7 +114,9 @@ public class TestRetryService {
                                                        now.plusMonths(1),
                                                        amount,
                                                        new BigDecimal("1.0"),
-                                                       Currency.USD));
+                                                       Currency.USD,
+                                                       new DateTime(DateTimeZone.UTC),
+                                                       new DateTime(DateTimeZone.UTC)));
 
         mockPaymentProviderPlugin.makeNextInvoiceFail();
 
@@ -139,7 +157,9 @@ public class TestRetryService {
                                                        now.plusMonths(1),
                                                        amount,
                                                        new BigDecimal("1.0"),
-                                                       Currency.USD));
+                                                       Currency.USD,
+                                                       new DateTime(DateTimeZone.UTC),
+                                                       new DateTime(DateTimeZone.UTC)));
 
         DateTime nextRetryDate = new DateTime(DateTimeZone.UTC).minusDays(1);
         DateTime paymentAttemptDate = nextRetryDate.minusDays(paymentConfig.getPaymentRetryDays().get(0));
@@ -172,6 +192,5 @@ public class TestRetryService {
         assertEquals(updatedAttempt.getPaymentAttemptId(), paymentAttempt.getPaymentAttemptId());
         assertEquals(paymentInfo.getPaymentId(), updatedAttempt.getPaymentId());
 
-        // TODO: what else to assert ?
     }
 }