killbill-aplcache

Added getPaymentInfo

11/26/2011 4:57:31 AM

Details

diff --git a/payment/src/main/java/com/ning/billing/payment/PaymentError.java b/payment/src/main/java/com/ning/billing/payment/PaymentError.java
index 7cdecbc..fc99f6c 100644
--- a/payment/src/main/java/com/ning/billing/payment/PaymentError.java
+++ b/payment/src/main/java/com/ning/billing/payment/PaymentError.java
@@ -15,31 +15,36 @@
  */
 
 package com.ning.billing.payment;
-
-import java.util.UUID;
-
 import com.ning.billing.util.eventbus.IEventBusType;
 
 public class PaymentError implements IEventBusType {
-    private final UUID id;
+    private final String type;
+    private final String message;
 
     public PaymentError(PaymentError src) {
-        this.id = src.id;
+        this.type = src.type;
+        this.message = src.message;
     }
 
-    public PaymentError(UUID id) {
-        this.id = id;
+    public PaymentError(String type, String message) {
+        this.type = type;
+        this.message = message;
     }
 
-    public UUID getId() {
-        return id;
+    public String getType() {
+        return type;
+    }
+
+    public String getMessage() {
+        return message;
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((message == null) ? 0 : message.hashCode());
+        result = prime * result + ((type == null) ? 0 : type.hashCode());
         return result;
     }
 
@@ -52,12 +57,23 @@ public class PaymentError implements IEventBusType {
         if (getClass() != obj.getClass())
             return false;
         PaymentError other = (PaymentError) obj;
-        if (id == null) {
-            if (other.id != null)
+        if (message == null) {
+            if (other.message != null)
+                return false;
+        }
+        else if (!message.equals(other.message))
+            return false;
+        if (type == null) {
+            if (other.type != null)
                 return false;
         }
-        else if (!id.equals(other.id))
+        else if (!type.equals(other.type))
             return false;
         return true;
     }
+
+    @Override
+    public String toString() {
+        return "PaymentError [type=" + type + ", message=" + message + "]";
+    }
 }
diff --git a/payment/src/main/java/com/ning/billing/payment/PaymentInfo.java b/payment/src/main/java/com/ning/billing/payment/PaymentInfo.java
index 1658af9..9b753c5 100644
--- a/payment/src/main/java/com/ning/billing/payment/PaymentInfo.java
+++ b/payment/src/main/java/com/ning/billing/payment/PaymentInfo.java
@@ -17,7 +17,6 @@
 package com.ning.billing.payment;
 
 import java.math.BigDecimal;
-import java.util.UUID;
 
 import org.joda.time.DateTime;
 
@@ -25,7 +24,7 @@ import com.ning.billing.util.eventbus.IEventBusType;
 
 public class PaymentInfo implements IEventBusType {
     public static class Builder {
-        private UUID id;
+        private String id;
         private BigDecimal amount;
         private BigDecimal appliedCreditBalanceAmount;
         private String bankIdentificationNumber;
@@ -58,7 +57,7 @@ public class PaymentInfo implements IEventBusType {
             this.updatedDate = src.updatedDate;
         }
 
-        public Builder setId(UUID id) {
+        public Builder setId(String id) {
             this.id = id;
             return this;
         }
@@ -140,7 +139,7 @@ public class PaymentInfo implements IEventBusType {
         }
     }
 
-    private final UUID id;
+    private final String id;
     private final BigDecimal amount;
     private final BigDecimal appliedCreditBalanceAmount;
     private final String bankIdentificationNumber;
@@ -170,7 +169,7 @@ public class PaymentInfo implements IEventBusType {
         this.updatedDate = src.updatedDate;
     }
 
-    public PaymentInfo(UUID id,
+    public PaymentInfo(String id,
                        BigDecimal amount,
                        BigDecimal appliedCreditBalanceAmount,
                        String bankIdentificationNumber,
@@ -202,7 +201,7 @@ public class PaymentInfo implements IEventBusType {
         return new Builder(this);
     }
 
-    public UUID getId() {
+    public String getId() {
         return id;
     }
 
diff --git a/payment/src/main/java/com/ning/billing/payment/PaymentInfoRequest.java b/payment/src/main/java/com/ning/billing/payment/PaymentInfoRequest.java
new file mode 100644
index 0000000..cdedee1
--- /dev/null
+++ b/payment/src/main/java/com/ning/billing/payment/PaymentInfoRequest.java
@@ -0,0 +1,23 @@
+package com.ning.billing.payment;
+
+import java.util.UUID;
+
+import com.ning.billing.util.eventbus.IEventBusType;
+
+public class PaymentInfoRequest implements IEventBusType {
+    private final UUID accountId;
+    private final String paymentId;
+
+    public PaymentInfoRequest(UUID accountId, String paymentId) {
+        this.accountId = accountId;
+        this.paymentId = paymentId;
+    }
+
+    public UUID getAccountId() {
+        return accountId;
+    }
+
+    public String getPaymentId() {
+        return paymentId;
+    }
+}
diff --git a/payment/src/main/java/com/ning/billing/payment/provider/PaymentProviderPlugin.java b/payment/src/main/java/com/ning/billing/payment/provider/PaymentProviderPlugin.java
index 25b7bcc..a108136 100644
--- a/payment/src/main/java/com/ning/billing/payment/provider/PaymentProviderPlugin.java
+++ b/payment/src/main/java/com/ning/billing/payment/provider/PaymentProviderPlugin.java
@@ -18,7 +18,11 @@ package com.ning.billing.payment.provider;
 
 import com.ning.billing.account.api.IAccount;
 import com.ning.billing.invoice.model.Invoice;
+import com.ning.billing.payment.PaymentError;
+import com.ning.billing.payment.PaymentInfo;
+import com.ning.billing.util.Either;
 
 public interface PaymentProviderPlugin {
-    public void processInvoice(IAccount account, Invoice invoice);
+    Either<PaymentError, PaymentInfo> processInvoice(IAccount account, Invoice invoice);
+    Either<PaymentError, PaymentInfo> getPaymentInfo(String paymentId);
 }
diff --git a/payment/src/test/java/com/ning/billing/payment/MockPaymentInfoReceiver.java b/payment/src/test/java/com/ning/billing/payment/MockPaymentInfoReceiver.java
new file mode 100644
index 0000000..2558b3a
--- /dev/null
+++ b/payment/src/test/java/com/ning/billing/payment/MockPaymentInfoReceiver.java
@@ -0,0 +1,35 @@
+package com.ning.billing.payment;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.google.common.eventbus.Subscribe;
+
+public class MockPaymentInfoReceiver {
+    private final List<PaymentInfo> processedPayments = Collections.synchronizedList(new ArrayList<PaymentInfo>());
+    private final List<PaymentError> errors = Collections.synchronizedList(new ArrayList<PaymentError>());
+
+    @Subscribe
+    public void processedPayment(PaymentInfo paymentInfo) {
+        processedPayments.add(paymentInfo);
+    }
+
+    @Subscribe
+    public void processedPaymentError(PaymentError paymentError) {
+        errors.add(paymentError);
+    }
+
+    public List<PaymentInfo> getProcessedPayments() {
+        return new ArrayList<PaymentInfo>(processedPayments);
+    }
+
+    public List<PaymentError> getErrors() {
+        return new ArrayList<PaymentError>(errors);
+    }
+
+    public void clear() {
+        processedPayments.clear();
+        errors.clear();
+    }
+}
\ No newline at end of file
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 557e42c..9c90733 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
@@ -16,34 +16,43 @@
 
 package com.ning.billing.payment.provider;
 
+import java.util.Map;
 import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
 
 import com.google.inject.Inject;
 import com.ning.billing.account.api.IAccount;
 import com.ning.billing.invoice.model.Invoice;
+import com.ning.billing.payment.PaymentError;
 import com.ning.billing.payment.PaymentInfo;
-import com.ning.billing.util.eventbus.IEventBus;
-import com.ning.billing.util.eventbus.IEventBus.EventBusException;
+import com.ning.billing.util.Either;
 
 public class MockPaymentProviderPlugin implements PaymentProviderPlugin {
     public static final String PLUGIN_NAME = "mock";
-
-    private final IEventBus eventBus;
+    private final Map<String, PaymentInfo> payments = new ConcurrentHashMap<String, PaymentInfo>();
 
     @Inject
-    public MockPaymentProviderPlugin(PaymentProviderPluginRegistry registry, IEventBus eventBus) {
-        this.eventBus = eventBus;
+    public MockPaymentProviderPlugin(PaymentProviderPluginRegistry registry) {
         registry.register(this, PLUGIN_NAME);
     }
 
     @Override
-    public void processInvoice(IAccount account, Invoice invoice) {
-        try {
-            eventBus.post(new PaymentInfo.Builder().setId(UUID.randomUUID()).build());
+    public Either<PaymentError, PaymentInfo> processInvoice(IAccount account, Invoice invoice) {
+        PaymentInfo payment = new PaymentInfo.Builder().setId(UUID.randomUUID().toString()).build();
+
+        payments.put(payment.getId(), payment);
+        return Either.right(payment);
+    }
+
+    @Override
+    public Either<PaymentError, PaymentInfo> getPaymentInfo(String paymentId) {
+        PaymentInfo payment = payments.get(paymentId);
+
+        if (payment == null) {
+            return Either.left(new PaymentError("notfound", "No payment found for id " + paymentId));
         }
-        catch (EventBusException ex) {
-            throw new RuntimeException(ex);
+        else {
+            return Either.right(payment);
         }
     }
-
 }
diff --git a/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java b/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
index 3453a0c..10d6518 100644
--- a/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
+++ b/payment/src/test/java/com/ning/billing/payment/TestPaymentProvider.java
@@ -18,13 +18,12 @@ package com.ning.billing.payment;
 
 import static com.jayway.awaitility.Awaitility.await;
 import static java.util.concurrent.TimeUnit.MINUTES;
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 import java.math.BigDecimal;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.Callable;
@@ -35,7 +34,6 @@ import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
 
-import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.account.api.IAccount;
@@ -49,44 +47,21 @@ import com.ning.billing.util.eventbus.IEventBus.EventBusException;
 
 @Guice(modules = PaymentTestModule.class)
 public class TestPaymentProvider {
-    private static class MockPaymentProcessor {
-        private final List<PaymentInfo> processedPayments = Collections.synchronizedList(new ArrayList<PaymentInfo>());
-        private final List<PaymentError> errors = Collections.synchronizedList(new ArrayList<PaymentError>());
-
-        @Subscribe
-        public void processedPayment(PaymentInfo paymentInfo) {
-            processedPayments.add(paymentInfo);
-        }
-
-        @Subscribe
-        public void processedPaymentError(PaymentError paymentError) {
-            errors.add(paymentError);
-        }
-
-        public List<PaymentInfo> getProcessedPayments() {
-            return new ArrayList<PaymentInfo>(processedPayments);
-        }
-
-        public List<PaymentError> getErrors() {
-            return new ArrayList<PaymentError>(errors);
-        }
-    }
-
     @Inject
     private IEventBus eventBus;
     @Inject
-    private InvoiceProcessor invoiceProcessor;
+    private RequestProcessor invoiceProcessor;
     @Inject
     private IAccountUserApi accountUserApi;
-    private MockPaymentProcessor mockPaymentProcessor;
+    private MockPaymentInfoReceiver paymentInfoReceiver;
 
     @BeforeMethod(alwaysRun = true)
     public void setUp() throws EventBusException {
-        mockPaymentProcessor = new MockPaymentProcessor();
+        paymentInfoReceiver = new MockPaymentInfoReceiver();
 
         eventBus.start();
         eventBus.register(invoiceProcessor);
-        eventBus.register(mockPaymentProcessor);
+        eventBus.register(paymentInfoReceiver);
     }
 
     @AfterMethod(alwaysRun = true)
@@ -119,14 +94,33 @@ public class TestPaymentProvider {
         await().atMost(1, MINUTES).until(new Callable<Boolean>() {
             @Override
             public Boolean call() throws Exception {
-                List<PaymentInfo> processedPayments = mockPaymentProcessor.getProcessedPayments();
-                List<PaymentError> errors = mockPaymentProcessor.getErrors();
+                List<PaymentInfo> processedPayments = paymentInfoReceiver.getProcessedPayments();
+                List<PaymentError> errors = paymentInfoReceiver.getErrors();
+
+                return processedPayments.size() == 1 || errors.size() == 1;
+            }
+        });
+
+        assertFalse(paymentInfoReceiver.getProcessedPayments().isEmpty());
+        assertTrue(paymentInfoReceiver.getErrors().isEmpty());
+
+        final PaymentInfo paymentInfo = paymentInfoReceiver.getProcessedPayments().get(0);
+        final PaymentInfoRequest infoRequest = new PaymentInfoRequest(account.getId(), paymentInfo.getId());
+
+        paymentInfoReceiver.clear();
+        eventBus.post(infoRequest);
+        await().atMost(1, MINUTES).until(new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                List<PaymentInfo> processedPayments = paymentInfoReceiver.getProcessedPayments();
+                List<PaymentError> errors = paymentInfoReceiver.getErrors();
 
                 return processedPayments.size() == 1 || errors.size() == 1;
             }
         });
 
-        assertFalse(mockPaymentProcessor.getProcessedPayments().isEmpty());
-        assertTrue(mockPaymentProcessor.getErrors().isEmpty());
+        assertFalse(paymentInfoReceiver.getProcessedPayments().isEmpty());
+        assertTrue(paymentInfoReceiver.getErrors().isEmpty());
+        assertEquals(paymentInfoReceiver.getProcessedPayments().get(0), paymentInfo);
     }
 }