killbill-memoizeit

Details

diff --git a/payment/src/main/java/org/killbill/billing/payment/core/sm/control/DefaultControlInitiated.java b/payment/src/main/java/org/killbill/billing/payment/core/sm/control/DefaultControlInitiated.java
index 4ead401..9a87984 100644
--- a/payment/src/main/java/org/killbill/billing/payment/core/sm/control/DefaultControlInitiated.java
+++ b/payment/src/main/java/org/killbill/billing/payment/core/sm/control/DefaultControlInitiated.java
@@ -1,6 +1,6 @@
 /*
- * Copyright 2014-2015 Groupon, Inc
- * Copyright 2014-2015 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project 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
@@ -63,6 +63,7 @@ public class DefaultControlInitiated implements LeavingStateCallback {
             final PaymentModelDao payment = paymentDao.getPayment(stateContext.getPaymentId(), stateContext.getInternalCallContext());
             Preconditions.checkNotNull(payment, "payment cannot be null for id " + stateContext.getPaymentId());
             stateContext.setPaymentExternalKey(payment.getExternalKey());
+            stateContext.setPaymentMethodId(payment.getPaymentMethodId());
         } else if (stateContext.getPaymentExternalKey() == null) {
             stateContext.setPaymentExternalKey(UUIDs.randomUUID().toString());
         }
@@ -74,6 +75,11 @@ public class DefaultControlInitiated implements LeavingStateCallback {
             stateContext.setPaymentTransactionExternalKey(UUIDs.randomUUID().toString());
         }
 
+        if (stateContext.getPaymentMethodId() == null) {
+            // Similar logic in PaymentAutomatonRunner
+            stateContext.setPaymentMethodId(stateContext.getAccount().getPaymentMethodId());
+        }
+
         if (state.getName().equals(initialState.getName()) || state.getName().equals(retriedState.getName())) {
             try {
                 //
diff --git a/payment/src/main/java/org/killbill/billing/payment/core/sm/control/PaymentStateControlContext.java b/payment/src/main/java/org/killbill/billing/payment/core/sm/control/PaymentStateControlContext.java
index 8b51735..2841907 100644
--- a/payment/src/main/java/org/killbill/billing/payment/core/sm/control/PaymentStateControlContext.java
+++ b/payment/src/main/java/org/killbill/billing/payment/core/sm/control/PaymentStateControlContext.java
@@ -1,6 +1,6 @@
 /*
- * Copyright 2014-2015 Groupon, Inc
- * Copyright 2014-2015 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project 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
@@ -72,7 +72,6 @@ public class PaymentStateControlContext extends PaymentStateContext {
         this.result = result;
     }
 
-
     public PaymentTransaction getCurrentTransaction() {
         if (result == null || result.getTransactions() == null) {
             return null;
@@ -80,7 +79,8 @@ public class PaymentStateControlContext extends PaymentStateContext {
         return Iterables.tryFind(result.getTransactions(), new Predicate<PaymentTransaction>() {
             @Override
             public boolean apply(final PaymentTransaction input) {
-                return ((DefaultPaymentTransaction) input).getAttemptId().equals(getAttemptId());
+                final DefaultPaymentTransaction defaultPaymentTransaction = (DefaultPaymentTransaction) input;
+                return defaultPaymentTransaction.getAttemptId() == null ? getAttemptId() == null : defaultPaymentTransaction.getAttemptId().equals(getAttemptId());
             }
         }).orNull();
     }
diff --git a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiWithControl.java b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiWithControl.java
index aaffeda..acb1be0 100644
--- a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiWithControl.java
+++ b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiWithControl.java
@@ -1,6 +1,6 @@
 /*
- * Copyright 2014-2015 Groupon, Inc
- * Copyright 2014-2015 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project 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
@@ -23,17 +23,17 @@ import java.util.UUID;
 
 import org.killbill.billing.account.api.Account;
 import org.killbill.billing.catalog.api.Currency;
-import org.killbill.billing.osgi.api.OSGIServiceDescriptor;
-import org.killbill.billing.osgi.api.OSGIServiceRegistration;
-import org.killbill.billing.payment.PaymentTestSuiteWithEmbeddedDB;
-import org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin;
-import org.killbill.billing.payment.provider.MockPaymentProviderPlugin;
 import org.killbill.billing.control.plugin.api.OnFailurePaymentControlResult;
 import org.killbill.billing.control.plugin.api.OnSuccessPaymentControlResult;
 import org.killbill.billing.control.plugin.api.PaymentControlApiException;
 import org.killbill.billing.control.plugin.api.PaymentControlContext;
 import org.killbill.billing.control.plugin.api.PaymentControlPluginApi;
 import org.killbill.billing.control.plugin.api.PriorPaymentControlResult;
+import org.killbill.billing.osgi.api.OSGIServiceDescriptor;
+import org.killbill.billing.osgi.api.OSGIServiceRegistration;
+import org.killbill.billing.payment.PaymentTestSuiteWithEmbeddedDB;
+import org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin;
+import org.killbill.billing.payment.provider.MockPaymentProviderPlugin;
 import org.killbill.billing.payment.retry.DefaultFailureCallResult;
 import org.killbill.billing.payment.retry.DefaultOnSuccessPaymentControlResult;
 import org.testng.Assert;
@@ -45,45 +45,107 @@ import com.google.inject.Inject;
 
 public class TestPaymentApiWithControl extends PaymentTestSuiteWithEmbeddedDB {
 
+    private static final PaymentOptions PAYMENT_OPTIONS = new PaymentOptions() {
+        @Override
+        public boolean isExternalPayment() {
+            return false;
+        }
+
+        @Override
+        public List<String> getPaymentControlPluginNames() {
+            return ImmutableList.of(TestPaymentControlPluginApi.PLUGIN_NAME);
+        }
+    };
+
     @Inject
     private OSGIServiceRegistration<PaymentControlPluginApi> controlPluginRegistry;
 
     private Account account;
-    private UUID newPaymentMethodId;
+    private TestPaymentControlPluginApi testPaymentControlPluginApi;
 
     @BeforeMethod(groups = "slow")
     public void beforeMethod() throws Exception {
         super.beforeMethod();
         account = testHelper.createTestAccount("bobo@gmail.com", true);
-        final PaymentMethodPlugin paymentMethodInfo = new DefaultNoOpPaymentMethodPlugin(UUID.randomUUID().toString(), false, null);
-        newPaymentMethodId = paymentApi.addPaymentMethod(account, paymentMethodInfo.getExternalPaymentMethodId(), MockPaymentProviderPlugin.PLUGIN_NAME, false, paymentMethodInfo, ImmutableList.<PluginProperty>of(), callContext);
 
+        testPaymentControlPluginApi = new TestPaymentControlPluginApi();
         controlPluginRegistry.registerService(new OSGIServiceDescriptor() {
-            @Override
-            public String getPluginSymbolicName() {
-                return null;
-            }
-
-            @Override
-            public String getPluginName() {
-                return TestPaymentControlPluginApi.PLUGIN_NAME;
-            }
-
-            @Override
-            public String getRegistrationName() {
-                return TestPaymentControlPluginApi.PLUGIN_NAME;
-            }
-        }, new TestPaymentControlPluginApi(newPaymentMethodId));
+                                                  @Override
+                                                  public String getPluginSymbolicName() {
+                                                      return null;
+                                                  }
+
+                                                  @Override
+                                                  public String getPluginName() {
+                                                      return TestPaymentControlPluginApi.PLUGIN_NAME;
+                                                  }
+
+                                                  @Override
+                                                  public String getRegistrationName() {
+                                                      return TestPaymentControlPluginApi.PLUGIN_NAME;
+                                                  }
+                                              },
+                                              testPaymentControlPluginApi);
+    }
 
+    // Verify Payment control API can be used to change the paymentMethodId on the fly and this is reflected in the created Payment.
+    @Test(groups = "slow")
+    public void testCreateAuthWithControl() throws PaymentApiException {
+        final PaymentMethodPlugin paymentMethodInfo = new DefaultNoOpPaymentMethodPlugin(UUID.randomUUID().toString(), false, null);
+        final UUID newPaymentMethodId = paymentApi.addPaymentMethod(account, paymentMethodInfo.getExternalPaymentMethodId(), MockPaymentProviderPlugin.PLUGIN_NAME, false, paymentMethodInfo, ImmutableList.<PluginProperty>of(), callContext);
+        testPaymentControlPluginApi.setNewPaymentMethodId(newPaymentMethodId);
+
+        final Payment payment = paymentApi.createAuthorizationWithPaymentControl(account, account.getPaymentMethodId(), null, BigDecimal.TEN, Currency.USD, UUID.randomUUID().toString(),
+                                                                                 UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext);
+        Assert.assertEquals(payment.getPaymentMethodId(), newPaymentMethodId);
+    }
+
+    @Test(groups = "slow")
+    public void testCreateAuthWithControlCaptureNoControl() throws PaymentApiException {
+        final BigDecimal requestedAmount = BigDecimal.TEN;
+
+        Payment payment = paymentApi.createAuthorizationWithPaymentControl(account, account.getPaymentMethodId(), null, requestedAmount, Currency.USD, UUID.randomUUID().toString(),
+                                                                           UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext);
+        Assert.assertEquals(payment.getAuthAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
+        Assert.assertEquals(payment.getTransactions().size(), 1);
+        Assert.assertNotNull(((DefaultPaymentTransaction) payment.getTransactions().get(0)).getAttemptId());
+
+        payment = paymentApi.createCapture(account, payment.getId(), payment.getAuthAmount(), payment.getCurrency(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), callContext);
+        Assert.assertEquals(payment.getAuthAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getCapturedAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getTransactions().size(), 2);
+        Assert.assertNotNull(((DefaultPaymentTransaction) payment.getTransactions().get(0)).getAttemptId());
+        Assert.assertNull(((DefaultPaymentTransaction) payment.getTransactions().get(1)).getAttemptId());
+    }
+
+    @Test(groups = "slow")
+    public void testCreateAuthNoControlCaptureWithControl() throws PaymentApiException {
+        final BigDecimal requestedAmount = BigDecimal.TEN;
+
+        Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, Currency.USD, UUID.randomUUID().toString(),
+                                                         UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), callContext);
+        Assert.assertEquals(payment.getAuthAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
+        Assert.assertEquals(payment.getTransactions().size(), 1);
+        Assert.assertNull(((DefaultPaymentTransaction) payment.getTransactions().get(0)).getAttemptId());
+
+        payment = paymentApi.createCaptureWithPaymentControl(account, payment.getId(), payment.getAuthAmount(), payment.getCurrency(), UUID.randomUUID().toString(),
+                                                             ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext);
+        Assert.assertEquals(payment.getAuthAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getCapturedAmount().compareTo(requestedAmount), 0);
+        Assert.assertEquals(payment.getTransactions().size(), 2);
+        Assert.assertNull(((DefaultPaymentTransaction) payment.getTransactions().get(0)).getAttemptId());
+        Assert.assertNotNull(((DefaultPaymentTransaction) payment.getTransactions().get(1)).getAttemptId());
     }
 
     public static class TestPaymentControlPluginApi implements PaymentControlPluginApi {
 
         public static final String PLUGIN_NAME = "TEST_CONTROL_API_PLUGIN_NAME";
 
-        private final UUID newPaymentMethodId;
+        private UUID newPaymentMethodId;
 
-        public TestPaymentControlPluginApi(final UUID newPaymentMethodId) {
+        public void setNewPaymentMethodId(final UUID newPaymentMethodId) {
             this.newPaymentMethodId = newPaymentMethodId;
         }
 
@@ -127,31 +189,4 @@ public class TestPaymentApiWithControl extends PaymentTestSuiteWithEmbeddedDB {
             return new DefaultFailureCallResult(null);
         }
     }
-
-    // Verify Payment control API can be used to change the paymentMethodId on the fly and this is reflected in the created Payment.
-    @Test(groups = "slow")
-    public void testCreateAuthWithControl() throws PaymentApiException {
-
-        final BigDecimal requestedAmount = BigDecimal.TEN;
-
-        final String paymentExternalKey = "dfdf";
-        final String transactionExternalKey = "qwqwqw";
-
-        final PaymentOptions paymentOptions = new PaymentOptions() {
-            @Override
-            public boolean isExternalPayment() {
-                return false;
-            }
-
-            @Override
-            public List<String> getPaymentControlPluginNames() {
-                return ImmutableList.of(TestPaymentControlPluginApi.PLUGIN_NAME);
-            }
-        };
-
-        final Payment payment = paymentApi.createAuthorizationWithPaymentControl(account, account.getPaymentMethodId(), null, requestedAmount, Currency.USD, paymentExternalKey, transactionExternalKey,
-                                                                                 ImmutableList.<PluginProperty>of(), paymentOptions, callContext);
-
-        Assert.assertEquals(payment.getPaymentMethodId(), newPaymentMethodId);
-    }
 }

pom.xml 2(+1 -1)

diff --git a/pom.xml b/pom.xml
index f79d4d0..978e6f8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,7 +21,7 @@
     <parent>
         <artifactId>killbill-oss-parent</artifactId>
         <groupId>org.kill-bill.billing</groupId>
-        <version>0.81</version>
+        <version>0.83</version>
     </parent>
     <artifactId>killbill</artifactId>
     <version>0.16.2-SNAPSHOT</version>
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPayment.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPayment.java
index 93b7374..b76434b 100644
--- a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPayment.java
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestPayment.java
@@ -80,7 +80,11 @@ public class TestPayment extends TestJaxrsBase {
     @Test(groups = "slow")
     public void testCreateRetrievePayment() throws Exception {
         final Account account = createAccountWithDefaultPaymentMethod();
-        testCreateRetrievePayment(account, null, UUID.randomUUID().toString(), 1);
+        final String externalPaymentKey = UUID.randomUUID().toString();
+        final UUID paymentId = testCreateRetrievePayment(account, null, externalPaymentKey, 1);
+
+        final Payment payment = killBillClient.getPaymentByExternalKey(externalPaymentKey);
+        assertEquals(payment.getPaymentId(), paymentId);
 
         final PaymentMethod paymentMethodJson = new PaymentMethod(null, UUID.randomUUID().toString(), account.getAccountId(), false, PLUGIN_NAME, new PaymentMethodPluginDetail());
         final PaymentMethod nonDefaultPaymentMethod = killBillClient.createPaymentMethod(paymentMethodJson, createdBy, reason, comment);
@@ -244,7 +248,7 @@ public class TestPayment extends TestJaxrsBase {
         Assert.assertNull(payment);
     }
 
-    private void testCreateRetrievePayment(final Account account, @Nullable final UUID paymentMethodId,
+    private UUID testCreateRetrievePayment(final Account account, @Nullable final UUID paymentMethodId,
                                            final String paymentExternalKey, final int paymentNb) throws Exception {
         // Authorization
         final String authTransactionExternalKey = UUID.randomUUID().toString();
@@ -290,6 +294,8 @@ public class TestPayment extends TestJaxrsBase {
                       BigDecimal.TEN, BigDecimal.TEN, new BigDecimal("2"), new BigDecimal("2"), 4, paymentNb);
         verifyPaymentTransaction(account, authPayment.getPaymentId(), paymentExternalKey, refundPayment.getTransactions().get(3),
                                  refundTransactionExternalKey, refundTransaction.getAmount(), "REFUND", "SUCCESS");
+
+        return authPayment.getPaymentId();
     }
 
     private Payment createVerifyTransaction(final Account account,