killbill-memoizeit

Details

diff --git a/api/src/main/java/com/ning/billing/payment/api/PaymentInfoEvent.java b/api/src/main/java/com/ning/billing/payment/api/PaymentInfoEvent.java
index 7cbd73d..c9b1a57 100644
--- a/api/src/main/java/com/ning/billing/payment/api/PaymentInfoEvent.java
+++ b/api/src/main/java/com/ning/billing/payment/api/PaymentInfoEvent.java
@@ -25,6 +25,8 @@ import com.ning.billing.util.bus.BusEvent;
 public interface PaymentInfoEvent extends Entity, BusEvent {
 
     public UUID getAccountId();
+    
+    public UUID getInvoiceId();
 
     public BigDecimal getAmount();
 
diff --git a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentAttempt.java b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentAttempt.java
index 2373ee0..8feb0e4 100644
--- a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentAttempt.java
+++ b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentAttempt.java
@@ -71,9 +71,14 @@ public class DefaultPaymentAttempt extends EntityBase implements PaymentAttempt 
     }
     
     public DefaultPaymentAttempt(UUID paymentAttemptId, Invoice invoice, PaymentAttemptStatus paymentAttemptStatus) {
-           this(paymentAttemptId, invoice.getId(), invoice.getAccountId(), invoice.getBalance(), invoice.getCurrency(), invoice.getInvoiceDate(), null, null, null, null, null, paymentAttemptStatus);
+        this(paymentAttemptId, invoice.getId(), invoice.getAccountId(), invoice.getBalance(), invoice.getCurrency(), invoice.getInvoiceDate(), null, null, null, null, null, paymentAttemptStatus);
     }
 
+    public DefaultPaymentAttempt(PaymentAttempt input, PaymentAttemptStatus paymentAttemptStatus) {
+        this(input.getId(), input.getInvoiceId(), input.getAccountId(), input.getAmount(), input.getCurrency(), input.getInvoiceDate(), null, null, null, null, null, paymentAttemptStatus);
+    }
+
+
     @Override public DateTime getInvoiceDate() {
         return invoiceDate;
     }
diff --git a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentInfoEvent.java b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentInfoEvent.java
index 759a4bc..eb48f30 100644
--- a/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentInfoEvent.java
+++ b/payment/src/main/java/com/ning/billing/payment/api/DefaultPaymentInfoEvent.java
@@ -34,6 +34,7 @@ import com.ning.billing.util.bus.BusEvent.BusEventType;
 public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEvent {
 
     private final UUID accountId;
+    private final UUID invoiceId;    
     private final BigDecimal amount;
     private final BigDecimal refundAmount;
     private final String paymentNumber;
@@ -53,6 +54,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
     @JsonCreator
     public DefaultPaymentInfoEvent(@JsonProperty("id") UUID id,
             @JsonProperty("accountId") UUID accountId,
+            @JsonProperty("invoiceId") UUID invoiceId,            
             @JsonProperty("amount") BigDecimal amount,
             @JsonProperty("refundAmount") BigDecimal refundAmount,
             @JsonProperty("bankIdentificationNumber") String bankIdentificationNumber,
@@ -70,6 +72,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
             @JsonProperty("updatedDate") DateTime updatedDate) {
         super(id);
         this.accountId = accountId;
+        this.invoiceId = invoiceId;
         this.amount = amount;
         this.refundAmount = refundAmount;
         this.bankIdentificationNumber = bankIdentificationNumber;
@@ -90,6 +93,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
     public DefaultPaymentInfoEvent(DefaultPaymentInfoEvent src) {
         this(src.id,
                 src.accountId,
+                src.invoiceId,
                 src.amount,
                 src.refundAmount,
                 src.bankIdentificationNumber,
@@ -109,7 +113,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
     
 
     public DefaultPaymentInfoEvent(PaymentInfoPlugin info, UUID accountId, UUID invoiceId) {
-        this(UUID.randomUUID(), accountId, info.getAmount(), info.getRefundAmount(), info.getBankIdentificationNumber(), info.getPaymentNumber(),
+        this(UUID.randomUUID(), accountId,  invoiceId, info.getAmount(), info.getRefundAmount(), info.getBankIdentificationNumber(), info.getPaymentNumber(),
                 info.getStatus(), info.getCardType(), info.getReferenceId(), info.getPaymentMethodId(), info.getPaymentMethod(), info.getCardType(), info.getCardCountry(),
                 null, info.getEffectiveDate(), info.getCreatedDate(), info.getUpdatedDate());
     }
@@ -142,6 +146,11 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
     }
 
     @Override
+    public UUID getInvoiceId() {
+        return invoiceId;
+    }
+
+    @Override
     public BigDecimal getAmount() {
         return amount;
     }
@@ -216,7 +225,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
 
         private UUID id;
         private UUID accountId;
-        private UUID paymentId;
+        private UUID invoiceId;
         private BigDecimal amount;
         private BigDecimal refundAmount;
         private String paymentNumber;
@@ -239,6 +248,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
         public Builder(DefaultPaymentInfoEvent src) {
             this.id = src.id;
             this.accountId = src.accountId;
+            this.invoiceId = src.invoiceId;
             this.amount = src.amount;
             this.refundAmount = src.refundAmount;
             this.paymentNumber = src.paymentNumber;
@@ -263,8 +273,8 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
         }
 
 
-        public Builder setPaymentId(UUID paymentId) {
-            this.paymentId = paymentId;
+        public Builder setInvoiceId(UUID invoiceId) {
+            this.invoiceId = invoiceId;
             return this;
         }
 
@@ -351,6 +361,7 @@ public class DefaultPaymentInfoEvent extends EntityBase implements PaymentInfoEv
         public PaymentInfoEvent build() {
             return new DefaultPaymentInfoEvent(id,
                     accountId,
+                    invoiceId,
                     amount,
                     refundAmount,
                     bankIdentificationNumber,
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/AuditedPaymentDao.java b/payment/src/main/java/com/ning/billing/payment/dao/AuditedPaymentDao.java
index 1e8594b..27f5ba1 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/AuditedPaymentDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/AuditedPaymentDao.java
@@ -61,14 +61,16 @@ public class AuditedPaymentDao implements PaymentDao {
 
     @Override
     public PaymentAttempt createPaymentAttempt(final PaymentAttempt paymentAttempt, final PaymentAttemptStatus paymentAttemptStatus, final CallContext context) {
+        
+        final PaymentAttempt newPaymentAttempt = new DefaultPaymentAttempt(paymentAttempt, paymentAttemptStatus);
         return paymentAttemptSqlDao.inTransaction(new Transaction<PaymentAttempt, PaymentAttemptSqlDao>() {
             @Override
             public PaymentAttempt inTransaction(PaymentAttemptSqlDao transactional, TransactionStatus status) throws Exception {
-                transactional.insertPaymentAttempt(paymentAttempt, context);
-                PaymentAttempt savedPaymentAttempt = transactional.getPaymentAttemptById(paymentAttempt.getId().toString());
+                transactional.insertPaymentAttempt(newPaymentAttempt, context);
+                PaymentAttempt savedPaymentAttempt = transactional.getPaymentAttemptById(newPaymentAttempt.getId().toString());
 
-                Long recordId = transactional.getRecordId(paymentAttempt.getId().toString());
-                EntityHistory<PaymentAttempt> history = new EntityHistory<PaymentAttempt>(paymentAttempt.getId(), recordId, paymentAttempt, ChangeType.INSERT);
+                Long recordId = transactional.getRecordId(newPaymentAttempt.getId().toString());
+                EntityHistory<PaymentAttempt> history = new EntityHistory<PaymentAttempt>(newPaymentAttempt.getId(), recordId, newPaymentAttempt, ChangeType.INSERT);
                 transactional.insertHistoryFromTransaction(history, context);
 
                 Long historyRecordId = transactional.getHistoryRecordId(recordId);
@@ -81,10 +83,12 @@ public class AuditedPaymentDao implements PaymentDao {
 
     @Override
     public PaymentAttempt createPaymentAttempt(final Invoice invoice, final PaymentAttemptStatus paymentAttemptStatus, final CallContext context) {
+
+        final PaymentAttempt paymentAttempt = new DefaultPaymentAttempt(UUID.randomUUID(), invoice, paymentAttemptStatus);
+        
         return paymentAttemptSqlDao.inTransaction(new Transaction<PaymentAttempt, PaymentAttemptSqlDao>() {
             @Override
             public PaymentAttempt inTransaction(PaymentAttemptSqlDao transactional, TransactionStatus status) throws Exception {
-                final PaymentAttempt paymentAttempt = new DefaultPaymentAttempt(UUID.randomUUID(), invoice, paymentAttemptStatus);
 
                 transactional.insertPaymentAttempt(paymentAttempt, context);
 
diff --git a/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java b/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
index f4fb78e..18ae0f4 100644
--- a/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
+++ b/payment/src/main/java/com/ning/billing/payment/dao/PaymentSqlDao.java
@@ -101,9 +101,8 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, UpdatableEn
         @Override
         public PaymentInfoEvent map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
 
-            // STEPH
-            UUID accountId = null;
-            
+            UUID accountId = getUUID(rs, "account_id");
+            UUID invoiceId = getUUID(rs, "invoice_id");            
             UUID id = getUUID(rs, "id");
             BigDecimal amount = rs.getBigDecimal("amount");
             BigDecimal refundAmount = rs.getBigDecimal("refund_amount");
@@ -122,6 +121,7 @@ public interface PaymentSqlDao extends Transactional<PaymentSqlDao>, UpdatableEn
 
             return new DefaultPaymentInfoEvent(id,
                     accountId,
+                    invoiceId,
                     amount,
                     refundAmount,
                     bankIdentificationNumber,
diff --git a/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg b/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
index 3f125ab..a9d67b5 100644
--- a/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
+++ b/payment/src/main/resources/com/ning/billing/payment/dao/PaymentSqlDao.sql.stg
@@ -39,6 +39,8 @@ updatePaymentInfo() ::= <<
 
 getPaymentInfoList(invoiceIds) ::= <<
     SELECT <paymentInfoFields("p.")>
+    , pa.account_id
+    , pa.invoice_id
       FROM payments p, payment_attempts pa
     WHERE pa.invoice_id in (<invoiceIds>)
        AND pa.payment_id = p.id
@@ -46,6 +48,8 @@ getPaymentInfoList(invoiceIds) ::= <<
 
 getLastPaymentInfo(invoiceIds) ::= <<
     SELECT <paymentInfoFields("p.")>
+    , pa.account_id
+    , pa.invoice_id
     FROM payments p, payment_attempts pa
     WHERE pa.invoice_id in (<invoiceIds>)
     AND pa.payment_id = p.id
@@ -55,6 +59,8 @@ getLastPaymentInfo(invoiceIds) ::= <<
 
 getPaymentInfoForPaymentAttemptId() ::= <<
     SELECT <paymentInfoFields("p.")>
+    , pa.account_id
+    , pa.invoice_id
       FROM payments p, payment_attempts pa
     WHERE pa.payment_attempt_id = :payment_attempt_id
        AND pa.payment_id = p.id
@@ -62,6 +68,8 @@ getPaymentInfoForPaymentAttemptId() ::= <<
 
 getPaymentInfo() ::= <<
     SELECT <paymentInfoFields()>
+    , null as account_id
+    , null as invoice_id
     FROM payments
     WHERE id = :id
 >>
diff --git a/payment/src/test/java/com/ning/billing/payment/api/TestEventJson.java b/payment/src/test/java/com/ning/billing/payment/api/TestEventJson.java
index 31e3b6a..71356d6 100644
--- a/payment/src/test/java/com/ning/billing/payment/api/TestEventJson.java
+++ b/payment/src/test/java/com/ning/billing/payment/api/TestEventJson.java
@@ -49,7 +49,7 @@ public class TestEventJson {
     
     @Test(groups= {"fast"})
     public void testPaymentInfoEvent() throws Exception {
-        PaymentInfoEvent e = new DefaultPaymentInfoEvent(UUID.randomUUID(), UUID.randomUUID(), new BigDecimal(12), new BigDecimal(12.9), "BNP", "eeert", "success",
+        PaymentInfoEvent e = new DefaultPaymentInfoEvent(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), new BigDecimal(12), new BigDecimal(12.9), "BNP", "eeert", "success",
                 "credit", "ref", "paypal", "paypal", "", "", UUID.randomUUID(), new DateTime(), new DateTime(), new DateTime());
         
         String json = mapper.writeValueAsString(e);
diff --git a/payment/src/test/java/com/ning/billing/payment/dao/TestPaymentDao.java b/payment/src/test/java/com/ning/billing/payment/dao/TestPaymentDao.java
index f398668..9960ab7 100644
--- a/payment/src/test/java/com/ning/billing/payment/dao/TestPaymentDao.java
+++ b/payment/src/test/java/com/ning/billing/payment/dao/TestPaymentDao.java
@@ -40,10 +40,11 @@ import com.ning.billing.payment.api.PaymentAttempt.PaymentAttemptStatus;
 import com.ning.billing.payment.api.PaymentInfoEvent;
 
 public abstract class TestPaymentDao {
+
     protected PaymentDao paymentDao;
     protected CallContext context = new TestCallContext("PaymentTests");
 
-    @Test
+    @Test(groups={"slow"})
     public void testCreatePayment() {
         PaymentInfoEvent paymentInfo = new DefaultPaymentInfoEvent.Builder().setId(UUID.randomUUID())
                 .setAmount(BigDecimal.TEN)
@@ -59,7 +60,7 @@ public abstract class TestPaymentDao {
         paymentDao.savePaymentInfo(paymentInfo, context);
     }
 
-    @Test
+    @Test(groups={"slow"})
     public void testUpdatePaymentInfo() {
         PaymentInfoEvent paymentInfo = new DefaultPaymentInfoEvent.Builder().setId(UUID.randomUUID())
                 .setAmount(BigDecimal.TEN)
@@ -77,7 +78,7 @@ public abstract class TestPaymentDao {
         paymentDao.updatePaymentInfo("CreditCard", paymentInfo.getId(), "Visa", "US", context);
     }
 
-    @Test
+    @Test(groups={"slow"})
     public void testUpdatePaymentAttempt() {
         PaymentAttempt paymentAttempt = new DefaultPaymentAttempt.Builder().setPaymentAttemptId(UUID.randomUUID())
                 .setPaymentId(UUID.randomUUID())
@@ -91,7 +92,7 @@ public abstract class TestPaymentDao {
         paymentDao.createPaymentAttempt(paymentAttempt, PaymentAttemptStatus.IN_PROCESSING, context);
     }
 
-    @Test
+    @Test(groups={"slow"})
     public void testGetPaymentForInvoice() throws AccountApiException {
         final UUID invoiceId = UUID.randomUUID();
         final UUID paymentAttemptId = UUID.randomUUID();
@@ -132,6 +133,7 @@ public abstract class TestPaymentDao {
                 .build();
 
         paymentDao.savePaymentInfo(originalPaymentInfo, thisContext);
+        paymentDao.updatePaymentAttemptWithPaymentId(originalPaymentAttempt.getId(), originalPaymentInfo.getId(), thisContext);
         PaymentInfoEvent paymentInfo = paymentDao.getPaymentInfoList(Arrays.asList(invoiceId)).get(0);
         Assert.assertEquals(paymentInfo, originalPaymentInfo);
 
diff --git a/payment/src/test/java/com/ning/billing/payment/plugin/api/MockPaymentInfoPlugin.java b/payment/src/test/java/com/ning/billing/payment/plugin/api/MockPaymentInfoPlugin.java
new file mode 100644
index 0000000..8d4428e
--- /dev/null
+++ b/payment/src/test/java/com/ning/billing/payment/plugin/api/MockPaymentInfoPlugin.java
@@ -0,0 +1,131 @@
+/* 
+ * 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.plugin.api;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.payment.api.PaymentInfoEvent;
+
+public class MockPaymentInfoPlugin implements PaymentInfoPlugin {
+
+    private final BigDecimal amount;
+    private final String bankIdentificationNumber;
+    private final DateTime createdDate;
+    private final DateTime effectiveDate;
+    private final String paymentNumber;
+    private final String paymentMethod;
+    private final String cardType;
+    private final String cardCountry;
+    private final String referenceId;    
+    private final String paymentMethodId;        
+    private final BigDecimal refundAmount;
+    private final String status;    
+    private final String type;
+    private final DateTime updatedDate;
+    
+    
+    public MockPaymentInfoPlugin(PaymentInfoEvent info) {
+        super();
+        this.amount = info.getAmount();
+        this.bankIdentificationNumber = info.getBankIdentificationNumber();
+        this.createdDate = info.getCreatedDate();
+        this.effectiveDate = info.getEffectiveDate();
+        this.paymentNumber = info.getPaymentNumber();
+        this.paymentMethod = info.getPaymentMethod();
+        this.cardType = info.getCardType();
+        this.cardCountry = info.getCardCountry();
+        this.referenceId = info.getReferenceId();
+        this.paymentMethodId = info.getPaymentMethodId();
+        this.refundAmount = info.getRefundAmount();
+        this.status = info.getStatus();
+        this.type = info.getType();
+        this.updatedDate = info.getUpdatedDate();
+    }
+
+
+    @Override
+    public BigDecimal getAmount() {
+        return amount;
+    }
+
+    @Override
+    public String getBankIdentificationNumber() {
+        return bankIdentificationNumber;
+    }
+
+    @Override
+    public DateTime getCreatedDate() {
+        return createdDate;
+    }
+
+    @Override
+    public DateTime getEffectiveDate() {
+        return effectiveDate;
+    }
+
+    @Override
+    public String getPaymentNumber() {
+        return paymentNumber;
+    }
+
+    @Override
+    public String getPaymentMethod() {
+        return paymentMethod;
+    }
+
+    @Override
+    public String getCardType() {
+        return cardType;
+    }
+
+    @Override
+    public String getCardCountry() {
+        return cardCountry;
+    }
+
+    @Override
+    public String getReferenceId() {
+        return referenceId;
+    }
+
+    @Override
+    public String getPaymentMethodId() {
+        return paymentMethodId;
+    }
+
+    @Override
+    public BigDecimal getRefundAmount() {
+        return refundAmount;
+    }
+
+    @Override
+    public String getStatus() {
+        return status;
+    }
+
+    @Override
+    public String getType() {
+        return type;
+    }
+
+    @Override
+    public DateTime getUpdatedDate() {
+        return updatedDate;
+    }
+}