killbill-aplcache

jaxrs: add tests for credit and chargeback resources These

6/8/2012 9:34:04 PM

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
index 22475d7..29a85e3 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/ChargebackJson.java
@@ -51,7 +51,6 @@ public class ChargebackJson {
         this.chargebackAmount = chargeback.getAmount().negate();
         this.paymentId = chargeback.getReversedInvoicePaymentId().toString();
         this.reason = null;
-
     }
 
     public DateTime getRequestedDate() {
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/ChargebackResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/ChargebackResource.java
index c8ff0b2..ec84f70 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/ChargebackResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/ChargebackResource.java
@@ -16,12 +16,6 @@
 
 package com.ning.billing.jaxrs.resources;
 
-import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
@@ -30,6 +24,9 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,6 +43,8 @@ import com.ning.billing.jaxrs.util.JaxrsUriBuilder;
 import com.ning.billing.payment.api.PaymentApi;
 import com.ning.billing.payment.api.PaymentApiException;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
 @Singleton
 @Path(JaxrsResource.CHARGEBACKS_PATH)
 public class ChargebackResource implements JaxrsResource {
@@ -70,10 +69,10 @@ public class ChargebackResource implements JaxrsResource {
     @GET
     @Path("/{chargebackId:" + UUID_PATTERN + "}")
     @Produces(APPLICATION_JSON)
-    public Response getChargeback(@PathParam("chargebackId") String chargebackId) {
+    public Response getChargeback(@PathParam("chargebackId") final String chargebackId) {
         try {
-            InvoicePayment chargeback = invoicePaymentApi.getChargebackById(UUID.fromString(chargebackId));
-            ChargebackJson chargebackJson = new ChargebackJson(chargeback);
+            final InvoicePayment chargeback = invoicePaymentApi.getChargebackById(UUID.fromString(chargebackId));
+            final ChargebackJson chargebackJson = new ChargebackJson(chargeback);
 
             return Response.status(Response.Status.OK).entity(chargebackJson).build();
         } catch (InvoiceApiException e) {
@@ -86,19 +85,19 @@ public class ChargebackResource implements JaxrsResource {
     @GET
     @Path("/accounts/{accountId:" + UUID_PATTERN + "}")
     @Produces(APPLICATION_JSON)
-    public Response getForAccount(@PathParam("accountId") String accountId) {
-        List<InvoicePayment> chargebacks = invoicePaymentApi.getChargebacksByAccountId(UUID.fromString(accountId));
-        List<ChargebackJson> chargebacksJson = convertToJson(chargebacks);
+    public Response getForAccount(@PathParam("accountId") final String accountId) {
+        final List<InvoicePayment> chargebacks = invoicePaymentApi.getChargebacksByAccountId(UUID.fromString(accountId));
+        final List<ChargebackJson> chargebacksJson = convertToJson(chargebacks);
 
-        ChargebackCollectionJson json = new ChargebackCollectionJson(accountId, chargebacksJson);
+        final ChargebackCollectionJson json = new ChargebackCollectionJson(accountId, chargebacksJson);
         return Response.status(Response.Status.OK).entity(json).build();
     }
 
     @GET
     @Path("/payments/{paymentId:" + UUID_PATTERN + "}")
     @Produces(APPLICATION_JSON)
-    public Response getForPayment(@PathParam("paymentId") String paymentId) {
-        UUID paymentAttemptId = null;
+    public Response getForPayment(@PathParam("paymentId") final String paymentId) {
+        final UUID paymentAttemptId;
         try {
             paymentAttemptId = paymentApi.getPaymentAttemptIdFromPaymentId(UUID.fromString(paymentId));
         } catch (PaymentApiException e) {
@@ -107,13 +106,13 @@ public class ChargebackResource implements JaxrsResource {
             return Response.status(Response.Status.NO_CONTENT).build();
         }
 
-        List<InvoicePayment> chargebacks = invoicePaymentApi.getChargebacksByPaymentAttemptId(paymentAttemptId);
-        List<ChargebackJson> chargebacksJson = convertToJson(chargebacks);
+        final List<InvoicePayment> chargebacks = invoicePaymentApi.getChargebacksByPaymentAttemptId(paymentAttemptId);
+        final List<ChargebackJson> chargebacksJson = convertToJson(chargebacks);
 
         try {
-            String accountId = invoicePaymentApi.getAccountIdFromInvoicePaymentId(UUID.fromString(paymentId)).toString();
+            final String accountId = invoicePaymentApi.getAccountIdFromInvoicePaymentId(UUID.fromString(paymentId)).toString();
 
-            ChargebackCollectionJson json = new ChargebackCollectionJson(accountId, chargebacksJson);
+            final ChargebackCollectionJson json = new ChargebackCollectionJson(accountId, chargebacksJson);
             return Response.status(Response.Status.OK).entity(json).build();
         } catch (InvoiceApiException e) {
             final String error = String.format("Failed to locate account for payment id %s", paymentId);
@@ -142,9 +141,9 @@ public class ChargebackResource implements JaxrsResource {
         }
     }
 
-    private List<ChargebackJson> convertToJson(List<InvoicePayment> chargebacks) {
-        List<ChargebackJson> result = new ArrayList<ChargebackJson>();
-        for (InvoicePayment chargeback : chargebacks) {
+    private List<ChargebackJson> convertToJson(final List<InvoicePayment> chargebacks) {
+        final List<ChargebackJson> result = new ArrayList<ChargebackJson>();
+        for (final InvoicePayment chargeback : chargebacks) {
             result.add(new ChargebackJson(chargeback));
         }
 
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CreditResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CreditResource.java
index 226457e..8e97509 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CreditResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/CreditResource.java
@@ -16,10 +16,6 @@
 
 package com.ning.billing.jaxrs.resources;
 
-import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
-
-import java.util.UUID;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
@@ -28,6 +24,7 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
+import java.util.UUID;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -44,6 +41,8 @@ import com.ning.billing.jaxrs.json.CreditJson;
 import com.ning.billing.jaxrs.util.Context;
 import com.ning.billing.jaxrs.util.JaxrsUriBuilder;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
 @Singleton
 @Path(JaxrsResource.CREDITS_PATH)
 public class CreditResource implements JaxrsResource {
@@ -55,7 +54,7 @@ public class CreditResource implements JaxrsResource {
     private final Context context;
 
     @Inject
-    public CreditResource(JaxrsUriBuilder uriBuilder, InvoiceUserApi invoiceUserApi, AccountUserApi accountUserApi, Context context) {
+    public CreditResource(final JaxrsUriBuilder uriBuilder, final InvoiceUserApi invoiceUserApi, final AccountUserApi accountUserApi, final Context context) {
         this.uriBuilder = uriBuilder;
         this.invoiceUserApi = invoiceUserApi;
         this.accountUserApi = accountUserApi;
@@ -65,10 +64,10 @@ public class CreditResource implements JaxrsResource {
     @GET
     @Path("/{creditId:" + UUID_PATTERN + "}")
     @Produces(APPLICATION_JSON)
-    public Response getCredit(@PathParam("creditId") String creditId) {
+    public Response getCredit(@PathParam("creditId") final String creditId) {
         try {
-            InvoiceItem credit = invoiceUserApi.getCreditById(UUID.fromString(creditId));
-            CreditJson creditJson = new CreditJson(credit);
+            final InvoiceItem credit = invoiceUserApi.getCreditById(UUID.fromString(creditId));
+            final CreditJson creditJson = new CreditJson(credit);
 
             return Response.status(Response.Status.OK).entity(creditJson).build();
         } catch (InvoiceApiException e) {
@@ -88,12 +87,12 @@ public class CreditResource implements JaxrsResource {
                                  @HeaderParam(HDR_REASON) final String reason,
                                  @HeaderParam(HDR_COMMENT) final String comment) {
         try {
-            Account account = accountUserApi.getAccountById(UUID.fromString(accountId));
+            final Account account = accountUserApi.getAccountById(UUID.fromString(accountId));
 
-            InvoiceItem credit = invoiceUserApi.insertCredit(account.getId(), json.getCreditAmount(), json.getEffectiveDate(),
-                                                             account.getCurrency(), context.createContext(createdBy, reason, comment));
+            final InvoiceItem credit = invoiceUserApi.insertCredit(account.getId(), json.getCreditAmount(), json.getEffectiveDate(),
+                                                                   account.getCurrency(), context.createContext(createdBy, reason, comment));
 
-            return uriBuilder.buildResponse(ChargebackResource.class, "getCredit", credit.getId());
+            return uriBuilder.buildResponse(CreditResource.class, "getCredit", credit.getId());
         } catch (InvoiceApiException e) {
             final String error = String.format("Failed to create credit %s", json);
             log.info(error, e);
@@ -106,8 +105,6 @@ public class CreditResource implements JaxrsResource {
             return Response.status(Response.Status.BAD_REQUEST).entity(error).build();
         }
     }
-
-
 }
 
 
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
new file mode 100644
index 0000000..9ed6a52
--- /dev/null
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestChargeback.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2010-2012 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.jaxrs;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.ning.billing.invoice.api.InvoicePayment;
+import com.ning.billing.jaxrs.json.ChargebackCollectionJson;
+import com.ning.billing.jaxrs.json.ChargebackJson;
+import com.ning.billing.jaxrs.resources.JaxrsResource;
+import com.ning.http.client.Response;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+@Test(groups = "slow", enabled = false)
+public class TestChargeback extends TestJaxrsBase {
+    private final String accountId = UUID.randomUUID().toString();
+    private InvoicePayment invoicePayment;
+
+    @BeforeMethod(groups = "slow")
+    public void setUp() throws Exception {
+        invoicePayment = createInvoicePayment();
+    }
+
+    @Test(groups = "slow", enabled = false)
+    public void testAddChargeback() throws Exception {
+        final ChargebackJson input = new ChargebackJson(new DateTime(DateTimeZone.UTC), new DateTime(DateTimeZone.UTC),
+                                                        BigDecimal.TEN, UUID.randomUUID().toString(), UUID.randomUUID().toString());
+        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(), javax.ws.rs.core.Response.Status.OK.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/" + accountId, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        verifyCollectionChargebackResponse(response, input);
+
+        // Find the chargeback by payment
+        response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + input.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(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
+        final ChargebackCollectionJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
+        assertEquals(objFromJson.getChargebacks().size(), 1);
+        assertEquals(objFromJson.getChargebacks().get(0), input);
+    }
+
+    private void verifySingleChargebackResponse(final Response response, final ChargebackJson input) throws IOException {
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
+        final ChargebackJson objFromJson = mapper.readValue(response.getResponseBody(), ChargebackJson.class);
+        assertEquals(objFromJson, input);
+    }
+
+    @Test(groups = "slow")
+    public void testInvoicePaymentDoesNotExist() throws Exception {
+        final ChargebackJson input = new ChargebackJson(new DateTime(DateTimeZone.UTC), new DateTime(DateTimeZone.UTC),
+                                                        BigDecimal.TEN, UUID.randomUUID().toString(), UUID.randomUUID().toString());
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Try to create the chargeback
+        final Response 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());
+    }
+
+    @Test(groups = "slow")
+    public void testBadRequest() throws Exception {
+        final ChargebackJson input = new ChargebackJson(null, null, null, null, null);
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Try to create the chargeback
+        final Response response = doPost(JaxrsResource.CHARGEBACKS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getResponseBody());
+    }
+
+    @Test(groups = "slow")
+    public void testNoChargebackForAccount() throws Exception {
+        final String accountId = UUID.randomUUID().toString();
+        final Response response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/accounts/" + accountId, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode(), response.getResponseBody());
+
+        final ChargebackCollectionJson chargebackCollectionJson = mapper.readValue(response.getResponseBody(), ChargebackCollectionJson.class);
+        Assert.assertEquals(chargebackCollectionJson.getAccountId(), accountId);
+        Assert.assertEquals(chargebackCollectionJson.getChargebacks().size(), 0);
+    }
+
+    @Test(groups = "slow")
+    public void testNoChargebackForPayment() throws Exception {
+        final String payment = UUID.randomUUID().toString();
+        final Response response = doGet(JaxrsResource.CHARGEBACKS_PATH + "/payments/" + payment, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode(), response.getResponseBody());
+    }
+
+    private InvoicePayment createInvoicePayment() {
+        // TODO - blocked on payment resource
+        return null;
+    }
+}
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestCredit.java b/server/src/test/java/com/ning/billing/jaxrs/TestCredit.java
new file mode 100644
index 0000000..4abee9d
--- /dev/null
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestCredit.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2010-2012 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.jaxrs;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.ning.billing.jaxrs.json.AccountJson;
+import com.ning.billing.jaxrs.json.CreditJson;
+import com.ning.billing.jaxrs.resources.JaxrsResource;
+import com.ning.http.client.Response;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+@Test(groups = "slow", enabled = false)
+public class TestCredit extends TestJaxrsBase {
+    AccountJson accountJson;
+
+    @BeforeMethod(groups = "slow")
+    public void setUp() throws Exception {
+        accountJson = createAccount(UUID.randomUUID().toString(), UUID.randomUUID().toString(), "foo@bar.com");
+        assertNotNull(accountJson);
+    }
+
+    @Test(groups = "slow")
+    public void testAddCreditToInvoice() throws Exception {
+        final CreditJson input = new CreditJson(BigDecimal.TEN, UUID.randomUUID(), UUID.randomUUID().toString(),
+                                                new DateTime(DateTimeZone.UTC), new DateTime(DateTimeZone.UTC),
+                                                UUID.randomUUID().toString());
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Create the credit
+        Response response = doPost(JaxrsResource.CREDITS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.CREATED.getStatusCode(), response.getResponseBody());
+
+        final String location = response.getHeader("Location");
+        assertNotNull(location);
+
+        // Retrieves by Id based on Location returned
+        response = doGetWithUrl(location, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.OK.getStatusCode());
+
+        final CreditJson objFromJson = mapper.readValue(response.getResponseBody(), CreditJson.class);
+        assertNotNull(objFromJson);
+        assertEquals(objFromJson, input);
+    }
+
+    @Test(groups = "slow")
+    public void testAccountDoesNotExist() throws Exception {
+        final CreditJson input = new CreditJson(BigDecimal.TEN, UUID.randomUUID(), UUID.randomUUID().toString(),
+                                                new DateTime(DateTimeZone.UTC), new DateTime(DateTimeZone.UTC),
+                                                UUID.randomUUID().toString());
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Try to create the credit
+        final Response response = doPost(JaxrsResource.CREDITS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), response.getResponseBody());
+    }
+
+    @Test(groups = "slow")
+    public void testBadRequest() throws Exception {
+        final CreditJson input = new CreditJson(null, null, null, null, null, null);
+        final String jsonInput = mapper.writeValueAsString(input);
+
+        // Try to create the credit
+        final Response response = doPost(JaxrsResource.CREDITS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), response.getResponseBody());
+    }
+
+    @Test(groups = "slow")
+    public void testCreditDoesNotExist() throws Exception {
+        final Response response = doGet(JaxrsResource.CREDITS_PATH + "/" + UUID.randomUUID().toString(), DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC);
+        assertEquals(response.getStatusCode(), javax.ws.rs.core.Response.Status.NOT_FOUND.getStatusCode(), response.getResponseBody());
+    }
+}