killbill-memoizeit

invoice, payment: first pass at fixing tests Signed-off-by:

6/12/2018 10:43:39 PM

Details

diff --git a/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteNoDB.java b/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteNoDB.java
index e633424..6c064ab 100644
--- a/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteNoDB.java
+++ b/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteNoDB.java
@@ -65,8 +65,6 @@ public abstract class InvoiceTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
     @Inject
     protected InvoiceUserApi invoiceUserApi;
     @Inject
-    protected InvoicePaymentApi invoicePaymentApi;
-    @Inject
     protected InvoiceGenerator generator;
     @Inject
     protected InvoiceConfig invoiceConfig;
diff --git a/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java b/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
index 49251f8..1ecaa5f 100644
--- a/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
+++ b/invoice/src/test/java/org/killbill/billing/invoice/InvoiceTestSuiteWithEmbeddedDB.java
@@ -70,8 +70,6 @@ public abstract class InvoiceTestSuiteWithEmbeddedDB extends GuicyKillbillTestSu
     @Inject
     protected InvoiceUserApi invoiceUserApi;
     @Inject
-    protected InvoicePaymentApi invoicePaymentApi;
-    @Inject
     protected InvoiceGenerator generator;
     @Inject
     protected BillingInternalApi billingApi;
diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
index b237566..6d87e45 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultInvoicePaymentApi.java
@@ -29,6 +29,7 @@ import org.killbill.billing.account.api.Account;
 import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.invoice.api.InvoiceInternalApi;
 import org.killbill.billing.invoice.api.InvoicePayment;
+import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi;
 import org.killbill.billing.util.UUIDs;
 import org.killbill.billing.util.callcontext.CallContext;
 import org.killbill.billing.util.callcontext.TenantContext;
@@ -66,7 +67,7 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
                 pluginProperties.add(pluginProperty);
             }
         }
-        pluginProperties.add(new PluginProperty("IPCD_INVOICE_ID", invoiceId, false));
+        pluginProperties.add(new PluginProperty("IPCD_INVOICE_ID", invoiceId.toString(), false));
 
         final String paymentTransactionExternalKey = MoreObjects.firstNonNull(originalPaymentTransactionExternalKey, UUIDs.randomUUID().toString());
         final Payment payment = paymentApi.createPurchaseWithPaymentControl(account,
@@ -78,7 +79,7 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
                                                                             paymentExternalKey,
                                                                             paymentTransactionExternalKey,
                                                                             pluginProperties,
-                                                                            paymentOptions,
+                                                                            buildPaymentOptions(paymentOptions),
                                                                             context);
 
         return getInvoicePayment(payment.getId(), paymentTransactionExternalKey, context);
@@ -106,7 +107,7 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
                                                                           effectiveDate,
                                                                           paymentTransactionExternalKey,
                                                                           pluginProperties,
-                                                                          paymentOptions,
+                                                                          buildPaymentOptions(paymentOptions),
                                                                           context);
 
         return getInvoicePayment(payment.getId(), paymentTransactionExternalKey, context);
@@ -141,7 +142,7 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
                                                                           paymentExternalKey,
                                                                           paymentTransactionExternalKey,
                                                                           pluginProperties,
-                                                                          paymentOptions,
+                                                                          buildPaymentOptions(paymentOptions),
                                                                           context);
 
         return getInvoicePayment(payment.getId(), paymentTransactionExternalKey, context);
@@ -179,4 +180,44 @@ public class DefaultInvoicePaymentApi implements InvoicePaymentApi {
         }
         return null;
     }
+
+    private PaymentOptions buildPaymentOptions(final PaymentOptions paymentOptions) {
+        final List<String> paymentControlPluginNames = new LinkedList<String>();
+        paymentControlPluginNames.addAll(paymentOptions.getPaymentControlPluginNames());
+        if (!paymentControlPluginNames.contains(InvoicePaymentControlPluginApi.PLUGIN_NAME)) {
+            paymentControlPluginNames.add(InvoicePaymentControlPluginApi.PLUGIN_NAME);
+        }
+
+        return new InvoicePaymentPaymentOptions(paymentOptions.isExternalPayment(), paymentControlPluginNames);
+    }
+
+    private static final class InvoicePaymentPaymentOptions implements PaymentOptions {
+
+        private final boolean isExternalPayment;
+        private final List<String> paymentControlPluginNames;
+
+        public InvoicePaymentPaymentOptions(final boolean isExternalPayment, final List<String> getPaymentControlPluginNames) {
+            this.isExternalPayment = isExternalPayment;
+            this.paymentControlPluginNames = getPaymentControlPluginNames;
+        }
+
+        @Override
+        public boolean isExternalPayment() {
+            return isExternalPayment;
+        }
+
+        @Override
+        public List<String> getPaymentControlPluginNames() {
+            return paymentControlPluginNames;
+        }
+
+        @Override
+        public String toString() {
+            final StringBuilder sb = new StringBuilder("InvoicePaymentPaymentOptions{");
+            sb.append("isExternalPayment=").append(isExternalPayment);
+            sb.append(", paymentControlPluginNames=").append(paymentControlPluginNames);
+            sb.append('}');
+            return sb.toString();
+        }
+    }
 }
diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentApi.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentApi.java
index c9ffb84..cf9d900 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentApi.java
@@ -131,10 +131,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
         }
 
         checkNotNullParameter(account, "account");
-        if (paymentId == null) {
-            checkNotNullParameter(amount, "amount");
-            checkNotNullParameter(currency, "currency");
-        }
         checkNotNullParameter(properties, "plugin properties");
         checkExternalKeyLength(paymentExternalKey);
 
@@ -228,8 +224,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
 
         checkNotNullParameter(account, "account");
         checkNotNullParameter(paymentId, "paymentId");
-        checkNotNullParameter(amount, "amount");
-        checkNotNullParameter(currency, "currency");
         checkNotNullParameter(properties, "plugin properties");
         checkExternalKeyLength(paymentTransactionExternalKey);
 
@@ -323,11 +317,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
         }
 
         checkNotNullParameter(account, "account");
-        if (paymentId == null) {
-            checkNotNullParameter(amount, "amount");
-            checkNotNullParameter(currency, "currency");
-        }
-
         checkNotNullParameter(properties, "plugin properties");
         checkExternalKeyLength(paymentTransactionExternalKey);
 
@@ -515,9 +504,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
         }
 
         checkNotNullParameter(account, "account");
-        if (paymentId == null) {
-            checkNotNullParameter(currency, "currency");
-        }
         checkNotNullParameter(paymentId, "paymentId");
         checkNotNullParameter(properties, "plugin properties");
         checkExternalKeyLength(paymentTransactionExternalKey);
@@ -617,10 +603,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
         }
 
         checkNotNullParameter(account, "account");
-        if (paymentId == null) {
-            checkNotNullParameter(amount, "amount");
-            checkNotNullParameter(currency, "currency");
-        }
         checkNotNullParameter(properties, "plugin properties");
         checkExternalKeyLength(paymentTransactionExternalKey);
 
@@ -816,8 +798,6 @@ public class DefaultPaymentApi extends DefaultApiBase implements PaymentApi {
         }
 
         checkNotNullParameter(account, "account");
-        checkNotNullParameter(amount, "amount");
-        checkNotNullParameter(currency, "currency");
         checkNotNullParameter(paymentId, "paymentId");
 
         final String transactionType = TransactionType.CHARGEBACK.name();
diff --git a/payment/src/main/java/org/killbill/billing/payment/invoice/InvoicePaymentControlPluginApi.java b/payment/src/main/java/org/killbill/billing/payment/invoice/InvoicePaymentControlPluginApi.java
index f4d2082..ecb7dd2 100644
--- a/payment/src/main/java/org/killbill/billing/payment/invoice/InvoicePaymentControlPluginApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/invoice/InvoicePaymentControlPluginApi.java
@@ -198,7 +198,7 @@ public final class InvoicePaymentControlPluginApi implements PaymentControlPlugi
                 case REFUND:
                     final Map<UUID, BigDecimal> idWithAmount = extractIdsWithAmountFromProperties(pluginProperties);
                     final PluginProperty prop = getPluginProperty(pluginProperties, PROP_IPCD_REFUND_WITH_ADJUSTMENTS);
-                    final boolean isAdjusted = prop != null ? Boolean.valueOf((String) prop.getValue()) : false;
+                    final boolean isAdjusted = prop != null && prop.getValue() != null ? Boolean.valueOf(prop.getValue().toString()) : false;
                     invoiceApi.recordRefund(paymentControlContext.getPaymentId(), paymentControlContext.getAmount(), isAdjusted, idWithAmount, paymentControlContext.getTransactionExternalKey(), internalContext);
                     break;
 
@@ -230,7 +230,7 @@ public final class InvoicePaymentControlPluginApi implements PaymentControlPlugi
                 case CREDIT:
                     final Map<UUID, BigDecimal> idWithAmountMap = extractIdsWithAmountFromProperties(pluginProperties);
                     final PluginProperty properties = getPluginProperty(pluginProperties, PROP_IPCD_REFUND_WITH_ADJUSTMENTS);
-                    final boolean isInvoiceAdjusted = properties != null ? Boolean.valueOf((String) properties.getValue()) : false;
+                    final boolean isInvoiceAdjusted = properties != null && properties.getValue() != null ? Boolean.valueOf(properties.getValue().toString()) : false;
 
                     final PluginProperty legacyPayment = getPluginProperty(pluginProperties, PROP_IPCD_PAYMENT_ID);
                     final UUID paymentId = legacyPayment != null ? (UUID) legacyPayment.getValue() : paymentControlContext.getPaymentId();
@@ -416,7 +416,7 @@ public final class InvoicePaymentControlPluginApi implements PaymentControlPlugi
         }
 
         final PluginProperty prop = getPluginProperty(pluginProperties, PROP_IPCD_REFUND_WITH_ADJUSTMENTS);
-        final boolean isAdjusted = prop != null ? Boolean.valueOf((String) prop.getValue()) : false;
+        final boolean isAdjusted = prop != null && prop.getValue() != null ? Boolean.valueOf(prop.getValue().toString()) : false;
         if (isAdjusted) {
             try {
                 invoiceApi.validateInvoiceItemAdjustments(paymentControlPluginContext.getPaymentId(), idWithAmount, internalContext);
diff --git a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApi.java b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApi.java
index a490e7f..8d86ede 100644
--- a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApi.java
+++ b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApi.java
@@ -35,7 +35,6 @@ import org.killbill.billing.control.plugin.api.PaymentControlApiException;
 import org.killbill.billing.invoice.api.Invoice;
 import org.killbill.billing.invoice.api.InvoiceApiException;
 import org.killbill.billing.invoice.api.InvoiceItem;
-import org.killbill.billing.invoice.api.InvoicePayment;
 import org.killbill.billing.osgi.api.OSGIServiceDescriptor;
 import org.killbill.billing.payment.MockRecurringInvoiceItem;
 import org.killbill.billing.payment.PaymentTestSuiteWithEmbeddedDB;
@@ -867,19 +866,19 @@ public class TestPaymentApi extends PaymentTestSuiteWithEmbeddedDB {
                                                             requestedAmount,
                                                             new BigDecimal("1.0"),
                                                             Currency.USD));
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
         assertEquals(payment.getExternalKey(), paymentExternalKey);
         assertEquals(payment.getPaymentMethodId(), account.getPaymentMethodId());
         assertEquals(payment.getAccountId(), account.getId());
@@ -1164,19 +1163,19 @@ public class TestPaymentApi extends PaymentTestSuiteWithEmbeddedDB {
                                                                      Currency.USD);
         invoice.addInvoiceItem(invoiceItem);
 
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
 
         final List<PluginProperty> refundProperties = ImmutableList.<PluginProperty>of();
         final Payment payment2 = paymentApi.createRefundWithPaymentControl(account, payment.getId(), requestedAmount, Currency.USD, null, transactionExternalKey2,
@@ -1218,19 +1217,19 @@ public class TestPaymentApi extends PaymentTestSuiteWithEmbeddedDB {
                                                                      Currency.USD);
         invoice.addInvoiceItem(invoiceItem);
 
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
 
         final List<PluginProperty> refundProperties = ImmutableList.<PluginProperty>of();
 
@@ -1267,27 +1266,27 @@ public class TestPaymentApi extends PaymentTestSuiteWithEmbeddedDB {
                                                                      Currency.USD);
         invoice.addInvoiceItem(invoiceItem);
 
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
 
         final List<PluginProperty> refundProperties = new ArrayList<PluginProperty>();
         final HashMap<UUID, BigDecimal> uuidBigDecimalHashMap = new HashMap<UUID, BigDecimal>();
         uuidBigDecimalHashMap.put(invoiceItem.getId(), null);
 
-        final InvoicePayment invoicePayment2 = invoicePaymentApi.createRefundForInvoice(true, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2,
-                                                                                        refundProperties, INVOICE_PAYMENT, callContext);
-        final Payment payment2 = paymentApi.getPayment(invoicePayment2.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createRefundForInvoice(true, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2,
+                                                 refundProperties, INVOICE_PAYMENT, callContext);
+        final Payment payment2 = paymentApi.getPayment(payment.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
 
         assertEquals(payment2.getTransactions().size(), 2);
         assertEquals(payment2.getExternalKey(), paymentExternalKey);
diff --git a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiNoDB.java b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiNoDB.java
index 6c30cf2..096b5cf 100644
--- a/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiNoDB.java
+++ b/payment/src/test/java/org/killbill/billing/payment/api/TestPaymentApiNoDB.java
@@ -26,10 +26,8 @@ import org.joda.time.LocalDate;
 import org.killbill.billing.account.api.Account;
 import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.invoice.api.Invoice;
-import org.killbill.billing.invoice.api.InvoicePayment;
 import org.killbill.billing.payment.MockRecurringInvoiceItem;
 import org.killbill.billing.payment.PaymentTestSuiteNoDB;
-import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi;
 import org.killbill.billing.payment.provider.DefaultNoOpPaymentMethodPlugin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,6 +35,7 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
 
 import static org.testng.Assert.assertEquals;
@@ -73,7 +72,7 @@ public class TestPaymentApiNoDB extends PaymentTestSuiteNoDB {
 
     @Test(groups = "fast")
     public void testSimpleInvoicePaymentWithNoAmount() throws Exception {
-        final BigDecimal invoiceAmount = new BigDecimal("10.0011");
+        final BigDecimal invoiceAmount = new BigDecimal("10");
         final BigDecimal requestedAmount = null;
         final BigDecimal expectedAmount = null;
 
@@ -125,28 +124,28 @@ public class TestPaymentApiNoDB extends PaymentTestSuiteNoDB {
                                                             Currency.USD));
 
         try {
-            final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                             invoice.getId(),
-                                                                                             account.getPaymentMethodId(),
-                                                                                             null,
-                                                                                             requestedAmount,
-                                                                                             account.getCurrency(),
-                                                                                             null,
-                                                                                             invoice.getId().toString(),
-                                                                                             UUID.randomUUID().toString(),
-                                                                                             ImmutableList.<PluginProperty>of(),
-                                                                                             PAYMENT_OPTIONS,
-                                                                                             callContext);
-            final Payment paymentInfo = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
-            if (expectedAmount == null) {
+            invoicePaymentApi.createPurchaseForInvoice(account,
+                                                       invoice.getId(),
+                                                       account.getPaymentMethodId(),
+                                                       null,
+                                                       requestedAmount,
+                                                       account.getCurrency(),
+                                                       null,
+                                                       invoice.getId().toString(),
+                                                       UUID.randomUUID().toString(),
+                                                       ImmutableList.<PluginProperty>of(),
+                                                       PAYMENT_OPTIONS,
+                                                       callContext);
+            final Payment paymentInfo = paymentApi.getPaymentByExternalKey(invoice.getId().toString(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+            if (requestedAmount != null && expectedAmount == null) {
                 fail("Expected to fail because requested amount > invoice amount");
             }
             assertNotNull(paymentInfo.getId());
-            assertTrue(paymentInfo.getPurchasedAmount().compareTo(expectedAmount) == 0);
+            assertTrue(paymentInfo.getPurchasedAmount().compareTo(MoreObjects.firstNonNull(expectedAmount, invoiceAmount)) == 0);
             assertNotNull(paymentInfo.getPaymentNumber());
             assertEquals(paymentInfo.getExternalKey(), invoice.getId().toString());
             assertEquals(paymentInfo.getCurrency(), Currency.USD);
-            assertTrue(paymentInfo.getTransactions().get(0).getAmount().compareTo(expectedAmount) == 0);
+            assertTrue(paymentInfo.getTransactions().get(0).getAmount().compareTo(MoreObjects.firstNonNull(expectedAmount, invoiceAmount)) == 0);
             assertEquals(paymentInfo.getTransactions().get(0).getCurrency(), Currency.USD);
             assertEquals(paymentInfo.getTransactions().get(0).getPaymentId(), paymentInfo.getId());
             assertEquals(paymentInfo.getTransactions().get(0).getTransactionType(), TransactionType.PURCHASE);
diff --git a/payment/src/test/java/org/killbill/billing/payment/TestJanitor.java b/payment/src/test/java/org/killbill/billing/payment/TestJanitor.java
index 83ab98f..4cf43a5 100644
--- a/payment/src/test/java/org/killbill/billing/payment/TestJanitor.java
+++ b/payment/src/test/java/org/killbill/billing/payment/TestJanitor.java
@@ -36,7 +36,6 @@ import org.killbill.billing.catalog.api.Currency;
 import org.killbill.billing.invoice.api.Invoice;
 import org.killbill.billing.invoice.api.InvoiceApiException;
 import org.killbill.billing.invoice.api.InvoiceItem;
-import org.killbill.billing.invoice.api.InvoicePayment;
 import org.killbill.billing.payment.api.Payment;
 import org.killbill.billing.payment.api.PaymentApiException;
 import org.killbill.billing.payment.api.PaymentOptions;
@@ -154,6 +153,10 @@ public class TestJanitor extends PaymentTestSuiteWithEmbeddedDB {
 
     @AfterMethod(groups = "slow")
     public void afterMethod() throws Exception {
+        if (hasFailed()) {
+            return;
+        }
+
         retryService.stop();
 
         eventBus.unregister(handler);
@@ -188,19 +191,19 @@ public class TestJanitor extends PaymentTestSuiteWithEmbeddedDB {
                                                             Currency.USD));
 
         testListener.pushExpectedEvent(NextEvent.PAYMENT);
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
         testListener.assertListenerStatus();
         assertEquals(payment.getTransactions().size(), 1);
         assertEquals(payment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
@@ -255,19 +258,19 @@ public class TestJanitor extends PaymentTestSuiteWithEmbeddedDB {
         invoice.addInvoiceItem(invoiceItem);
 
         testListener.pushExpectedEvent(NextEvent.PAYMENT);
-        final InvoicePayment invoicePayment = invoicePaymentApi.createPurchaseForInvoice(account,
-                                                                                         invoice.getId(),
-                                                                                         account.getPaymentMethodId(),
-                                                                                         null,
-                                                                                         requestedAmount,
-                                                                                         Currency.USD,
-                                                                                         null,
-                                                                                         paymentExternalKey,
-                                                                                         transactionExternalKey,
-                                                                                         ImmutableList.<PluginProperty>of(),
-                                                                                         INVOICE_PAYMENT,
-                                                                                         callContext);
-        final Payment payment = paymentApi.getPayment(invoicePayment.getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
+        invoicePaymentApi.createPurchaseForInvoice(account,
+                                                   invoice.getId(),
+                                                   account.getPaymentMethodId(),
+                                                   null,
+                                                   requestedAmount,
+                                                   Currency.USD,
+                                                   null,
+                                                   paymentExternalKey,
+                                                   transactionExternalKey,
+                                                   ImmutableList.<PluginProperty>of(),
+                                                   INVOICE_PAYMENT,
+                                                   callContext);
+        final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
         testListener.assertListenerStatus();
 
         final List<PluginProperty> refundProperties = new ArrayList<PluginProperty>();
@@ -275,9 +278,9 @@ public class TestJanitor extends PaymentTestSuiteWithEmbeddedDB {
         uuidBigDecimalHashMap.put(invoiceItem.getId(), new BigDecimal("1.0"));
 
         testListener.pushExpectedEvent(NextEvent.PAYMENT);
-        final InvoicePayment invoicePayment2 = invoicePaymentApi.createRefundForInvoice(false, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2,
-                                                                                        refundProperties, INVOICE_PAYMENT, callContext);
-        final Payment payment2 = paymentApi.getPayment(invoicePayment2.getPaymentId(), false, false, refundProperties, callContext);
+        invoicePaymentApi.createRefundForInvoice(false, uuidBigDecimalHashMap, account, payment.getId(), null, Currency.USD, null, transactionExternalKey2,
+                                                 refundProperties, INVOICE_PAYMENT, callContext);
+        final Payment payment2 = paymentApi.getPayment(payment.getId(), false, false, refundProperties, callContext);
         testListener.assertListenerStatus();
 
         assertEquals(payment2.getTransactions().size(), 2);