Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from mrsofiane/fix/open-retrofit-to-comsumer-a…
Browse files Browse the repository at this point in the history
…nd-replace-validationException

Fix: open retrofit for consumers and add invoice exception
  • Loading branch information
mrsofiane authored Sep 10, 2023
2 parents 15d9b50 + cc3550b commit 829f129
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
17 changes: 8 additions & 9 deletions lib/src/main/java/chargily/epay/java/ChargilyClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ChargilyResponse> createInvoice(Invoice invoice) throws IOException, ValidationException {
public Response<ChargilyResponse> createInvoice(Invoice invoice) throws IOException, InvoiceException {
var validations = new InvoiceValidator().validate(invoice);
if (!validations.isValid())
throw new InvoiceException(validations);
Expand All @@ -46,11 +45,11 @@ public Response<ChargilyResponse> 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);
Expand All @@ -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<ChargilyResponse> callback) throws ValidationException {
public void submitInvoiceAsync(Invoice invoice, ChargilyCallback<ChargilyResponse> callback) throws InvoiceException {
ValidationResult validations = new InvoiceValidator().validate(invoice);
if (!validations.isValid())
throw new InvoiceException(validations);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/main/java/chargily/epay/java/InvoiceException.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
5 changes: 2 additions & 3 deletions lib/src/test/java/chargily/epay/java/ChargilyClientTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 829f129

Please sign in to comment.