killbill-memoizeit

server: add chargeback test for deleted payment methods Make

2/4/2013 5:24:31 PM

Details

diff --git a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
index 1a8dbde..65563d7 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/KillbillClient.java
@@ -78,6 +78,7 @@ import com.google.common.collect.ImmutableMap;
 
 import static com.ning.billing.jaxrs.resources.JaxrsResource.ACCOUNTS;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.BUNDLES;
+import static com.ning.billing.jaxrs.resources.JaxrsResource.QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.QUERY_PAYMENT_METHOD_PLUGIN_INFO;
 import static com.ning.billing.jaxrs.resources.JaxrsResource.SUBSCRIPTIONS;
 import static org.testng.Assert.assertEquals;
@@ -580,6 +581,13 @@ public abstract class KillbillClient extends ServerTestSuiteWithEmbeddedDB {
         return paymentMethodJson;
     }
 
+    protected void deletePaymentMethod(final String paymentMethodId, final Boolean deleteDefault) throws IOException {
+        final String paymentMethodURI = JaxrsResource.PAYMENT_METHODS_PATH + "/" + paymentMethodId;
+
+        final Response response = doDelete(paymentMethodURI, ImmutableMap.<String, String>of(QUERY_DELETE_DEFAULT_PM_WITH_AUTO_PAY_OFF, deleteDefault.toString()), DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
+    }
+
     protected List<PaymentJsonSimple> getPaymentsForAccount(final String accountId) throws IOException {
         final String paymentsURI = JaxrsResource.ACCOUNTS_PATH + "/" + accountId + "/" + JaxrsResource.PAYMENTS;
         final Response paymentsResponse = doGet(paymentsURI, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
index 569bc3b..e22d58a 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
@@ -45,6 +45,7 @@ import com.google.common.collect.ImmutableMap;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 public class TestChargeback extends TestJaxrsBase {
@@ -52,30 +53,9 @@ public class TestChargeback extends TestJaxrsBase {
     @Test(groups = "slow")
     public void testAddChargeback() throws Exception {
         final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
-        final ChargebackJson input = new ChargebackJson(null, null, BigDecimal.TEN, payment.getPaymentId(), null, null);
-        final String jsonInput = mapper.writeValueAsString(input);
-
-        // Create the chargeback
-        Response response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode(), response.getResponseBody());
-
-        // Find the chargeback by location
-        final String location = response.getHeader("Location");
-        assertNotNull(location);
-        response = doGetWithUrl(location, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifySingleChargebackResponse(response, input);
-
-
-        // Find the chargeback by account
-        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifyCollectionChargebackResponse(response, input);
-
-        // Find the chargeback by payment
-        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + payment.getPaymentId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
-        verifyCollectionChargebackResponse(response, input);
+        createAndVerifyChargeback(payment);
     }
 
-
     @Test(groups = "slow")
     public void testMultipleChargeback() throws Exception {
         final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
@@ -86,7 +66,7 @@ public class TestChargeback extends TestJaxrsBase {
 
         //
         int count = 4;
-        Response response = null;
+        Response response;
         while (count-- > 0) {
             response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
             assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.CREATED.getStatusCode(), response.getResponseBody());
@@ -96,14 +76,13 @@ public class TestChargeback extends TestJaxrsBase {
         response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), response.getResponseBody());
 
-
         // Find the chargeback by account
         response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
         ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
         assertEquals(objFromJson.getChargebacks().size(), 4);
         for (int i = 0; i < objFromJson.getChargebacks().size(); i++) {
-            ChargebackJson chargeBack = objFromJson.getChargebacks().get(i);
+            final ChargebackJson chargeBack = objFromJson.getChargebacks().get(i);
             assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
             assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
         }
@@ -113,23 +92,23 @@ public class TestChargeback extends TestJaxrsBase {
         assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
         objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
         assertEquals(objFromJson.getChargebacks().size(), 4);
-
     }
 
+    @Test(groups = "slow")
+    public void testAddChargebackForDeletedPaymentMethod() throws Exception {
+        final PaymentJsonSimple payment = createAccountWithInvoiceAndPayment();
 
-    private void verifyCollectionChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
-        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
-        final ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
-        assertEquals(objFromJson.getChargebacks().size(), 1);
-        ChargebackJson chargeBack = objFromJson.getChargebacks().get(0);
-        assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
-        assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
-    }
+        // Check the payment method exists
+        assertEquals(getAccountById(payment.getAccountId()).getPaymentMethodId(), payment.getPaymentMethodId());
+        assertEquals(getPaymentMethod(payment.getPaymentMethodId()).getAccountId(), payment.getAccountId());
 
-    private void verifySingleChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
-        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
-        final ChargebackJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackJson.class);
-        assertTrue(objFromJson.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+        // Delete the payment method
+        deletePaymentMethod(payment.getPaymentMethodId(), true);
+
+        // Check the payment method was deleted
+        assertNull(getAccountById(payment.getAccountId()).getPaymentMethodId());
+
+        createAndVerifyChargeback(payment);
     }
 
     @Test(groups = "slow")
@@ -173,6 +152,44 @@ public class TestChargeback extends TestJaxrsBase {
         //assertEquals(response.getStatusCode(),Status.NO_CONTENT.getStatusCode(), response.getResponseBody());
     }
 
+    private void createAndVerifyChargeback(final PaymentJsonSimple payment) throws IOException {
+        final ChargebackJson input = new ChargebackJson(null, null, BigDecimal.TEN, payment.getPaymentId(), null, null);
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Create the chargeback
+        Response response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode(), response.getResponseBody());
+
+        // Find the chargeback by location
+        final String location = response.getHeader("Location");
+        assertNotNull(location);
+        response = doGetWithUrl(location, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifySingleChargebackResponse(response, input);
+
+        // Find the chargeback by account
+        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + payment.getAccountId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifyCollectionChargebackResponse(response, input);
+
+        // Find the chargeback by payment
+        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + payment.getPaymentId(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifyCollectionChargebackResponse(response, input);
+    }
+
+    private void verifyCollectionChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
+        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
+        final ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
+        assertEquals(objFromJson.getChargebacks().size(), 1);
+        final ChargebackJson chargeBack = objFromJson.getChargebacks().get(0);
+        assertTrue(chargeBack.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+        assertEquals(chargeBack.getPaymentId(), input.getPaymentId());
+    }
+
+    private void verifySingleChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
+        assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
+        final ChargebackJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackJson.class);
+        assertTrue(objFromJson.getChargebackAmount().compareTo(input.getChargebackAmount()) == 0);
+    }
+
     private PaymentJsonSimple createAccountWithInvoiceAndPayment() throws Exception {
         final InvoiceJsonSimple invoice = createAccountWithInvoice();
         return getPayment(invoice);