killbill-memoizeit

payment: add test to verify fix from 5cec8e353552e022e557eff4b4a730716ba76497 Signed-off-by:

2/18/2016 12:27:54 PM

Details

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);
-    }
 }