From cc3550b059a187d2ec8e15693bfa847c76c47da5 Mon Sep 17 00:00:00 2001 From: Sofiane Louchene Date: Sun, 10 Sep 2023 23:09:11 +0200 Subject: [PATCH] Fix: open retrofit for consumers and add invoice exception --- lib/build.gradle | 2 +- .../java/chargily/epay/java/ChargilyClient.java | 17 ++++++++--------- .../chargily/epay/java/InvoiceException.java | 5 ++--- .../chargily/epay/java/ChargilyClientTest.java | 5 ++--- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/build.gradle b/lib/build.gradle index 759e38d..d56b583 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -37,11 +37,11 @@ dependencies { // This dependency is exported to consumers, that is to say found on their compile classpath. api 'org.apache.commons:commons-math3:3.6.1' + api 'com.squareup.retrofit2:retrofit:2.9.0' // This dependency is used internally, and not exposed to consumers on their own compile classpath. implementation 'com.google.guava:guava:31.0.1-jre' implementation 'com.github.mvallim:java-fluent-validator:1.10.0' - implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.google.code.gson:gson:2.9.0' testImplementation 'org.awaitility:awaitility:4.2.0' diff --git a/lib/src/main/java/chargily/epay/java/ChargilyClient.java b/lib/src/main/java/chargily/epay/java/ChargilyClient.java index ed671c1..99b2a5f 100644 --- a/lib/src/main/java/chargily/epay/java/ChargilyClient.java +++ b/lib/src/main/java/chargily/epay/java/ChargilyClient.java @@ -1,7 +1,6 @@ package chargily.epay.java; import br.com.fluentvalidator.context.ValidationResult; -import br.com.fluentvalidator.exception.ValidationException; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -28,13 +27,13 @@ public ChargilyClient(String apiKey) { * * @param invoice Invoice information to be sent to Chargily API. * @return ChargilyResponse that contains checkoutUrl. - * @throws IOException if a problem occurred talking to the server. - * @throws ValidationException if the invoice object is not valid. + * @throws IOException if a problem occurred talking to the server. + * @throws InvoiceException if the invoice object is not valid. * @see #submitInvoiceAsync(Invoice, ChargilyCallback) submitInvoiceAsync * @deprecated Use {@link #submitInvoice(Invoice)} instead. This method will be removed in future versions. */ @Deprecated(since = "1.1", forRemoval = true) - public Response createInvoice(Invoice invoice) throws IOException, ValidationException { + public Response createInvoice(Invoice invoice) throws IOException, InvoiceException { var validations = new InvoiceValidator().validate(invoice); if (!validations.isValid()) throw new InvoiceException(validations); @@ -46,11 +45,11 @@ public Response createInvoice(Invoice invoice) throws IOExcept * * @param invoice Invoice information to be sent to the Chargily API. * @return ChargilyResponse containing the checkout URL. - * @throws ValidationException if the invoice object is not valid. - * @throws IOException if a problem occurred talking to the server. + * @throws InvoiceException if the invoice object is not valid. + * @throws IOException if a problem occurred talking to the server. * @see #submitInvoiceAsync(Invoice, ChargilyCallback) submitInvoiceAsync */ - public ChargilyResponse submitInvoice(Invoice invoice) throws ValidationException, IOException { + public ChargilyResponse submitInvoice(Invoice invoice) throws InvoiceException, IOException { ValidationResult validations = new InvoiceValidator().validate(invoice); if (!validations.isValid()) { throw new InvoiceException(validations); @@ -68,10 +67,10 @@ public ChargilyResponse submitInvoice(Invoice invoice) throws ValidationExceptio * @param invoice Invoice information to be sent to the Chargily API. * @param callback Chargily Callback object with OnResponse and OnFailure methods. The callback * will receive a {@link ChargilyResponse} object. - * @throws ValidationException if the invoice object is not valid. + * @throws InvoiceException if the invoice object is not valid. * @see #submitInvoice(Invoice) submitInvoice */ - public void submitInvoiceAsync(Invoice invoice, ChargilyCallback callback) throws ValidationException { + public void submitInvoiceAsync(Invoice invoice, ChargilyCallback callback) throws InvoiceException { ValidationResult validations = new InvoiceValidator().validate(invoice); if (!validations.isValid()) throw new InvoiceException(validations); diff --git a/lib/src/main/java/chargily/epay/java/InvoiceException.java b/lib/src/main/java/chargily/epay/java/InvoiceException.java index ef9404f..75c9744 100644 --- a/lib/src/main/java/chargily/epay/java/InvoiceException.java +++ b/lib/src/main/java/chargily/epay/java/InvoiceException.java @@ -1,12 +1,11 @@ package chargily.epay.java; import br.com.fluentvalidator.context.ValidationResult; -import br.com.fluentvalidator.exception.ValidationException; -public class InvoiceException extends ValidationException { +public class InvoiceException extends RuntimeException { public InvoiceException(ValidationResult validationResult) { - super(validationResult); + super(validationResult.toString()); } } diff --git a/lib/src/test/java/chargily/epay/java/ChargilyClientTest.java b/lib/src/test/java/chargily/epay/java/ChargilyClientTest.java index f299783..4b8c94c 100644 --- a/lib/src/test/java/chargily/epay/java/ChargilyClientTest.java +++ b/lib/src/test/java/chargily/epay/java/ChargilyClientTest.java @@ -1,6 +1,5 @@ package chargily.epay.java; -import br.com.fluentvalidator.exception.ValidationException; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import retrofit2.Call; @@ -49,7 +48,7 @@ void should_throw_validation_exception_when_client_invoice_is_null() { invoice.setClientName(null); // When, Then - assertThatExceptionOfType(ValidationException.class) + assertThatExceptionOfType(InvoiceException.class) .isThrownBy(() -> chargilyClient.submitInvoice(invoice)) .withMessageContaining("client name cannot be null or empty"); @@ -140,7 +139,7 @@ void should_throw_valid_exception_when_amount_invoice_is_null() { invoice.setAmount(null); // When, Then - assertThatExceptionOfType(ValidationException.class) + assertThatExceptionOfType(InvoiceException.class) .isThrownBy(() -> chargilyClient.submitInvoiceAsync(invoice, null)) .withMessageContaining("invoice amount cannot be less than 75.0"); }