diff --git a/pom.xml b/pom.xml index c8046920..2496dc9b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.bitpay bitpay_sdk - 9.0.2 + 10.0.0 jar BitPay diff --git a/src/main/java/com/bitpay/sdk/Client.java b/src/main/java/com/bitpay/sdk/Client.java index ddbdcfeb..68438ece 100644 --- a/src/main/java/com/bitpay/sdk/Client.java +++ b/src/main/java/com/bitpay/sdk/Client.java @@ -19,32 +19,9 @@ import com.bitpay.sdk.client.RefundClient; import com.bitpay.sdk.client.SettlementClient; import com.bitpay.sdk.client.WalletClient; -import com.bitpay.sdk.exceptions.BillCreationException; -import com.bitpay.sdk.exceptions.BillDeliveryException; -import com.bitpay.sdk.exceptions.BillQueryException; -import com.bitpay.sdk.exceptions.BillUpdateException; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.InvoiceCancellationException; -import com.bitpay.sdk.exceptions.InvoiceCreationException; -import com.bitpay.sdk.exceptions.InvoiceQueryException; -import com.bitpay.sdk.exceptions.InvoiceUpdateException; -import com.bitpay.sdk.exceptions.LedgerQueryException; -import com.bitpay.sdk.exceptions.PayoutCancellationException; -import com.bitpay.sdk.exceptions.PayoutCreationException; -import com.bitpay.sdk.exceptions.PayoutNotificationException; -import com.bitpay.sdk.exceptions.PayoutQueryException; -import com.bitpay.sdk.exceptions.PayoutRecipientCancellationException; -import com.bitpay.sdk.exceptions.PayoutRecipientCreationException; -import com.bitpay.sdk.exceptions.PayoutRecipientNotificationException; -import com.bitpay.sdk.exceptions.PayoutRecipientQueryException; -import com.bitpay.sdk.exceptions.PayoutRecipientUpdateException; -import com.bitpay.sdk.exceptions.RateQueryException; -import com.bitpay.sdk.exceptions.RefundCancellationException; -import com.bitpay.sdk.exceptions.RefundCreationException; -import com.bitpay.sdk.exceptions.RefundQueryException; -import com.bitpay.sdk.exceptions.RefundUpdateException; -import com.bitpay.sdk.exceptions.SettlementQueryException; -import com.bitpay.sdk.exceptions.WalletQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.bill.Bill; import com.bitpay.sdk.model.invoice.Invoice; @@ -64,11 +41,9 @@ import com.bitpay.sdk.util.JsonMapperFactory; import com.bitpay.sdk.util.KeyUtils; import com.bitpay.sdk.util.TokenContainer; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.json.JsonMapper; import java.io.File; -import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collection; @@ -86,12 +61,12 @@ */ public class Client { - private static final String LOAD_PRIVATE_KEY_EXCEPTION = + protected static final String LOAD_PRIVATE_KEY_EXCEPTION = "When trying to load private key. Make sure the configuration details are correct " + "and the private key and tokens are valid : "; - private final GuidGenerator guidGenerator; - private final BitPayClient bitPayClient; - private final TokenContainer tokenContainer; + protected GuidGenerator guidGenerator; + protected BitPayClient bitPayClient; + protected TokenContainer tokenContainer; /** * Return the identity of this client (i.e. the public key). @@ -102,9 +77,9 @@ public class Client { * Constructor for POS facade. * * @param token POS token - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - public Client(PosToken token) throws BitPayException { + public Client(PosToken token) throws BitPayGenericException { this(token, Environment.PROD); } @@ -113,14 +88,14 @@ public Client(PosToken token) throws BitPayException { * * @param token POS token * @param environment Environment - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ public Client( PosToken token, Environment environment - ) throws BitPayException { + ) throws BitPayGenericException { if (Objects.isNull(token) || Objects.isNull(environment)) { - throw new BitPayException(null, "Missing required constructor parameters"); + BitPayExceptionProvider.throwMissingParameterException(); } this.tokenContainer = new TokenContainer(); @@ -142,7 +117,7 @@ public Client( * @param tokenContainer Object containing the available tokens. * @param proxyDetails HttpHost Optional Proxy setting (set to NULL to ignore) * @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore) - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ public Client( Environment environment, @@ -150,22 +125,17 @@ public Client( TokenContainer tokenContainer, HttpHost proxyDetails, CredentialsProvider proxyCredentials - ) throws BitPayException { - try { - ECKey ecKey = getEcKey(privateKey); - this.tokenContainer = tokenContainer; - this.deriveIdentity(ecKey); - this.bitPayClient = new BitPayClient( - getHttpClient(proxyDetails, proxyCredentials), - new HttpRequestFactory(), - getBaseUrl(environment), - ecKey - ); - this.guidGenerator = new GuidGenerator(); - } catch (Exception e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Config) : " + e.getMessage()); - } + ) throws BitPayGenericException { + ECKey ecKey = getEcKey(privateKey); + this.tokenContainer = tokenContainer; + this.deriveIdentity(ecKey); + this.bitPayClient = new BitPayClient( + getHttpClient(proxyDetails, proxyCredentials), + new HttpRequestFactory(), + getBaseUrl(environment), + ecKey + ); + this.guidGenerator = new GuidGenerator(); } /** @@ -174,36 +144,28 @@ public Client( * @param configFilePath The path to the configuration file. * @param proxy HttpHost Optional Proxy setting (set to NULL to ignore) * @param proxyCredentials CredentialsProvider Optional Proxy Basic Auth Credentials (set to NULL to ignore) - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ public Client( ConfigFilePath configFilePath, HttpHost proxy, CredentialsProvider proxyCredentials - ) throws BitPayException { - try { - Config config = this.buildConfigFromFile(configFilePath); - this.tokenContainer = new TokenContainer(config); - ECKey ecKey = this.getEcKey(config); - if (Objects.isNull(ecKey)) { - throw new BitPayException(null, "Missing ECKey"); - } - - this.deriveIdentity(ecKey); - this.bitPayClient = new BitPayClient( - getHttpClient(proxy, proxyCredentials), - new HttpRequestFactory(), - getBaseUrl(config.getEnvironment()), - ecKey - ); - this.guidGenerator = new GuidGenerator(); - } catch (JsonProcessingException | URISyntaxException e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Config) : " + e.getMessage()); - } catch (Exception e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Config) : " + e.getMessage()); + ) throws BitPayGenericException { + Config config = this.buildConfigFromFile(configFilePath); + this.tokenContainer = new TokenContainer(config); + ECKey ecKey = this.getEcKey(config); + if (Objects.isNull(ecKey)) { + BitPayExceptionProvider.throwValidationException("Missing ECKey"); } + + this.deriveIdentity(ecKey); + this.bitPayClient = new BitPayClient( + getHttpClient(proxy, proxyCredentials), + new HttpRequestFactory(), + getBaseUrl(config.getEnvironment()), + ecKey + ); + this.guidGenerator = new GuidGenerator(); } /** @@ -231,9 +193,9 @@ public Client( * * @param token the token * @return the client - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - public static Client createPosClient(PosToken token) throws BitPayException { + public static Client createPosClient(PosToken token) throws BitPayGenericException { return new Client(token); } @@ -243,12 +205,12 @@ public static Client createPosClient(PosToken token) throws BitPayException { * @param token the token * @param environment environment * @return the client - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ public static Client createPosClient( PosToken token, Environment environment - ) throws BitPayException { + ) throws BitPayGenericException { return new Client(token, environment); } @@ -259,13 +221,13 @@ public static Client createPosClient( * @param tokenContainer the token container * @param environment environment * @return Client Client - * @throws BitPayException BitPayException + * @throws BitPayGenericException BitPayGenericException class */ public static Client createClientByPrivateKey( PrivateKey privateKey, TokenContainer tokenContainer, Environment environment - ) throws BitPayException { + ) throws BitPayGenericException { Environment env = Objects.isNull(environment) ? Environment.PROD : environment; return new Client(env, privateKey, tokenContainer, null, null); @@ -276,9 +238,9 @@ public static Client createClientByPrivateKey( * * @param configFilePath the config file path * @return the client - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - public static Client createClientByConfigFilePath(ConfigFilePath configFilePath) throws BitPayException { + public static Client createClientByConfigFilePath(ConfigFilePath configFilePath) throws BitPayGenericException { return new Client(configFilePath, null, null); } @@ -287,10 +249,11 @@ public static Client createClientByConfigFilePath(ConfigFilePath configFilePath) * Authorize this client with the server using the specified pairing code (Server Initiated Pairing). * * @param pairingCode A code obtained from the server; typically from bitpay.com/api-tokens. - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Request an API Token */ - public void authorizeClient(String pairingCode) throws BitPayException { + public void authorizeClient(String pairingCode) throws BitPayApiException, BitPayGenericException { this.createAuthorizationClient().authorizeClient(pairingCode); } @@ -299,10 +262,11 @@ public void authorizeClient(String pairingCode) throws BitPayException { * * @param facade Defines the level of API access being requested * @return A pairing code for claim at https://bitpay.com/dashboard/merchant/api-tokens. - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Request an API Token */ - public String authorizeClient(Facade facade) throws BitPayException { + public String authorizeClient(Facade facade) throws BitPayApiException, BitPayGenericException { return this.createAuthorizationClient().authorizeClient(facade); } @@ -311,9 +275,9 @@ public String authorizeClient(Facade facade) throws BitPayException { * * @param facade The identifier for the desired resource. * @return The token associated with resource. - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ - public String getAccessToken(Facade facade) throws BitPayException { + public String getAccessToken(Facade facade) throws BitPayGenericException { return this.tokenContainer.getAccessToken(facade); } @@ -322,9 +286,9 @@ public String getAccessToken(Facade facade) throws BitPayException { * * @param key The identifier for the desired resource. * @return The token associated with resource. - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ - public String getAccessToken(String key) throws BitPayException { + public String getAccessToken(String key) throws BitPayGenericException { return this.tokenContainer.getAccessToken(key); } @@ -333,9 +297,9 @@ public String getAccessToken(String key) throws BitPayException { * * @param currencyCode String Currency code for which the info will be retrieved. * @return Map|null - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - public Map getCurrencyInfo(String currencyCode) throws BitPayException { + public Map getCurrencyInfo(String currencyCode) throws BitPayGenericException { CurrencyClient client = CurrencyClient.getInstance(this.bitPayClient); return client.getInfo(currencyCode); } @@ -345,21 +309,16 @@ public Map getCurrencyInfo(String currencyCode) throws BitPayExc * * @param invoice An Invoice object with request parameters defined. * @return A BitPay generated Invoice object. - * @throws InvoiceCreationException InvoiceCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create an Invoice */ - public Invoice createInvoice(Invoice invoice) throws InvoiceCreationException { - try { - InvoiceClient client = getInvoiceClient(); - Facade facade = getFacadeBasedOnAccessToken(); - boolean signRequest = isSignRequest(facade); + public Invoice createInvoice(Invoice invoice) throws BitPayGenericException, BitPayApiException { + InvoiceClient client = getInvoiceClient(); + Facade facade = getFacadeBasedOnAccessToken(); + boolean signRequest = isSignRequest(facade); - return client.create(invoice, facade, signRequest); - } catch (BitPayException ex) { - throw new InvoiceCreationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceCreationException(null, e.getMessage()); - } + return client.create(invoice, facade, signRequest); } /** @@ -369,23 +328,18 @@ public Invoice createInvoice(Invoice invoice) throws InvoiceCreationException { * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay generated Invoice object. - * @throws InvoiceCreationException InvoiceCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create an Invoice */ public Invoice createInvoice( Invoice invoice, Facade facade, boolean signRequest - ) throws InvoiceCreationException { - try { - InvoiceClient client = getInvoiceClient(); + ) throws BitPayGenericException, BitPayApiException { + InvoiceClient client = getInvoiceClient(); - return client.create(invoice, facade, signRequest); - } catch (BitPayException ex) { - throw new InvoiceCreationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceCreationException(null, e.getMessage()); - } + return client.create(invoice, facade, signRequest); } /** @@ -393,20 +347,15 @@ public Invoice createInvoice( * * @param invoiceId The id of the invoice to retrieve. * @return A BitPay Invoice object. - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve an Invoice */ - public Invoice getInvoice(String invoiceId) throws BitPayException { + public Invoice getInvoice(String invoiceId) throws BitPayGenericException, BitPayApiException { Facade facade = getFacadeBasedOnAccessToken(); boolean signRequest = isSignRequest(facade); - try { - return this.getInvoiceClient().get(invoiceId, facade, signRequest); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceQueryException(null, e.getMessage()); - } + return this.getInvoiceClient().get(invoiceId, facade, signRequest); } /** @@ -416,21 +365,16 @@ public Invoice getInvoice(String invoiceId) throws BitPayException { * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay Invoice object. - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve an Invoice */ public Invoice getInvoice( String invoiceId, Facade facade, boolean signRequest - ) throws BitPayException { - try { - return this.getInvoiceClient().get(invoiceId, facade, signRequest); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceQueryException(null, e.getMessage()); - } + ) throws BitPayGenericException, BitPayApiException { + return this.getInvoiceClient().get(invoiceId, facade, signRequest); } /** @@ -439,10 +383,11 @@ public Invoice getInvoice( * * @param guid The guid of the invoice to retrieve. * @return A BitPay Invoice object. - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve an Invoice by GUID */ - public Invoice getInvoiceByGuid(String guid) throws InvoiceQueryException { + public Invoice getInvoiceByGuid(String guid) throws BitPayGenericException, BitPayApiException { Facade facade = getFacadeBasedOnAccessToken(); boolean signRequest = isSignRequest(facade); return this.getInvoiceClient().getByGuid(guid, facade, signRequest); @@ -456,14 +401,15 @@ public Invoice getInvoiceByGuid(String guid) throws InvoiceQueryException { * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay Invoice object. - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve an Invoice by GUID */ public Invoice getInvoiceByGuid( String guid, Facade facade, Boolean signRequest - ) throws InvoiceQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().getByGuid(guid, facade, signRequest); } @@ -477,8 +423,8 @@ public Invoice getInvoiceByGuid( * @param limit Maximum results that the query will return (useful for paging results). * @param offset Number of results to offset (ex. skip 10 will give you results starting with the 11th * @return A list of BitPay Invoice objects. - * @throws BitPayException BitPayException class - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Invoices Filtered by Query */ public List getInvoices( @@ -488,7 +434,7 @@ public List getInvoices( String orderId, Integer limit, Integer offset - ) throws BitPayException, InvoiceQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().getInvoices(dateStart, dateEnd, status, orderId, limit, offset); } @@ -497,11 +443,12 @@ public List getInvoices( * * @param invoiceId the id of the invoice for which you want to fetch an event token. * @return InvoiceEventToken event token - * @throws BitPayException BitPayException + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.8.0 * @see Retrieve an Event Token */ - public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayException { + public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().getInvoiceEventToken(invoiceId); } @@ -514,8 +461,8 @@ public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayExc * @param buyerEmail The buyer's email address. * @param autoVerify Skip the user verification on sandbox ONLY. * @return A BitPay generated Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceUpdateException InvoiceUpdateException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Update an Invoice */ public Invoice updateInvoice( @@ -524,7 +471,7 @@ public Invoice updateInvoice( String smsCode, String buyerEmail, Boolean autoVerify - ) throws BitPayException, InvoiceUpdateException { + ) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().update(invoiceId, buyerSms, smsCode, buyerEmail, autoVerify); } @@ -534,13 +481,13 @@ public Invoice updateInvoice( * @param invoiceId The id of the invoice to updated. * @param status The status of the invoice to be updated, can be "confirmed" or "complete". * @return A BitPay generated Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceUpdateException InvoiceUpdateException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ public Invoice payInvoice( String invoiceId, String status - ) throws BitPayException, InvoiceUpdateException { + ) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().pay(invoiceId, status); } @@ -549,11 +496,11 @@ public Invoice payInvoice( * * @param invoiceId The Id of the BitPay invoice to be canceled. * @return A BitPay generated Invoice object. - * @throws InvoiceCancellationException InvoiceCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Cancel an Invoice */ - public Invoice cancelInvoice(String invoiceId) throws InvoiceCancellationException, BitPayException { + public Invoice cancelInvoice(String invoiceId) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().cancel(invoiceId); } @@ -564,14 +511,14 @@ public Invoice cancelInvoice(String invoiceId) throws InvoiceCancellationExcepti * @param invoiceId The Id of the BitPay invoice to be canceled. * @param forceCancel If 'true' it will cancel the invoice even if no contact information is present. * @return A BitPay generated Invoice object. - * @throws InvoiceCancellationException InvoiceCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Cancel an Invoice */ public Invoice cancelInvoice( String invoiceId, Boolean forceCancel - ) throws InvoiceCancellationException, BitPayException { + ) throws BitPayGenericException, BitPayApiException { return this.getInvoiceClient().cancel(invoiceId, forceCancel); } @@ -583,10 +530,11 @@ public Invoice cancelInvoice( * correlate the invoice with an order ID in their system, which can be used as a lookup variable * in Retrieve Invoice by GUID. * @return Invoice Invoice - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Cancel an Invoice by GUID */ - public Invoice cancelInvoiceByGuid(String guid) throws BitPayException { + public Invoice cancelInvoiceByGuid(String guid) throws BitPayApiException, BitPayGenericException { return this.getInvoiceClient().cancelByGuid(guid, false); } @@ -601,13 +549,14 @@ public Invoice cancelInvoiceByGuid(String guid) throws BitPayException { * Note: Canceling a paid invoice without contact information requires a manual support * process and is not recommended. * @return Invoice Invoice - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Cancel an Invoice by GUID */ public Invoice cancelInvoiceByGuid( String guid, Boolean forceCancel - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { return this.getInvoiceClient().cancelByGuid(guid, forceCancel); } @@ -617,10 +566,11 @@ public Invoice cancelInvoiceByGuid( * * @param invoiceId The id of the invoice for which you want the last webhook to be resent. * @return Boolean status of request - * @throws BitPayException BitPayException + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Request an Invoice Webhook to be Resent */ - public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayException { + public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayApiException, BitPayGenericException { return this.getInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId); } @@ -636,8 +586,8 @@ public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayEx * @param buyerPaysRefundFee Whether the buyer should pay the refund fee (default is merchant) * @param reference Present only if specified. Used as reference label for the refund. Max str length = 100 * @return An updated Refund Object - * @throws RefundCreationException RefundCreationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create a Refund Request */ public Refund createRefund( @@ -647,7 +597,7 @@ public Refund createRefund( Boolean immediate, Boolean buyerPaysRefundFee, String reference - ) throws RefundCreationException, BitPayException { + ) throws BitPayGenericException, BitPayApiException { Refund refund = new Refund(); refund.setInvoice(invoiceId); refund.setAmount(amount); @@ -673,8 +623,8 @@ public Refund createRefund( * @param guid Variable provided by the merchant and designed to be used by the merchant * to correlate the refund with a refund ID in their system * @return An updated Refund Object - * @throws RefundCreationException RefundCreationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 * @see Create a Refund Request */ @@ -686,7 +636,7 @@ public Refund createRefund( Boolean buyerPaysRefundFee, String reference, String guid - ) throws RefundCreationException, BitPayException { + ) throws BitPayGenericException, BitPayApiException { Refund refund = new Refund(); refund.setInvoice(invoiceId); refund.setAmount(amount); @@ -705,12 +655,12 @@ public Refund createRefund( * @param refund Refund class which provided data - invoice id, amount, preview, immediate, buyerPaysRefundFee, * reference and guid for create request * @return An updated Refund Object - * @throws RefundCreationException RefundCreationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 * @see Create a Refund Request */ - public Refund createRefund(Refund refund) throws RefundCreationException, BitPayException { + public Refund createRefund(Refund refund) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().create(refund); } @@ -719,11 +669,11 @@ public Refund createRefund(Refund refund) throws RefundCreationException, BitPay * * @param refundId The BitPay refund ID. * @return A BitPay Refund object with the associated Refund object. - * @throws RefundQueryException RefundQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve a Refund Request */ - public Refund getRefund(String refundId) throws RefundQueryException, BitPayException { + public Refund getRefund(String refundId) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().getById(refundId); } @@ -732,11 +682,12 @@ public Refund getRefund(String refundId) throws RefundQueryException, BitPayExce * * @param guid The BitPay refund GUID. * @return A BitPay Refund object with the associated Refund object. - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @since 8.7.0 * @see Retrieve a Refund by GUID Request */ - public Refund getRefundByGuid(String guid) throws BitPayException { + public Refund getRefundByGuid(String guid) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().getByGuid(guid); } @@ -745,11 +696,11 @@ public Refund getRefundByGuid(String guid) throws BitPayException { * * @param invoiceId The BitPay invoice object having the associated refunds. * @return A list of BitPay Refund objects with the associated Refund objects. - * @throws RefundQueryException RefundQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Refunds of an Invoice */ - public List getRefunds(String invoiceId) throws RefundQueryException, BitPayException { + public List getRefunds(String invoiceId) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().getRefundsByInvoiceId(invoiceId); } @@ -759,14 +710,11 @@ public List getRefunds(String invoiceId) throws RefundQueryException, Bi * @param refundId A BitPay refund ID. * @param status The new status for the refund to be updated. * @return A BitPay generated Refund object. - * @throws RefundUpdateException RefundUpdateException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Update a Refund Request */ - public Refund updateRefund( - String refundId, - String status - ) throws RefundUpdateException, BitPayException { + public Refund updateRefund(String refundId, String status) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().update(refundId, status); } @@ -776,15 +724,12 @@ public Refund updateRefund( * @param guid A BitPay refund Guid. * @param status The new status for the refund to be updated. * @return A BitPay generated Refund object. - * @throws RefundUpdateException RefundUpdateException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 * @see Update a Refund by GUID Request */ - public Refund updateRefundByGuid( - String guid, - String status - ) throws RefundUpdateException, BitPayException { + public Refund updateRefundByGuid(String guid, String status) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().updateByGuid(guid, status); } @@ -793,11 +738,11 @@ public Refund updateRefundByGuid( * * @param refundId A BitPay refund ID. * @return An updated Refund Object - * @throws RefundCreationException RefundCreationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Request a Refund Notification to be Resent */ - public Boolean sendRefundNotification(String refundId) throws RefundCreationException, BitPayException { + public Boolean sendRefundNotification(String refundId) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().sendRefundNotification(refundId); } @@ -806,11 +751,11 @@ public Boolean sendRefundNotification(String refundId) throws RefundCreationExce * * @param refundId The refund Id for the refund to be canceled. * @return An updated Refund Object. - * @throws RefundCancellationException RefundCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Cancel a Refund Request */ - public Refund cancelRefund(String refundId) throws RefundCancellationException, BitPayException { + public Refund cancelRefund(String refundId) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().cancel(refundId); } @@ -819,12 +764,12 @@ public Refund cancelRefund(String refundId) throws RefundCancellationException, * * @param guid The refund Guid for the refund to be canceled. * @return An updated Refund Object. - * @throws RefundCancellationException RefundCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 * @see Cancel a Refund by GUID Request */ - public Refund cancelRefundByGuid(String guid) throws RefundCancellationException, BitPayException { + public Refund cancelRefundByGuid(String guid) throws BitPayGenericException, BitPayApiException { return this.getRefundClient().cancelByGuid(guid); } @@ -833,11 +778,11 @@ public Refund cancelRefundByGuid(String guid) throws RefundCancellationException * * @param bill An Bill object with request parameters defined. * @return A BitPay generated Bill object. - * @throws BillCreationException BillCreationException class - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create a Bill */ - public Bill createBill(Bill bill) throws BillCreationException, BitPayException { + public Bill createBill(Bill bill) throws BitPayGenericException, BitPayApiException { Facade facade = this.getFacadeBasedOnAccessToken(); boolean signRequest = isSignRequest(facade); @@ -851,15 +796,15 @@ public Bill createBill(Bill bill) throws BillCreationException, BitPayException * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay generated Bill object. - * @throws BitPayException BitPayException class - * @throws BillCreationException BillCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create a Bill */ public Bill createBill( Bill bill, Facade facade, boolean signRequest - ) throws BitPayException, BillCreationException { + ) throws BitPayGenericException, BitPayApiException { return this.getBillClient().create(bill, facade, signRequest); } @@ -868,10 +813,11 @@ public Bill createBill( * * @param billId The id of the bill to retrieve. * @return A BitPay Bill object. - * @throws BitPayException the bit pay exception + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Retrieve a Bill */ - public Bill getBill(String billId) throws BitPayException { + public Bill getBill(String billId) throws BitPayGenericException, BitPayApiException { Facade facade = this.getFacadeBasedOnAccessToken(); boolean signRequest = isSignRequest(facade); @@ -885,15 +831,15 @@ public Bill getBill(String billId) throws BitPayException { * @param facade The facade used to retrieve it. * @param signRequest Signed request. * @return A BitPay Bill object. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve a Bill */ public Bill getBill( String billId, Facade facade, boolean signRequest - ) throws BitPayException, BillQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getBillClient().get(billId, facade, signRequest); } @@ -902,11 +848,11 @@ public Bill getBill( * * @param status The status to filter the bills. * @return A list of BitPay Bill objects. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Bills by Status */ - public List getBills(String status) throws BitPayException, BillQueryException { + public List getBills(String status) throws BitPayGenericException, BitPayApiException { return this.getBillClient().getBills(status); } @@ -914,11 +860,11 @@ public List getBills(String status) throws BitPayException, BillQueryExcep * Retrieve a collection of BitPay bills. * * @return A list of BitPay Bill objects. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayApiException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Bills by Status */ - public List getBills() throws BitPayException, BillQueryException { + public List getBills() throws BitPayGenericException, BitPayApiException { return this.getBillClient().getBills(); } @@ -928,14 +874,11 @@ public List getBills() throws BitPayException, BillQueryException { * @param bill A Bill object with the parameters to update defined. * @param billId The Id of the Bill to udpate. * @return An updated Bill object. - * @throws BitPayException BitPayException class - * @throws BillUpdateException BillUpdateException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Update a Bill */ - public Bill updateBill( - Bill bill, - String billId - ) throws BitPayException, BillUpdateException { + public Bill updateBill(Bill bill, String billId) throws BitPayGenericException, BitPayApiException { return this.getBillClient().update(bill, billId); } @@ -945,13 +888,14 @@ public Bill updateBill( * @param billId The id of the requested bill. * @param billToken The token of the requested bill. * @return A response status returned from the API. - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Deliver a Bill Via Email */ public String deliverBill( String billId, String billToken - ) throws BitPayException { + ) throws BitPayGenericException, BitPayApiException { Facade facade = this.getFacadeBasedOnAccessToken(); boolean signRequest = isSignRequest(facade); @@ -965,14 +909,15 @@ public String deliverBill( * @param billToken The token of the requested bill. * @param signRequest Allow unsigned request * @return A response status returned from the API. - * @throws BillDeliveryException BillDeliveryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Deliver a Bill Via Email */ public String deliverBill( String billId, String billToken, boolean signRequest - ) throws BillDeliveryException { + ) throws BitPayGenericException, BitPayApiException { return this.getBillClient().deliver(billId, billToken, signRequest); } @@ -983,14 +928,15 @@ public String deliverBill( * Current supported values are BTC and BCH. * @param currency the fiat currency for which you want to fetch the baseCurrency rates * @return A Rate object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.8.0 * @see Retrieve the rates for a cryptocurrency / fiat pair */ public Rate getRate( String baseCurrency, String currency - ) throws RateQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getRateClient().get(baseCurrency, currency); } @@ -998,9 +944,10 @@ public Rate getRate( * Retrieve the exchange rate table maintained by BitPay. See https://bitpay.com/bitcoin-exchange-rates. * * @return A Rates object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Rates getRates() throws RateQueryException { + public Rates getRates() throws BitPayGenericException, BitPayApiException { return this.getRateClient().getRates(); } @@ -1010,11 +957,12 @@ public Rates getRates() throws RateQueryException { * @param baseCurrency the cryptocurrency for which you want to fetch the rates. * Current supported values are BTC and BCH. * @return A Rates object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.8.0 * @see Retrieve all the rates for a given cryptocurrency */ - public Rates getRates(String baseCurrency) throws RateQueryException { + public Rates getRates(String baseCurrency) throws BitPayGenericException, BitPayApiException { return this.getRateClient().getRates(baseCurrency); } @@ -1025,15 +973,15 @@ public Rates getRates(String baseCurrency) throws RateQueryException { * @param dateStart The first date for the query filter. * @param dateEnd The last date for the query filter. * @return Ledger entries list. - * @throws BitPayException BitPayException class - * @throws LedgerQueryException LedgerQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Ledger Entries */ public List getLedgerEntries( String currency, String dateStart, String dateEnd - ) throws BitPayException, LedgerQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getLedgerClient().getEntries(currency, dateStart, dateEnd); } @@ -1041,11 +989,11 @@ public List getLedgerEntries( * Retrieve a list of ledgers using the merchant facade. * * @return A list of Ledger objects populated with the currency and current balance of each one. - * @throws BitPayException BitPayException class - * @throws LedgerQueryException LedgerQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Account Balances */ - public List getLedgers() throws BitPayException, LedgerQueryException { + public List getLedgers() throws BitPayGenericException, BitPayApiException { return this.getLedgerClient().getLedgers(); } @@ -1054,12 +1002,12 @@ public List getLedgers() throws BitPayException, LedgerQueryException { * * @param recipients PayoutRecipients A PayoutRecipients object with request parameters defined. * @return array A list of BitPay PayoutRecipients objects. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientCreationException PayoutRecipientCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Invite Recipients */ - public List submitPayoutRecipients(PayoutRecipients recipients) throws BitPayException, - PayoutRecipientCreationException { + public List submitPayoutRecipients(PayoutRecipients recipients) + throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().submit(recipients); } @@ -1071,15 +1019,15 @@ public List submitPayoutRecipients(PayoutRecipients recipients) * paging results). result). * @param offset int Offset for paging. * @return array A list of BitPayRecipient objects. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientQueryException PayoutRecipientQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Recipients by Status */ public List getPayoutRecipients( String status, Integer limit, Integer offset - ) throws BitPayException, PayoutRecipientQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().getRecipientsByFilters(status, limit, offset); } @@ -1089,12 +1037,11 @@ public List getPayoutRecipients( * * @param recipientId String The id of the recipient to retrieve. * @return PayoutRecipient A BitPay PayoutRecipient object. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientQueryException PayoutRecipientQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve a Recipient */ - public PayoutRecipient getPayoutRecipient(String recipientId) - throws BitPayException, PayoutRecipientQueryException { + public PayoutRecipient getPayoutRecipient(String recipientId) throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().get(recipientId); } @@ -1105,14 +1052,14 @@ public PayoutRecipient getPayoutRecipient(String recipientId) * @param recipient PayoutRecipients A PayoutRecipient object with updated * parameters defined. * @return The updated recipient object. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientUpdateException PayoutRecipientUpdateException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Update a Recipient */ public PayoutRecipient updatePayoutRecipient( String recipientId, PayoutRecipient recipient - ) throws BitPayException, PayoutRecipientUpdateException { + ) throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().update(recipientId, recipient); } @@ -1121,12 +1068,11 @@ public PayoutRecipient updatePayoutRecipient( * * @param recipientId String The id of the recipient to cancel. * @return True if the delete operation was successful, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientCancellationException PayoutRecipientCancellationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Remove a Recipient */ - public Boolean deletePayoutRecipient(String recipientId) - throws BitPayException, PayoutRecipientCancellationException { + public Boolean deletePayoutRecipient(String recipientId) throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().delete(recipientId); } @@ -1135,12 +1081,12 @@ public Boolean deletePayoutRecipient(String recipientId) * * @param recipientId String A BitPay recipient ID. * @return True if the notification was successfully sent, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientNotificationException PayoutRecipientNotificationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Request a Recipient Webhook to be Resent */ public Boolean requestPayoutRecipientNotification(String recipientId) - throws PayoutRecipientNotificationException, BitPayException { + throws BitPayGenericException, BitPayApiException { return this.getPayoutRecipientsClient().requestNotification(recipientId); } @@ -1149,11 +1095,11 @@ public Boolean requestPayoutRecipientNotification(String recipientId) * * @param payout Payout A Payout object with request parameters defined. * @return A BitPay generated Payout object. - * @throws BitPayException BitPayException class - * @throws PayoutCreationException PayoutCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create a Payout */ - public Payout submitPayout(Payout payout) throws BitPayException, PayoutCreationException { + public Payout submitPayout(Payout payout) throws BitPayGenericException, BitPayApiException { return this.getPayoutClient().submit(payout); } @@ -1162,11 +1108,11 @@ public Payout submitPayout(Payout payout) throws BitPayException, PayoutCreation * * @param payouts Collection of Payout objects with request parameters defined. * @return A BitPay PayoutGroup with generated Payout objects and information's about not created payouts. - * @throws BitPayException BitPayException class - * @throws PayoutCreationException PayoutCreationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Create a Payouts */ - public PayoutGroup submitPayouts(Collection payouts) throws BitPayException, PayoutCreationException { + public PayoutGroup submitPayouts(Collection payouts) throws BitPayGenericException, BitPayApiException { return this.getPayoutGroupClient().submit(payouts); } @@ -1176,11 +1122,11 @@ public PayoutGroup submitPayouts(Collection payouts) throws BitPayExcept * * @param payoutId String The id of the payout to retrieve. * @return A BitPay Payout object. - * @throws BitPayException BitPayException class - * @throws PayoutQueryException PayoutQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve a Payout */ - public Payout getPayout(String payoutId) throws BitPayException, PayoutQueryException { + public Payout getPayout(String payoutId) throws BitPayGenericException, BitPayApiException { return this.getPayoutClient().get(payoutId); } @@ -1189,11 +1135,11 @@ public Payout getPayout(String payoutId) throws BitPayException, PayoutQueryExce * * @param payoutId String The id of the payout to cancel. * @return True if the refund was successfully canceled, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutCancellationException PayoutCancellationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Cancel a Payout */ - public Boolean cancelPayout(String payoutId) throws BitPayException, PayoutCancellationException { + public Boolean cancelPayout(String payoutId) throws BitPayGenericException, BitPayApiException { return this.getPayoutClient().cancel(payoutId); } @@ -1202,11 +1148,11 @@ public Boolean cancelPayout(String payoutId) throws BitPayException, PayoutCance * * @param groupId String The id of the payout group to cancel. * @return A BitPay PayoutGroup with cancelled Payout objects and information's about not cancelled payouts. - * @throws BitPayException BitPayException class - * @throws PayoutCancellationException PayoutCancellationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Cancel a Payouts */ - public PayoutGroup cancelPayouts(String groupId) throws BitPayException, PayoutCancellationException { + public PayoutGroup cancelPayouts(String groupId) throws BitPayGenericException, BitPayApiException { return this.getPayoutGroupClient().cancel(groupId); } @@ -1222,8 +1168,8 @@ public PayoutGroup cancelPayouts(String groupId) throws BitPayException, PayoutC * @param offset int Offset for paging. * @param groupId String The optional group id assigned to payout. * @return A list of BitPay Payout objects. - * @throws BitPayException BitPayException class - * @throws PayoutQueryException PayoutQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Payouts Filtered by Query */ public List getPayouts( @@ -1234,7 +1180,7 @@ public List getPayouts( Integer limit, Integer offset, String groupId - ) throws BitPayException, PayoutQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getPayoutClient().getPayouts(startDate, endDate, status, reference, limit, offset, groupId); } @@ -1243,12 +1189,11 @@ public List getPayouts( * * @param payoutId String The id of the payout to notify. * @return True if the notification was successfully sent, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutNotificationException PayoutNotificationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Request a Payout Webhook to be Resent */ - public Boolean requestPayoutNotification(String payoutId) - throws BitPayException, PayoutNotificationException { + public Boolean requestPayoutNotification(String payoutId) throws BitPayGenericException, BitPayApiException { return this.getPayoutClient().requestNotification(payoutId); } @@ -1264,8 +1209,8 @@ public Boolean requestPayoutNotification(String payoutId) * @param limit Maximum number of settlements to retrieve. * @param offset Offset for paging. * @return A list of BitPay Settlement objects. - * @throws BitPayException BitPayException class - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve Settlements */ public List getSettlements( @@ -1275,7 +1220,7 @@ public List getSettlements( String status, Integer limit, Integer offset - ) throws BitPayException, SettlementQueryException { + ) throws BitPayGenericException, BitPayApiException { return this.getSettlementClient().getSettlements(currency, dateStart, dateEnd, status, limit, offset); } @@ -1284,11 +1229,11 @@ public List getSettlements( * * @param settlementId Settlement Id. * @return A BitPay Settlement object. - * @throws BitPayException BitPayException class - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Retrieve a Settlement */ - public Settlement getSettlement(String settlementId) throws BitPayException, SettlementQueryException { + public Settlement getSettlement(String settlementId) throws BitPayGenericException, BitPayApiException { return this.getSettlementClient().get(settlementId); } @@ -1299,14 +1244,12 @@ public Settlement getSettlement(String settlementId) throws BitPayException, Set * @param settlementId Settlement ID. * @param token Settlement token. * @return A detailed BitPay Settlement object. - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Fetch a Reconciliation Report */ - public Settlement getSettlementReconciliationReport( - String settlementId, - String token - ) - throws SettlementQueryException { + public Settlement getSettlementReconciliationReport(String settlementId, String token) + throws BitPayGenericException, BitPayApiException { return this.getSettlementClient().getSettlementReconciliationReport(settlementId, token); } @@ -1314,11 +1257,11 @@ public Settlement getSettlementReconciliationReport( * Retrieve all supported wallets. * * @return A list of wallet objets. - * @throws WalletQueryException WalletQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class * @see Retrieve the Supported Wallets */ - public List getSupportedWallets() throws WalletQueryException, BitPayException { + public List getSupportedWallets() throws BitPayGenericException, BitPayApiException { return this.getWalletClient().getSupportedWallets(); } @@ -1335,15 +1278,6 @@ public RateClient getRateClient() { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /** - * Sets the logger level of reporting. - * - * @param loggerLevel int BitPayLogger constant (OFF, INFO, WARN, ERR, DEBUG) - */ - public void setLoggerLevel(int loggerLevel) { - this.bitPayClient.setLoggerLevel(loggerLevel); - } - /** * Gets http client. * @@ -1372,26 +1306,25 @@ protected HttpClient getHttpClient( * * @param privateKey the private key * @return ECKey - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - protected ECKey getEcKey(PrivateKey privateKey) throws BitPayException { + protected ECKey getEcKey(PrivateKey privateKey) throws BitPayGenericException { File privateKeyFile = new File(privateKey.value()); if (privateKeyFile.exists() && KeyUtils.privateKeyExists(privateKey.value().replace("\"", ""))) { try { return KeyUtils.loadEcKey(); } catch (Exception e) { - throw new BitPayException( - null, - LOAD_PRIVATE_KEY_EXCEPTION + e.getMessage() - ); + BitPayExceptionProvider.throwGenericExceptionWithMessage(LOAD_PRIVATE_KEY_EXCEPTION + e.getMessage()); } } else { try { return KeyUtils.createEcKeyFromHexString(privateKey.value()); } catch (Exception e) { - throw new BitPayException(null, "Private Key file not found"); + BitPayExceptionProvider.throwGenericExceptionWithMessage("Private Key file not found"); } } + + return null; } /** @@ -1399,9 +1332,9 @@ protected ECKey getEcKey(PrivateKey privateKey) throws BitPayException { * * @param config the config * @return ECKey - * @throws Exception the exception + * @throws BitPayGenericException BitPayGenericException class */ - protected ECKey getEcKey(Config config) throws Exception { + protected ECKey getEcKey(Config config) throws BitPayGenericException { try { if (KeyUtils.privateKeyExists( config.getEnvConfig(config.getEnvironment()).path("PrivateKeyPath").toString() @@ -1415,13 +1348,11 @@ protected ECKey getEcKey(Config config) throws Exception { return KeyUtils.createEcKeyFromHexString(keyHex); } } - return null; } catch (Exception e) { - throw new BitPayException( - null, - LOAD_PRIVATE_KEY_EXCEPTION + e.getMessage() - ); + BitPayExceptionProvider.throwGenericExceptionWithMessage(LOAD_PRIVATE_KEY_EXCEPTION + e.getMessage()); } + + return null; } /** @@ -1429,15 +1360,14 @@ protected ECKey getEcKey(Config config) throws Exception { * * @param ecKey ECKey * @throws IllegalArgumentException the illegal argument exception - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - protected void deriveIdentity(ECKey ecKey) throws IllegalArgumentException, BitPayException { + protected void deriveIdentity(ECKey ecKey) throws BitPayGenericException { // Identity in this implementation is defined to be the SIN. try { this.identity = KeyUtils.deriveSin(ecKey); } catch (Exception e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Token array) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Token array", e.getMessage()); } } @@ -1479,21 +1409,23 @@ protected String getBaseUrl(Environment environment) { * * @param configFilePath the config file path * @return the Config class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ - protected Config buildConfigFromFile(ConfigFilePath configFilePath) throws BitPayException { + protected Config buildConfigFromFile(ConfigFilePath configFilePath) throws BitPayGenericException { + Config config = null; try { byte[] jsonData = Files.readAllBytes(Paths.get(configFilePath.value())); JsonMapper mapper = JsonMapperFactory.create(); //read JSON like DOM Parser JsonNode rootNode = mapper.readTree(jsonData); JsonNode bitPayConfiguration = rootNode.path("BitPayConfiguration"); - return mapper.readValue(bitPayConfiguration.toString(), Config.class); - } catch (JsonProcessingException e) { - throw new BitPayException(null, "failed to read configuration file : " + e.getMessage()); + config = mapper.readValue(bitPayConfiguration.toString(), Config.class); } catch (Exception e) { - throw new BitPayException(null, "failed to read configuration file : " + e.getMessage()); + BitPayExceptionProvider.throwGenericExceptionWithMessage( + "Failed to read configuration file : " + e.getMessage()); } + + return config; } /** diff --git a/src/main/java/com/bitpay/sdk/Config.java b/src/main/java/com/bitpay/sdk/Config.java index 9b770358..86fcb6fd 100644 --- a/src/main/java/com/bitpay/sdk/Config.java +++ b/src/main/java/com/bitpay/sdk/Config.java @@ -34,7 +34,7 @@ public class Config { /** * BitPay Plugin Info Version. */ - public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v9.0.2"; + public static final String BITPAY_PLUGIN_INFO = "BitPay_Java_Client_v10.0.0"; /** * BitPay Api Frame. */ diff --git a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java index d5766e38..542576ff 100644 --- a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java +++ b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java @@ -5,7 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.Token; import com.bitpay.sdk.util.GuidGenerator; @@ -16,7 +18,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.http.HttpResponse; /** * The type Authorization client. @@ -52,9 +53,10 @@ public AuthorizationClient( * Authorize (pair) this client with the server using the specified pairing code. * * @param pairingCode A code obtained from the server; typically from bitpay.com/api-tokens. - * @throws BitPayException BitPayException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public void authorizeClient(String pairingCode) throws BitPayException { + public void authorizeClient(String pairingCode) throws BitPayApiException, BitPayGenericException { Token token = new Token(); token.setId(this.identity); token.setGuid(this.guidGenerator.execute()); @@ -62,26 +64,23 @@ public void authorizeClient(String pairingCode) throws BitPayException { JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(token); } catch (JsonProcessingException e) { - throw new BitPayException(null, "failed to serialize Token object : " + e.getMessage()); + BitPayExceptionProvider.throwGenericExceptionWithMessage( + "Failed to serialize Token object : " + e.getMessage()); } - HttpResponse response = this.bitPayClient.post("tokens", json); + String jsonResponse = this.bitPayClient.post("tokens", json); - List tokens; + List tokens = null; try { - tokens = Arrays.asList(mapper.readValue(this.bitPayClient.responseToJsonString(response), Token[].class)); + tokens = Arrays.asList(mapper.readValue(jsonResponse, Token[].class)); } catch (JsonProcessingException e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Tokens) : " + e.getMessage()); - } catch (Exception e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Tokens) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Tokens", e.getMessage()); } for (Token t : tokens) { @@ -94,12 +93,14 @@ public void authorizeClient(String pairingCode) throws BitPayException { * * @param facade Defines the level of API access being requested * @return A pairing code for claim at https://bitpay.com/dashboard/merchant/api-tokens. - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public String authorizeClient(Facade facade) throws BitPayException { + public String authorizeClient(Facade facade) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(facade)) { - throw new BitPayException(null, "missing required parameter"); + BitPayExceptionProvider.throwValidationException("Missing required parameter"); } + Token token = new Token(); token.setId(this.identity); token.setGuid(this.guidGenerator.execute()); @@ -108,32 +109,31 @@ public String authorizeClient(Facade facade) throws BitPayException { JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(token); } catch (JsonProcessingException e) { - throw new BitPayException(null, "failed to serialize Token object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Token", e.getMessage()); } - HttpResponse response = this.bitPayClient.post("tokens", json); + String response = this.bitPayClient.post("tokens", json); - List tokens; + List tokens = null; try { - tokens = Arrays.asList(mapper.readValue(this.bitPayClient.responseToJsonString(response), Token[].class)); + tokens = Arrays.asList(mapper.readValue(response, Token[].class)); // Expecting a single token resource. if (tokens.size() != 1) { - throw new BitPayException(null, "failed to get token resource; expected 1 token, got " + tokens.size()); + BitPayExceptionProvider.throwDeserializeResourceException( + "Token", + "expected 1 token, got " + tokens.size() + ); } } catch (JsonProcessingException e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Tokens) : " + e.getMessage()); - } catch (Exception e) { - throw new BitPayException(null, - "failed to deserialize BitPay server response (Tokens) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Tokens", e.getMessage()); } this.accessToken.put(tokens.get(0).getFacade(), tokens.get(0).getValue()); diff --git a/src/main/java/com/bitpay/sdk/client/BillClient.java b/src/main/java/com/bitpay/sdk/client/BillClient.java index 168cd0b8..a0775559 100644 --- a/src/main/java/com/bitpay/sdk/client/BillClient.java +++ b/src/main/java/com/bitpay/sdk/client/BillClient.java @@ -5,11 +5,10 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BillCreationException; -import com.bitpay.sdk.exceptions.BillDeliveryException; -import com.bitpay.sdk.exceptions.BillQueryException; -import com.bitpay.sdk.exceptions.BillUpdateException; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.exceptions.BitPayValidationException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.bill.Bill; import com.bitpay.sdk.util.JsonMapperFactory; @@ -23,7 +22,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -31,7 +29,6 @@ */ public class BillClient implements ResourceClient { - private static final String EXCEPTION_MESSAGE = "failed to deserialize BitPay server response (%s) : "; private static BillClient instance; private final BitPayClient bitPayClient; private final TokenContainer accessTokens; @@ -75,36 +72,35 @@ public static BillClient getInstance( * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay generated Bill object. - * @throws BitPayException BitPayException class - * @throws BillCreationException BillCreationException class + * @throws BitPayValidationException BitPayValidationException + * @throws BitPayGenericException BitPayGenericException + * @throws BitPayApiException BitPayApiException */ public Bill create( Bill bill, Facade facade, boolean signRequest - ) throws BitPayException, BillCreationException { + ) throws BitPayValidationException, BitPayGenericException, BitPayApiException { if (Objects.isNull(bill) || Objects.isNull(facade)) { - throw new BillCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } String token = this.accessTokens.getAccessToken(facade); bill.setToken(token); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(bill); } catch (JsonProcessingException e) { - throw new BillCreationException(null, "failed to serialize Bill object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage()); } + String jsonResponse = this.bitPayClient.post("bills", json, signRequest); + try { - HttpResponse response = this.bitPayClient.post("bills", json, signRequest); - bill = mapper.readerForUpdating(bill).readValue(this.bitPayClient.responseToJsonString(response)); + bill = mapper.readerForUpdating(bill).readValue(jsonResponse); } catch (Exception e) { - throw new BillCreationException( - null, - String.format(EXCEPTION_MESSAGE, "Bill") + e.getMessage() - ); + BitPayExceptionProvider.throwDeserializeResourceException("Bill", e.getMessage()); } return bill; @@ -117,36 +113,29 @@ public Bill create( * @param facade The facade used to retrieve it. * @param signRequest Signed request. * @return A BitPay Bill object. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayValidationException BitPayValidationException class + * @throws BitPayApiException BitPayApiException class */ public Bill get( String billId, Facade facade, boolean signRequest - ) throws BitPayException, BillQueryException { + ) throws BitPayGenericException, BitPayApiException { if (Objects.isNull(billId) || Objects.isNull(facade)) { - throw new BillQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } String token = this.accessTokens.getAccessToken(facade); final List params = new ArrayList(); ParameterAdder.execute(params, "token", token); - Bill bill; + Bill bill = null; + String jsonResponse = this.bitPayClient.get("bills/" + billId, params, signRequest); try { - HttpResponse response = this.bitPayClient.get("bills/" + billId, params, signRequest); - bill = JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Bill.class); + bill = JsonMapperFactory.create().readValue(jsonResponse, Bill.class); } catch (JsonProcessingException e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bill") + e.getMessage() - ); - } catch (Exception e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bill") + e.getMessage() - ); + BitPayExceptionProvider.throwDeserializeResourceException("Bill", e.getMessage()); } return bill; @@ -157,30 +146,22 @@ public Bill get( * * @param status The status to filter the bills. * @return A list of BitPay Bill objects. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayGenericException + * @throws BitPayApiException BitPayApiException */ - public List getBills(String status) throws BitPayException, BillQueryException { + public List getBills(String status) throws BitPayGenericException, BitPayApiException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); ParameterAdder.execute(params, "status", status); - List bills; + List bills = null; try { - HttpResponse response = this.bitPayClient.get("bills", params); + String jsonResponse = this.bitPayClient.get("bills", params); bills = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Bill[].class)); + JsonMapperFactory.create().readValue(jsonResponse, Bill[].class)); } catch (JsonProcessingException e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bills") + e.getMessage() - ); - } catch (Exception e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bills") + e.getMessage() - ); + BitPayExceptionProvider.throwDeserializeResourceException("Bill", e.getMessage()); } return bills; @@ -190,29 +171,21 @@ public List getBills(String status) throws BitPayException, BillQueryExcep * Retrieve a collection of BitPay bills. * * @return A list of BitPay Bill objects. - * @throws BitPayException BitPayException class - * @throws BillQueryException BillQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public List getBills() throws BitPayException, BillQueryException { + public List getBills() throws BitPayGenericException, BitPayApiException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - List bills; + List bills = null; + String jsonResponse = this.bitPayClient.get("bills", params); try { - HttpResponse response = this.bitPayClient.get("bills", params); bills = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Bill[].class)); + JsonMapperFactory.create().readValue(jsonResponse, Bill[].class)); } catch (JsonProcessingException e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bills") + e.getMessage() - ); - } catch (Exception e) { - throw new BillQueryException( - null, - String.format(EXCEPTION_MESSAGE, "Bills") + e.getMessage() - ); + BitPayExceptionProvider.throwDeserializeResourceException("Bills", e.getMessage()); } return bills; @@ -224,32 +197,33 @@ public List getBills() throws BitPayException, BillQueryException { * @param bill A Bill object with the parameters to update defined. * @param billId The Id of the Bill to udpate. * @return An updated Bill object. - * @throws BitPayException BitPayException class - * @throws BillUpdateException BillUpdateException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayValidationException BitPayValidationException class + * @throws BitPayApiException BitPayApiException class */ public Bill update( Bill bill, String billId - ) throws BitPayException, BillUpdateException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(billId) || Objects.isNull(bill)) { - throw new BillUpdateException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } + JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; + try { json = mapper.writeValueAsString(bill); } catch (JsonProcessingException e) { - throw new BillUpdateException(null, "failed to serialize Bill object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage()); } + String jsonResponse = this.bitPayClient.update("bills/" + billId, json); + try { - HttpResponse response = this.bitPayClient.update("bills/" + billId, json); - bill = mapper.readerForUpdating(bill).readValue(this.bitPayClient.responseToJsonString(response)); - } catch (Exception e) { - throw new BillUpdateException( - null, - String.format(EXCEPTION_MESSAGE, "Bill") + e.getMessage() - ); + bill = mapper.readerForUpdating(bill).readValue(jsonResponse); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Bills", e.getMessage()); } return bill; @@ -262,38 +236,31 @@ public Bill update( * @param billToken The token of the requested bill. * @param signRequest Allow unsigned request * @return A response status returned from the API. - * @throws BillDeliveryException BillDeliveryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayValidationException BitPayValidationException class + * @throws BitPayGenericException BitPayGenericException class */ public String deliver( String billId, String billToken, boolean signRequest - ) throws BillDeliveryException { + ) throws BitPayApiException, BitPayValidationException, BitPayGenericException { if (Objects.isNull(billId) || Objects.isNull(billToken)) { - throw new BillDeliveryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } Map map = new HashMap<>(); map.put("token", billToken); JsonMapper mapper = JsonMapperFactory.create(); - String json; - String result; + String json = null; + try { json = mapper.writeValueAsString(map); } catch (JsonProcessingException e) { - throw new BillDeliveryException(null, "failed to serialize Bill object : " + e.getMessage()); - } - - try { - HttpResponse response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest); - result = this.bitPayClient.responseToJsonString(response).replace("\"", ""); - } catch (Exception e) { - throw new BillDeliveryException( - null, - String.format(EXCEPTION_MESSAGE, "Bill") + e.getMessage() - ); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - return result; + String response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest); + return response.replace("\"", ""); } } diff --git a/src/main/java/com/bitpay/sdk/client/BitPayClient.java b/src/main/java/com/bitpay/sdk/client/BitPayClient.java index 5f76c8a7..ec906245 100644 --- a/src/main/java/com/bitpay/sdk/client/BitPayClient.java +++ b/src/main/java/com/bitpay/sdk/client/BitPayClient.java @@ -6,22 +6,18 @@ package com.bitpay.sdk.client; import com.bitpay.sdk.Config; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.util.BitPayLogger; -import com.bitpay.sdk.util.JsonMapperFactory; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.logger.LoggerProvider; import com.bitpay.sdk.util.KeyUtils; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.json.JsonMapper; -import java.io.UnsupportedEncodingException; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; -import org.apache.http.ParseException; -import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; @@ -38,7 +34,6 @@ */ public class BitPayClient { - private static BitPayLogger logger = new BitPayLogger(BitPayLogger.OFF); private final HttpClient httpClient; private final HttpRequestFactory httpRequestFactory; private final String baseUrl; @@ -70,12 +65,13 @@ public BitPayClient( * @param uri the uri * @param parameters the parameters * @return the http response - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public HttpResponse get( + public String get( final String uri, final List parameters - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { return get(uri, parameters, true); } @@ -85,53 +81,63 @@ public HttpResponse get( * @param uri the uri * @param parameters the parameters * @param signatureRequired the signature required - * @return the http response - * @throws BitPayException the bit pay exception + * @return the json http response + * @throws BitPayGenericException BitPayGenericException + * @throws BitPayApiException BitPayApiException */ - public HttpResponse get( + public String get( final String uri, final List parameters, final boolean signatureRequired - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { + String jsonResponse = null; + try { String fullUrl = this.baseUrl + uri; final HttpGet httpGet = this.httpRequestFactory.createHttpGet(fullUrl); if (parameters != null) { - fullUrl += "?" + URLEncodedUtils.format(parameters, "UTF-8"); httpGet.setURI(new URI(fullUrl)); } + if (signatureRequired) { httpGet.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl)); httpGet.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); } + httpGet.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); httpGet.addHeader("x-accept-version", Config.BITPAY_API_VERSION); httpGet.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); httpGet.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); + LoggerProvider.getLogger().logRequest(HttpGet.METHOD_NAME, fullUrl, null); + + HttpResponse response = this.httpClient.execute(httpGet); + + final HttpEntity entity = response.getEntity(); + String jsonString = EntityUtils.toString(entity, "UTF-8"); + + LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, jsonString); - logger.info(httpGet.toString()); - return this.httpClient.execute(httpGet); + jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); - } catch (final URISyntaxException e) { - throw new BitPayException(null, "Error: GET failed\n" + e.getMessage()); - } catch (final ClientProtocolException e) { - throw new BitPayException(null, "Error: GET failed\n" + e.getMessage()); - } catch (final Exception e) { - throw new BitPayException(null, "Error: GET failed\n" + e.getMessage()); + } catch (IOException | URISyntaxException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); } + + return jsonResponse; } /** * Send GET request. * * @param uri the uri - * @return the http response - * @throws BitPayException the bit pay exception + * @return json http response + * @throws BitPayApiException BitPayApiException + * @throws BitPayGenericException BitPayGenericException */ - public HttpResponse get(final String uri) throws BitPayException { + public String get(final String uri) throws BitPayApiException, BitPayGenericException { return this.get(uri, null, false); } @@ -140,41 +146,47 @@ public HttpResponse get(final String uri) throws BitPayException { * * @param uri the uri * @param parameters the parameters - * @return the http response - * @throws BitPayException the bit pay exception + * @return json http response + * @throws BitPayApiException BitPayApiException + * @throws BitPayGenericException BitPayGenericException */ - public HttpResponse delete( + public String delete( final String uri, final List parameters - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { + String jsonResponse = null; + try { String fullUrl = this.baseUrl + uri; final HttpDelete httpDelete = this.httpRequestFactory.createHttpDelete(fullUrl); if (parameters != null) { - fullUrl += "?" + URLEncodedUtils.format(parameters, "UTF-8"); - httpDelete.setURI(new URI(fullUrl)); - - httpDelete.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); - httpDelete.addHeader("x-accept-version", Config.BITPAY_API_VERSION); - httpDelete.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); - httpDelete.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); - httpDelete.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl)); - httpDelete.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); } - logger.info(httpDelete.toString()); - return this.httpClient.execute(httpDelete); + httpDelete.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); + httpDelete.addHeader("x-accept-version", Config.BITPAY_API_VERSION); + httpDelete.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); + httpDelete.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); + httpDelete.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl)); + httpDelete.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); + + LoggerProvider.getLogger().logRequest(HttpDelete.METHOD_NAME, fullUrl, null); + + HttpResponse response = this.httpClient.execute(httpDelete); + + final HttpEntity entity = response.getEntity(); + String jsonString = EntityUtils.toString(entity, "UTF-8"); + + LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, jsonString); - } catch (final URISyntaxException e) { - throw new BitPayException(null, "Error: DELETE failed\n" + e.getMessage()); - } catch (final ClientProtocolException e) { - throw new BitPayException(null, "Error: DELETE failed\n" + e.getMessage()); - } catch (final Exception e) { - throw new BitPayException(null, "Error: DELETE failed\n" + e.getMessage()); + jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + } catch (IOException | URISyntaxException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); } + + return jsonResponse; } /** @@ -182,13 +194,14 @@ public HttpResponse delete( * * @param uri the uri * @param json the json - * @return the http response - * @throws BitPayException the bit pay exception + * @return the json http response + * @throws BitPayApiException BitPayApiException + * @throws BitPayGenericException BitPayGenericException */ - public HttpResponse post( + public String post( final String uri, final String json - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { return this.post(uri, json, false); } @@ -198,16 +211,20 @@ public HttpResponse post( * @param uri the uri * @param json the json * @param signatureRequired the signature required - * @return the http response - * @throws BitPayException the bit pay exception + * @return json response + * @throws BitPayApiException BitPayApiException + * @throws BitPayGenericException BitPayGenericException */ - public HttpResponse post( + public String post( final String uri, final String json, final boolean signatureRequired - ) throws BitPayException { + ) throws BitPayApiException, BitPayGenericException { + String jsonResponse = null; + try { - final HttpPost httpPost = this.httpRequestFactory.createHttpPost(this.baseUrl + uri); + final String endpoint = this.baseUrl + uri; + final HttpPost httpPost = this.httpRequestFactory.createHttpPost(endpoint); httpPost.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8))); @@ -222,16 +239,21 @@ public HttpResponse post( httpPost.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); httpPost.addHeader("Content-Type", "application/json"); - logger.info(httpPost.toString()); - return this.httpClient.execute(httpPost); + LoggerProvider.getLogger().logRequest(HttpPost.METHOD_NAME, endpoint, httpPost.toString()); + + HttpResponse response = this.httpClient.execute(httpPost); + + final HttpEntity entity = response.getEntity(); + String jsonString = EntityUtils.toString(entity, "UTF-8"); + + LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, jsonString); - } catch (final UnsupportedEncodingException e) { - throw new BitPayException(null, "Error: POST failed\n" + e.getMessage()); - } catch (final ClientProtocolException e) { - throw new BitPayException(null, "Error: POST failed\n" + e.getMessage()); - } catch (final Exception e) { - throw new BitPayException(null, "Error: POST failed\n" + e.getMessage()); + jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + } catch (IOException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); } + + return jsonResponse; } /** @@ -239,124 +261,44 @@ public HttpResponse post( * * @param uri the uri * @param json the json - * @return the http response - * @throws BitPayException the bit pay exception + * @return json response + * @throws BitPayApiException BitPayApiException + * @throws BitPayGenericException BitPayGenericException */ - public HttpResponse update( + public String update( final String uri, final String json - ) throws BitPayException { - try { - final HttpPut put = this.httpRequestFactory.createHttpPut(this.baseUrl + uri); - - put.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8))); - - put.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json)); - put.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); - put.addHeader("x-accept-version", Config.BITPAY_API_VERSION); - put.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); - put.addHeader("Content-Type", "application/json"); - put.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); - put.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); - - logger.info(put.toString()); - return this.httpClient.execute(put); - - } catch (final UnsupportedEncodingException e) { - throw new BitPayException(null, "Error: PUT failed\n" + e.getMessage()); - } catch (final ClientProtocolException e) { - throw new BitPayException(null, "Error: PUT failed\n" + e.getMessage()); - } catch (final Exception e) { - throw new BitPayException(null, "Error: PUT failed\n" + e.getMessage()); - } - } - - /** - * Convert HttpResponse for Json string. - * - * @param response the response - * @return the string - * @throws BitPayException the bit pay exception - */ - public String responseToJsonString(final HttpResponse response) throws BitPayException { - if (response == null) { - throw new BitPayException(null, "Error: HTTP response is null"); - } + ) throws BitPayApiException, BitPayGenericException { + String jsonResponse = null; try { - // Get the JSON string from the response. - final HttpEntity entity = response.getEntity(); + final String endpoint = this.baseUrl + uri; + final HttpPut httpPut = this.httpRequestFactory.createHttpPut(endpoint); - String jsonString; + httpPut.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8))); - jsonString = EntityUtils.toString(entity, "UTF-8"); - logger.info("RESPONSE: " + jsonString); - final JsonMapper mapper = JsonMapperFactory.create(); + httpPut.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json)); + httpPut.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); + httpPut.addHeader("x-accept-version", Config.BITPAY_API_VERSION); + httpPut.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); + httpPut.addHeader("Content-Type", "application/json"); + httpPut.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); + httpPut.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); - final JsonNode rootNode = mapper.readTree(jsonString); - JsonNode node = rootNode.get("status"); - if (node != null) { - if ("error".equals(node.toString().replace("\"", ""))) { - throw new BitPayException(rootNode.get("code").textValue(), rootNode.get("message").textValue()); - } - } + LoggerProvider.getLogger().logRequest(HttpPut.METHOD_NAME, endpoint, httpPut.toString()); - node = rootNode.get("error"); - if (node != null) { - throw new BitPayException(null, "Error: " + node.asText()); - } - - node = rootNode.get("errors"); - if (node != null) { - String message = "Multiple errors:"; - - if (node.isArray()) { - for (final JsonNode errorNode : node) { - message += "\n" + errorNode.asText(); - } - - throw new BitPayException(null, message); - } - } - - node = rootNode.get("status"); - if (node != null) { - if ("error".equals(node.toString().replace("\"", ""))) { - throw new BitPayException(rootNode.get("code").textValue(), rootNode.get("message").textValue()); - } - if ("success".equals(node.toString().replace("\"", ""))) { - node = rootNode.get("data"); - - if ("{}".equals(node.toString())) { - return rootNode.toString(); - } - } - } + HttpResponse response = this.httpClient.execute(httpPut); - node = rootNode.get("data"); - if (node != null) { - jsonString = node.toString(); - } + final HttpEntity entity = response.getEntity(); + String jsonString = EntityUtils.toString(entity, "UTF-8"); - return jsonString; + LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, jsonString); - } catch (final ParseException e) { - throw new BitPayException(null, "failed to retrieve HTTP response body : " + e.getMessage()); - } catch (final JsonMappingException e) { - throw new BitPayException(null, "failed to parse json response to map : " + e.getMessage()); - } catch (final BitPayException e) { - throw new BitPayException(e.getStatusCode(), e.getReasonPhrase()); - } catch (final Exception e) { - throw new BitPayException(null, "failed to retrieve HTTP response body : " + e.getMessage()); + jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + } catch (IOException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); } - } - /** - * Sets the logger level of reporting. - * - * @param loggerLevel int BitPayLogger constant (OFF, INFO, WARN, ERR, DEBUG) - */ - public void setLoggerLevel(final int loggerLevel) { - logger = new BitPayLogger(loggerLevel); + return jsonResponse; } } diff --git a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java index 0e4a554d..e48c765e 100644 --- a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java +++ b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java @@ -5,7 +5,8 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.util.JsonMapperFactory; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.json.JsonMapper; @@ -14,8 +15,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpEntity; -import org.apache.http.util.EntityUtils; /** * The type Currency client. @@ -30,11 +29,11 @@ public class CurrencyClient implements ResourceClient { * Instantiates a new Currency client. * * @param client the client - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ - private CurrencyClient(BitPayClient client) throws BitPayException { + private CurrencyClient(BitPayClient client) throws BitPayGenericException { if (Objects.isNull(client)) { - throw new BitPayException(null, "failed init Currency Client"); + BitPayExceptionProvider.throwGenericExceptionWithMessage("Failed init Currency Client"); } this.client = client; } @@ -44,9 +43,9 @@ private CurrencyClient(BitPayClient client) throws BitPayException { * * @param bitPayClient BitPay Client * @return CurrencyClient - * @throws BitPayException BitPayException + * @throws BitPayGenericException BitPayGenericException class */ - public static CurrencyClient getInstance(BitPayClient bitPayClient) throws BitPayException { + public static CurrencyClient getInstance(BitPayClient bitPayClient) throws BitPayGenericException { if (Objects.isNull(instance)) { instance = new CurrencyClient(bitPayClient); } @@ -59,12 +58,11 @@ public static CurrencyClient getInstance(BitPayClient bitPayClient) throws BitPa * * @param currencyCode String Currency code for which the info will be retrieved. * @return Map |null - * @throws BitPayException the bit pay exception */ @SuppressWarnings("unchecked") - public Map getInfo(String currencyCode) throws BitPayException { + public Map getInfo(String currencyCode) throws BitPayGenericException { if (Objects.isNull(currencyCode)) { - throw new BitPayException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } if (Objects.isNull(this.currenciesInfo)) { @@ -73,7 +71,6 @@ public Map getInfo(String currencyCode) throws BitPayException { for (Object currency : this.currenciesInfo) { Map currencyInfo = JsonMapperFactory.create().convertValue(currency, Map.class); - if (currencyInfo.get("code").toString().equals(currencyCode)) { return currencyInfo; } @@ -84,17 +81,10 @@ public Map getInfo(String currencyCode) throws BitPayException { /** * Load currencies info. - * - * @throws BitPayException BitPayException class */ - @SuppressWarnings("unchecked") - private void loadCurrencies() throws BitPayException { + private void loadCurrencies() { try { - HttpEntity newEntity = this.client.get("currencies").getEntity(); - - String jsonString; - - jsonString = EntityUtils.toString(newEntity, "UTF-8"); + String jsonString = this.client.get("currencies"); JsonMapper mapper = JsonMapperFactory.create(); diff --git a/src/main/java/com/bitpay/sdk/client/InvoiceClient.java b/src/main/java/com/bitpay/sdk/client/InvoiceClient.java index 7777a4f7..3a7d9d63 100644 --- a/src/main/java/com/bitpay/sdk/client/InvoiceClient.java +++ b/src/main/java/com/bitpay/sdk/client/InvoiceClient.java @@ -5,11 +5,10 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.InvoiceCancellationException; -import com.bitpay.sdk.exceptions.InvoiceCreationException; -import com.bitpay.sdk.exceptions.InvoiceQueryException; -import com.bitpay.sdk.exceptions.InvoiceUpdateException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.exceptions.BitPayValidationException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.invoice.Invoice; import com.bitpay.sdk.model.invoice.InvoiceEventToken; @@ -26,7 +25,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -83,41 +81,40 @@ public static InvoiceClient getInstance( * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay generated Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceCreationException InvoiceCreationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayValidationException BitPayValidationException class + * @throws BitPayGenericException BitPayGenericException class */ public Invoice create( Invoice invoice, Facade facade, Boolean signRequest - ) throws BitPayException, InvoiceCreationException { + ) throws BitPayApiException, BitPayValidationException, BitPayGenericException { if (Objects.isNull(invoice) || Objects.isNull(facade)) { - throw new InvoiceCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } invoice.setToken(this.accessTokens.getAccessToken(facade)); invoice.setGuid(Objects.isNull(invoice.getGuid()) ? this.guidGenerator.execute() : invoice.getGuid()); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(invoice); } catch (JsonProcessingException e) { - throw new InvoiceCreationException(null, "failed to serialize Invoice object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Invoice", e.getMessage()); } - try { - HttpResponse response = this.bitPayClient.post("invoices", json, signRequest); - invoice = mapper.readerForUpdating(invoice).readValue(this.bitPayClient.responseToJsonString(response)); - this.accessTokens.put(invoice.getId(), invoice.getToken()); + String jsonResponse = this.bitPayClient.post("invoices", json, signRequest); - } catch (BitPayException ex) { - throw new InvoiceCreationException(ex.getStatusCode(), ex.getReasonPhrase()); + try { + invoice = mapper.readerForUpdating(invoice).readValue(jsonResponse); } catch (Exception e) { - throw new InvoiceCreationException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } + this.accessTokens.put(invoice.getId(), invoice.getToken()); + return invoice; } @@ -129,32 +126,25 @@ public Invoice create( * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public Invoice get( String invoiceId, Facade facade, Boolean signRequest - ) throws BitPayException, - InvoiceQueryException { + ) throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(facade)); - Invoice invoice; + Invoice invoice = null; + + String jsonResponse = this.bitPayClient.get("invoices/" + invoiceId, params, signRequest); try { - HttpResponse response = this.bitPayClient.get("invoices/" + invoiceId, params, signRequest); - invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); + invoice = JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); - } catch (Exception e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; @@ -168,33 +158,29 @@ public Invoice get( * @param facade The facade used to create it. * @param signRequest Signed request. * @return A BitPay Invoice object. - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayValidationException BitPayValidationException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ public Invoice getByGuid( String guid, Facade facade, Boolean signRequest - ) throws InvoiceQueryException { + ) throws BitPayValidationException, BitPayGenericException, BitPayApiException { if (Objects.isNull(guid) || Objects.isNull(facade)) { - throw new InvoiceQueryException(null, "missing required parameters"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); - Invoice invoice; + Invoice invoice = null; + + ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(facade)); + String jsonResponse = this.bitPayClient.get("invoices/guid/" + guid, params, signRequest); try { - ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(facade)); - HttpResponse response = this.bitPayClient.get("invoices/guid/" + guid, params, signRequest); - invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); + invoice = JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); - } catch (Exception e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; @@ -210,8 +196,8 @@ public Invoice getByGuid( * @param limit Maximum results that the query will return (useful for paging results). * @param offset Number of results to offset (ex. skip 10 will give you results starting with the 11th * @return A list of BitPay Invoice objects. - * @throws BitPayException BitPayException class - * @throws InvoiceQueryException InvoiceQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public List getInvoices( String dateStart, @@ -220,9 +206,9 @@ public List getInvoices( String orderId, Integer limit, Integer offset - ) throws BitPayException, InvoiceQueryException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(dateStart) || Objects.isNull(dateEnd)) { - throw new InvoiceQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); @@ -238,21 +224,13 @@ public List getInvoices( ParameterAdder.execute(params, "offset", offset.toString()); } - List invoices; + List invoices = null; + String jsonResponse = this.bitPayClient.get("invoices", params); try { - HttpResponse response = this.bitPayClient.get("invoices", params); - invoices = Arrays.asList( - JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Invoice[].class)); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); + invoices = Arrays.asList(JsonMapperFactory.create().readValue(jsonResponse, Invoice[].class)); } catch (JsonProcessingException e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoices) : " + e.getMessage()); - } catch (Exception e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoices) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoices; @@ -263,25 +241,24 @@ public List getInvoices( * * @param invoiceId the id of the invoice for which you want to fetch an event token * @return InvoiceEventToken event token - * @throws BitPayException BitPayException + * @throws BitPayApiException BitPayException + * @throws BitPayGenericException BitPayGenericException */ - public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayException { + public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); + String jsonResponse = this.bitPayClient.get("invoices/" + invoiceId + "/events", params); + InvoiceEventToken result = null; + try { - HttpResponse response = this.bitPayClient.get("invoices/" + invoiceId + "/events", params); - return JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), InvoiceEventToken.class); - } catch (BitPayException ex) { - throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase()); + result = JsonMapperFactory.create() + .readValue(jsonResponse, InvoiceEventToken.class); } catch (JsonProcessingException e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoices) : " + e.getMessage()); - } catch (Exception e) { - throw new InvoiceQueryException(null, - "failed to deserialize BitPay server response (Invoices) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } + + return result; } /** @@ -293,8 +270,8 @@ public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayExc * @param buyerEmail The buyer's email address. * @param autoVerify Skip the user verification on sandbox ONLY. * @return A BitPay generated Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceUpdateException InvoiceUpdateException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public Invoice update( String invoiceId, @@ -302,9 +279,9 @@ public Invoice update( String smsCode, String buyerEmail, Boolean autoVerify - ) throws BitPayException, InvoiceUpdateException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(invoiceId)) { - throw new InvoiceUpdateException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final Map params = new HashMap<>(); @@ -326,24 +303,22 @@ public Invoice update( } JsonMapper mapper = JsonMapperFactory.create(); - String json; - Invoice invoice; + String json = null; + Invoice invoice = null; try { json = mapper.writeValueAsString(params); } catch (JsonProcessingException e) { - throw new InvoiceUpdateException(null, "failed to serialize object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + String response = this.bitPayClient.update("invoices/" + invoiceId, json); + try { - HttpResponse response = this.bitPayClient.update("invoices/" + invoiceId, json); invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceUpdateException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceUpdateException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + JsonMapperFactory.create().readValue(response, Invoice.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; @@ -355,37 +330,34 @@ public Invoice update( * @param invoiceId The id of the invoice to updated. * @param status The status of the invoice to be updated, can be "confirmed" or "complete". * @return A BitPay generated Invoice object. - * @throws BitPayException BitPayException class - * @throws InvoiceUpdateException InvoiceUpdateException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public Invoice pay( String invoiceId, String status - ) throws BitPayException, InvoiceUpdateException { + ) throws BitPayApiException, BitPayGenericException { final Map params = new HashMap<>(); params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT)); if (status != null) { params.put("status", status); } JsonMapper mapper = JsonMapperFactory.create(); - String json; - Invoice invoice; + String json = null; + Invoice invoice = null; try { json = mapper.writeValueAsString(params); } catch (JsonProcessingException e) { - throw new InvoiceUpdateException(null, "failed to serialize object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + String response = this.bitPayClient.update("invoices/pay/" + invoiceId, json); try { - HttpResponse response = this.bitPayClient.update("invoices/pay/" + invoiceId, json); invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceUpdateException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceUpdateException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + JsonMapperFactory.create().readValue(response, Invoice.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; @@ -396,17 +368,11 @@ public Invoice pay( * * @param invoiceId The Id of the BitPay invoice to be canceled. * @return A BitPay generated Invoice object. - * @throws InvoiceCancellationException InvoiceCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Invoice cancel(String invoiceId) throws InvoiceCancellationException, BitPayException { - try { - return this.cancel(invoiceId, false); - } catch (BitPayException ex) { - throw new InvoiceCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceCancellationException(null, e.getMessage()); - } + public Invoice cancel(String invoiceId) throws BitPayApiException, BitPayGenericException { + return this.cancel(invoiceId, false); } /** @@ -415,40 +381,44 @@ public Invoice cancel(String invoiceId) throws InvoiceCancellationException, Bit * @param invoiceId The Id of the BitPay invoice to be canceled. * @param forceCancel If 'true' it will cancel the invoice even if no contact information is present. * @return A BitPay generated Invoice object. - * @throws InvoiceCancellationException InvoiceCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayException class */ public Invoice cancel( String invoiceId, Boolean forceCancel - ) throws InvoiceCancellationException, BitPayException { + ) throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); if (forceCancel) { ParameterAdder.execute(params, "forceCancel", forceCancel.toString()); } - Invoice invoice; + Invoice invoice = null; + + String response = this.bitPayClient.delete("invoices/" + invoiceId, params); try { - HttpResponse response = this.bitPayClient.delete("invoices/" + invoiceId, params); invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceCancellationException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + JsonMapperFactory.create().readValue(response, Invoice.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; } - public Invoice cancelByGuid( - String guid, - Boolean forceCancel - ) throws BitPayException { + /** + * Cancel Invoice by Guid. + * + * @param guid Guid + * @param forceCancel Force Cancel + * @return Invoice + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class + */ + public Invoice cancelByGuid(String guid, Boolean forceCancel) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(guid) || Objects.isNull(forceCancel)) { - throw new InvoiceCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); @@ -456,55 +426,57 @@ public Invoice cancelByGuid( if (forceCancel.equals(true)) { ParameterAdder.execute(params, "forceCancel", forceCancel.toString()); } - Invoice invoice; + Invoice invoice = null; + + String response = this.bitPayClient.delete("invoices/guid/" + guid, params); try { - HttpResponse response = this.bitPayClient.delete("invoices/guid/" + guid, params); invoice = - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Invoice.class); - } catch (BitPayException ex) { - throw new InvoiceCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new InvoiceCancellationException(null, - "failed to deserialize BitPay server response (Invoice) : " + e.getMessage()); + JsonMapperFactory.create().readValue(response, Invoice.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } return invoice; } - public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayException { + /** + * Request an Invoice Webhook to be Resent. + * + * @param invoiceId Invoice ID + * @return Boolean + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class + */ + public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayApiException, BitPayGenericException { final Map params = new HashMap<>(); params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT)); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(params); } catch (JsonProcessingException e) { - throw new InvoiceUpdateException(null, "failed to serialize object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - try { - HttpResponse response = this.bitPayClient.post("invoices/" + invoiceId + "/notifications", json); - String jsonString = this.bitPayClient.responseToJsonString(response); - return jsonString.replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); - } catch (Exception e) { - throw new BitPayException(null, "failed to process request : " + e.getMessage()); - } + String jsonResponse = this.bitPayClient.post("invoices/" + invoiceId + "/notifications", json); + return jsonResponse.replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); } private void validateRequiredField( String buyerSms, String buyerEmail - ) throws InvoiceUpdateException { + ) throws BitPayValidationException { if (buyerSms == null && buyerEmail == null) { - throw new InvoiceUpdateException(null, + BitPayExceptionProvider.throwValidationException( "Updating the invoice requires Mobile Phone Number for SMS reception."); } if (Objects.nonNull(buyerSms) && Objects.nonNull(buyerEmail)) { - throw new InvoiceUpdateException(null, "Updating an invoice will require EITHER an SMS or E-mail)"); + BitPayExceptionProvider.throwValidationException( + "Updating an invoice will require EITHER an SMS or E-mail)"); } } @@ -512,7 +484,7 @@ private void validateSmsCode( String buyerSms, String smsCode, Boolean autoVerify - ) throws InvoiceUpdateException { + ) throws BitPayValidationException { if (Objects.isNull(autoVerify)) { return; } @@ -529,7 +501,7 @@ private void validateSmsCode( return; } - throw new InvoiceUpdateException(null, + BitPayExceptionProvider.throwValidationException( "If provided alongside a valid SMS, will bypass the need to complete an SMS challenge"); } } diff --git a/src/main/java/com/bitpay/sdk/client/LedgerClient.java b/src/main/java/com/bitpay/sdk/client/LedgerClient.java index 01ec94c5..a80d9452 100644 --- a/src/main/java/com/bitpay/sdk/client/LedgerClient.java +++ b/src/main/java/com/bitpay/sdk/client/LedgerClient.java @@ -5,8 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.LedgerQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.ledger.Ledger; import com.bitpay.sdk.model.ledger.LedgerEntry; @@ -18,7 +19,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -69,16 +69,16 @@ public static LedgerClient getInstance( * @param dateStart The first date for the query filter. * @param dateEnd The last date for the query filter. * @return A list of Ledger entries. - * @throws BitPayException BitPayException class - * @throws LedgerQueryException LedgerQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public List getEntries( String currency, String dateStart, String dateEnd - ) throws BitPayException, LedgerQueryException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(currency) || Objects.isNull(dateStart) || Objects.isNull(dateEnd)) { - throw new BitPayException(null, "missing mandatory fields"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); @@ -86,17 +86,18 @@ public List getEntries( ParameterAdder.execute(params, "startDate", dateStart); ParameterAdder.execute(params, "endDate", dateEnd); + String jsonResponse = this.bitPayClient.get("ledgers/" + currency, params); + + List entries = null; + try { - HttpResponse response = this.bitPayClient.get("ledgers/" + currency, params); - return Arrays.asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), LedgerEntry[].class)); + entries = Arrays.asList(JsonMapperFactory.create() + .readValue(jsonResponse, LedgerEntry[].class)); } catch (JsonProcessingException e) { - throw new LedgerQueryException(null, - "failed to deserialize BitPay server response (Ledger) : " + e.getMessage()); - } catch (Exception e) { - throw new LedgerQueryException(null, - "failed to deserialize BitPay server response (Ledger) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Ledger", e.getMessage()); } + + return entries; } /** @@ -104,26 +105,23 @@ public List getEntries( * Retrieve a list of ledgers using the merchant facade. * * @return A list of Ledger objects populated with the currency and current balance of each one. - * @throws BitPayException BitPayException class - * @throws LedgerQueryException LedgerQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public List getLedgers() throws BitPayException, LedgerQueryException { + public List getLedgers() throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - List ledgers; + List ledgers = null; + + String jsonResponse = this.bitPayClient.get("ledgers", params); try { - HttpResponse response = this.bitPayClient.get("ledgers", params); ledgers = Arrays .asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Ledger[].class)); + .readValue(jsonResponse, Ledger[].class)); } catch (JsonProcessingException e) { - throw new LedgerQueryException(null, - "failed to deserialize BitPay server response (Ledger) : " + e.getMessage()); - } catch (Exception e) { - throw new LedgerQueryException(null, - "failed to deserialize BitPay server response (Ledger) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Ledger", e.getMessage()); } return ledgers; diff --git a/src/main/java/com/bitpay/sdk/client/PayoutClient.java b/src/main/java/com/bitpay/sdk/client/PayoutClient.java index 38aa5385..5b8793c8 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutClient.java @@ -5,11 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.PayoutCancellationException; -import com.bitpay.sdk.exceptions.PayoutCreationException; -import com.bitpay.sdk.exceptions.PayoutNotificationException; -import com.bitpay.sdk.exceptions.PayoutQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.payout.Payout; import com.bitpay.sdk.util.JsonMapperFactory; @@ -25,7 +23,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -68,31 +65,33 @@ public static PayoutClient getInstance(BitPayClient bitPayClient, TokenContainer * * @param payout Payout A Payout object with request parameters defined. * @return A BitPay generated Payout object. - * @throws BitPayException BitPayException class - * @throws PayoutCreationException PayoutCreationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public Payout submit(Payout payout) throws BitPayException, PayoutCreationException { + public Payout submit(Payout payout) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(payout)) { - throw new PayoutCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } + String token = this.accessTokens.getAccessToken(Facade.PAYOUT); payout.setToken(token); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(payout); } catch (JsonProcessingException e) { - throw new PayoutCreationException(null, "failed to serialize Payout object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Payout", e.getMessage()); } + + String jsonResponse = this.bitPayClient.post("payouts", json, true); + try { - HttpResponse response = this.bitPayClient.post("payouts", json, true); payout = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Payout.class); - } catch (Exception e) { - throw new PayoutCreationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + .readValue(jsonResponse, Payout.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Payout", e.getMessage()); } return payout; @@ -104,30 +103,27 @@ public Payout submit(Payout payout) throws BitPayException, PayoutCreationExcept * * @param payoutId String The id of the payout to retrieve. * @return A BitPay Payout object. - * @throws BitPayException BitPayException class - * @throws PayoutQueryException PayoutQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public Payout get(String payoutId) throws BitPayException, PayoutQueryException { + public Payout get(String payoutId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(payoutId)) { - throw new PayoutQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } String token = this.accessTokens.getAccessToken(Facade.PAYOUT); final List params = new ArrayList(); ParameterAdder.execute(params, "token", token); - Payout payout; + Payout payout = null; + + String jsonResponse = this.bitPayClient.get("payouts/" + payoutId, params, true); try { - HttpResponse response = this.bitPayClient.get("payouts/" + payoutId, params, true); payout = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Payout.class); + .readValue(jsonResponse, Payout.class); } catch (JsonProcessingException e) { - throw new PayoutQueryException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutQueryException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout", e.getMessage()); } return payout; @@ -138,33 +134,29 @@ public Payout get(String payoutId) throws BitPayException, PayoutQueryException * * @param payoutId String The id of the payout to cancel. * @return True if the refund was successfully canceled, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutCancellationException PayoutCancellationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public Boolean cancel(String payoutId) throws BitPayException, PayoutCancellationException { + public Boolean cancel(String payoutId) + throws BitPayApiException, BitPayGenericException { if (Objects.isNull(payoutId)) { - throw new PayoutCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); - boolean result; + boolean result = false; JsonMapper mapper = JsonMapperFactory.create(); + String jsonResponse = this.bitPayClient.delete("payouts/" + payoutId, params); + try { - HttpResponse response = this.bitPayClient.delete("payouts/" + payoutId, params); - String jsonString = this.bitPayClient.responseToJsonString(response); - JsonNode rootNode = mapper.readTree(jsonString); + JsonNode rootNode = mapper.readTree(jsonResponse); JsonNode node = rootNode.get("status"); result = node.toString().replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); - } catch (JsonProcessingException e) { - throw new PayoutCancellationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutCancellationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeException(e.getMessage()); } return result; @@ -182,8 +174,8 @@ public Boolean cancel(String payoutId) throws BitPayException, PayoutCancellatio * @param offset int Offset for paging. * @param groupId String The optional group id assigned to payout. * @return A list of BitPay Payout objects. - * @throws BitPayException BitPayException class - * @throws PayoutQueryException PayoutQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public List getPayouts( String startDate, @@ -193,7 +185,7 @@ public List getPayouts( Integer limit, Integer offset, String groupId - ) throws BitPayException, PayoutQueryException { + ) throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); ParameterAdder.execute(params, "startDate", startDate); @@ -210,19 +202,16 @@ public List getPayouts( ParameterAdder.execute(params, "groupId", groupId); } - List payouts; + List payouts = null; + + String jsonResponse = this.bitPayClient.get("payouts", params, true); try { - HttpResponse response = this.bitPayClient.get("payouts", params, true); payouts = Arrays .asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Payout[].class)); + .readValue(jsonResponse, Payout[].class)); } catch (JsonProcessingException e) { - throw new PayoutQueryException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutQueryException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout", e.getMessage()); } return payouts; @@ -233,13 +222,13 @@ public List getPayouts( * * @param payoutId String The id of the payout to notify.. * @return True if the notification was successfully sent, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutNotificationException PayoutNotificationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public Boolean requestNotification(String payoutId) - throws BitPayException, PayoutNotificationException { + throws BitPayApiException, BitPayGenericException { if (Objects.isNull(payoutId)) { - throw new PayoutNotificationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final Map params = new HashMap<>(); @@ -247,26 +236,23 @@ public Boolean requestNotification(String payoutId) JsonMapper mapper = JsonMapperFactory.create(); - boolean result; - String json; + boolean result = false; + String json = null; try { json = mapper.writeValueAsString(params); } catch (JsonProcessingException e) { - throw new PayoutNotificationException(null, "failed to serialize payout batch object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + String jsonResponse = this.bitPayClient.post("payouts/" + payoutId + "/notifications", json, true); + try { - HttpResponse response = this.bitPayClient.post("payouts/" + payoutId + "/notifications", json, true); - String jsonString = this.bitPayClient.responseToJsonString(response); - JsonNode rootNode = mapper.readTree(jsonString); + JsonNode rootNode = mapper.readTree(jsonResponse); JsonNode node = rootNode.get("status"); result = node.toString().replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); - } catch (BitPayException ex) { - throw new PayoutNotificationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new PayoutNotificationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeException(e.getMessage()); } return result; diff --git a/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java b/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java index ce9499b3..b510aee7 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java @@ -5,9 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.PayoutCancellationException; -import com.bitpay.sdk.exceptions.PayoutCreationException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.payout.Payout; import com.bitpay.sdk.model.payout.PayoutGroup; @@ -22,7 +22,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -65,12 +64,12 @@ public static PayoutGroupClient getInstance(BitPayClient bitPayClient, TokenCont * * @param payouts Collection of Payout objects with request parameters defined. * @return A BitPay PayoutGroup with generated Payout objects and information's about not created payouts. - * @throws BitPayException BitPayException class - * @throws PayoutCreationException PayoutCreationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public PayoutGroup submit(Collection payouts) throws BitPayException, PayoutCreationException { + public PayoutGroup submit(Collection payouts) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(payouts)) { - throw new PayoutCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } String token = this.accessTokens.getAccessToken(Facade.PAYOUT); Map parameters = new HashMap<>(); @@ -78,21 +77,25 @@ public PayoutGroup submit(Collection payouts) throws BitPayException, Pa parameters.put("token", token); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; + PayoutGroup result = null; try { json = mapper.writeValueAsString(parameters); } catch (JsonProcessingException e) { - throw new PayoutCreationException(null, "failed to serialize Payouts object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + + String jsonResponse = this.bitPayClient.post("payouts/group", json, true); + try { - HttpResponse response = this.bitPayClient.post("payouts/group", json, true); - return JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutGroup.class); + result = JsonMapperFactory.create() + .readValue(jsonResponse, PayoutGroup.class); } catch (Exception e) { - throw new PayoutCreationException(null, - "failed to deserialize BitPay server response (Payout Group) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Group", e.getMessage()); } + + return result; } /** @@ -100,27 +103,29 @@ public PayoutGroup submit(Collection payouts) throws BitPayException, Pa * * @param groupId String The id of the payout group to cancel. * @return A BitPay PayoutGroup with cancelled Payout objects and information's about not cancelled payouts. - * @throws BitPayException BitPayException class - * @throws PayoutCancellationException PayoutCancellationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public PayoutGroup cancel(String groupId) throws BitPayException, PayoutCancellationException { + public PayoutGroup cancel(String groupId) + throws BitPayApiException, BitPayGenericException { if (Objects.isNull(groupId)) { - throw new PayoutCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } + PayoutGroup result = null; + final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); + String jsonResponse = this.bitPayClient.delete("payouts/group/" + groupId, params); + try { - HttpResponse response = this.bitPayClient.delete("payouts/group/" + groupId, params); - return JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutGroup.class); + result = JsonMapperFactory.create() + .readValue(jsonResponse, PayoutGroup.class); } catch (JsonProcessingException e) { - throw new PayoutCancellationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutCancellationException(null, - "failed to deserialize BitPay server response (Payout) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Group", e.getMessage()); } + + return result; } } diff --git a/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java b/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java index 0bdfb885..8cc6e735 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java @@ -5,12 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.PayoutRecipientCancellationException; -import com.bitpay.sdk.exceptions.PayoutRecipientCreationException; -import com.bitpay.sdk.exceptions.PayoutRecipientNotificationException; -import com.bitpay.sdk.exceptions.PayoutRecipientQueryException; -import com.bitpay.sdk.exceptions.PayoutRecipientUpdateException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.payout.PayoutRecipient; import com.bitpay.sdk.model.payout.PayoutRecipients; @@ -28,7 +25,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -83,40 +79,35 @@ public static PayoutRecipientsClient getInstance( * * @param recipients PayoutRecipients A PayoutRecipients object with request parameters defined. * @return array A list of BitPay PayoutRecipients objects.. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientCreationException PayoutRecipientCreationException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public List submit(PayoutRecipients recipients) - throws BitPayException, PayoutRecipientCreationException { + public List submit(PayoutRecipients recipients) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(recipients)) { - throw new PayoutRecipientCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } recipients.setToken(this.accessTokens.getAccessToken(Facade.PAYOUT)); recipients.setGuid(Objects.isNull(recipients.getGuid()) ? this.guidGenerator.execute() : recipients.getGuid()); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(recipients); } catch (JsonProcessingException e) { - throw new PayoutRecipientCreationException(null, - "failed to serialize PayoutRecipients object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Payout Recipients", e.getMessage()); } - List recipientsList; + List recipientsList = null; + + String jsonResponse = this.bitPayClient.post("recipients", json, true); try { - HttpResponse response = this.bitPayClient.post("recipients", json, true); recipientsList = Arrays .asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutRecipient[].class)); + .readValue(jsonResponse, PayoutRecipient[].class)); } catch (JsonProcessingException e) { - throw new PayoutRecipientCreationException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutRecipientCreationException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Recipient", e.getMessage()); } return recipientsList; @@ -130,11 +121,11 @@ public List submit(PayoutRecipients recipients) * paging results). result). * @param offset int Offset for paging. * @return array A list of BitPayRecipient objects. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientQueryException PayoutRecipientQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public List getRecipientsByFilters(String status, Integer limit, Integer offset) - throws BitPayException, PayoutRecipientQueryException { + throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); @@ -146,19 +137,16 @@ public List getRecipientsByFilters(String status, Integer limit ParameterAdder.execute(params, "offset", offset.toString()); } - List recipientsList; + List recipientsList = null; + + String jsonResponse = this.bitPayClient.get("recipients", params, true); try { - HttpResponse response = this.bitPayClient.get("recipients", params, true); recipientsList = Arrays .asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutRecipient[].class)); + .readValue(jsonResponse, PayoutRecipient[].class)); } catch (JsonProcessingException e) { - throw new PayoutRecipientQueryException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutRecipientQueryException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Recipient", e.getMessage()); } return recipientsList; @@ -170,30 +158,27 @@ public List getRecipientsByFilters(String status, Integer limit * * @param recipientId String The id of the recipient to retrieve. * @return PayoutRecipient A BitPay PayoutRecipient object. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientQueryException PayoutRecipientQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public PayoutRecipient get(String recipientId) - throws BitPayException, PayoutRecipientQueryException { + throws BitPayApiException, BitPayGenericException { if (Objects.isNull(recipientId)) { - throw new PayoutRecipientQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); - PayoutRecipient recipient; + PayoutRecipient recipient = null; + + String jsonResponse = this.bitPayClient.get("recipients/" + recipientId, params, true); try { - HttpResponse response = this.bitPayClient.get("recipients/" + recipientId, params, true); recipient = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutRecipient.class); + .readValue(jsonResponse, PayoutRecipient.class); } catch (JsonProcessingException e) { - throw new PayoutRecipientQueryException(null, - "failed to deserialize BitPay server response (PayoutRecipient) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutRecipientQueryException(null, - "failed to deserialize BitPay server response (PayoutRecipient) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Recipient", e.getMessage()); } return recipient; @@ -206,38 +191,35 @@ public PayoutRecipient get(String recipientId) * @param recipient PayoutRecipients A PayoutRecipient object with updated * parameters defined. * @return The updated recipient object. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientUpdateException PayoutRecipientUpdateException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public PayoutRecipient update(String recipientId, PayoutRecipient recipient) - throws BitPayException, PayoutRecipientUpdateException { + throws BitPayApiException, BitPayGenericException { if (Objects.isNull(recipient) || Objects.isNull(recipientId)) { - throw new PayoutRecipientUpdateException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } + recipient.setToken(this.accessTokens.getAccessToken(Facade.PAYOUT)); recipient.setGuid(Objects.isNull(recipient.getGuid()) ? this.guidGenerator.execute() : recipient.getGuid()); JsonMapper mapper = JsonMapperFactory.create(); - String json; + String json = null; try { json = mapper.writeValueAsString(recipient); } catch (JsonProcessingException e) { - throw new PayoutRecipientUpdateException(null, - "failed to serialize PayoutRecipient object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeResourceException("Payout Recipient", e.getMessage()); } - PayoutRecipient updateRecipient; + PayoutRecipient updateRecipient = null; + + String response = this.bitPayClient.update("recipients/" + recipientId, json); try { - HttpResponse response = this.bitPayClient.update("recipients/" + recipientId, json); updateRecipient = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), PayoutRecipient.class); + .readValue(response, PayoutRecipient.class); } catch (JsonProcessingException e) { - throw new PayoutRecipientUpdateException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); - } catch (Exception e) { - throw new PayoutRecipientUpdateException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Payout Recipient", e.getMessage()); } return updateRecipient; @@ -247,33 +229,28 @@ public PayoutRecipient update(String recipientId, PayoutRecipient recipient) * Cancel a BitPay Payout recipient. * * @param recipientId String The id of the recipient to cancel. - * @return True if the delete operation was successfull, false otherwise. - * @throws BitPayException BitPayException class - * @throws PayoutRecipientCancellationException PayoutRecipientCancellationException class + * @return True if the delete operation was successful, false otherwise. + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public Boolean delete(String recipientId) - throws BitPayException, PayoutRecipientCancellationException { + public Boolean delete(String recipientId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(recipientId)) { - throw new PayoutRecipientCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); JsonMapper mapper = JsonMapperFactory.create(); - Boolean result; + Boolean result = null; + String jsonResponse = this.bitPayClient.delete("recipients/" + recipientId, params); try { - HttpResponse response = this.bitPayClient.delete("recipients/" + recipientId, params); - String jsonString = this.bitPayClient.responseToJsonString(response); - JsonNode rootNode = mapper.readTree(jsonString); + JsonNode rootNode = mapper.readTree(jsonResponse); JsonNode node = rootNode.get("status"); result = node.toString().replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); - } catch (BitPayException ex) { - throw new PayoutRecipientCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new PayoutRecipientCancellationException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeException(e.getMessage()); } return result; @@ -284,40 +261,36 @@ public Boolean delete(String recipientId) * * @param recipientId String A BitPay recipient ID. * @return True if the notification was successfully sent, false otherwise. - * @throws PayoutRecipientNotificationException PayoutRecipientNotificationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Boolean requestNotification(String recipientId) - throws PayoutRecipientNotificationException, BitPayException { + public Boolean requestNotification(String recipientId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(recipientId)) { - throw new PayoutRecipientNotificationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final Map params = new HashMap<>(); params.put("token", this.accessTokens.getAccessToken(Facade.PAYOUT)); JsonMapper mapper = JsonMapperFactory.create(); - Boolean result; - String json; + Boolean result = null; + String json = null; try { json = mapper.writeValueAsString(params); } catch (JsonProcessingException e) { - throw new PayoutRecipientNotificationException(null, - "failed to serialize PayoutRecipient object : " + e.getMessage()); + BitPayExceptionProvider.throwSerializeParamsException(e.getMessage()); } + String jsonResponse = this.bitPayClient.post("recipients/" + recipientId + "/notifications", json, true); + + JsonNode rootNode = null; try { - HttpResponse response = this.bitPayClient.post("recipients/" + recipientId + "/notifications", json, true); - String jsonString = this.bitPayClient.responseToJsonString(response); - JsonNode rootNode = mapper.readTree(jsonString); + rootNode = mapper.readTree(jsonResponse); JsonNode node = rootNode.get("status"); result = node.toString().replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); - } catch (BitPayException ex) { - throw new PayoutRecipientNotificationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (Exception e) { - throw new PayoutRecipientNotificationException(null, - "failed to deserialize BitPay server response (PayoutRecipients) : " + e.getMessage()); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeException(e.getMessage()); } return result; diff --git a/src/main/java/com/bitpay/sdk/client/RateClient.java b/src/main/java/com/bitpay/sdk/client/RateClient.java index e620252b..479680ce 100644 --- a/src/main/java/com/bitpay/sdk/client/RateClient.java +++ b/src/main/java/com/bitpay/sdk/client/RateClient.java @@ -5,7 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.RateQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.rate.Rate; import com.bitpay.sdk.model.rate.Rates; import com.bitpay.sdk.util.JsonMapperFactory; @@ -13,7 +15,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.http.HttpResponse; /** * The type Rate client. @@ -53,48 +54,46 @@ public static RateClient getInstance(BitPayClient bitPayClient) { * Current supported values are BTC and BCH. * @param currency the fiat currency for which you want to fetch the baseCurrency rates. * @return A Rates object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.8.0 * @see Exchange rates */ public Rate get( String baseCurrency, String currency - ) throws RateQueryException { + ) throws BitPayGenericException, BitPayApiException { + String jsonResponse = this.bitPayClient.get("rates/" + baseCurrency + "/" + currency); + + Rate rate = null; + try { - HttpResponse response = this.bitPayClient.get("rates/" + baseCurrency + "/" + currency); - final String content = this.bitPayClient.responseToJsonString(response); - return JsonMapperFactory.create().readValue(content, Rate.class); + rate = JsonMapperFactory.create().readValue(jsonResponse, Rate.class); } catch (JsonProcessingException e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); - } catch (Exception e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Rate", e.getMessage()); } + + return rate; } /** * Retrieve the exchange rate table maintained by BitPay. * * @return A Rates object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @see Exchange rates */ - public Rates getRates() throws RateQueryException { - List rates; + public Rates getRates() throws BitPayGenericException, BitPayApiException { + List rates = null; + String response = this.bitPayClient.get("rates"); try { - HttpResponse response = this.bitPayClient.get("rates"); rates = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Rate[].class) + JsonMapperFactory.create().readValue(response, Rate[].class) ); } catch (JsonProcessingException e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); - } catch (Exception e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Rates", e.getMessage()); } return new Rates(rates); @@ -106,24 +105,21 @@ public Rates getRates() throws RateQueryException { * @param baseCurrency the cryptocurrency for which you want to fetch the rates. * Current supported values are BTC and BCH. * @return A Rates object populated with the BitPay exchange rate table. - * @throws RateQueryException RateQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.8.0 * @see Exchange rates */ - public Rates getRates(String baseCurrency) throws RateQueryException { - List rates; + public Rates getRates(String baseCurrency) throws BitPayGenericException, BitPayApiException { + List rates = null; + String response = this.bitPayClient.get("rates/" + baseCurrency); try { - HttpResponse response = this.bitPayClient.get("rates/" + baseCurrency); rates = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Rate[].class) + JsonMapperFactory.create().readValue(response, Rate[].class) ); } catch (JsonProcessingException e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); - } catch (Exception e) { - throw new RateQueryException(null, - "failed to deserialize BitPay server response (Rates) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Rates", e.getMessage()); } return new Rates(rates); diff --git a/src/main/java/com/bitpay/sdk/client/RefundClient.java b/src/main/java/com/bitpay/sdk/client/RefundClient.java index 5c71dd39..77e7899f 100644 --- a/src/main/java/com/bitpay/sdk/client/RefundClient.java +++ b/src/main/java/com/bitpay/sdk/client/RefundClient.java @@ -5,11 +5,10 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.RefundCancellationException; -import com.bitpay.sdk.exceptions.RefundCreationException; -import com.bitpay.sdk.exceptions.RefundQueryException; -import com.bitpay.sdk.exceptions.RefundUpdateException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.exceptions.BitPayValidationException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.invoice.Refund; import com.bitpay.sdk.util.GuidGenerator; @@ -26,7 +25,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -81,33 +79,32 @@ public static RefundClient getInstance( * * @param refund Refund request data * @return Refund - * @throws BitPayException BitPayException +` * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class` */ - public Refund create(final Refund refund) throws BitPayException { + public Refund create(final Refund refund) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(refund)) { - throw new RefundCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final Map params = createBasicParamsForCreate(refund); - final Refund result; final JsonMapper mapper = JsonMapperFactory.create(); - final String json; + Refund result = null; + String json = null; try { json = mapper.writeValueAsString(params); } catch (final JsonProcessingException e) { - throw new RefundCreationException(null, "failed to serialize Refund object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + final String jsonResponse = this.bitPayClient.post("refunds/", json, true); + try { - final HttpResponse response = this.bitPayClient.post("refunds/", json, true); result = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); - } catch (final BitPayException ex) { - throw new RefundCreationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundCreationException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + .readValue(jsonResponse, Refund.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return result; @@ -118,29 +115,24 @@ public Refund create(final Refund refund) throws BitPayException { * * @param refundId The BitPay refund ID. * @return A BitPay Refund object with the associated Refund object. - * @throws RefundQueryException RefundQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Refund getById(final String refundId) throws RefundQueryException, BitPayException { + public Refund getById(final String refundId) throws BitPayApiException, BitPayGenericException { validateRefundId(refundId); - final Refund refund; + Refund refund = null; final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); + final String jsonResponse = this.bitPayClient.get("refunds/" + refundId, params, true); + try { - final HttpResponse response = this.bitPayClient.get("refunds/" + refundId, params, true); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); + .readValue(jsonResponse, Refund.class); } catch (final JsonProcessingException e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); - } catch (final BitPayException ex) { - throw new RefundQueryException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; @@ -151,28 +143,23 @@ public Refund getById(final String refundId) throws RefundQueryException, BitPay * * @param guid Guid * @return Refund - * @throws BitPayException BitPayException + * @throws BitPayApiException BitPayException */ - public Refund getByGuid(final String guid) throws BitPayException { + public Refund getByGuid(final String guid) throws BitPayApiException, BitPayGenericException { validateRefundId(guid); - final Refund refund; + Refund refund = null; final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); + final String jsonResponse = this.bitPayClient.get("refunds/guid/" + guid, params, true); + try { - final HttpResponse response = this.bitPayClient.get("refunds/guid/" + guid, params, true); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); + .readValue(jsonResponse, Refund.class); } catch (final JsonProcessingException e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); - } catch (final BitPayException ex) { - throw new RefundQueryException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; @@ -183,30 +170,26 @@ public Refund getByGuid(final String guid) throws BitPayException { * * @param invoiceId The BitPay invoice object having the associated refunds. * @return A list of BitPay Refund objects with the associated Refund objects. - * @throws RefundQueryException RefundQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public List getRefundsByInvoiceId(final String invoiceId) throws RefundQueryException, BitPayException { + public List getRefundsByInvoiceId(final String invoiceId) + throws BitPayApiException, BitPayGenericException { validateRefundId(invoiceId); - final List refunds; + List refunds = null; final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); ParameterAdder.execute(params, "invoiceId", invoiceId); + String jsonResponse = this.bitPayClient.get("refunds/", params, true); + try { - final HttpResponse response = this.bitPayClient.get("refunds/", params, true); refunds = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Refund[].class) + JsonMapperFactory.create().readValue(jsonResponse, Refund[].class) ); } catch (final JsonProcessingException e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); - } catch (final BitPayException ex) { - throw new RefundQueryException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundQueryException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refunds; @@ -218,30 +201,28 @@ public List getRefundsByInvoiceId(final String invoiceId) throws RefundQ * @param refundId A BitPay refund ID. * @param status The new status for the refund to be updated. * @return A BitPay generated Refund object. - * @throws RefundUpdateException RefundUpdateException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ public Refund update( final String refundId, final String status - ) throws RefundUpdateException, BitPayException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(refundId) || Objects.isNull(status)) { - throw new RefundUpdateException(null, + BitPayExceptionProvider.throwGenericExceptionWithMessage( "Updating the refund requires a refund ID and a new status to be set."); } final String json = getUpdateRefundJson(status); - final Refund refund; + Refund refund = null; + + final String jsonResponse = this.bitPayClient.update("refunds/" + refundId, json); try { - final HttpResponse response = this.bitPayClient.update("refunds/" + refundId, json); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); - } catch (final BitPayException ex) { - throw new RefundUpdateException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundUpdateException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + .readValue(jsonResponse, Refund.class); + } catch (final JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; @@ -253,31 +234,29 @@ public Refund update( * @param guid A BitPay refund Guid. * @param status The new status for the refund to be updated. * @return A BitPay generated Refund object. - * @throws RefundUpdateException RefundUpdateException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 */ public Refund updateByGuid( final String guid, final String status - ) throws RefundUpdateException, BitPayException { + ) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(guid) || Objects.isNull(status)) { - throw new RefundUpdateException(null, + BitPayExceptionProvider.throwGenericExceptionWithMessage( "Updating the refund requires a refund ID and a new status to be set."); } final String json = getUpdateRefundJson(status); - final Refund refund; + Refund refund = null; + + String jsonResponse = this.bitPayClient.update("refunds/guid/" + guid, json); try { - final HttpResponse response = this.bitPayClient.update("refunds/guid/" + guid, json); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); - } catch (final BitPayException ex) { - throw new RefundUpdateException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundUpdateException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + .readValue(jsonResponse, Refund.class); + } catch (final JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; @@ -288,38 +267,35 @@ public Refund updateByGuid( * * @param refundId A BitPay refund ID. * @return An updated Refund Object - * @throws RefundCreationException RefundCreationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Boolean sendRefundNotification(final String refundId) throws RefundCreationException, BitPayException { + public Boolean sendRefundNotification(final String refundId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(refundId)) { - throw new RefundCreationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } final Map params = new HashMap<>(); params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT)); final JsonMapper mapper = JsonMapperFactory.create(); - final Boolean result; - final String json; + String json = null; + Boolean result = null; try { json = mapper.writeValueAsString(params); } catch (final JsonProcessingException e) { - throw new RefundCreationException(null, "failed to serialize Refund object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } + String jsonResponse = this.bitPayClient.post("refunds/" + refundId + "/notifications", json, true); + try { - final HttpResponse response = this.bitPayClient.post("refunds/" + refundId + "/notifications", json, true); - final String jsonString = this.bitPayClient.responseToJsonString(response); - final JsonNode rootNode = mapper.readTree(jsonString); + final JsonNode rootNode = mapper.readTree(jsonResponse); final JsonNode node = rootNode.get("status"); result = "success".equals(node.toString().replace("\"", "").toLowerCase(Locale.ROOT)); - } catch (final BitPayException ex) { - throw new RefundCreationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundCreationException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeException(e.getMessage()); } return result; @@ -330,28 +306,26 @@ public Boolean sendRefundNotification(final String refundId) throws RefundCreati * * @param refundId The refund Id for the refund to be canceled. * @return An updated Refund Object. - * @throws RefundCancellationException RefundCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public Refund cancel(final String refundId) throws RefundCancellationException, BitPayException { + public Refund cancel(final String refundId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(refundId)) { - throw new RefundCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } - final Refund refund; + Refund refund = null; final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); + String jsonResponse = this.bitPayClient.delete("refunds/" + refundId, params); + try { - final HttpResponse response = this.bitPayClient.delete("refunds/" + refundId, params); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); - } catch (final BitPayException ex) { - throw new RefundCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundCancellationException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + .readValue(jsonResponse, Refund.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; @@ -362,35 +336,33 @@ public Refund cancel(final String refundId) throws RefundCancellationException, * * @param guid The refund Guid for the refund to be canceled. * @return An updated Refund Object. - * @throws RefundCancellationException RefundCancellationException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class * @since 8.7.0 */ - public Refund cancelByGuid(final String guid) throws RefundCancellationException, BitPayException { + public Refund cancelByGuid(final String guid) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(guid)) { - throw new RefundCancellationException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } - final Refund refund; + Refund refund = null; final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); + String jsonResponse = this.bitPayClient.delete("refunds/guid/" + guid, params); + try { - final HttpResponse response = this.bitPayClient.delete("refunds/guid/" + guid, params); refund = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Refund.class); - } catch (final BitPayException ex) { - throw new RefundCancellationException(ex.getStatusCode(), ex.getReasonPhrase()); - } catch (final Exception e) { - throw new RefundCancellationException(null, - "failed to deserialize BitPay server response (Refund) : " + e.getMessage()); + .readValue(jsonResponse, Refund.class); + } catch (JsonProcessingException e) { + BitPayExceptionProvider.throwDeserializeResourceException("Refund", e.getMessage()); } return refund; } - private String getUpdateRefundJson(final String status) throws BitPayException { + private String getUpdateRefundJson(final String status) throws BitPayGenericException { final Map params = new HashMap<>(); params.put("token", this.accessTokens.getAccessToken(Facade.MERCHANT)); @@ -399,18 +371,18 @@ private String getUpdateRefundJson(final String status) throws BitPayException { } final JsonMapper mapper = JsonMapperFactory.create(); - final String json; + String json = null; try { json = mapper.writeValueAsString(params); } catch (final JsonProcessingException e) { - throw new RefundUpdateException(null, "failed to serialize object : " + e.getMessage()); + BitPayExceptionProvider.throwEncodeException(e.getMessage()); } return json; } - private Map createBasicParamsForCreate(final Refund refund) throws BitPayException { + private Map createBasicParamsForCreate(final Refund refund) throws BitPayGenericException { final String guid = Objects.isNull(refund.getGuid()) ? this.guidGenerator.execute() : refund.getGuid(); final String invoiceId = refund.getInvoice(); final Double amount = refund.getAmount(); @@ -420,7 +392,8 @@ private Map createBasicParamsForCreate(final Refund refund) thro final String reference = refund.getReference(); if (invoiceId == null && amount == null) { - throw new RefundCreationException(null, "Invoice ID, amount and currency are required to issue a refund."); + BitPayExceptionProvider.throwValidationException( + "Invoice ID, amount and currency are required to issue a refund."); } final Map params = new HashMap<>(); @@ -448,9 +421,9 @@ private Map createBasicParamsForCreate(final Refund refund) thro return params; } - private void validateRefundId(final String refundId) throws RefundQueryException { + private void validateRefundId(final String refundId) throws BitPayValidationException { if (Objects.isNull(refundId)) { - throw new RefundQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } } } diff --git a/src/main/java/com/bitpay/sdk/client/ResponseParser.java b/src/main/java/com/bitpay/sdk/client/ResponseParser.java new file mode 100644 index 00000000..39878645 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/client/ResponseParser.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.client; + +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.util.JsonMapperFactory; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.json.JsonMapper; +import java.io.IOException; + +public class ResponseParser { + + /** + * Convert HttpResponse for Json string. + * + * @param jsonResponse the response + * @return the string + * @throws BitPayApiException the bit pay exception + */ + public static String getJsonDataFromJsonResponse(final String jsonResponse) throws BitPayApiException { + if (jsonResponse == null) { + BitPayExceptionProvider.throwApiExceptionWithMessage("HTTP response is null"); + } + + String jsonString = null; + + try { + final JsonMapper mapper = JsonMapperFactory.create(); + final JsonNode rootNode = mapper.readTree(jsonResponse); + JsonNode node = rootNode.get("status"); + if (node != null) { + final String status = node.toString().replace("\"", ""); + if ("error".equals(status)) { + BitPayExceptionProvider.throwApiExceptionWithMessage( + rootNode.get("message").textValue(), + getCode(rootNode) + ); + } + + if ("success".equals(status)) { + node = rootNode.get("data"); + + if ("{}".equals(node.toString())) { + return rootNode.toString(); + } + } + } + + handleError(rootNode); + + node = rootNode.get("data"); + if (node != null) { + return node.toString(); + } + + return jsonResponse; + } catch (final IOException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + } + + return jsonString; + } + + private static void handleError(JsonNode rootNode) throws BitPayApiException { + if (!rootNode.has("error")) { + return; + } + + BitPayExceptionProvider.throwApiExceptionWithMessage( + rootNode.get("error").textValue(), + getCode(rootNode) + ); + } + + private static String getCode(JsonNode rootNode) { + if (rootNode.has("code")) { + return rootNode.get("code").textValue(); + } + + return null; + } +} diff --git a/src/main/java/com/bitpay/sdk/client/SettlementClient.java b/src/main/java/com/bitpay/sdk/client/SettlementClient.java index 9e4857b5..fa13fb61 100644 --- a/src/main/java/com/bitpay/sdk/client/SettlementClient.java +++ b/src/main/java/com/bitpay/sdk/client/SettlementClient.java @@ -5,8 +5,9 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.SettlementQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.settlement.Settlement; import com.bitpay.sdk.util.JsonMapperFactory; @@ -18,7 +19,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.http.HttpResponse; import org.apache.http.message.BasicNameValuePair; /** @@ -74,8 +74,8 @@ public static SettlementClient getInstance( * @param limit Maximum number of settlements to retrieve. * @param offset Offset for paging. * @return A list of BitPay Settlement objects. - * @throws BitPayException BitPayException class - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ public List getSettlements( String currency, @@ -84,11 +84,7 @@ public List getSettlements( String status, Integer limit, Integer offset - ) throws BitPayException, - SettlementQueryException { - limit = limit != null ? limit : 100; - offset = offset != null ? offset : 0; - + ) throws BitPayApiException, BitPayGenericException { final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); ParameterAdder.execute(params, "startDate", dateStart); @@ -102,19 +98,16 @@ public List getSettlements( ParameterAdder.execute(params, "offset", offset.toString()); } - List settlements; + List settlements = null; + + String jsonResponse = this.bitPayClient.get("settlements", params); try { - HttpResponse response = this.bitPayClient.get("settlements", params); settlements = Arrays.asList(JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Settlement[].class)); + .readValue(jsonResponse, Settlement[].class)); } catch (JsonProcessingException e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (Settlement) : " + e.getMessage()); - } catch (Exception e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (Settlement) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Settlement", e.getMessage()); } return settlements; @@ -125,30 +118,27 @@ public List getSettlements( * * @param settlementId Settlement Id. * @return A BitPay Settlement object. - * @throws BitPayException BitPayException class - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayApiException BitPayApiException class + * @throws BitPayGenericException BitPayGenericException class */ - public Settlement get(String settlementId) throws BitPayException, SettlementQueryException { + public Settlement get(String settlementId) throws BitPayApiException, BitPayGenericException { if (Objects.isNull(settlementId)) { - throw new SettlementQueryException(null, "missing required parameter"); + BitPayExceptionProvider.throwMissingParameterException(); } - Settlement settlement; + Settlement settlement = null; String token = this.accessTokens.getAccessToken(Facade.MERCHANT); final ObjectMapper objectMapper = JsonMapperFactory.create(); final List params = new ArrayList(); ParameterAdder.execute(params, "token", token); + String response = this.bitPayClient.get("settlements/" + settlementId, params); + try { - HttpResponse response = this.bitPayClient.get("settlements/" + settlementId, params); settlement = - objectMapper.readValue(this.bitPayClient.responseToJsonString(response), Settlement.class); + objectMapper.readValue(response, Settlement.class); } catch (JsonProcessingException e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (Settlement) : " + e.getMessage()); - } catch (Exception e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (Settlement) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Settlement", e.getMessage()); } return settlement; @@ -160,32 +150,29 @@ public Settlement get(String settlementId) throws BitPayException, SettlementQue * @param settlementId Settlement ID. * @param token Settlement token. * @return A detailed BitPay Settlement object. - * @throws SettlementQueryException SettlementQueryException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ public Settlement getSettlementReconciliationReport( String settlementId, String token - ) throws SettlementQueryException { + ) throws BitPayGenericException, BitPayApiException { final List params = new ArrayList(); if (Objects.isNull(settlementId) || Objects.isNull(token)) { - throw new SettlementQueryException(null, "missing id/token"); + BitPayExceptionProvider.throwMissingParameterException(); } ParameterAdder.execute(params, "token", token); - Settlement reconciliationReport; + Settlement reconciliationReport = null; + + String jsonResponse = this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params); try { - HttpResponse response = - this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params); reconciliationReport = JsonMapperFactory.create() - .readValue(this.bitPayClient.responseToJsonString(response), Settlement.class); + .readValue(jsonResponse, Settlement.class); } catch (JsonProcessingException e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (ReconciliationReport) : " + e.getMessage()); - } catch (Exception e) { - throw new SettlementQueryException(null, - "failed to deserialize BitPay server response (ReconciliationReport) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Settlement", e.getMessage()); } return reconciliationReport; diff --git a/src/main/java/com/bitpay/sdk/client/WalletClient.java b/src/main/java/com/bitpay/sdk/client/WalletClient.java index bd7b01c3..e3bb5d83 100644 --- a/src/main/java/com/bitpay/sdk/client/WalletClient.java +++ b/src/main/java/com/bitpay/sdk/client/WalletClient.java @@ -5,15 +5,15 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.WalletQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.wallet.Wallet; import com.bitpay.sdk.util.JsonMapperFactory; import com.fasterxml.jackson.core.JsonProcessingException; import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.http.HttpResponse; /** * The type Wallet client. @@ -50,23 +50,20 @@ public static WalletClient getInstance(BitPayClient bitPayClient) { * Retrieve all supported wallets. * * @return A list of wallet objets. - * @throws WalletQueryException WalletQueryException class - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public List getSupportedWallets() throws WalletQueryException, BitPayException { - List wallets; + public List getSupportedWallets() throws BitPayApiException, BitPayGenericException { + List wallets = null; + + String jsonResponse = this.bitPayClient.get("supportedwallets"); try { - HttpResponse response = this.bitPayClient.get("supportedwallets"); wallets = Arrays.asList( - JsonMapperFactory.create().readValue(this.bitPayClient.responseToJsonString(response), Wallet[].class) + JsonMapperFactory.create().readValue(jsonResponse, Wallet[].class) ); } catch (JsonProcessingException e) { - throw new WalletQueryException(null, - "failed to deserialize BitPay server response (Wallet) : " + e.getMessage()); - } catch (Exception e) { - throw new WalletQueryException(null, - "failed to deserialize BitPay server response (Wallet) : " + e.getMessage()); + BitPayExceptionProvider.throwDeserializeResourceException("Wallet", e.getMessage()); } return wallets; diff --git a/src/main/java/com/bitpay/sdk/exceptions/BillCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/BillCreationException.java deleted file mode 100644 index ee0feef2..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/BillCreationException.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Bill cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the Bill: 00

- *

Error digits for the Bill:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class BillCreationException extends BillException { - /** - * Construct the BillCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public BillCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create bill"; - String bitPayCode = "BITPAY-BILL-CREATE"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BillDeliveryException.java b/src/main/java/com/bitpay/sdk/exceptions/BillDeliveryException.java deleted file mode 100644 index 6fa2a352..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/BillDeliveryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Bill cannot be delivered. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the Bill: 00

- *

Error digits for the Bill:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010002
- * 
- * - * @see Rest API Error Codes - */ -public class BillDeliveryException extends BillException { - /** - * Construct the BillDeliveryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public BillDeliveryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to deliver bill"; - String bitPayCode = "BITPAY-BILL-DELIVERY"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BillException.java b/src/main/java/com/bitpay/sdk/exceptions/BillException.java deleted file mode 100644 index 732cb30c..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/BillException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Bill. - * - * @see com.bitpay.sdk.exceptions.BillCreationException - * @see com.bitpay.sdk.exceptions.BillQueryException - * @see com.bitpay.sdk.exceptions.BillDeliveryException - * @see com.bitpay.sdk.exceptions.BillUpdateException - * - * @see Rest API Error Codes - */ -public class BillException extends BitPayException { - /** - * Construct the BillException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public BillException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the bill"; - String bitPayCode = "BITPAY-BILL-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BillQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/BillQueryException.java deleted file mode 100644 index 29c23b31..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/BillQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Bill cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the Bill: 00

- *

Error digits for the Bill:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020002
- * 
- * - * @see Rest API Error Codes - */ -public class BillQueryException extends BillException { - /** - * Construct the BillQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public BillQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve bill"; - String bitPayCode = "BITPAY-BILL-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BillUpdateException.java b/src/main/java/com/bitpay/sdk/exceptions/BillUpdateException.java deleted file mode 100644 index 0188358a..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/BillUpdateException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a PUT when the Bill cannot be updated. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 03

- *

Resource digits for the Bill: 00

- *

Error digits for the Bill:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 030002
- * 
- * - * @see Rest API Error Codes - */ -public class BillUpdateException extends BillException { - /** - * Construct the BillUpdateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public BillUpdateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to update bill"; - String bitPayCode = "BITPAY-BILL-UPDATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BitPayApiException.java b/src/main/java/com/bitpay/sdk/exceptions/BitPayApiException.java new file mode 100644 index 00000000..e5f14552 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/exceptions/BitPayApiException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.exceptions; + +public class BitPayApiException extends BitPayException { + + private static final long serialVersionUID = 8211452784073681895L; + + private final String code; + + public BitPayApiException(String message, String code) { + super(message); + this.code = code; + } + + /** + *

An error code consists of 6 digits.

+ *

The first two digits of an error code represent the HTTP method that was used to call it.

+ *

The next two digits refer to the resource that was impacted.

+ *

The last two digits refer to the specific error.

+ *

eg. 010103 - Missing parameters for Invoice POST request.

+ * + * @return String + */ + public String getCode() { + return this.code; + } +} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BitPayException.java b/src/main/java/com/bitpay/sdk/exceptions/BitPayException.java index 38fb25bf..eae1473e 100644 --- a/src/main/java/com/bitpay/sdk/exceptions/BitPayException.java +++ b/src/main/java/com/bitpay/sdk/exceptions/BitPayException.java @@ -5,82 +5,21 @@ package com.bitpay.sdk.exceptions; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.util.TextUtils; - /** * General BitPay Exception which is inherited by all other exceptions. * * @see Rest API Error Codes */ -public class BitPayException extends ClientProtocolException { - private static final long serialVersionUID = -7186627969477257933L; - - /** - * String [optional] The Exception code to throw. - */ - private final String statusCode; +public class BitPayException extends Exception { - /** - * String [optional] The Exception message to throw. - */ - private final String reasonPhrase; + private static final long serialVersionUID = -5407556346434827903L; /** * Construct the BitPayException. * - * @param statusCode String [optional] The Exception code to throw. - * @param reasonPhrase String [optional] The Exception message to throw. - */ - public BitPayException( - String statusCode, - String reasonPhrase - ) { - super(String.format("Status: %s" + (TextUtils.isBlank(reasonPhrase) ? "" : " -> Reason: %s"), - buildStatus(statusCode), buildMessage(reasonPhrase))); - this.statusCode = statusCode; - this.reasonPhrase = reasonPhrase; - } - - private static String buildMessage(String message) { - String bitPayMessage = "Unexpected Bitpay exeption."; - String bitPayCode = "BITPAY-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } - - private static String buildStatus(String status) { - if (status == null) { - status = "000000"; - } - - return status; - } - - /** - *

An error code consists of 6 digits.

- *

The first two digits of an error code represent the HTTP method that was used to call it.

- *

The next two digits refer to the resource that was impacted.

- *

The last two digits refer to the specific error.

- *

eg. 010103 - Missing parameters for Invoice POST request.

- * - * @return String - */ - public String getStatusCode() { - return this.statusCode; - } - - /** - *

Reason phrase including BitPay Code and BitPay Message.

- *

eg. BITPAY-BILL-UPDATE: Failed to update bill -> failed to deserialize BitPay server response (Bill)

- * - * @return String + * @param message String [optional] The Exception message. */ - public String getReasonPhrase() { - return this.reasonPhrase; + public BitPayException(String message) { + super(message); } } diff --git a/src/main/java/com/bitpay/sdk/exceptions/BitPayExceptionProvider.java b/src/main/java/com/bitpay/sdk/exceptions/BitPayExceptionProvider.java new file mode 100644 index 00000000..b8565e22 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/exceptions/BitPayExceptionProvider.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.exceptions; + +import com.bitpay.sdk.logger.LoggerProvider; +import java.util.Objects; + +public class BitPayExceptionProvider { + + public static final String GENERIC_API_UNMAPPED_ERROR_CODE = "000000"; + + /** + * Throw Generic (Non-Api) Exception. + * + * @param message error message + * @throws BitPayGenericException BitPayGenericException class + */ + public static void throwGenericExceptionWithMessage(String message) throws BitPayGenericException { + logErrorMessage(message); + + throw new BitPayGenericException(message); + } + + /** + * Throw API Exception. + * + * @param message error message + * @throws BitPayApiException BitPayApiException class + */ + public static void throwApiExceptionWithMessage(String message) throws BitPayApiException { + logErrorMessage(message); + + throw new BitPayApiException(message, GENERIC_API_UNMAPPED_ERROR_CODE); + } + + /** + * Throw API Exception with specific message and code. + * + * @param message error message + * @param code BitPay error code + * @throws BitPayApiException BitPayApiException + */ + public static void throwApiExceptionWithMessage(String message, String code) throws BitPayApiException { + logErrorMessage(message); + + throw new BitPayApiException(message, code); + } + + public static void throwValidationException(String message) throws BitPayValidationException { + logErrorMessage(message); + + throw new BitPayValidationException(message); + } + + public static void throwMissingParameterException() throws BitPayValidationException { + final String message = "Missing required parameter"; + + logErrorMessage(message); + + throw new BitPayValidationException(message); + } + + public static void throwDeserializeResourceException(String resource, String exceptionMessage) + throws BitPayGenericException { + String message = String.format( + "Failed to deserialize BitPay server response ( %s ) : " + exceptionMessage, resource); + BitPayExceptionProvider.throwGenericExceptionWithMessage(message); + } + + public static void throwDeserializeException(String exceptionMessage) + throws BitPayGenericException { + String message = "Failed to deserialize BitPay server response : " + exceptionMessage; + BitPayExceptionProvider.throwGenericExceptionWithMessage(message); + } + + public static void throwEncodeException(String exceptionMessage) + throws BitPayGenericException { + String message = "Failed to encode params : " + exceptionMessage; + BitPayExceptionProvider.throwGenericExceptionWithMessage(message); + } + + public static void throwSerializeResourceException(String resource, String exceptionMessage) + throws BitPayGenericException { + String message = String.format("Failed to serialize ( %s ) : " + exceptionMessage, resource); + BitPayExceptionProvider.throwGenericExceptionWithMessage(message); + } + + public static void throwSerializeParamsException(String exceptionMessage) throws BitPayGenericException { + String message = "Failed to serialize params : " + exceptionMessage; + BitPayExceptionProvider.throwGenericExceptionWithMessage(message); + } + + public static void throwInvalidCurrencyException(String currencyCode) throws BitPayValidationException { + String message = String.format("Currency code %s must be a type of Model.Currency", currencyCode); + BitPayExceptionProvider.throwValidationException(message); + } + + private static void logErrorMessage(String message) { + if (Objects.nonNull(message)) { + LoggerProvider.getLogger().logError(message); + } + } +} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BitPayGenericException.java b/src/main/java/com/bitpay/sdk/exceptions/BitPayGenericException.java new file mode 100644 index 00000000..a714d52e --- /dev/null +++ b/src/main/java/com/bitpay/sdk/exceptions/BitPayGenericException.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.exceptions; + +public class BitPayGenericException extends BitPayException { + + private static final long serialVersionUID = -4496707441469582396L; + + public BitPayGenericException(String message) { + super(message); + } +} diff --git a/src/main/java/com/bitpay/sdk/exceptions/BitPayValidationException.java b/src/main/java/com/bitpay/sdk/exceptions/BitPayValidationException.java new file mode 100644 index 00000000..d16946d3 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/exceptions/BitPayValidationException.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.exceptions; + +public class BitPayValidationException extends BitPayGenericException { + + private static final long serialVersionUID = -6721535639566485766L; + + public BitPayValidationException(String message) { + super(message); + } +} diff --git a/src/main/java/com/bitpay/sdk/exceptions/CurrencyException.java b/src/main/java/com/bitpay/sdk/exceptions/CurrencyException.java deleted file mode 100644 index 33008f80..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/CurrencyException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Currency. - * - * @see com.bitpay.sdk.exceptions.CurrencyQueryException - * @see Rest API Error Codes - */ -public class CurrencyException extends BitPayException { - /** - * Construct the CurrencyException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public CurrencyException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the currencies"; - String bitPayCode = "BITPAY-CURRENCY-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/CurrencyQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/CurrencyQueryException.java deleted file mode 100644 index 5b2a667a..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/CurrencyQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Currency cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class CurrencyQueryException extends CurrencyException { - /** - * Construct the CurrencyQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public CurrencyQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve currencies"; - String bitPayCode = "BITPAY-CURRENCY-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/InvoiceCancellationException.java b/src/main/java/com/bitpay/sdk/exceptions/InvoiceCancellationException.java deleted file mode 100644 index 07c103fb..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/InvoiceCancellationException.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a DELETE when the Invoice cannot be cancelled. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 04

- *

Resource digits for the Invoice: 01

- *

Error digits for the Invoice:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 08 - Invoice is missing email or SMS
  • - *
  • 09 - SMS not verified
  • - *
  • 10 - Invoice price is below minimum threshold
  • - *
  • 11 - Invoice price is above maximum threshold
  • - *
  • 12 - Invalid SMS number
  • - *
  • 13 - Error verifying SMS
  • - *
  • 14 - Unable to update contact information on high value transaction
  • - *
  • 15 - Email already set on invoice
  • - *
  • 16 - Unable to perform action outside of demo environment
  • - *
  • 17 - Invalid invoice state
  • - *
  • 18 - Misconfigured account
  • - *
- *
- * eg 040101
- * 
- * - * @see Rest API Error Codes - */ -public class InvoiceCancellationException extends InvoiceException { - /** - * Construct the InvoiceCancellationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public InvoiceCancellationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to cancel invoice"; - String bitPayCode = "BITPAY-INVOICE-CANCEL"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/InvoiceCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/InvoiceCreationException.java deleted file mode 100644 index e9110d97..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/InvoiceCreationException.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Invoice cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the Invoice: 01

- *

Error digits for the Invoice:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 08 - Invoice is missing email or SMS
  • - *
  • 09 - SMS not verified
  • - *
  • 10 - Invoice price is below minimum threshold
  • - *
  • 11 - Invoice price is above maximum threshold
  • - *
  • 12 - Invalid SMS number
  • - *
  • 13 - Error verifying SMS
  • - *
  • 14 - Unable to update contact information on high value transaction
  • - *
  • 15 - Email already set on invoice
  • - *
  • 16 - Unable to perform action outside of demo environment
  • - *
  • 17 - Invalid invoice state
  • - *
  • 18 - Misconfigured account
  • - *
- *
- * eg 010101
- * 
- * - * @see Rest API Error Codes - */ -public class InvoiceCreationException extends InvoiceException { - /** - * Construct the InvoiceCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public InvoiceCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create invoice"; - String bitPayCode = "BITPAY-INVOICE-CREATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/InvoiceException.java b/src/main/java/com/bitpay/sdk/exceptions/InvoiceException.java deleted file mode 100644 index 7b89baa3..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/InvoiceException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Invoice. - * - * @see com.bitpay.sdk.exceptions.InvoiceCancellationException - * @see com.bitpay.sdk.exceptions.InvoiceCreationException - * @see com.bitpay.sdk.exceptions.InvoiceQueryException - * @see com.bitpay.sdk.exceptions.InvoiceUpdateException - * - * @see Rest API Error Codes - */ -public class InvoiceException extends BitPayException { - /** - * Construct the InvoiceException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public InvoiceException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the invoice"; - String bitPayCode = "BITPAY-INVOICE-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/InvoiceQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/InvoiceQueryException.java deleted file mode 100644 index f4d252b0..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/InvoiceQueryException.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Invoice cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the Invoice: 01

- *

Error digits for the Invoice:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 08 - Invoice is missing email or SMS
  • - *
  • 09 - SMS not verified
  • - *
  • 10 - Invoice price is below minimum threshold
  • - *
  • 11 - Invoice price is above maximum threshold
  • - *
  • 12 - Invalid SMS number
  • - *
  • 13 - Error verifying SMS
  • - *
  • 14 - Unable to update contact information on high value transaction
  • - *
  • 15 - Email already set on invoice
  • - *
  • 16 - Unable to perform action outside of demo environment
  • - *
  • 17 - Invalid invoice state
  • - *
  • 18 - Misconfigured account
  • - *
- *
- * eg 040101
- * 
- * - * @see Rest API Error Codes - */ -public class InvoiceQueryException extends InvoiceException { - /** - * Construct the InvoiceQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public InvoiceQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve invoice"; - String bitPayCode = "BITPAY-INVOICE-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/InvoiceUpdateException.java b/src/main/java/com/bitpay/sdk/exceptions/InvoiceUpdateException.java deleted file mode 100644 index d53f48a8..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/InvoiceUpdateException.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a PUT when the Invoice cannot be updated. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 03

- *

Resource digits for the Invoice: 01

- *

Error digits for the Invoice:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 08 - Invoice is missing email or SMS
  • - *
  • 09 - SMS not verified
  • - *
  • 10 - Invoice price is below minimum threshold
  • - *
  • 11 - Invoice price is above maximum threshold
  • - *
  • 12 - Invalid SMS number
  • - *
  • 13 - Error verifying SMS
  • - *
  • 14 - Unable to update contact information on high value transaction
  • - *
  • 15 - Email already set on invoice
  • - *
  • 16 - Unable to perform action outside of demo environment
  • - *
  • 17 - Invalid invoice state
  • - *
  • 18 - Misconfigured account
  • - *
- *
- * eg 030101
- * 
- * - * @see Rest API Error Codes - */ -public class InvoiceUpdateException extends InvoiceException { - /** - * Construct the InvoiceUpdateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public InvoiceUpdateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to update invoice"; - String bitPayCode = "BITPAY-INVOICE-UPDATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/LedgerException.java b/src/main/java/com/bitpay/sdk/exceptions/LedgerException.java deleted file mode 100644 index a39bbccf..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/LedgerException.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Ledger. - * - * @see com.bitpay.sdk.exceptions.LedgerQueryException - * - * @see Rest API Error Codes - */ -public class LedgerException extends BitPayException { - /** - * Construct the LedgerException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public LedgerException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the ledger"; - String bitPayCode = "BITPAY-LEDGER-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/LedgerQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/LedgerQueryException.java deleted file mode 100644 index 7c5bfa42..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/LedgerQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Ledger cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the Ledger: 00

- *

Error digits for the Ledger:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class LedgerQueryException extends LedgerException { - /** - * Construct the LedgerQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public LedgerQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve ledger"; - String bitPayCode = "BITPAY-LEDGER-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationException.java deleted file mode 100644 index affb03a9..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a DELETE when the Payout Batch cannot be cancelled. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 04

- *

Resource digits for the Payout Batch: 00

- *

Error digits for the Payout Batch:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 040001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutBatchCancellationException extends PayoutBatchException { - /** - * Construct the PayoutBatchCancellationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutBatchCancellationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to cancel payout batch."; - String bitPayCode = "BITPAY-PAYOUT-BATCH-CANCELLATION"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCreationException.java deleted file mode 100644 index 8e5a37bb..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchCreationException.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout Batch cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the Payout Batch: 00

- *

Error digits for the Payout Batch:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutBatchCreationException extends PayoutBatchException { - /** - * Construct the PayoutBatchCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutBatchCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create payout batch."; - String bitPayCode = "BITPAY-PAYOUT-BATCH-CREATE"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchException.java deleted file mode 100644 index c70f5ee3..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Payout Batch. - * - * @see com.bitpay.sdk.exceptions.PayoutBatchCancellationException - * @see com.bitpay.sdk.exceptions.PayoutBatchCreationException - * @see com.bitpay.sdk.exceptions.PayoutBatchNotificationException - * @see com.bitpay.sdk.exceptions.PayoutBatchQueryException - * - * @see Rest API Error Codes - */ -public class PayoutBatchException extends BitPayException { - /** - * Construct the PayoutBatchException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutBatchException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occured while trying to manage the payout batch."; - String bitPayCode = "BITPAY-PAYOUT-BATCH-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationException.java deleted file mode 100644 index 033ef163..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout Batch Notification cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutBatchNotificationException extends PayoutBatchException { - /** - * Construct the PayoutBatchNotificationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutBatchNotificationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to send payout batch notification."; - String bitPayCode = "BITPAY-PAYOUT-BATCH-NOTIFICATION"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchQueryException.java deleted file mode 100644 index 7b18a2b3..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutBatchQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout Batch cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the Payout Batch: 00

- *

Error digits for the Payout Batch:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutBatchQueryException extends PayoutBatchException { - /** - * Construct the PayoutBatchQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutBatchQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve payout batch."; - String bitPayCode = "BITPAY-PAYOUT-BATCH-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutCancellationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutCancellationException.java deleted file mode 100644 index 8ecf82ba..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutCancellationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a DELETE when the Payout cannot be cancelled. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 04

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 040001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutCancellationException extends PayoutException { - /** - * Construct the PayoutCancellationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutCancellationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to cancel payout."; - String bitPayCode = "BITPAY-PAYOUT-CANCEL"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutCreationException.java deleted file mode 100644 index e3add26f..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutCreationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutCreationException extends PayoutException { - /** - * Construct the PayoutCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create payout."; - String bitPayCode = "BITPAY-PAYOUT-SUBMIT"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutException.java deleted file mode 100644 index 3008819c..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Payout Batch. - * - * @see com.bitpay.sdk.exceptions.PayoutCancellationException - * @see com.bitpay.sdk.exceptions.PayoutCreationException - * @see com.bitpay.sdk.exceptions.PayoutNotificationException - * @see com.bitpay.sdk.exceptions.PayoutQueryException - * - * @see Rest API Error Codes - */ -public class PayoutException extends BitPayException { - /** - * Construct the PayoutException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the payout."; - String bitPayCode = "BITPAY-PAYOUT-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutNotificationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutNotificationException.java deleted file mode 100644 index 91bc18d3..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutNotificationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout Notification cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutNotificationException extends PayoutException { - /** - * Construct the PayoutNotificationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutNotificationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to send payout notification."; - String bitPayCode = "BITPAY-PAYOUT-NOTIFICATION"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutQueryException.java deleted file mode 100644 index f9127b87..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Payout cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutQueryException extends PayoutException { - /** - * Construct the PayoutQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve payout."; - String bitPayCode = "BITPAY-PAYOUT-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationException.java deleted file mode 100644 index c5c6cd7d..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Payout Recipient cannot be cancelled. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 04

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 040001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutRecipientCancellationException extends PayoutRecipientException { - /** - * Construct the PayoutRecipientCancellationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientCancellationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to cancel payout recipient"; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-CANCELLATION"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationException.java deleted file mode 100644 index c60815bd..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationException.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Payout Recipient cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutRecipientCreationException extends PayoutRecipientException { - /** - * Construct the PayoutRecipientCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to submit payout recipient."; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-CREATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} - diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientException.java deleted file mode 100644 index 27f9df58..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientException.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Payout Recipient. - * - * @see com.bitpay.sdk.exceptions.PayoutRecipientCancellationException - * @see com.bitpay.sdk.exceptions.PayoutRecipientCreationException - * @see com.bitpay.sdk.exceptions.PayoutRecipientUpdateException - * @see com.bitpay.sdk.exceptions.PayoutRecipientNotificationException - * @see com.bitpay.sdk.exceptions.PayoutRecipientQueryException - * - * @see Rest API Error Codes - */ -public class PayoutRecipientException extends BitPayException { - /** - * Construct the PayoutRecipientException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the payout recipient"; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationException.java deleted file mode 100644 index f0e2e59c..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Payout Recipient Notification cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutRecipientNotificationException extends PayoutRecipientException { - /** - * Construct the PayoutRecipientNotificationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientNotificationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to send payout recipient notification."; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-NOTIFICATION"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryException.java deleted file mode 100644 index 7a54f2d3..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Payout Recipient cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutRecipientQueryException extends PayoutRecipientException { - /** - * Construct the PayoutRecipientQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve payout recipient."; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateException.java b/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateException.java deleted file mode 100644 index 720a6c4a..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a PUT when the Payout Recipient cannot be updated. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 03

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 030001
- * 
- * - * @see Rest API Error Codes - */ -public class PayoutRecipientUpdateException extends PayoutRecipientException { - /** - * Construct the PayoutRecipientUpdateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public PayoutRecipientUpdateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to update payout recipient."; - String bitPayCode = "BITPAY-PAYOUT-RECIPIENT-UPDATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RateException.java b/src/main/java/com/bitpay/sdk/exceptions/RateException.java deleted file mode 100644 index 43807cc8..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RateException.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Rate. - * - * @see com.bitpay.sdk.exceptions.RateQueryException - * - * @see Rest API Error Codes - */ -public class RateException extends BitPayException { - /** - * Construct the RateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the rates"; - String bitPayCode = "BITPAY-RATES-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RateQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/RateQueryException.java deleted file mode 100644 index 1ba88525..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RateQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Rate cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020001
- * 
- * - * @see Rest API Error Codes - */ -public class RateQueryException extends RateException { - /** - * Construct the RateQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RateQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve rates"; - String bitPayCode = "BITPAY-RATES-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RefundCancellationException.java b/src/main/java/com/bitpay/sdk/exceptions/RefundCancellationException.java deleted file mode 100644 index e8308751..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RefundCancellationException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a DELETE when the Refund cannot be cancelled. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 04

- *

Resource digits for the this class: 02

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Refund not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 04 - Active refund request already exists
  • - *
  • 07 - Invalid invoice state for refund
  • - *
  • 08 - Fees are greater than refund amount
  • - *
- *
- * eg 040201
- * 
- * - * @see Rest API Error Codes - */ -public class RefundCancellationException extends RefundException { - /** - * Construct the RefundCancellationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RefundCancellationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to cancel refund"; - String bitPayCode = "BITPAY-REFUND-CANCEL"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RefundCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/RefundCreationException.java deleted file mode 100644 index 235c9c8a..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RefundCreationException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Refund cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 02

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Refund not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 04 - Active refund request already exists
  • - *
  • 07 - Invalid invoice state for refund
  • - *
  • 08 - Fees are greater than refund amount
  • - *
- *
- * eg 010201
- * 
- * - * @see Rest API Error Codes - */ -public class RefundCreationException extends RefundException { - /** - * Construct the RefundCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RefundCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create refund"; - String bitPayCode = "BITPAY-REFUND-CREATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RefundException.java b/src/main/java/com/bitpay/sdk/exceptions/RefundException.java deleted file mode 100644 index 724104cf..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RefundException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Refund. - * - * @see com.bitpay.sdk.exceptions.RefundCancellationException - * @see com.bitpay.sdk.exceptions.RefundCreationException - * @see com.bitpay.sdk.exceptions.RefundUpdateException - * @see com.bitpay.sdk.exceptions.RefundQueryException - * - * @see Rest API Error Codes - */ -public class RefundException extends BitPayException { - /** - * Construct the RefundException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RefundException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the refund"; - String bitPayCode = "BITPAY-REFUND-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RefundQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/RefundQueryException.java deleted file mode 100644 index 5be81f5d..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RefundQueryException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Refund cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 02

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Refund not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 04 - Active refund request already exists
  • - *
  • 07 - Invalid invoice state for refund
  • - *
  • 08 - Fees are greater than refund amount
  • - *
- *
- * eg 020201
- * 
- * - * @see Rest API Error Codes - */ -public class RefundQueryException extends RefundException { - /** - * Construct the RefundQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RefundQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve refund"; - String bitPayCode = "BITPAY-REFUND-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/RefundUpdateException.java b/src/main/java/com/bitpay/sdk/exceptions/RefundUpdateException.java deleted file mode 100644 index b625f142..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/RefundUpdateException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a PUT when the Refund cannot be updated. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 03

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Refund not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
  • 04 - Active refund request already exists
  • - *
  • 07 - Invalid invoice state for refund
  • - *
  • 08 - Fees are greater than refund amount
  • - *
- *
- * eg 020301
- * 
- * - * @see Rest API Error Codes - */ -public class RefundUpdateException extends RefundException { - /** - * Construct the RefundUpdateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public RefundUpdateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to update refund"; - String bitPayCode = "BITPAY-REFUND-UPDATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SettlementException.java b/src/main/java/com/bitpay/sdk/exceptions/SettlementException.java deleted file mode 100644 index 8932be89..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SettlementException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Settlement. - * - * @see com.bitpay.sdk.exceptions.SettlementQueryException - * @see Rest API Error Codes - */ -public class SettlementException extends BitPayException { - /** - * Construct the SettlementException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SettlementException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the settlements"; - String bitPayCode = "BITPAY-SETTLEMENTS-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SettlementQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/SettlementQueryException.java deleted file mode 100644 index 2b671bf2..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SettlementQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Settlement cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020002
- * 
- * - * @see Rest API Error Codes - */ -public class SettlementQueryException extends SettlementException { - /** - * Construct the SettlementQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SettlementQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve settlements"; - String bitPayCode = "BITPAY-SETTLEMENTS-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionCreationException.java b/src/main/java/com/bitpay/sdk/exceptions/SubscriptionCreationException.java deleted file mode 100644 index 263587b7..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionCreationException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a POST when the Subscription cannot be created. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 01

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 010002
- * 
- * - * @see Rest API Error Codes - */ -public class SubscriptionCreationException extends SubscriptionException { - /** - * Construct the SubscriptionCreationException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SubscriptionCreationException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to create subscription"; - String bitPayCode = "BITPAY-SUBSCRIPTION-CREATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionException.java b/src/main/java/com/bitpay/sdk/exceptions/SubscriptionException.java deleted file mode 100644 index 00b42e9f..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionException.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Refund. - * - * @see com.bitpay.sdk.exceptions.SubscriptionCreationException - * @see com.bitpay.sdk.exceptions.SubscriptionUpdateException - * @see com.bitpay.sdk.exceptions.SubscriptionQueryException - * - * @see Rest API Error Codes - */ -public class SubscriptionException extends BitPayException { - /** - * Construct the SubscriptionException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SubscriptionException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage the subscription"; - String bitPayCode = "BITPAY-SUBSCRIPTION-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/SubscriptionQueryException.java deleted file mode 100644 index 5aa653b4..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Subscription cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020002
- * 
- * - * @see Rest API Error Codes - */ -public class SubscriptionQueryException extends SubscriptionException { - /** - * Construct the SubscriptionQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SubscriptionQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve subscription"; - String bitPayCode = "BITPAY-SUBSCRIPTION-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionUpdateException.java b/src/main/java/com/bitpay/sdk/exceptions/SubscriptionUpdateException.java deleted file mode 100644 index 0254572b..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/SubscriptionUpdateException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a PUT when the Subscription cannot be updated. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 03

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 030002
- * 
- * - * @see Rest API Error Codes - */ -public class SubscriptionUpdateException extends SubscriptionException { - /** - * Construct the SubscriptionUpdateException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public SubscriptionUpdateException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to update subscription"; - String bitPayCode = "BITPAY-SUBSCRIPTION-UPDATE"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/WalletException.java b/src/main/java/com/bitpay/sdk/exceptions/WalletException.java deleted file mode 100644 index 5efeed06..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/WalletException.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - * Exception which is extended by other exceptions related with Wallet. - * - * @see com.bitpay.sdk.exceptions.WalletQueryException - * - * @see Rest API Error Codes - */ -public class WalletException extends BitPayException { - /** - * Construct the WalletException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public WalletException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "An unexpected error occurred while trying to manage wallets"; - String bitPayCode = "BITPAY-WALLET-GENERIC"; - - if (message.isEmpty() || !message.contains("BITPAY-")) { - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - } - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/exceptions/WalletQueryException.java b/src/main/java/com/bitpay/sdk/exceptions/WalletQueryException.java deleted file mode 100644 index b324516b..00000000 --- a/src/main/java/com/bitpay/sdk/exceptions/WalletQueryException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2019 BitPay. - * All rights reserved. - */ - -package com.bitpay.sdk.exceptions; - -/** - *

- * Exception thrown for a GET when the Wallet cannot be retrieved. - *

- *
    - *
  • First two digits: HTTP method
  • - *
  • Second two digits: Resource
  • - *
  • Final two digits: Error
  • - *
- *

HTTP method digits for this class: 02

- *

Resource digits for the this class: 00

- *

Error digits for this class:

- *
    - *
  • 00 - Generic server error
  • - *
  • 01 - Resource not found
  • - *
  • 02 - Invalid parameters
  • - *
  • 03 - Missing parameters
  • - *
- *
- * eg 020002
- * 
- * - * @see Rest API Error Codes - */ -public class WalletQueryException extends WalletException { - /** - * Construct the WalletQueryException. - * - * @param status String [optional] The Exception code to throw. - * @param message String [optional] The Exception message to throw. - */ - public WalletQueryException( - String status, - String message - ) { - super(status, buildMessage(message)); - } - - private static String buildMessage(String message) { - String bitPayMessage = "Failed to retrieve supported wallets"; - String bitPayCode = "BITPAY-WALLET-GET"; - - message = bitPayCode + ": " + bitPayMessage + " -> " + message; - - return message; - } -} diff --git a/src/main/java/com/bitpay/sdk/logger/BitPayLogger.java b/src/main/java/com/bitpay/sdk/logger/BitPayLogger.java new file mode 100644 index 00000000..058536d3 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/logger/BitPayLogger.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.logger; + +public interface BitPayLogger { + + void logRequest( + String method, + String endpoint, + String json + ); + + void logResponse( + String method, + String endpoint, + String json + ); + + void logError(String message); +} diff --git a/src/main/java/com/bitpay/sdk/logger/EmptyLogger.java b/src/main/java/com/bitpay/sdk/logger/EmptyLogger.java new file mode 100644 index 00000000..27ea535d --- /dev/null +++ b/src/main/java/com/bitpay/sdk/logger/EmptyLogger.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.logger; + +public class EmptyLogger implements BitPayLogger { + + @Override + public void logRequest(String method, String endpoint, String json) { + } + + @Override + public void logResponse(String method, String endpoint, String json) { + } + + @Override + public void logError(String message) { + } +} diff --git a/src/main/java/com/bitpay/sdk/logger/LoggerProvider.java b/src/main/java/com/bitpay/sdk/logger/LoggerProvider.java new file mode 100644 index 00000000..a5ef1967 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/logger/LoggerProvider.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +package com.bitpay.sdk.logger; + +import java.util.Objects; + +public class LoggerProvider { + + private static BitPayLogger logger = null; + + private LoggerProvider() { + } + + public static BitPayLogger getLogger() { + if (Objects.isNull(logger)) { + logger = new EmptyLogger(); + } + + return logger; + } + + /** + * Set BitPayLogger. + * + * @param bitPayLogger BitPayLogger + */ + public static void setLogger(BitPayLogger bitPayLogger) { + logger = bitPayLogger; + } +} diff --git a/src/main/java/com/bitpay/sdk/logger/Slf4jLogger.java b/src/main/java/com/bitpay/sdk/logger/Slf4jLogger.java new file mode 100644 index 00000000..8f8060f8 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/logger/Slf4jLogger.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + +//package com.bitpay.sdk.logger; +// +//import org.slf4j.LoggerFactory; +// +//public class Slf4jLogger implements BitPayLogger { +// +// private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Slf4jLogger.class); +// +// @Override +// public void logRequest(String method, String endpoint, String json) { +// logger.info("Request method: " + method + " Endpoint: " + endpoint + " Json: " + json); +// } +// +// @Override +// public void logResponse(String method, String endpoint, String json) { +// logger.info("Response method: " + method + " Endpoint: " + endpoint + " Json: " + json); +// } +// +// @Override +// public void logError(String message) { +// logger.error(message); +// } +//} diff --git a/src/main/java/com/bitpay/sdk/model/bill/Bill.java b/src/main/java/com/bitpay/sdk/model/bill/Bill.java index 03189dfd..94e0994b 100644 --- a/src/main/java/com/bitpay/sdk/model/bill/Bill.java +++ b/src/main/java/com/bitpay/sdk/model/bill/Bill.java @@ -5,7 +5,8 @@ package com.bitpay.sdk.model.bill; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Currency; import com.bitpay.sdk.model.ModelConfiguration; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -120,12 +121,12 @@ public String getCurrency() { * Sets ISO 4217 3-character currency code. This is the currency associated with the price field. * * @param currency the currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException the bit pay exception */ @JsonProperty("currency") - public void setCurrency(final String currency) throws BitPayException { + public void setCurrency(final String currency) throws BitPayGenericException { if (!Currency.isValid(currency)) { - throw new BitPayException(null, "Error: currency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(currency); } this.currency = currency; diff --git a/src/main/java/com/bitpay/sdk/model/invoice/Invoice.java b/src/main/java/com/bitpay/sdk/model/invoice/Invoice.java index 7aa558f6..e43f622c 100644 --- a/src/main/java/com/bitpay/sdk/model/invoice/Invoice.java +++ b/src/main/java/com/bitpay/sdk/model/invoice/Invoice.java @@ -5,7 +5,8 @@ package com.bitpay.sdk.model.invoice; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Currency; import com.bitpay.sdk.model.ModelConfiguration; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -198,13 +199,13 @@ public String getCurrency() { * This is the currency associated with the price field. * * @param currency the currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class * @see Supported currencies */ @JsonProperty("currency") - public void setCurrency(final String currency) throws BitPayException { + public void setCurrency(final String currency) throws BitPayGenericException { if (!Currency.isValid(currency)) { - throw new BitPayException(null, "Error: currency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(currency); } this.currency = currency; @@ -649,12 +650,12 @@ public String getSelectedTransactionCurrency() { * "DOGE", "DAI" and "WBTC". If not yet selected, this field will not be returned. * * @param selectedTransactionCurrency the selected transaction currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ @JsonProperty("selectedTransactionCurrency") - public void setSelectedTransactionCurrency(final String selectedTransactionCurrency) throws BitPayException { + public void setSelectedTransactionCurrency(final String selectedTransactionCurrency) throws BitPayGenericException { if (!Currency.isValid(selectedTransactionCurrency)) { - throw new BitPayException(null, "Error: selectedTransactionCurrency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(selectedTransactionCurrency); } this.selectedTransactionCurrency = this.currency; diff --git a/src/main/java/com/bitpay/sdk/model/invoice/RefundInfo.java b/src/main/java/com/bitpay/sdk/model/invoice/RefundInfo.java index 888fbc3b..2041260a 100644 --- a/src/main/java/com/bitpay/sdk/model/invoice/RefundInfo.java +++ b/src/main/java/com/bitpay/sdk/model/invoice/RefundInfo.java @@ -5,7 +5,8 @@ package com.bitpay.sdk.model.invoice; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Currency; import com.bitpay.sdk.model.ModelConfiguration; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -68,12 +69,12 @@ public String getCurrency() { * Sets reference currency used for the refund, usually the same as the currency used to create the invoice. * * @param currency the currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException BitPayGenericException class */ @JsonProperty("currency") - public void setCurrency(String currency) throws BitPayException { + public void setCurrency(String currency) throws BitPayGenericException { if (!Currency.isValid(currency)) { - throw new BitPayException(null, "Error: currency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(currency); } this.currency = currency; diff --git a/src/main/java/com/bitpay/sdk/model/payout/Payout.java b/src/main/java/com/bitpay/sdk/model/payout/Payout.java index a5acfcb1..b8e8b25d 100644 --- a/src/main/java/com/bitpay/sdk/model/payout/Payout.java +++ b/src/main/java/com/bitpay/sdk/model/payout/Payout.java @@ -5,7 +5,8 @@ package com.bitpay.sdk.model.payout; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Currency; import com.bitpay.sdk.model.ModelConfiguration; import com.bitpay.sdk.util.DateDeserializer; @@ -162,12 +163,12 @@ public String getCurrency() { * Sets currency code set for the batch amount (ISO 4217 3-character currency code). * * @param currency the currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException the bit pay exception */ @JsonProperty("currency") - public void setCurrency(final String currency) throws BitPayException { + public void setCurrency(final String currency) throws BitPayGenericException { if (!Currency.isValid(currency)) { - throw new BitPayException(null, "Error: currency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(currency); } this.currency = currency; } @@ -194,12 +195,12 @@ public String getLedgerCurrency() { * e.g. your settlement currency. * * @param ledgerCurrency the ledger currency - * @throws BitPayException the bit pay exception + * @throws BitPayGenericException the bit pay exception */ @JsonProperty("ledgerCurrency") - public void setLedgerCurrency(final String ledgerCurrency) throws BitPayException { + public void setLedgerCurrency(final String ledgerCurrency) throws BitPayGenericException { if (!Currency.isValid(ledgerCurrency)) { - throw new BitPayException(null, "Error: currency code must be a type of Model.Currency"); + BitPayExceptionProvider.throwInvalidCurrencyException(ledgerCurrency); } this.ledgerCurrency = ledgerCurrency; } diff --git a/src/main/java/com/bitpay/sdk/model/rate/Rates.java b/src/main/java/com/bitpay/sdk/model/rate/Rates.java index d4c86296..1c53333e 100644 --- a/src/main/java/com/bitpay/sdk/model/rate/Rates.java +++ b/src/main/java/com/bitpay/sdk/model/rate/Rates.java @@ -6,7 +6,8 @@ package com.bitpay.sdk.model.rate; import com.bitpay.sdk.client.RateClient; -import com.bitpay.sdk.exceptions.RateQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; @@ -42,14 +43,11 @@ public List getRates() { * Update rates. * * @param rateClient the rate client - * @throws RateQueryException the rate query exception + * @throws BitPayGenericException BitPayGenericException class + * @throws BitPayApiException BitPayApiException class */ - public void update(RateClient rateClient) throws RateQueryException { - try { - this.rates = rateClient.getRates().getRates(); - } catch (Exception e) { - throw new RateQueryException(null, e.getMessage()); - } + public void update(RateClient rateClient) throws BitPayGenericException, BitPayApiException { + this.rates = rateClient.getRates().getRates(); } /** diff --git a/src/main/java/com/bitpay/sdk/util/KeyUtils.java b/src/main/java/com/bitpay/sdk/util/KeyUtils.java index 424f01ce..75bc1405 100644 --- a/src/main/java/com/bitpay/sdk/util/KeyUtils.java +++ b/src/main/java/com/bitpay/sdk/util/KeyUtils.java @@ -5,6 +5,8 @@ package com.bitpay.sdk.util; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -12,7 +14,6 @@ import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URI; import java.net.URISyntaxException; @@ -21,6 +22,7 @@ import org.bitcoinj.core.ECKey; import org.bitcoinj.core.ECKey.ECDSASignature; import org.bitcoinj.core.Sha256Hash; +import org.bitcoinj.crypto.KeyCrypterException; /** * The type Key utils. @@ -65,11 +67,10 @@ public static ECKey createEcKey() { * @param privateKey the private key * @return the ec key */ - public static ECKey createEcKeyFromHexString(String privateKey) { + public static ECKey createEcKeyFromHexString(String privateKey) throws BitPayGenericException { byte[] bytes = hexToBytes(privateKey); - ECKey ecKey = ECKey.fromASN1(bytes); - return ecKey; + return ECKey.fromASN1(bytes); } /** @@ -79,7 +80,7 @@ public static ECKey createEcKeyFromHexString(String privateKey) { * @return the ec key * @throws IOException the io exception */ - public static ECKey createEcKeyFromHexStringFile(String privKeyFile) throws IOException { + public static ECKey createEcKeyFromHexStringFile(String privKeyFile) throws IOException, BitPayGenericException { return createEcKeyFromHexString(getKeyStringFromFile(privKeyFile)); } @@ -224,9 +225,7 @@ public static void saveEcKeyAsHex(ECKey ecKey) throws IOException { */ public static String loadEcKeyAsHex(ECKey ecKey) throws IOException { byte[] bytes = ecKey.toASN1(); - String keyHex = bytesToHex(bytes); - - return keyHex; + return bytesToHex(bytes); } /** @@ -234,9 +233,9 @@ public static String loadEcKeyAsHex(ECKey ecKey) throws IOException { * * @param ecKey the ec key * @return the string - * @throws IllegalArgumentException the illegal argument exception + * @throws BitPayGenericException the illegal argument exception */ - public static String deriveSin(ECKey ecKey) throws IllegalArgumentException { + public static String deriveSin(ECKey ecKey) throws BitPayGenericException { // Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step). byte[] pubKeyHash = ecKey.getPubKeyHash(); @@ -268,20 +267,28 @@ public static String deriveSin(ECKey ecKey) throws IllegalArgumentException { * @param key the key * @param input the input * @return the string - * @throws UnsupportedEncodingException the unsupported encoding exception + * @throws BitPayGenericException BitPayGenericException class */ public static String sign( ECKey key, String input - ) throws UnsupportedEncodingException { - byte[] data = input.getBytes(StandardCharsets.UTF_8); + ) throws BitPayGenericException { + String result = null; - Sha256Hash hash = Sha256Hash.of(data); - ECDSASignature sig = key.sign(hash, null); + try { + byte[] data = input.getBytes(StandardCharsets.UTF_8); - byte[] bytes = sig.encodeToDER(); + Sha256Hash hash = Sha256Hash.of(data); + ECDSASignature sig = key.sign(hash, null); - return bytesToHex(bytes); + byte[] bytes = sig.encodeToDER(); + + result = bytesToHex(bytes); + } catch (KeyCrypterException e) { + BitPayExceptionProvider.throwGenericExceptionWithMessage("Wrong ecKey. " + e.getMessage()); + } + + return result; } private static int getHexVal(char hex) { @@ -294,13 +301,14 @@ private static int getHexVal(char hex) { * * @param hex the hex * @return the byte [] - * @throws IllegalArgumentException the illegal argument exception + * @throws BitPayGenericException BitPayGenericException class */ - public static byte[] hexToBytes(String hex) throws IllegalArgumentException { + public static byte[] hexToBytes(String hex) throws BitPayGenericException { char[] hexArray = hex.toCharArray(); if (hex.length() % 2 == 1) { - throw new IllegalArgumentException("Error: The binary key cannot have an odd number of digits"); + BitPayExceptionProvider.throwGenericExceptionWithMessage( + "Error: The binary key cannot have an odd number of digits"); } byte[] arr = new byte[hex.length() >> 1]; diff --git a/src/main/java/com/bitpay/sdk/util/TokenContainer.java b/src/main/java/com/bitpay/sdk/util/TokenContainer.java index 33502a8b..57f5b193 100644 --- a/src/main/java/com/bitpay/sdk/util/TokenContainer.java +++ b/src/main/java/com/bitpay/sdk/util/TokenContainer.java @@ -6,7 +6,8 @@ package com.bitpay.sdk.util; import com.bitpay.sdk.Config; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.fasterxml.jackson.databind.JsonNode; import java.util.Hashtable; @@ -49,9 +50,9 @@ public TokenContainer(Config configuration) { * * @param facade The identifier for the desired resource. * @return The token associated with resource. - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ - public String getAccessToken(Facade facade) throws BitPayException { + public String getAccessToken(Facade facade) throws BitPayGenericException { return this.getAccessToken(facade.toString()); } @@ -60,11 +61,12 @@ public String getAccessToken(Facade facade) throws BitPayException { * * @param key The identifier for the desired resource. * @return The token associated with resource. - * @throws BitPayException BitPayException class + * @throws BitPayGenericException BitPayGenericException class */ - public String getAccessToken(String key) throws BitPayException { + public String getAccessToken(String key) throws BitPayGenericException { if (!this.data.containsKey(key)) { - throw new BitPayException(null, "There is no token for the specified key : " + key); + BitPayExceptionProvider.throwGenericExceptionWithMessage( + "There is no token for the specified key : " + key); } return this.data.get(key); diff --git a/src/test/java/com/bitpay/sdk/ClientTest.java b/src/test/java/com/bitpay/sdk/ClientTest.java index bfe878f2..64354d0d 100644 --- a/src/test/java/com/bitpay/sdk/ClientTest.java +++ b/src/test/java/com/bitpay/sdk/ClientTest.java @@ -5,16 +5,12 @@ package com.bitpay.sdk; import com.bitpay.sdk.client.BitPayClient; -import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.InvoiceCancellationException; -import com.bitpay.sdk.exceptions.RefundCreationException; -import com.bitpay.sdk.exceptions.RefundQueryException; -import com.bitpay.sdk.exceptions.RefundUpdateException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.exceptions.BitPayValidationException; +import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.model.bill.Bill; import com.bitpay.sdk.model.bill.Item; -import com.bitpay.sdk.model.Facade; -import com.bitpay.sdk.model.payout.PayoutGroup; -import com.bitpay.sdk.model.payout.PayoutGroupFailed; import com.bitpay.sdk.model.invoice.Buyer; import com.bitpay.sdk.model.invoice.Invoice; import com.bitpay.sdk.model.invoice.InvoiceEventToken; @@ -22,6 +18,8 @@ import com.bitpay.sdk.model.ledger.Ledger; import com.bitpay.sdk.model.ledger.LedgerEntry; import com.bitpay.sdk.model.payout.Payout; +import com.bitpay.sdk.model.payout.PayoutGroup; +import com.bitpay.sdk.model.payout.PayoutGroupFailed; import com.bitpay.sdk.model.payout.PayoutRecipient; import com.bitpay.sdk.model.payout.PayoutRecipients; import com.bitpay.sdk.model.rate.Rate; @@ -42,9 +40,7 @@ import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; -import org.apache.http.HttpEntity; import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; import org.apache.http.client.CredentialsProvider; import org.apache.http.message.BasicNameValuePair; import org.junit.jupiter.api.Assertions; @@ -77,10 +73,6 @@ public class ClientTest { private TokenContainer accessTokens; @Mock private GuidGenerator guidGenerator; - @Mock - private HttpResponse httpResponse; - @Mock - private HttpEntity httpEntity; @BeforeEach public void clearResourceClients() { @@ -88,7 +80,7 @@ public void clearResourceClients() { } @Test - public void it_should_provide_pos_client() throws BitPayException { + public void it_should_provide_pos_client() throws BitPayGenericException { // given String posToken = "posToken"; @@ -100,7 +92,7 @@ public void it_should_provide_pos_client() throws BitPayException { } @Test - public void it_should_provide_client_by_key() throws BitPayException { + public void it_should_provide_client_by_key() throws BitPayGenericException { // given String privateKey = "3082013102010104208ae30afbc7e93cb10cb983f70863e546b53f0b2c6158b1a71b576fd09790cff3a081e33081e0020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f3044042000000000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000000704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a124032200035d6a7e38d7c08b8a626e2390d0360a72a58bd1c5e1348e0eb810d4bbab3d3adf"; @@ -116,7 +108,7 @@ public void it_should_provide_client_by_key() throws BitPayException { } @Test - public void it_should_provide_client_by_config() throws BitPayException { + public void it_should_provide_client_by_config() throws BitPayGenericException { // given String path = System.getProperty("user.dir") + "/src/test/java/com/bitpay/sdk/BitPay.config.json"; @@ -129,8 +121,8 @@ public void it_should_provide_client_by_config() throws BitPayException { } @Test - public void it_should_throws_bitpayexception_for_invalid_privateKey() throws BitPayException { - BitPayException exception = Assertions.assertThrows(BitPayException.class, () -> { + public void it_should_throws_BitPayApiException_for_invalid_privateKey() { + BitPayGenericException exception = Assertions.assertThrows(BitPayGenericException.class, () -> { new Client( Environment.TEST, new PrivateKey("invalid"), @@ -140,23 +132,19 @@ public void it_should_throws_bitpayexception_for_invalid_privateKey() throws Bit ); }); - Assertions.assertEquals( - "Status: 000000 -> Reason: failed to deserialize BitPay server response (Config) : Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Private Key file not found", - exception.getMessage() - ); + Assertions.assertEquals("Private Key file not found", exception.getMessage()); } @Test - public void it_should_authorize_client_by_pairing_code() throws BitPayException { + public void it_should_authorize_client_by_pairing_code() throws BitPayApiException, BitPayGenericException { // given String pairingCode = "123123123"; Mockito.when(this.guidGenerator.execute()).thenReturn(EXAMPLE_UUID); - Mockito.when(this.bitPayClient.post("tokens", - "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) - .thenReturn(this.httpResponse); final String responseString = "[{\"policies\":[{\"policy\":\"id\",\"method\":\"active\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"t0k3n\",\"facade\":\"merchant\",\"dateCreated\":1668425446554,\"pairingExpiration\":1668511846554,\"pairingCode\":\"123123123\"}]"; - Mockito.when(this.bitPayClient.responseToJsonString(httpResponse)).thenReturn(responseString); + Mockito.when(this.bitPayClient.post("tokens", + "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) + .thenReturn(responseString); Client testedClass = this.getTestedClass(); @@ -164,20 +152,19 @@ public void it_should_authorize_client_by_pairing_code() throws BitPayException testedClass.authorizeClient(pairingCode); // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Mockito.verify(this.accessTokens, Mockito.times(1)).put("merchant", "t0k3n"); } @Test - public void it_should_authorize_client_by_facade() throws BitPayException { + public void it_should_authorize_client_by_facade() throws BitPayApiException, BitPayGenericException { // given Mockito.when(this.guidGenerator.execute()).thenReturn(EXAMPLE_UUID); - Mockito.when(this.bitPayClient.post("tokens", - "{\"count\":1,\"facade\":\"merchant\",\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"}")) - .thenReturn(this.httpResponse); + final String responseString = "[{\"policies\":[{\"policy\":\"id\",\"method\":\"inactive\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"G7XM9fcM1gtCN7DUr8ZWtPGVFLTKiYWanHR4kvqsnjP3\",\"facade\":\"merchant\",\"label\":\"merchantwebsite.com\",\"dateCreated\":1621340364865,\"pairingExpiration\":1621426764865,\"pairingCode\":\"C4Lg7oW\"}]"; - Mockito.when(this.bitPayClient.responseToJsonString(httpResponse)).thenReturn(responseString); + Mockito.when(this.bitPayClient.post("tokens", + "{\"count\":1,\"facade\":\"merchant\",\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"}")) + .thenReturn(responseString); Client testedClass = this.getTestedClass(); @@ -185,22 +172,20 @@ public void it_should_authorize_client_by_facade() throws BitPayException { testedClass.authorizeClient(Facade.MERCHANT); // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Mockito.verify(this.accessTokens, Mockito.times(1)) .put("merchant", "G7XM9fcM1gtCN7DUr8ZWtPGVFLTKiYWanHR4kvqsnjP3"); } @Test - public void it_should_test_requestClientAuthorization() throws BitPayException { + public void it_should_test_requestClientAuthorization() throws BitPayApiException, BitPayGenericException { // given String pairingCode = "123123123"; Mockito.when(this.guidGenerator.execute()).thenReturn(EXAMPLE_UUID); - Mockito.when(this.bitPayClient.post("tokens", - "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) - .thenReturn(this.httpResponse); final String responseString = "[{\"policies\":[{\"policy\":\"id\",\"method\":\"active\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"t0k3n\",\"facade\":\"merchant\",\"dateCreated\":1668425446554,\"pairingExpiration\":1668511846554,\"pairingCode\":\"\"}]"; - Mockito.when(this.bitPayClient.responseToJsonString(httpResponse)).thenReturn(responseString); + Mockito.when(this.bitPayClient.post("tokens", + "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) + .thenReturn(responseString); Client testedClass = this.getTestedClass(); @@ -208,12 +193,11 @@ public void it_should_test_requestClientAuthorization() throws BitPayException { testedClass.authorizeClient(pairingCode); // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Mockito.verify(this.accessTokens, Mockito.times(1)).put("merchant", "t0k3n"); } @Test - public void it_should_test_getAccessToken() throws BitPayException { + public void it_should_test_getAccessToken() throws BitPayGenericException { // given Client testedClass = this.getTestedClass(); String tokenKey = "tokenKey"; @@ -226,18 +210,16 @@ public void it_should_test_getAccessToken() throws BitPayException { } @Test - public void it_should_test_getCurrencyInfo() throws IOException { + public void it_should_test_getCurrencyInfo() throws BitPayGenericException, BitPayApiException { // given String response = getPreparedJsonDataFromFile("currencies.json"); InputStream inputStream = new ByteArrayInputStream(response.getBytes()); - Mockito.when(this.bitPayClient.get("currencies")).thenReturn(this.httpResponse); - Mockito.when(this.httpResponse.getEntity()).thenReturn(this.httpEntity); - Mockito.when(this.httpEntity.getContent()).thenReturn(inputStream); + Mockito.when(this.bitPayClient.get("currencies")).thenReturn(response); Client testedClass = this.getTestedClass(); // when - Map result = testedClass.getCurrencyInfo("USD"); + Map result = testedClass.getCurrencyInfo("USD"); // then Assertions.assertEquals("USD", result.get("code")); @@ -246,13 +228,12 @@ public void it_should_test_getCurrencyInfo() throws IOException { } @Test - public void it_should_test_create_invoice_by_merchant() throws IOException { + public void it_should_test_create_invoice_by_merchant() + throws BitPayGenericException, BitPayApiException { // given Invoice invoice = getInvoiceExample(); Mockito.when(this.bitPayClient.post("invoices", getPreparedJsonDataFromFile("createInvoiceRequest.json"), true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("createInvoiceResponse.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)) @@ -275,14 +256,12 @@ public void it_should_test_create_invoice_by_merchant() throws IOException { } @Test - public void it_should_test_createInvoice_by_pos() throws IOException { + public void it_should_test_createInvoice_by_pos() throws BitPayGenericException, BitPayApiException { // given Invoice invoice = getInvoiceExample(); Mockito .when(this.bitPayClient.post("invoices", getPreparedJsonDataFromFile("createInvoiceRequest.json"), false)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("createInvoiceResponse.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn("someToken"); @@ -304,7 +283,7 @@ public void it_should_test_createInvoice_by_pos() throws IOException { } @Test - public void it_should_test_getInvoice_by_merchant() throws BitPayException { + public void it_should_test_getInvoice_by_merchant() throws BitPayApiException, BitPayGenericException { // given String id = "UZjwcYkWAKfTMn9J1yyfs4"; final String facadeToken = "someToken"; @@ -313,8 +292,6 @@ public void it_should_test_getInvoice_by_merchant() throws BitPayException { Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(true))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)) @@ -325,14 +302,13 @@ public void it_should_test_getInvoice_by_merchant() throws BitPayException { Invoice result = testedClass.getInvoice(id); // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Mockito.verify(this.bitPayClient, Mockito.times(1)) .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(true)); Assertions.assertEquals("chc9kj52-04g0-4b6f-941d-3a844e352758", result.getGuid()); } @Test - public void it_should_test_getInvoice_by_pos() throws BitPayException { + public void it_should_test_getInvoice_by_pos() throws BitPayApiException, BitPayGenericException { // given String id = "UZjwcYkWAKfTMn9J1yyfs4"; final String facadeToken = "someToken"; @@ -341,8 +317,6 @@ public void it_should_test_getInvoice_by_pos() throws BitPayException { Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(false))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(facadeToken); @@ -352,14 +326,13 @@ public void it_should_test_getInvoice_by_pos() throws BitPayException { Invoice result = testedClass.getInvoice(id); // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Mockito.verify(this.bitPayClient, Mockito.times(1)) .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(false)); Assertions.assertEquals("chc9kj52-04g0-4b6f-941d-3a844e352758", result.getGuid()); } @Test - public void it_should_test_getInvoiceByGuid() throws BitPayException { + public void it_should_test_getInvoiceByGuid() throws BitPayApiException, BitPayGenericException { // given String guid = "chc9kj52-04g0-4b6f-941d-3a844e352758"; String merchantToken = "merchantToken"; @@ -368,8 +341,6 @@ public void it_should_test_getInvoiceByGuid() throws BitPayException { Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/guid/" + guid), ArgumentMatchers.eq(expectedParams), ArgumentMatchers.eq(true))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -379,12 +350,11 @@ public void it_should_test_getInvoiceByGuid() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("chc9kj52-04g0-4b6f-941d-3a844e352758", result.getGuid()); } @Test - public void it_should_test_getInvoices() throws BitPayException { + public void it_should_test_getInvoices() throws BitPayApiException, BitPayGenericException { // given String merchantToken = "merchantToken"; List expectedParams = new ArrayList(); @@ -394,8 +364,6 @@ public void it_should_test_getInvoices() throws BitPayException { expectedParams.add(new BasicNameValuePair("status", "complete")); expectedParams.add(new BasicNameValuePair("limit", "1")); Mockito.when(this.bitPayClient.get(ArgumentMatchers.eq("invoices"), ArgumentMatchers.eq(expectedParams))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoices.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -412,19 +380,16 @@ public void it_should_test_getInvoices() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertFalse(result.isEmpty()); } @Test - public void it_should_get_invoice_event_token() throws BitPayException { + public void it_should_get_invoice_event_token() throws BitPayApiException, BitPayGenericException { // given String merchantToken = "merchantToken"; List expectedParams = new ArrayList(); expectedParams.add(new BasicNameValuePair("token", merchantToken)); Mockito.when(this.bitPayClient.get(ArgumentMatchers.eq("invoices/GZRP3zgNHTDf8F5BmdChKz/events"), ArgumentMatchers.eq(expectedParams))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoiceEventToken.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -434,15 +399,14 @@ public void it_should_get_invoice_event_token() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("4MuqDPt93i9Xbf8SnAPniwbGeNLW8A3ScgAmukFMgFUFRqTLuuhVdAFfePPysVqL2P", result.getToken()); Assertions.assertEquals(Arrays.asList("payment", "confirmation"), result.getEvents()); Assertions.assertEquals(Arrays.asList("subscribe", "unsubscribe"), result.getActions()); } @Test - public void it_should_test_throws_exception_for_missing_arguments_in_updateInvoice() throws BitPayException { - Assertions.assertThrows(BitPayException.class, () -> { + public void it_should_test_throws_validation_exception_for_missing_arguments_in_updateInvoice() throws BitPayValidationException { + Assertions.assertThrows(BitPayValidationException.class, () -> { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; @@ -463,15 +427,13 @@ public void it_should_test_throws_exception_for_missing_arguments_in_updateInvoi } @Test - public void it_should_test_updateInvoice() throws BitPayException { + public void it_should_test_updateInvoice() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; Mockito.when(this.bitPayClient.update(ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq("{\"buyerSms\":\"+12223334444\",\"token\":\"merchantToken\"}"))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); @@ -488,12 +450,11 @@ public void it_should_test_updateInvoice() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("chc9kj52-04g0-4b6f-941d-3a844e352758", result.getGuid()); } @Test - public void it_should_test_payInvoice() throws BitPayException { + public void it_should_test_payInvoice() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; @@ -501,8 +462,6 @@ public void it_should_test_payInvoice() throws BitPayException { Mockito.when(this.bitPayClient.update(ArgumentMatchers.eq("invoices/pay/" + invoiceId), ArgumentMatchers.eq("{\"token\":\"merchantToken\",\"status\":\"complete\"}"))) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -515,12 +474,11 @@ public void it_should_test_payInvoice() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("chc9kj52-04g0-4b6f-941d-3a844e352758", result.getGuid()); } @Test - public void it_should_force_cancel_invoice() throws BitPayException { + public void it_should_force_cancel_invoice() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; @@ -530,9 +488,7 @@ public void it_should_force_cancel_invoice() throws BitPayException { Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -542,12 +498,11 @@ public void it_should_force_cancel_invoice() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("invoices/" + invoiceId, expectedParams); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("payment#1234", result.getGuid()); } @Test - public void it_should_cancel_invoice() throws BitPayException { + public void it_should_cancel_invoice() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; @@ -556,9 +511,7 @@ public void it_should_cancel_invoice() throws BitPayException { Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -568,12 +521,11 @@ public void it_should_cancel_invoice() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("invoices/" + invoiceId, expectedParams); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("payment#1234", result.getGuid()); } @Test - public void it_should_cancel_invoice_by_guid() throws BitPayException { + public void it_should_cancel_invoice_by_guid() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String guidId = "payment#1234"; @@ -582,9 +534,7 @@ public void it_should_cancel_invoice_by_guid() throws BitPayException { Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/guid/" + guidId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -594,14 +544,13 @@ public void it_should_cancel_invoice_by_guid() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("invoices/guid/" + guidId, expectedParams); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(guidId, result.getGuid()); } @Test - public void it_should_throws_exception_for_cancel_invoice_for_invalid_state_of_cancel() throws BitPayException { - InvoiceCancellationException exception = Assertions.assertThrows( - InvoiceCancellationException.class, + public void it_should_throws_exception_for_cancel_invoice_for_invalid_state_of_cancel() throws BitPayApiException { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { final String merchantToken = "merchantToken"; final String guidId = "chc9kj52-04g0-4b6f-941d-3a844e352757"; @@ -610,25 +559,28 @@ public void it_should_throws_exception_for_cancel_invoice_for_invalid_state_of_c Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/guid/" + guidId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenThrow(new BitPayException(null, "Error: Invalid invoice state for cancel")); + ).thenThrow(new BitPayApiException("Error: Invalid invoice state for cancel", "00000")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); // when - Invoice result = testedClass.cancelInvoiceByGuid(guidId, false); + testedClass.cancelInvoiceByGuid(guidId, false); } ); Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-INVOICE-CANCEL: Failed to cancel invoice -> Error: Invalid invoice state for cancel", + "Error: Invalid invoice state for cancel", exception.getMessage() ); + + Assertions.assertEquals( + "00000", + exception.getCode() + ); } @Test - public void it_should_request_invoice_webhook_to_be_resent() throws BitPayException { + public void it_should_request_invoice_webhook_to_be_resent() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; @@ -636,9 +588,7 @@ public void it_should_request_invoice_webhook_to_be_resent() throws BitPayExcept Mockito.when(this.bitPayClient.post( ArgumentMatchers.eq("invoices/" + invoiceId + "/notifications"), ArgumentMatchers.eq(requestJson)) - ).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn("\"Success\""); + ).thenReturn("\"Success\""); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -648,20 +598,17 @@ public void it_should_request_invoice_webhook_to_be_resent() throws BitPayExcept // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("invoices/" + invoiceId + "/notifications", requestJson); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_create_refund() throws BitPayException { + public void it_should_create_refund() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); Mockito.when(this.guidGenerator.execute()).thenReturn(EXAMPLE_UUID); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); @@ -684,21 +631,18 @@ public void it_should_create_refund() throws BitPayException { createRefundJsonRequest, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(invoiceId, result.getInvoice()); Assertions.assertEquals(EXAMPLE_UUID, result.getGuid()); } @Test - public void it_should_create_refund_with_guid() throws BitPayException { + public void it_should_create_refund_with_guid() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -722,21 +666,18 @@ public void it_should_create_refund_with_guid() throws BitPayException { createRefundJsonRequest, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(invoiceId, result.getInvoice()); Assertions.assertEquals(EXAMPLE_UUID, result.getGuid()); } @Test - public void it_should_create_refund_using_refund_object() throws BitPayException { + public void it_should_create_refund_using_refund_object() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "UZjwcYkWAKfTMn9J1yyfs4"; final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -756,15 +697,14 @@ public void it_should_create_refund_using_refund_object() throws BitPayException Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("refunds/", createRefundJsonRequest, true); Mockito.verify(this.guidGenerator, Mockito.times(0)).execute(); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(invoiceId, result.getInvoice()); Assertions.assertEquals(EXAMPLE_UUID, result.getGuid()); } @Test - public void it_should_throws_refundCreationException_for_missing_invoice_id_and_amount_for_createRefund() { - RefundCreationException exception = Assertions.assertThrows( - RefundCreationException.class, + public void it_should_throws_bitpay_validation_exception_for_missing_invoice_id_and_amount_for_createRefund() { + BitPayValidationException exception = Assertions.assertThrows( + BitPayValidationException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -775,15 +715,15 @@ public void it_should_throws_refundCreationException_for_missing_invoice_id_and_ ); Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-REFUND-CREATE: Failed to create refund -> Invoice ID, amount and currency are required to issue a refund.", + "Invoice ID, amount and currency are required to issue a refund.", exception.getMessage() ); } @Test - public void it_should_throws_refundCreationException_for_invalid_sendRefundNotification() { - RefundCreationException exception = Assertions.assertThrows( - RefundCreationException.class, + public void it_should_throws_bitpay_api_exception_for_invalid_sendRefundNotification() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -791,7 +731,7 @@ public void it_should_throws_refundCreationException_for_invalid_sendRefundNotif ArgumentMatchers.eq("refunds/1/notifications"), ArgumentMatchers.anyString(), ArgumentMatchers.eq(true) - )).thenThrow(new BitPayException("500", "error message")); + )).thenThrow(new BitPayApiException("error message", "500")); // when testedClass.sendRefundNotification("1"); @@ -799,15 +739,15 @@ public void it_should_throws_refundCreationException_for_invalid_sendRefundNotif ); Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-CREATE: Failed to create refund -> error message", + "error message", exception.getMessage() ); } @Test - public void it_should_throws_refundCreationException_for_api_issue_for_create_refund() { - RefundCreationException exception = Assertions.assertThrows( - RefundCreationException.class, + public void it_should_throws_bitpay_api_exception_for_api_issue_for_create_refund() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -815,7 +755,7 @@ public void it_should_throws_refundCreationException_for_api_issue_for_create_re ArgumentMatchers.eq("refunds/"), ArgumentMatchers.anyString(), ArgumentMatchers.eq(true) - )).thenThrow(new BitPayException("500", "error message")); + )).thenThrow(new BitPayApiException("error message", "500")); // when testedClass.createRefund("123", 2.20, true, true, true, "no"); @@ -823,15 +763,15 @@ public void it_should_throws_refundCreationException_for_api_issue_for_create_re ); Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-CREATE: Failed to create refund -> error message", + "error message", exception.getMessage() ); } @Test - public void it_should_throws_refundQueryException_for_api_issue_for_get_refund() { - RefundQueryException exception = Assertions.assertThrows( - RefundQueryException.class, + public void it_should_throws_bitpay_api_exception_for_api_issue_for_get_refund() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -840,7 +780,7 @@ public void it_should_throws_refundQueryException_for_api_issue_for_get_refund() ArgumentMatchers.eq("refunds/" + id), ArgumentMatchers.any(), ArgumentMatchers.eq(true) - )).thenThrow(new BitPayException("500", "error message")); + )).thenThrow(new BitPayApiException("error message", "500")); // when testedClass.getRefund(id); @@ -848,15 +788,15 @@ public void it_should_throws_refundQueryException_for_api_issue_for_get_refund() ); Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-GET: Failed to retrieve refund -> error message", + "error message", exception.getMessage() ); } @Test - public void it_should_throws_refundQueryException_for_api_issue_for_get_refunds() { - RefundQueryException exception = Assertions.assertThrows( - RefundQueryException.class, + public void it_should_throws_bitpay_api_exception_for_api_issue_for_get_refunds() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -865,7 +805,7 @@ public void it_should_throws_refundQueryException_for_api_issue_for_get_refunds( ArgumentMatchers.eq("refunds/"), ArgumentMatchers.any(), ArgumentMatchers.eq(true) - )).thenThrow(new BitPayException("500", "error message")); + )).thenThrow(new BitPayApiException("error message", "500")); // when testedClass.getRefunds(id); @@ -873,40 +813,15 @@ public void it_should_throws_refundQueryException_for_api_issue_for_get_refunds( ); Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-GET: Failed to retrieve refund -> error message", + "error message", exception.getMessage() ); } @Test - public void it_should_throws_refundUpdateException_for_api_issue_for_get_refunds() { - RefundQueryException exception = Assertions.assertThrows( - RefundQueryException.class, - () -> { - // given - Client testedClass = this.getTestedClass(); - String id = "12"; - Mockito.when(this.bitPayClient.get( - ArgumentMatchers.eq("refunds/"), - ArgumentMatchers.any(), - ArgumentMatchers.eq(true) - )).thenThrow(new BitPayException("500", "error message")); - - // when - testedClass.getRefunds(id); - } - ); - - Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-GET: Failed to retrieve refund -> error message", - exception.getMessage() - ); - } - - @Test - public void it_should_throws_refundUpdateException_for_api_issue_for_update_refund() { - RefundUpdateException exception = Assertions.assertThrows( - RefundUpdateException.class, + public void it_should_throws_bitpay_api_exception_for_api_issue_for_update_refund() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, () -> { // given Client testedClass = this.getTestedClass(); @@ -914,7 +829,7 @@ public void it_should_throws_refundUpdateException_for_api_issue_for_update_refu Mockito.when(this.bitPayClient.update( ArgumentMatchers.eq("refunds/12"), ArgumentMatchers.eq("{\"token\":null,\"status\":\"complete\"}") - )).thenThrow(new BitPayException("500", "error message")); + )).thenThrow(new BitPayApiException("error message", "500")); // when testedClass.updateRefund(id, "complete"); @@ -922,13 +837,13 @@ public void it_should_throws_refundUpdateException_for_api_issue_for_update_refu ); Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-REFUND-UPDATE: Failed to update refund -> error message", + "error message", exception.getMessage() ); } @Test - public void it_should_get_refund_by_id() throws BitPayException { + public void it_should_get_refund_by_id() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String refundId = "WoE46gSLkJQS48RJEiNw3L"; @@ -937,8 +852,6 @@ public void it_should_get_refund_by_id() throws BitPayException { params.add(new BasicNameValuePair("token", "merchantToken")); Mockito.when(this.bitPayClient.get("refunds/" + refundId, params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getRefund.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); @@ -954,12 +867,11 @@ public void it_should_get_refund_by_id() throws BitPayException { params, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(refundId, result.getId()); } @Test - public void it_should_get_refund_by_guid() throws BitPayException { + public void it_should_get_refund_by_guid() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; @@ -967,8 +879,6 @@ public void it_should_get_refund_by_guid() throws BitPayException { params.add(new BasicNameValuePair("token", "merchantToken")); Mockito.when(this.bitPayClient.get("refunds/guid/" + EXAMPLE_UUID, params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getRefund.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); @@ -984,12 +894,11 @@ public void it_should_get_refund_by_guid() throws BitPayException { params, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(EXAMPLE_UUID, result.getGuid()); } @Test - public void it_should_test_get_refunds() throws BitPayException { + public void it_should_test_get_refunds() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String invoiceId = "Hpqc63wvE1ZjzeeH4kEycF"; @@ -1000,8 +909,6 @@ public void it_should_test_get_refunds() throws BitPayException { params.add(new BasicNameValuePair("invoiceId", invoiceId)); Mockito.when(this.bitPayClient.get("refunds/", params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1016,12 +923,11 @@ public void it_should_test_get_refunds() throws BitPayException { params, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(invoiceId, result.get(0).getInvoice()); } @Test - public void it_should_update_refund_by_id() throws BitPayException { + public void it_should_update_refund_by_id() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String status = "complete"; @@ -1036,9 +942,7 @@ public void it_should_update_refund_by_id() throws BitPayException { Mockito.when(this.bitPayClient.update( "refunds/" + refundId, requestedJson - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1048,12 +952,11 @@ public void it_should_update_refund_by_id() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).update("refunds/" + refundId, requestedJson); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(refundId, result.getId()); } @Test - public void it_should_update_refund_by_guid() throws BitPayException { + public void it_should_update_refund_by_guid() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String status = "complete"; @@ -1068,9 +971,7 @@ public void it_should_update_refund_by_guid() throws BitPayException { Mockito.when(this.bitPayClient.update( "refunds/guid/" + guid, requestedJson - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1080,12 +981,11 @@ public void it_should_update_refund_by_guid() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).update("refunds/guid/" + guid, requestedJson); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(guid, result.getGuid()); } @Test - public void it_should_test_sendRefundNotification() throws BitPayException { + public void it_should_test_sendRefundNotification() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String refundId = "WoE46gSLkJQS48RJEiNw3L"; @@ -1099,9 +999,7 @@ public void it_should_test_sendRefundNotification() throws BitPayException { "refunds/" + refundId + "/notifications", requestedJson, true - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1115,12 +1013,11 @@ public void it_should_test_sendRefundNotification() throws BitPayException { requestedJson, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_cancel_refund_by_id() throws BitPayException { + public void it_should_cancel_refund_by_id() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String refundId = "WoE46gSLkJQS48RJEiNw3L"; @@ -1132,9 +1029,7 @@ public void it_should_cancel_refund_by_id() throws BitPayException { Mockito.when(this.bitPayClient.delete( "refunds/" + refundId, params - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1145,12 +1040,11 @@ public void it_should_cancel_refund_by_id() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("refunds/" + refundId, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(refundId, result.getId()); } @Test - public void it_should_cancel_refund_by_guid() throws BitPayException { + public void it_should_cancel_refund_by_guid() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "merchantToken"; final String guid = EXAMPLE_UUID; @@ -1162,9 +1056,7 @@ public void it_should_cancel_refund_by_guid() throws BitPayException { Mockito.when(this.bitPayClient.delete( "refunds/guid/" + guid, params - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(getRefundsJsonConvertedResponse); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1175,12 +1067,11 @@ public void it_should_cancel_refund_by_guid() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("refunds/guid/" + guid, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(guid, result.getGuid()); } @Test - public void it_should_test_createBill_by_merchant_facade() throws BitPayException { + public void it_should_test_createBill_by_merchant_facade() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "AKnJyeLF1BjAfgfDbVUzHXk64N1WuDq3R9xtZouQFhSv"; final String createBillApiRequest = getPreparedJsonDataFromFile("createBillRequest.json"); @@ -1190,9 +1081,7 @@ public void it_should_test_createBill_by_merchant_facade() throws BitPayExceptio "bills", createBillApiRequest, true - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("createBillResponse.json")); + )).thenReturn(getPreparedJsonDataFromFile("createBillResponse.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); @@ -1204,12 +1093,11 @@ public void it_should_test_createBill_by_merchant_facade() throws BitPayExceptio // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("bills", createBillApiRequest, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.getId()); } @Test - public void it_should_test_createBill_by_pos_facade() throws BitPayException { + public void it_should_test_createBill_by_pos_facade() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "AKnJyeLF1BjAfgfDbVUzHXk64N1WuDq3R9xtZouQFhSv"; final String createBillApiRequest = getPreparedJsonDataFromFile("createBillRequest.json"); @@ -1219,9 +1107,7 @@ public void it_should_test_createBill_by_pos_facade() throws BitPayException { "bills", createBillApiRequest, false - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("createBillPosResponse.json")); + )).thenReturn(getPreparedJsonDataFromFile("createBillPosResponse.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(merchantToken); @@ -1233,12 +1119,11 @@ public void it_should_test_createBill_by_pos_facade() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.POS); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("bills", createBillApiRequest, false); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.getId()); } @Test - public void it_should_test_getBill_by_merchant_facade() throws BitPayException { + public void it_should_test_getBill_by_merchant_facade() throws BitPayApiException, BitPayGenericException { // given final String facadeToken = "AKnJyeLF1BjAfgfDbVUzHXk64N1WuDq3R9xtZouQFhSv"; @@ -1246,8 +1131,6 @@ public void it_should_test_getBill_by_merchant_facade() throws BitPayException { params.add(new BasicNameValuePair("token", facadeToken)); Mockito.when(this.bitPayClient.get("bills/" + BILL_ID, params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getBill.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(facadeToken); @@ -1260,12 +1143,11 @@ public void it_should_test_getBill_by_merchant_facade() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("bills/" + BILL_ID, params, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.getId()); } @Test - public void it_should_test_getBill_by_pos_facade() throws BitPayException { + public void it_should_test_getBill_by_pos_facade() throws BitPayApiException, BitPayGenericException { // given final String facadeToken = "AKnJyeLF1BjAfgfDbVUzHXk64N1WuDq3R9xtZouQFhSv"; @@ -1273,8 +1155,6 @@ public void it_should_test_getBill_by_pos_facade() throws BitPayException { params.add(new BasicNameValuePair("token", facadeToken)); Mockito.when(this.bitPayClient.get("bills/" + BILL_ID, params, false)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getBill.json")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(facadeToken); @@ -1289,12 +1169,11 @@ public void it_should_test_getBill_by_pos_facade() throws BitPayException { Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.POS); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("bills/" + BILL_ID, params, false); Mockito.verify(this.bitPayClient, Mockito.times(0)).get("bills/" + BILL_ID, params, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.getId()); } @Test - public void it_should_test_getBills() throws BitPayException { + public void it_should_test_getBills() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", MERCHANT_TOKEN)); @@ -1302,9 +1181,7 @@ public void it_should_test_getBills() throws BitPayException { Mockito.when(this.bitPayClient.get( "bills", params - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getBills.json")); + )).thenReturn(getPreparedJsonDataFromFile("getBills.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Client testedClass = this.getTestedClass(); @@ -1315,13 +1192,12 @@ public void it_should_test_getBills() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("bills", params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.get(0).getId()); Assertions.assertEquals(2, result.size()); } @Test - public void it_should_test_getBills_by_status() throws BitPayException { + public void it_should_test_getBills_by_status() throws BitPayApiException, BitPayGenericException { // given final String status = "complete"; @@ -1332,9 +1208,7 @@ public void it_should_test_getBills_by_status() throws BitPayException { Mockito.when(this.bitPayClient.get( "bills", params - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getBills.json")); + )).thenReturn(getPreparedJsonDataFromFile("getBills.json")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Client testedClass = this.getTestedClass(); @@ -1345,13 +1219,12 @@ public void it_should_test_getBills_by_status() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("bills", params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.get(0).getId()); Assertions.assertEquals(2, result.size()); } @Test - public void it_should_test_updateBill() throws BitPayException { + public void it_should_test_updateBill() throws BitPayApiException, BitPayGenericException { // given final String merchantToken = "AKnJyeLF1BjAfgfDbVUzHXk64N1WuDq3R9xtZouQFhSv"; final Bill bill = this.getBillExample(merchantToken); @@ -1363,9 +1236,7 @@ public void it_should_test_updateBill() throws BitPayException { Mockito.when(this.bitPayClient.update( "bills/" + BILL_ID, updateBillApiRequest - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getBill.json")); + )).thenReturn(getPreparedJsonDataFromFile("getBill.json")); Client testedClass = this.getTestedClass(); @@ -1374,21 +1245,18 @@ public void it_should_test_updateBill() throws BitPayException { // then Mockito.verify(this.bitPayClient, Mockito.times(1)).update("bills/" + BILL_ID, updateBillApiRequest); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(BILL_ID, result.getId()); } @Test - public void it_should_test_deliverBill_by_merchant_facade() throws BitPayException { + public void it_should_test_deliverBill_by_merchant_facade() throws BitPayApiException, BitPayGenericException { // given final String billToken = "billToken"; Mockito.when(this.bitPayClient.post( "bills/" + BILL_ID + "/deliveries", "{\"token\":\"billToken\"}", true - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn("Success"); + )).thenReturn("Success"); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Client testedClass = this.getTestedClass(); @@ -1402,21 +1270,18 @@ public void it_should_test_deliverBill_by_merchant_facade() throws BitPayExcepti "{\"token\":\"billToken\"}", true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("Success", result); } @Test - public void it_should_test_deliverBill_by_pos_facade() throws BitPayException { + public void it_should_test_deliverBill_by_pos_facade() throws BitPayApiException, BitPayGenericException { // given final String billToken = "billToken"; Mockito.when(this.bitPayClient.post( "bills/" + BILL_ID + "/deliveries", "{\"token\":\"billToken\"}", false - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn("Success"); + )).thenReturn("Success"); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Client testedClass = this.getTestedClass(); @@ -1430,15 +1295,13 @@ public void it_should_test_deliverBill_by_pos_facade() throws BitPayException { "{\"token\":\"billToken\"}", false ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("Success", result); } @Test - public void it_should_return_rate() throws BitPayException { + public void it_should_return_rate() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("rates/BCH/USD")).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("rates/BCH/USD")) .thenReturn("{\"code\": \"USD\", \"name\": \"US Dollar\", \"rate\": 100.99}"); Client testedClass = this.getTestedClass(); @@ -1452,11 +1315,9 @@ public void it_should_return_rate() throws BitPayException { } @Test - public void it_should_test_get_rates() throws BitPayException { + public void it_should_test_get_rates() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("rates")).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getRates.json")); + Mockito.when(this.bitPayClient.get("rates")).thenReturn(getPreparedJsonDataFromFile("getRates.json")); Client testedClass = this.getTestedClass(); @@ -1469,11 +1330,9 @@ public void it_should_test_get_rates() throws BitPayException { } @Test - public void it_should_get_rates_by_base_currency() throws BitPayException { + public void it_should_get_rates_by_base_currency() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("rates/USD")).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("getRates.json")); + Mockito.when(this.bitPayClient.get("rates/USD")).thenReturn(getPreparedJsonDataFromFile("getRates.json")); Client testedClass = this.getTestedClass(); @@ -1486,7 +1345,7 @@ public void it_should_get_rates_by_base_currency() throws BitPayException { } @Test - public void it_should_get_ledger_entries() throws BitPayException { + public void it_should_get_ledger_entries() throws BitPayApiException, BitPayGenericException { // given final String currency = "USD"; final String dateStart = "2021-5-10"; @@ -1498,8 +1357,7 @@ public void it_should_get_ledger_entries() throws BitPayException { params.add(new BasicNameValuePair("endDate", dateEnd)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); - Mockito.when(this.bitPayClient.get("ledgers/" + currency, params)).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("ledgers/" + currency, params)) .thenReturn(getPreparedJsonDataFromFile("getLedgers.json")); Client testedClass = this.getTestedClass(); @@ -1510,20 +1368,18 @@ public void it_should_get_ledger_entries() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("ledgers/" + currency, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(3, result.size()); Assertions.assertEquals("20210510_fghij", result.get(0).getDescription()); } @Test - public void it_should_test_getLedgers() throws BitPayException { + public void it_should_test_getLedgers() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", MERCHANT_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); - Mockito.when(this.bitPayClient.get("ledgers", params)).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("ledgers", params)) .thenReturn(getPreparedJsonDataFromFile("getLedgerBalances.json")); Client testedClass = this.getTestedClass(); @@ -1534,13 +1390,12 @@ public void it_should_test_getLedgers() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("ledgers", params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(3, result.size()); Assertions.assertEquals(2389.82, result.get(1).getBalance()); } @Test - public void it_should_test_submitPayoutRecipients() throws BitPayException { + public void it_should_test_submitPayoutRecipients() throws BitPayApiException, BitPayGenericException { // given PayoutRecipients recipients = getPayoutRecipientsExample(); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); @@ -1548,9 +1403,7 @@ public void it_should_test_submitPayoutRecipients() throws BitPayException { "recipients", getPreparedJsonDataFromFile("submitPayoutRecipientsRequest.json"), true - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("submitPayoutRecipientsResponse.json")); + )).thenReturn(getPreparedJsonDataFromFile("submitPayoutRecipientsResponse.json")); Client testedClass = this.getTestedClass(); @@ -1564,7 +1417,6 @@ public void it_should_test_submitPayoutRecipients() throws BitPayException { getPreparedJsonDataFromFile("submitPayoutRecipientsRequest.json"), true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(2, result.size()); Assertions.assertEquals( RECIPIENT_TOKEN, @@ -1573,7 +1425,7 @@ public void it_should_test_submitPayoutRecipients() throws BitPayException { } @Test - public void it_should_test_getPayoutRecipients() throws BitPayException { + public void it_should_test_getPayoutRecipients() throws BitPayApiException, BitPayGenericException { // given final String status = "invited"; final Integer limit = 1; @@ -1587,8 +1439,7 @@ public void it_should_test_getPayoutRecipients() throws BitPayException { params.add(new BasicNameValuePair("limit", limit.toString())); params.add(new BasicNameValuePair("offset", offset.toString())); - Mockito.when(this.bitPayClient.get("recipients", params, true)).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("recipients", params, true)) .thenReturn(getPreparedJsonDataFromFile("retrieveRecipientsResponse.json")); Client testedClass = this.getTestedClass(); @@ -1599,7 +1450,6 @@ public void it_should_test_getPayoutRecipients() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("recipients", params, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(2, result.size()); Assertions.assertEquals( RECIPIENT_TOKEN, @@ -1608,14 +1458,13 @@ public void it_should_test_getPayoutRecipients() throws BitPayException { } @Test - public void it_should_test_getPayoutRecipient() throws BitPayException { + public void it_should_test_getPayoutRecipient() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", PAYOUT_ACCESS_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); - Mockito.when(this.bitPayClient.get("recipients/" + RECIPIENT_ID, params, true)).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("recipients/" + RECIPIENT_ID, params, true)) .thenReturn(getPreparedJsonDataFromFile("retrieveRecipientResponse.json")); Client testedClass = this.getTestedClass(); @@ -1627,7 +1476,6 @@ public void it_should_test_getPayoutRecipient() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("recipients/" + RECIPIENT_ID, params, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals( RECIPIENT_TOKEN, result.getToken() @@ -1635,7 +1483,7 @@ public void it_should_test_getPayoutRecipient() throws BitPayException { } @Test - public void it_should_test_updatePayoutRecipient() throws BitPayException { + public void it_should_test_updatePayoutRecipient() throws BitPayApiException, BitPayGenericException { // given final PayoutRecipient payoutRecipient = new PayoutRecipient(); final String label = "Bob123"; @@ -1647,9 +1495,7 @@ public void it_should_test_updatePayoutRecipient() throws BitPayException { Mockito.when(this.bitPayClient.update( "recipients/" + RECIPIENT_ID, getPreparedJsonDataFromFile("updatePayoutRecipientRequest.json") - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("retrieveRecipientResponse.json")); + )).thenReturn(getPreparedJsonDataFromFile("retrieveRecipientResponse.json")); Client testedClass = this.getTestedClass(); @@ -1663,20 +1509,18 @@ public void it_should_test_updatePayoutRecipient() throws BitPayException { "recipients/" + RECIPIENT_ID, getPreparedJsonDataFromFile("updatePayoutRecipientRequest.json") ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(RECIPIENT_TOKEN, result.getToken()); Assertions.assertEquals(label, result.getLabel()); } @Test - public void it_should_test_deletePayoutRecipient() throws BitPayException { + public void it_should_test_deletePayoutRecipient() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", PAYOUT_ACCESS_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); - Mockito.when(this.bitPayClient.delete("recipients/" + RECIPIENT_ID, params)).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.delete("recipients/" + RECIPIENT_ID, params)) .thenReturn(getPreparedJsonDataFromFile("success.json")); Client testedClass = this.getTestedClass(); @@ -1690,12 +1534,11 @@ public void it_should_test_deletePayoutRecipient() throws BitPayException { "recipients/" + RECIPIENT_ID, params ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_test_requestPayoutRecipientNotification() throws BitPayException { + public void it_should_test_requestPayoutRecipientNotification() throws BitPayApiException, BitPayGenericException { // given final String requestJson = "{\"token\":\"3LKKrrNB2BcVAu2Y24QQ78GrKUk2ANLK4eLo85Q1a2HU\"}"; @@ -1704,9 +1547,7 @@ public void it_should_test_requestPayoutRecipientNotification() throws BitPayExc "recipients/" + RECIPIENT_ID + "/notifications", requestJson, true - )).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) - .thenReturn(getPreparedJsonDataFromFile("success.json")); + )).thenReturn(getPreparedJsonDataFromFile("success.json")); Client testedClass = this.getTestedClass(); @@ -1721,20 +1562,17 @@ public void it_should_test_requestPayoutRecipientNotification() throws BitPayExc requestJson, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_test_submitPayout() throws BitPayException { + public void it_should_test_submitPayout() throws BitPayApiException, BitPayGenericException { // given final String requestJson = getPreparedJsonDataFromFile("submitPayoutRequest.json"); Payout payout = getPayoutExample(); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts", requestJson, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("submitPayoutResponse.json")); Client testedClass = this.getTestedClass(); @@ -1746,7 +1584,6 @@ public void it_should_test_submitPayout() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("payouts", requestJson, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals( "6RZSTPtnzEaroAe2X4YijenRiqteRDNvzbT8NjtcHjUVd9FUFwa7dsX8RFgRDDC5SL", result.getToken() @@ -1754,15 +1591,13 @@ public void it_should_test_submitPayout() throws BitPayException { } @Test - public void it_should_test_submitPayoutGroup() throws BitPayException, ParseException { + public void it_should_test_submitPayoutGroup() throws BitPayApiException, BitPayGenericException, ParseException { // given final String requestJson = getPreparedJsonDataFromFile("submitPayoutGroupRequest.json"); Collection payouts = Arrays.asList(getPayoutExample(), getPayoutExample()); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts/group", requestJson, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("submitPayoutGroupResponse.json")); Client testedClass = this.getTestedClass(); @@ -1774,7 +1609,6 @@ public void it_should_test_submitPayoutGroup() throws BitPayException, ParseExce // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).post("payouts/group", requestJson, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertFalse(result.getPayouts().isEmpty()); Payout firstPayout = result.getPayouts().get(0); @@ -1814,15 +1648,13 @@ public void it_should_test_submitPayoutGroup() throws BitPayException, ParseExce } @Test - public void it_should_test_getPayout() throws BitPayException { + public void it_should_test_getPayout() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", PAYOUT_ACCESS_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.get("payouts/" + PAYOUT_ID, params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("submitPayoutResponse.json")); Client testedClass = this.getTestedClass(); @@ -1836,20 +1668,17 @@ public void it_should_test_getPayout() throws BitPayException { Mockito.verify(this.bitPayClient, Mockito.times(1)).get( "payouts/" + PAYOUT_ID, params, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(PAYOUT_ID, result.getId()); } @Test - public void it_should_test_cancelPayout() throws BitPayException { + public void it_should_test_cancelPayout() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", PAYOUT_ACCESS_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.delete("payouts/" + PAYOUT_ID, params)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("success.json")); Client testedClass = this.getTestedClass(); @@ -1861,22 +1690,18 @@ public void it_should_test_cancelPayout() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("payouts/" + PAYOUT_ID, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_test_cancelPayoutGroup() throws BitPayException { + public void it_should_test_cancelPayoutGroup() throws BitPayApiException, BitPayGenericException { // given final List params = new ArrayList(); params.add(new BasicNameValuePair("token", PAYOUT_ACCESS_TOKEN)); Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.delete("payouts/group/" + PAYOUT_ID, params)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("cancelPayoutGroupResponse.json")); - Client testedClass = this.getTestedClass(); // when @@ -1886,7 +1711,6 @@ public void it_should_test_cancelPayoutGroup() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).delete("payouts/group/" + PAYOUT_ID, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertFalse(result.getPayouts().isEmpty()); Assertions.assertFalse(result.getFailed().isEmpty()); @@ -1897,7 +1721,7 @@ public void it_should_test_cancelPayoutGroup() throws BitPayException { } @Test - public void it_should_test_getPayouts() throws BitPayException { + public void it_should_test_getPayouts() throws BitPayApiException, BitPayGenericException { // given final String startDate = "2021-05-27"; final String endDate = "2021-05-31"; @@ -1913,8 +1737,6 @@ public void it_should_test_getPayouts() throws BitPayException { Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.get("payouts", params, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getPayouts.json")); Client testedClass = this.getTestedClass(); @@ -1933,12 +1755,11 @@ public void it_should_test_getPayouts() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.PAYOUT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("payouts", params, true); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(2, result.size()); } @Test - public void it_should_test_requestPayoutNotification() throws BitPayException { + public void it_should_test_requestPayoutNotification() throws BitPayApiException, BitPayGenericException { // given final String requestJson = "{\"token\":\"3LKKrrNB2BcVAu2Y24QQ78GrKUk2ANLK4eLo85Q1a2HU\"}"; @@ -1947,8 +1768,6 @@ public void it_should_test_requestPayoutNotification() throws BitPayException { Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts/" + PAYOUT_ID + "/notifications", requestJson, true)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("success.json")); Client testedClass = this.getTestedClass(); @@ -1963,12 +1782,11 @@ public void it_should_test_requestPayoutNotification() throws BitPayException { requestJson, true ); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertTrue(result); } @Test - public void it_should_test_getSettlements() throws BitPayException { + public void it_should_test_getSettlements() throws BitPayApiException, BitPayGenericException { // given final String currency = "USD"; final String dateStart = "2021-5-10"; @@ -1988,8 +1806,6 @@ public void it_should_test_getSettlements() throws BitPayException { Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("settlements", params)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getSettlementsResponse.json")); Client testedClass = this.getTestedClass(); @@ -2007,13 +1823,12 @@ public void it_should_test_getSettlements() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("settlements", params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(2, result.size()); Assertions.assertEquals("KBkdURgmE3Lsy9VTnavZHX", result.get(0).getId()); } @Test - public void it_should_test_getSettlement() throws BitPayException { + public void it_should_test_getSettlement() throws BitPayApiException, BitPayGenericException { // given final String settlementId = "DNFnN3fFjjzLn6if5bdGJC"; @@ -2022,8 +1837,6 @@ public void it_should_test_getSettlement() throws BitPayException { Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("settlements/" + settlementId, params)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getSettlementResponse.json")); Client testedClass = this.getTestedClass(); @@ -2034,12 +1847,11 @@ public void it_should_test_getSettlement() throws BitPayException { // then Mockito.verify(this.accessTokens, Mockito.times(1)).getAccessToken(Facade.MERCHANT); Mockito.verify(this.bitPayClient, Mockito.times(1)).get("settlements/" + settlementId, params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("RPWTabW8urd3xWv2To989v", result.getId()); } @Test - public void it_should_test_getSettlementReconciliationReport() throws BitPayException { + public void it_should_test_getSettlementReconciliationReport() throws BitPayApiException, BitPayGenericException { // given final String settlementId = "DNFnN3fFjjzLn6if5bdGJC"; final String settlementToken = "5T1T5yGDEtFDYe8jEVBSYLHKewPYXZrDLvZxtXBzn69fBbZYitYQYH4BFYFvvaVU7D"; @@ -2048,8 +1860,6 @@ public void it_should_test_getSettlementReconciliationReport() throws BitPayExce params.add(new BasicNameValuePair("token", settlementToken)); Mockito.when(this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params)) - .thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) .thenReturn(getPreparedJsonDataFromFile("getSettlementReconciliationReportResponse.json")); Client testedClass = this.getTestedClass(); @@ -2060,16 +1870,14 @@ public void it_should_test_getSettlementReconciliationReport() throws BitPayExce // then Mockito.verify(this.bitPayClient, Mockito.times(1)) .get("settlements/" + settlementId + "/reconciliationreport", params); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals("RvNuCTMAkURKimwgvSVEMP", result.getId()); Assertions.assertEquals(2389.82F, result.getTotalAmount()); } @Test - public void it_should_test_getSupportedWallets() throws BitPayException { + public void it_should_test_getSupportedWallets() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("supportedwallets")).thenReturn(this.httpResponse); - Mockito.when(this.bitPayClient.responseToJsonString(this.httpResponse)) + Mockito.when(this.bitPayClient.get("supportedwallets")) .thenReturn(getPreparedJsonDataFromFile("getSupportedWalletsResponse.json")); Client testedClass = this.getTestedClass(); @@ -2079,7 +1887,6 @@ public void it_should_test_getSupportedWallets() throws BitPayException { // then Mockito.verify(this.bitPayClient, Mockito.times(1)).get("supportedwallets"); - Mockito.verify(this.bitPayClient, Mockito.times(1)).responseToJsonString(this.httpResponse); Assertions.assertEquals(7, result.size()); Assertions.assertEquals("bitpay-wallet.png", result.get(0).getAvatar()); Assertions.assertEquals("https://bitpay.com/img/wallet-logos/bitpay-wallet.png", result.get(0).getImage()); @@ -2099,18 +1906,6 @@ public void it_should_get_http_client() { ); } - @Test - public void it_should_test_setLoggerLevel() { - // given - Client testedClass = this.getTestedClass(); - - // when - testedClass.setLoggerLevel(3); - - // then - Mockito.verify(this.bitPayClient, Mockito.times(1)).setLoggerLevel(3); - } - private Invoice getInvoiceExample() { Invoice invoice = new Invoice(10.0, "USD"); invoice.setOrderId(EXAMPLE_UUID); @@ -2137,7 +1932,7 @@ private Invoice getInvoiceExample() { return invoice; } - private Bill getBillExample(String merchantToken) throws BitPayException { + private Bill getBillExample(String merchantToken) throws BitPayGenericException { List cc = new ArrayList(); cc.add("jane@doe.com"); @@ -2189,7 +1984,7 @@ private PayoutRecipients getPayoutRecipientsExample() { return new PayoutRecipients(recipients); } - private Payout getPayoutExample() throws BitPayException { + private Payout getPayoutExample() throws BitPayGenericException { final Payout payout = new Payout(); payout.setAmount(10.00); payout.setCurrency("USD"); @@ -2205,8 +2000,6 @@ private Payout getPayoutExample() throws BitPayException { } /** - * Be aware. I removed "data" index from original request - * * @param fileName filename of json * @return json response */ diff --git a/src/test/java/com/bitpay/sdk/ConfigTest.java b/src/test/java/com/bitpay/sdk/ConfigTest.java index 5a2964df..48ba5d84 100644 --- a/src/test/java/com/bitpay/sdk/ConfigTest.java +++ b/src/test/java/com/bitpay/sdk/ConfigTest.java @@ -58,7 +58,7 @@ public void it_should_returns_bitpay_api_version() { @Test public void it_should_returns_bitpay_plugin_info() { - Assertions.assertTrue(Config.BITPAY_PLUGIN_INFO.contains("BitPay_Java_Client_v9")); + Assertions.assertTrue(Config.BITPAY_PLUGIN_INFO.contains("BitPay_Java_Client_v10.0.0")); } @Test diff --git a/src/test/java/com/bitpay/sdk/client/AuthorizationClientTest.java b/src/test/java/com/bitpay/sdk/client/AuthorizationClientTest.java index af347ad0..b1a8447b 100644 --- a/src/test/java/com/bitpay/sdk/client/AuthorizationClientTest.java +++ b/src/test/java/com/bitpay/sdk/client/AuthorizationClientTest.java @@ -4,6 +4,8 @@ package com.bitpay.sdk.client; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.Facade; import com.bitpay.sdk.util.TokenContainer; import java.io.IOException; @@ -17,7 +19,7 @@ public AuthorizationClientTest() throws IOException { } @Test - public void it_should_authorize_client_by_facade() throws IOException { + public void it_should_authorize_client_by_facade() throws BitPayGenericException, BitPayApiException { // given Mockito.when(this.uuidGenerator.execute()).thenReturn(AbstractClientTest.EXAMPLE_UUID); this.addServerJsonResponse( diff --git a/src/test/java/com/bitpay/sdk/client/BitPayClientTest.java b/src/test/java/com/bitpay/sdk/client/BitPayClientTest.java index 3022dca7..1499ecd4 100644 --- a/src/test/java/com/bitpay/sdk/client/BitPayClientTest.java +++ b/src/test/java/com/bitpay/sdk/client/BitPayClientTest.java @@ -5,14 +5,13 @@ package com.bitpay.sdk.client; import com.bitpay.sdk.Config; -import com.bitpay.sdk.exceptions.BitPayException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; @@ -20,7 +19,6 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.bitcoinj.core.ECKey; import org.junit.jupiter.api.Assertions; @@ -42,7 +40,8 @@ public class BitPayClientTest { private HttpRequestFactory httpRequestFactory; @Test - public void it_should_test_get_request_without_parameters() throws IOException, URISyntaxException { + public void it_should_prepare_get_request_without_parameters() + throws IOException, URISyntaxException { // given final String testUrl = "/test"; final HttpGet httpGet = Mockito.mock(HttpGet.class); @@ -54,7 +53,11 @@ public void it_should_test_get_request_without_parameters() throws IOException, BitPayClient testedClass = this.getTestedClass(); // when - testedClass.get(testUrl, parameters); + try { + testedClass.get(testUrl, parameters); + } catch (Exception e) { + // missing response + } // then Mockito.verify(httpClient, Mockito.times(1)).execute(httpGet); @@ -71,7 +74,8 @@ public void it_should_test_get_request_without_parameters() throws IOException, } @Test - public void it_should_test_get_request_with_parameters() throws IOException, URISyntaxException { + public void it_should_prepare_get_request_with_parameters() + throws IOException, URISyntaxException { // given final String testUrl = "/test"; final HttpGet httpGet = Mockito.mock(HttpGet.class); @@ -83,7 +87,11 @@ public void it_should_test_get_request_with_parameters() throws IOException, URI BitPayClient testedClass = this.getTestedClass(); // when - testedClass.get(testUrl, parameters, true); + try { + testedClass.get(testUrl, parameters, true); + } catch (Exception e) { + // missing response + } // then Mockito.verify(httpClient, Mockito.times(1)).execute(httpGet); @@ -100,10 +108,9 @@ public void it_should_test_get_request_with_parameters() throws IOException, URI } @Test - public void it_should_throws_bitpayexception_when_something_is_wrong_for_get_requests() - throws IOException, URISyntaxException { - BitPayException exception1 = Assertions.assertThrows( - BitPayException.class, + public void it_should_throws_bitpay_api_exception_when_something_is_wrong_for_get_requests() { + BitPayApiException exception1 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpGet httpGet = Mockito.mock(HttpGet.class); Mockito.when(this.httpRequestFactory.createHttpGet(ArgumentMatchers.anyString())).thenReturn(httpGet); @@ -113,13 +120,12 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_get_req ); Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: GET failed\n" + - "Illegal character in hostname at index 16: http://localhost[/]?", + "Illegal character in hostname at index 16: http://localhost[/]?", exception1.getMessage() ); - BitPayException exception2 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception2 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpGet httpGet = Mockito.mock(HttpGet.class); Mockito.when(this.httpRequestFactory.createHttpGet(ArgumentMatchers.anyString())).thenReturn(httpGet); @@ -130,15 +136,11 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_get_req } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: GET failed\n" + - "Exception message", - exception2.getMessage() - ); + Assertions.assertEquals("Exception message", exception2.getMessage()); - BitPayException exception3 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception3 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpGet httpGet = Mockito.mock(HttpGet.class); Mockito.when(this.httpRequestFactory.createHttpGet(ArgumentMatchers.anyString())).thenReturn(httpGet); @@ -149,18 +151,14 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_get_req "Error: GET failed\n" + "Exception message" ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: GET failed\n" + - "Exception message", - exception3.getMessage() - ); + Assertions.assertEquals("Exception message", exception3.getMessage()); } @Test - public void it_should_throws_bitpayexception_when_something_is_wrong_for_delete_requests() + public void it_should_throws_bitpay_api_exception_when_something_is_wrong_for_delete_requests() throws IOException, URISyntaxException { - BitPayException exception1 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception1 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpDelete httpDelete = Mockito.mock(HttpDelete.class); Mockito.when(this.httpRequestFactory.createHttpDelete(ArgumentMatchers.anyString())) @@ -171,13 +169,12 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_delete_ ); Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: DELETE failed\n" + "Illegal character in hostname at index 16: http://localhost[/]?", exception1.getMessage() ); - BitPayException exception2 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception2 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpDelete httpDelete = Mockito.mock(HttpDelete.class); Mockito.when(this.httpRequestFactory.createHttpDelete(ArgumentMatchers.anyString())) @@ -190,13 +187,12 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_delete_ ); Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: DELETE failed\n" + - "Exception message", + "Exception message", exception2.getMessage() ); - BitPayException exception3 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception3 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpDelete httpDelete = Mockito.mock(HttpDelete.class); Mockito.when(this.httpRequestFactory.createHttpDelete(ArgumentMatchers.anyString())) @@ -207,18 +203,14 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_delete_ } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: DELETE failed\n" + - "Exception message", - exception3.getMessage() - ); + Assertions.assertEquals("Exception message", exception3.getMessage()); } @Test - public void it_should_throws_bitpayexception_when_something_is_wrong_for_post_requests() + public void it_should_throws_bitpay_api_exception_when_something_is_wrong_for_post_requests() throws IOException, URISyntaxException { - BitPayException exception1 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception1 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpPost httpPost = Mockito.mock(HttpPost.class); Mockito.when(this.httpRequestFactory.createHttpPost(ArgumentMatchers.anyString())) @@ -230,15 +222,11 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_post_re } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: POST failed\n" + - "Exception message", - exception1.getMessage() - ); + Assertions.assertEquals("Exception message", exception1.getMessage()); - BitPayException exception2 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception2 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpPost httpPost = Mockito.mock(HttpPost.class); Mockito.when(this.httpRequestFactory.createHttpPost(ArgumentMatchers.anyString())) @@ -249,18 +237,14 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_post_re } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: POST failed\n" + - "Exception message", - exception2.getMessage() - ); + Assertions.assertEquals("Exception message", exception2.getMessage()); } @Test - public void it_should_throws_bitpayexception_when_something_is_wrong_for_put_requests() + public void it_should_throws_bitpay_api_exception_when_something_is_wrong_for_put_requests() throws IOException, URISyntaxException { - BitPayException exception1 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception1 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpPut httpPut = Mockito.mock(HttpPut.class); Mockito.when(this.httpRequestFactory.createHttpPut(ArgumentMatchers.anyString())) @@ -272,14 +256,10 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_put_req } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: PUT failed\n" - + "Exception message", - exception1.getMessage() - ); + Assertions.assertEquals("Exception message", exception1.getMessage()); - BitPayException exception2 = Assertions.assertThrows( - BitPayException.class, + BitPayApiException exception2 = Assertions.assertThrows( + BitPayApiException.class, () -> { final HttpPut httpPut = Mockito.mock(HttpPut.class); Mockito.when(this.httpRequestFactory.createHttpPut(ArgumentMatchers.anyString())) @@ -290,15 +270,11 @@ public void it_should_throws_bitpayexception_when_something_is_wrong_for_put_req } ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: PUT failed\n" + - "Exception message", - exception2.getMessage() - ); + Assertions.assertEquals("Exception message", exception2.getMessage()); } @Test - public void it_should_test_delete_request() throws IOException, URISyntaxException { + public void it_should_prepare_delete_request() throws IOException, URISyntaxException { // given final String testUrl = "/test"; final HttpDelete httpDelete = Mockito.mock(HttpDelete.class); @@ -310,7 +286,11 @@ public void it_should_test_delete_request() throws IOException, URISyntaxExcepti BitPayClient testedClass = this.getTestedClass(); // when - testedClass.delete(testUrl, parameters); + try { + testedClass.delete(testUrl, parameters); + } catch (Exception e) { + // missing response + } // then Mockito.verify(httpClient, Mockito.times(1)).execute(httpDelete); @@ -327,7 +307,7 @@ public void it_should_test_delete_request() throws IOException, URISyntaxExcepti } @Test - public void it_should_test_post_request() throws IOException, URISyntaxException { + public void it_should_prepare_post_request() throws IOException { // given final String testUrl = "/test"; final String json = "{\"key\": \"value\"}"; @@ -338,7 +318,11 @@ public void it_should_test_post_request() throws IOException, URISyntaxException BitPayClient testedClass = this.getTestedClass(); // when - testedClass.post(testUrl, json, true); + try { + testedClass.post(testUrl, json, true); + } catch (Exception e) { + // missing response + } // then Mockito.verify(httpClient, Mockito.times(1)).execute(httpPost); @@ -355,7 +339,7 @@ public void it_should_test_post_request() throws IOException, URISyntaxException } @Test - public void it_should_test_put_request() throws IOException, URISyntaxException { + public void it_should_prepare_put_request() throws BitPayGenericException, BitPayApiException, IOException { // given final String testUrl = "/test"; final String json = "{\"key\": \"value\"}"; @@ -366,7 +350,11 @@ public void it_should_test_put_request() throws IOException, URISyntaxException BitPayClient testedClass = this.getTestedClass(); // when - testedClass.update(testUrl, json); + try { + testedClass.update(testUrl, json); + } catch (Exception e) { + // missing response + } // then Mockito.verify(httpClient, Mockito.times(1)).execute(httpPut); @@ -382,107 +370,7 @@ public void it_should_test_put_request() throws IOException, URISyntaxException .addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); } - @Test - public void it_should_test_convert_httpResponse_for_json() throws BitPayException { - // given - final String responseContent = "{\"status\":\"success\",\"data\":{},\"message\":null}"; - BitPayClient testedClass = this.getTestedClass(); - HttpResponse httpResponse = Mockito.mock(HttpResponse.class); - HttpEntity httpEntity = new StringEntity(responseContent, "UTF-8"); - Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); - - // when - String result = testedClass.responseToJsonString(httpResponse); - - // then - Assertions.assertEquals(responseContent, result); - } - - @Test - public void it_should_return_data_value_from_httpResponse() throws BitPayException { - // given - final String responseContent = "{\"status\":\"success\",\"data\":{\"my\":\"data\"},\"message\":null}"; - BitPayClient testedClass = this.getTestedClass(); - HttpResponse httpResponse = Mockito.mock(HttpResponse.class); - HttpEntity httpEntity = new StringEntity(responseContent, "UTF-8"); - Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); - - // when - String result = testedClass.responseToJsonString(httpResponse); - - // then - Assertions.assertEquals("{\"my\":\"data\"}", result); - } - - @Test - public void it_should_throws_bitpayexception_for_missing_response() { - BitPayException exception = Assertions.assertThrows( - BitPayException.class, - () -> { - // given - BitPayClient testedClass = this.getTestedClass(); - - // when - testedClass.responseToJsonString(null); - } - ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error: HTTP response is null", - exception.getMessage() - ); - } - - @Test - public void it_should_throws_bitpayexception_for_response_with_error() { - BitPayException exception = Assertions.assertThrows( - BitPayException.class, - () -> { - // given - final String responseContent = "{\"code\":\"500\",\"status\":\"error\",\"data\":{},\"message\":\"Error message text\"}"; - BitPayClient testedClass = this.getTestedClass(); - HttpResponse httpResponse = Mockito.mock(HttpResponse.class); - HttpEntity httpEntity = new StringEntity(responseContent, "UTF-8"); - Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); - // when - String result = testedClass.responseToJsonString(httpResponse); - - // then - Assertions.assertEquals(responseContent, result); - } - ); - Assertions.assertEquals( - "Status: 500 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Error message text", - exception.getMessage() - ); - - } - - @Test - public void it_should_throws_bitpayexception_for_response_with_errors() { - BitPayException exception = Assertions.assertThrows( - BitPayException.class, - () -> { - // given - final String responseContent = "{\"code\":\"500\",\"status\":\"errors\",\"errors\":[\"Error1\", \"Error2\"],\"data\":{},\"message\":\"Error message text\"}"; - BitPayClient testedClass = this.getTestedClass(); - HttpResponse httpResponse = Mockito.mock(HttpResponse.class); - HttpEntity httpEntity = new StringEntity(responseContent, "UTF-8"); - Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); - - // when - String result = testedClass.responseToJsonString(httpResponse); - - // then - Assertions.assertEquals(responseContent, result); - } - ); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> Multiple errors:\n" + - "Error1\n" + "Error2", - exception.getMessage() - ); - } private BitPayClient getTestedClass() { return new BitPayClient(this.httpClient, this.httpRequestFactory, BASE_URL, new ECKey()); diff --git a/src/test/java/com/bitpay/sdk/client/RateClientTest.java b/src/test/java/com/bitpay/sdk/client/RateClientTest.java index 0fbead0b..40bb137d 100644 --- a/src/test/java/com/bitpay/sdk/client/RateClientTest.java +++ b/src/test/java/com/bitpay/sdk/client/RateClientTest.java @@ -4,7 +4,8 @@ package com.bitpay.sdk.client; -import com.bitpay.sdk.exceptions.RateQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.rate.Rate; import com.bitpay.sdk.model.rate.Rates; import org.junit.jupiter.api.Assertions; @@ -13,7 +14,7 @@ public class RateClientTest extends AbstractClientTest { @Test - public void it_should_get_rate() throws RateQueryException { + public void it_should_get_rate() throws BitPayGenericException, BitPayApiException { // given this.addServerJsonResponse( "/rates/BCH/USD", @@ -30,7 +31,7 @@ public void it_should_get_rate() throws RateQueryException { } @Test - public void it_should_get_rates() throws RateQueryException { + public void it_should_get_rates() throws BitPayGenericException, BitPayApiException { // given this.addServerJsonResponse( "/rates", @@ -48,7 +49,7 @@ public void it_should_get_rates() throws RateQueryException { } @Test - public void it_should_get_rates_by_base_currency() throws RateQueryException { + public void it_should_get_rates_by_base_currency() throws BitPayGenericException, BitPayApiException { // given this.addServerJsonResponse( "/rates/BTC", diff --git a/src/test/java/com/bitpay/sdk/client/ResponseParserTest.java b/src/test/java/com/bitpay/sdk/client/ResponseParserTest.java new file mode 100644 index 00000000..7ae84028 --- /dev/null +++ b/src/test/java/com/bitpay/sdk/client/ResponseParserTest.java @@ -0,0 +1,86 @@ +package com.bitpay.sdk.client; + +import com.bitpay.sdk.exceptions.BitPayApiException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class ResponseParserTest { + + @Test + public void it_should_test_convert_success_response() throws BitPayApiException { + // given + final String responseContent = "{\"status\":\"success\",\"data\":{},\"message\":null}"; + + // when + String result = ResponseParser.getJsonDataFromJsonResponse(responseContent); + + // then + Assertions.assertEquals(responseContent, result); + } + + @Test + public void it_should_return_data_value_from_json_response() throws BitPayApiException { + // given + final String responseContent = "{\"status\":\"success\",\"data\":{\"my\":\"data\"},\"message\":null}"; + + // when + String result = ResponseParser.getJsonDataFromJsonResponse(responseContent); + + // then + Assertions.assertEquals("{\"my\":\"data\"}", result); + } + + @Test + public void it_should_throws_bitpay_api_exception_for_missing_response() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, + () -> { + // given + + + // when + ResponseParser.getJsonDataFromJsonResponse(null); + } + ); + Assertions.assertEquals( + "HTTP response is null", + exception.getMessage() + ); + } + + @Test + public void it_should_throws_bitpay_api_exception_for_response_with_error() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, + () -> { + // given + final String responseContent = "{\"code\":\"500\",\"status\":\"error\",\"data\":{},\"message\":\"Error message text\"}"; + + // when + String result = ResponseParser.getJsonDataFromJsonResponse(responseContent); + + // then + Assertions.assertEquals(responseContent, result); + } + ); + Assertions.assertEquals("Error message text", exception.getMessage()); + } + + @Test + public void it_should_throws_bitpay_api_exception_for_response_with_errors() { + BitPayApiException exception = Assertions.assertThrows( + BitPayApiException.class, + () -> { + // given + final String responseContent = "{\"code\":\"500\",\"status\":\"error\",\"data\":{},\"message\":\"Error message text\"}"; + + // when + String result = ResponseParser.getJsonDataFromJsonResponse(responseContent); + + // then + Assertions.assertEquals(responseContent, result); + } + ); + Assertions.assertEquals("Error message text", exception.getMessage()); + } +} diff --git a/src/test/java/com/bitpay/sdk/client/SettlementClientTest.java b/src/test/java/com/bitpay/sdk/client/SettlementClientTest.java index 78bc0811..30daf338 100644 --- a/src/test/java/com/bitpay/sdk/client/SettlementClientTest.java +++ b/src/test/java/com/bitpay/sdk/client/SettlementClientTest.java @@ -4,8 +4,9 @@ package com.bitpay.sdk.client; +import com.bitpay.sdk.exceptions.BitPayApiException; import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.SettlementQueryException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.settlement.Settlement; import java.util.List; import org.junit.jupiter.api.Assertions; @@ -17,7 +18,7 @@ public class SettlementClientTest extends AbstractClientTest { public void it_should_get_settlements() throws BitPayException { // given this.addServerJsonResponse( - "/settlements?token=someMerchantToken&startDate=2021-5-10&endDate=2021-5-12&status=processing&limit=100&offset=0", + "/settlements?token=someMerchantToken&startDate=2021-5-10&endDate=2021-5-12&status=processing", "GET", null, getPreparedJsonDataFromFile("getSettlementsResponse.json") @@ -120,7 +121,7 @@ public void it_should_get_settlement() throws BitPayException { } @Test - public void it_should_get_settlement_reconciliation_report() throws SettlementQueryException { + public void it_should_get_settlement_reconciliation_report() throws BitPayGenericException, BitPayApiException { // given this.addServerJsonResponse( // diff --git a/src/test/java/com/bitpay/sdk/exceptions/AbstractTestException.java b/src/test/java/com/bitpay/sdk/exceptions/AbstractTestException.java deleted file mode 100644 index d3589584..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/AbstractTestException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 BitPay - */ - -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; - -abstract class AbstractTestException { - - protected static final String REASON_MESSAGE = "reasonMsg"; - protected static final String STATUS_CODE = "123456"; - - protected void testExceptionWithoutStatus( - final BitPayException exception, - final String message, - final String code - ) { - Assertions.assertEquals( - code + ": " + message + " -> " + REASON_MESSAGE, - exception.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: " + code + ": " + message + " -> " + REASON_MESSAGE, - exception.getMessage()); - Assertions.assertNull(exception.getStatusCode()); - } - - protected void testExceptionWithStatus( - final BitPayException exception, - final String message, - final String code - ) { - Assertions.assertEquals( - code + ": " + message + " -> " + REASON_MESSAGE, - exception.getReasonPhrase()); - Assertions.assertEquals( - "Status: " + STATUS_CODE + " -> Reason: " + code + ": " + message + " -> " + REASON_MESSAGE, - exception.getMessage()); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/BillCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BillCreationExceptionTest.java deleted file mode 100644 index d9b2d840..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BillCreationExceptionTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BillCreationExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BillCreationException testedClass = this.getTestedClass(null, message); - - // then - Assertions - .assertEquals("BITPAY-BILL-CREATE: Failed to create bill -> reasonMsg", testedClass.getReasonPhrase()); - Assertions.assertEquals("Status: 000000 -> Reason: BITPAY-BILL-CREATE: Failed to create bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BillCreationException testedClass = this.getTestedClass(status, message); - - // then - Assertions - .assertEquals("BITPAY-BILL-CREATE: Failed to create bill -> reasonMsg", testedClass.getReasonPhrase()); - Assertions.assertEquals("Status: 123456 -> Reason: BITPAY-BILL-CREATE: Failed to create bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BillCreationException getTestedClass(String status, String message) { - return new BillCreationException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/BillDeliveryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BillDeliveryExceptionTest.java deleted file mode 100644 index 279c0457..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BillDeliveryExceptionTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BillDeliveryExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BillDeliveryException testedClass = this.getTestedClass(null, message); - - // then - Assertions - .assertEquals("BITPAY-BILL-DELIVERY: Failed to deliver bill -> reasonMsg", testedClass.getReasonPhrase()); - Assertions.assertEquals("Status: 000000 -> Reason: BITPAY-BILL-DELIVERY: Failed to deliver bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BillDeliveryException testedClass = this.getTestedClass(status, message); - - // then - Assertions - .assertEquals("BITPAY-BILL-DELIVERY: Failed to deliver bill -> reasonMsg", testedClass.getReasonPhrase()); - Assertions.assertEquals("Status: 123456 -> Reason: BITPAY-BILL-DELIVERY: Failed to deliver bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BillDeliveryException getTestedClass(String status, String message) { - return new BillDeliveryException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/BillExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BillExceptionTest.java deleted file mode 100644 index 87f40530..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BillExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BillExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BillException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-GENERIC: An unexpected error occurred while trying to manage the bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-BILL-GENERIC: An unexpected error occurred while trying to manage the bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BillException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-GENERIC: An unexpected error occurred while trying to manage the bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-BILL-GENERIC: An unexpected error occurred while trying to manage the bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BillException getTestedClass(String status, String message) { - return new BillException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/BillQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BillQueryExceptionTest.java deleted file mode 100644 index ce2a4780..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BillQueryExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BillQueryExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BillQueryException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-GET: Failed to retrieve bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-BILL-GET: Failed to retrieve bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BillQueryException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-GET: Failed to retrieve bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-BILL-GET: Failed to retrieve bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BillQueryException getTestedClass(String status, String message) { - return new BillQueryException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/BillUpdateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BillUpdateExceptionTest.java deleted file mode 100644 index 0c7f0268..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BillUpdateExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BillUpdateExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BillUpdateException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-UPDATE: Failed to update bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-BILL-UPDATE: Failed to update bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BillUpdateException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-BILL-UPDATE: Failed to update bill -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-BILL-UPDATE: Failed to update bill -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BillUpdateException getTestedClass(String status, String message) { - return new BillUpdateException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/BitPayExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/BitPayExceptionTest.java deleted file mode 100644 index 251fd402..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/BitPayExceptionTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class BitPayExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - BitPayException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals("reasonMsg", testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - BitPayException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-GENERIC: Unexpected Bitpay exeption. -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private BitPayException getTestedClass(String status, String message) { - return new BitPayException(status, message); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/CurrencyExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/CurrencyExceptionTest.java deleted file mode 100644 index 66f3138e..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/CurrencyExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class CurrencyExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - CurrencyException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-CURRENCY-GENERIC: An unexpected error occurred while trying to manage the currencies -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-CURRENCY-GENERIC: An unexpected error occurred while trying to manage the currencies -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - CurrencyException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-CURRENCY-GENERIC: An unexpected error occurred while trying to manage the currencies -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-CURRENCY-GENERIC: An unexpected error occurred while trying to manage the currencies -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private CurrencyException getTestedClass(String status, String message) { - return new CurrencyException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/CurrencyQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/CurrencyQueryExceptionTest.java deleted file mode 100644 index 331037db..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/CurrencyQueryExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class CurrencyQueryExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - CurrencyQueryException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-CURRENCY-GET: Failed to retrieve currencies -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-CURRENCY-GET: Failed to retrieve currencies -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - CurrencyQueryException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-CURRENCY-GET: Failed to retrieve currencies -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-CURRENCY-GET: Failed to retrieve currencies -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private CurrencyQueryException getTestedClass(String status, String message) { - return new CurrencyQueryException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/InvoiceCancellationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/InvoiceCancellationExceptionTest.java deleted file mode 100644 index bd947ebd..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/InvoiceCancellationExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class InvoiceCancellationExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - InvoiceCancellationException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-CANCEL: Failed to cancel invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-INVOICE-CANCEL: Failed to cancel invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - InvoiceCancellationException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-CANCEL: Failed to cancel invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-INVOICE-CANCEL: Failed to cancel invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private InvoiceCancellationException getTestedClass(String status, String message) { - return new InvoiceCancellationException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/InvoiceCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/InvoiceCreationExceptionTest.java deleted file mode 100644 index 1a9396b5..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/InvoiceCreationExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class InvoiceCreationExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - InvoiceCreationException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-CREATE: Failed to create invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-INVOICE-CREATE: Failed to create invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - InvoiceCreationException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-CREATE: Failed to create invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-INVOICE-CREATE: Failed to create invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private InvoiceCreationException getTestedClass(String status, String message) { - return new InvoiceCreationException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/InvoiceExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/InvoiceExceptionTest.java deleted file mode 100644 index f7a0f278..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/InvoiceExceptionTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class InvoiceExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - InvoiceException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-GENERIC: An unexpected error occurred while trying to manage the invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-INVOICE-GENERIC: An unexpected error occurred while trying to manage the invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - InvoiceException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-GENERIC: An unexpected error occurred while trying to manage the invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-INVOICE-GENERIC: An unexpected error occurred while trying to manage the invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private InvoiceException getTestedClass(String status, String message) { - return new InvoiceException(status, message); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/InvoiceQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/InvoiceQueryExceptionTest.java deleted file mode 100644 index 223d0075..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/InvoiceQueryExceptionTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class InvoiceQueryExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - // given - String message = "reasonMsg"; - - // when - InvoiceQueryException testedClass = this.getTestedClass(null, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-GET: Failed to retrieve invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 000000 -> Reason: BITPAY-INVOICE-GET: Failed to retrieve invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertNull(testedClass.getStatusCode()); - } - - @Test - public void it_should_create_exception_with_status() { - // given - String message = "reasonMsg"; - String status = "123456"; - - // when - InvoiceQueryException testedClass = this.getTestedClass(status, message); - - // then - Assertions.assertEquals( - "BITPAY-INVOICE-GET: Failed to retrieve invoice -> reasonMsg", - testedClass.getReasonPhrase()); - Assertions.assertEquals( - "Status: 123456 -> Reason: BITPAY-INVOICE-GET: Failed to retrieve invoice -> reasonMsg", - testedClass.getMessage()); - Assertions.assertSame("123456", testedClass.getStatusCode()); - } - - private InvoiceQueryException getTestedClass(String status, String message) { - return new InvoiceQueryException(status, message); - } - -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/InvoiceUpdateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/InvoiceUpdateExceptionTest.java deleted file mode 100644 index db7cc89a..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/InvoiceUpdateExceptionTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class InvoiceUpdateExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus( - this.getTestedClass(null), - "Failed to update invoice", - "BITPAY-INVOICE-UPDATE" - ); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus( - this.getTestedClass(AbstractTestException.STATUS_CODE), - "Failed to update invoice", - "BITPAY-INVOICE-UPDATE" - ); - } - - private InvoiceUpdateException getTestedClass(String status) { - return new InvoiceUpdateException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/LedgerExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/LedgerExceptionTest.java deleted file mode 100644 index d7436805..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/LedgerExceptionTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class LedgerExceptionTest extends AbstractTestException { - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus( - this.getTestedClass(null), - "An unexpected error occurred while trying to manage the ledger", - "BITPAY-LEDGER-GENERIC" - ); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus( - this.getTestedClass(AbstractTestException.STATUS_CODE), - "An unexpected error occurred while trying to manage the ledger", - "BITPAY-LEDGER-GENERIC" - ); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - LedgerException exception = new LedgerException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private LedgerException getTestedClass(String status) { - return new LedgerException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/LedgerQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/LedgerQueryExceptionTest.java deleted file mode 100644 index d48b4845..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/LedgerQueryExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class LedgerQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve ledger"; - private static final String CODE = "BITPAY-LEDGER-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus( - this.getTestedClass(null), - MESSAGE, - CODE - ); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus( - this.getTestedClass(AbstractTestException.STATUS_CODE), - MESSAGE, - CODE - ); - } - - private LedgerQueryException getTestedClass(String status) { - return new LedgerQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationExceptionTest.java deleted file mode 100644 index d78899cd..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCancellationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutBatchCancellationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to cancel payout batch."; - private static final String CODE = "BITPAY-PAYOUT-BATCH-CANCELLATION"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutBatchCancellationException getTestedClass(String status) { - return new PayoutBatchCancellationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCreationExceptionTest.java deleted file mode 100644 index f16e4698..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchCreationExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class PayoutBatchCreationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to create payout batch."; - private static final String CODE = "BITPAY-PAYOUT-BATCH-CREATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - PayoutBatchCreationException exception = new PayoutBatchCreationException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private PayoutBatchCreationException getTestedClass(String status) { - return new PayoutBatchCreationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchExceptionTest.java deleted file mode 100644 index 4f3029bc..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class PayoutBatchExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occured while trying to manage the payout batch."; - private static final String CODE = "BITPAY-PAYOUT-BATCH-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - PayoutBatchException exception = new PayoutBatchException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private PayoutBatchException getTestedClass(String status) { - return new PayoutBatchException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationExceptionTest.java deleted file mode 100644 index 4f1df87b..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchNotificationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutBatchNotificationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to send payout batch notification."; - private static final String CODE = "BITPAY-PAYOUT-BATCH-NOTIFICATION"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutBatchNotificationException getTestedClass(String status) { - return new PayoutBatchNotificationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchQueryExceptionTest.java deleted file mode 100644 index b894fff0..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutBatchQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutBatchQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve payout batch."; - private static final String CODE = "BITPAY-PAYOUT-BATCH-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutBatchQueryException getTestedClass(String status) { - return new PayoutBatchQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutCancellationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutCancellationExceptionTest.java deleted file mode 100644 index f0bf5eca..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutCancellationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutCancellationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to cancel payout."; - private static final String CODE = "BITPAY-PAYOUT-CANCEL"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutCancellationException getTestedClass(String status) { - return new PayoutCancellationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutCreationExceptionTest.java deleted file mode 100644 index b080323b..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutCreationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutCreationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to create payout."; - private static final String CODE = "BITPAY-PAYOUT-SUBMIT"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutCreationException getTestedClass(String status) { - return new PayoutCreationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutExceptionTest.java deleted file mode 100644 index 01d18dc5..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class PayoutExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the payout."; - private static final String CODE = "BITPAY-PAYOUT-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - PayoutException exception = new PayoutException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private PayoutException getTestedClass(String status) { - return new PayoutException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutNotificationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutNotificationExceptionTest.java deleted file mode 100644 index 97029c95..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutNotificationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutNotificationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to send payout notification."; - private static final String CODE = "BITPAY-PAYOUT-NOTIFICATION"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutNotificationException getTestedClass(String status) { - return new PayoutNotificationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutQueryExceptionTest.java deleted file mode 100644 index 42b9371c..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve payout."; - private static final String CODE = "BITPAY-PAYOUT-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutQueryException getTestedClass(String status) { - return new PayoutQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationExceptionTest.java deleted file mode 100644 index 2e88a564..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCancellationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutRecipientCancellationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to cancel payout recipient"; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-CANCELLATION"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutRecipientCancellationException getTestedClass(String status) { - return new PayoutRecipientCancellationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationExceptionTest.java deleted file mode 100644 index d8786a7f..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientCreationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutRecipientCreationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to submit payout recipient."; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-CREATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutRecipientCreationException getTestedClass(String status) { - return new PayoutRecipientCreationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientExceptionTest.java deleted file mode 100644 index 4865fe67..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class PayoutRecipientExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the payout recipient"; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - PayoutRecipientException exception = new PayoutRecipientException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private PayoutRecipientException getTestedClass(String status) { - return new PayoutRecipientException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationExceptionTest.java deleted file mode 100644 index 040177e9..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientNotificationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutRecipientNotificationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to send payout recipient notification."; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-NOTIFICATION"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutRecipientNotificationException getTestedClass(String status) { - return new PayoutRecipientNotificationException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryExceptionTest.java deleted file mode 100644 index 1e6f6c97..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutRecipientQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve payout recipient."; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutRecipientQueryException getTestedClass(String status) { - return new PayoutRecipientQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateExceptionTest.java deleted file mode 100644 index 7fdf6226..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/PayoutRecipientUpdateExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class PayoutRecipientUpdateExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to update payout recipient."; - private static final String CODE = "BITPAY-PAYOUT-RECIPIENT-UPDATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private PayoutRecipientUpdateException getTestedClass(String status) { - return new PayoutRecipientUpdateException(status, AbstractTestException.REASON_MESSAGE); - } -} diff --git a/src/test/java/com/bitpay/sdk/exceptions/RateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RateExceptionTest.java deleted file mode 100644 index c55b7120..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RateExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class RateExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the rates"; - private static final String CODE = "BITPAY-RATES-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - RateException exception = new RateException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private RateException getTestedClass(String status) { - return new RateException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RateQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RateQueryExceptionTest.java deleted file mode 100644 index fab7e7a6..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RateQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class RateQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve rates"; - private static final String CODE = "BITPAY-RATES-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private RateQueryException getTestedClass(String status) { - return new RateQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RefundCancellationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RefundCancellationExceptionTest.java deleted file mode 100644 index b439028d..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RefundCancellationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class RefundCancellationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to cancel refund"; - private static final String CODE = "BITPAY-REFUND-CANCEL"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private RefundCancellationException getTestedClass(String status) { - return new RefundCancellationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RefundCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RefundCreationExceptionTest.java deleted file mode 100644 index 46474b75..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RefundCreationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class RefundCreationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to create refund"; - private static final String CODE = "BITPAY-REFUND-CREATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private RefundCreationException getTestedClass(String status) { - return new RefundCreationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RefundExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RefundExceptionTest.java deleted file mode 100644 index 33173a82..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RefundExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class RefundExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the refund"; - private static final String CODE = "BITPAY-REFUND-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - RefundException exception = new RefundException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private RefundException getTestedClass(String status) { - return new RefundException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RefundQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RefundQueryExceptionTest.java deleted file mode 100644 index f75ebe00..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RefundQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class RefundQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve refund"; - private static final String CODE = "BITPAY-REFUND-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private RefundQueryException getTestedClass(String status) { - return new RefundQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/RefundUpdateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/RefundUpdateExceptionTest.java deleted file mode 100644 index 7462d037..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/RefundUpdateExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class RefundUpdateExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to update refund"; - private static final String CODE = "BITPAY-REFUND-UPDATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private RefundUpdateException getTestedClass(String status) { - return new RefundUpdateException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SettlementExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SettlementExceptionTest.java deleted file mode 100644 index 8571cd3b..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SettlementExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class SettlementExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the settlements"; - private static final String CODE = "BITPAY-SETTLEMENTS-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - SettlementException exception = new SettlementException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private SettlementException getTestedClass(String status) { - return new SettlementException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SettlementQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SettlementQueryExceptionTest.java deleted file mode 100644 index d75fd48e..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SettlementQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class SettlementQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve settlements"; - private static final String CODE = "BITPAY-SETTLEMENTS-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private SettlementQueryException getTestedClass(String status) { - return new SettlementQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionCreationExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SubscriptionCreationExceptionTest.java deleted file mode 100644 index a9df6890..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionCreationExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class SubscriptionCreationExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to create subscription"; - private static final String CODE = "BITPAY-SUBSCRIPTION-CREATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private SubscriptionCreationException getTestedClass(String status) { - return new SubscriptionCreationException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SubscriptionExceptionTest.java deleted file mode 100644 index b2f2b950..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class SubscriptionExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage the subscription"; - private static final String CODE = "BITPAY-SUBSCRIPTION-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - SubscriptionException exception = new SubscriptionException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private SubscriptionException getTestedClass(String status) { - return new SubscriptionException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SubscriptionQueryExceptionTest.java deleted file mode 100644 index c368e6ae..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class SubscriptionQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve subscription"; - private static final String CODE = "BITPAY-SUBSCRIPTION-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private SubscriptionQueryException getTestedClass(String status) { - return new SubscriptionQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionUpdateExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/SubscriptionUpdateExceptionTest.java deleted file mode 100644 index 6665ff89..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/SubscriptionUpdateExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class SubscriptionUpdateExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to update subscription"; - private static final String CODE = "BITPAY-SUBSCRIPTION-UPDATE"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private SubscriptionUpdateException getTestedClass(String status) { - return new SubscriptionUpdateException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/WalletExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/WalletExceptionTest.java deleted file mode 100644 index 552484c4..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/WalletExceptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class WalletExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "An unexpected error occurred while trying to manage wallets"; - private static final String CODE = "BITPAY-WALLET-GENERIC"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - @Test - public void it_should_build_exception_with_different_bitpay_message() { - final String message = "BITPAY-CUSTOM"; - WalletException exception = new WalletException(null, message); - Assertions.assertEquals(message, exception.getReasonPhrase()); - } - - private WalletException getTestedClass(String status) { - return new WalletException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/exceptions/WalletQueryExceptionTest.java b/src/test/java/com/bitpay/sdk/exceptions/WalletQueryExceptionTest.java deleted file mode 100644 index e3b9af4d..00000000 --- a/src/test/java/com/bitpay/sdk/exceptions/WalletQueryExceptionTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitpay.sdk.exceptions; - -import org.junit.jupiter.api.Test; - -public class WalletQueryExceptionTest extends AbstractTestException { - - private static final String MESSAGE = "Failed to retrieve supported wallets"; - private static final String CODE = "BITPAY-WALLET-GET"; - - @Test - public void it_should_create_exception_without_status() { - this.testExceptionWithoutStatus(this.getTestedClass(null), MESSAGE, CODE); - } - - @Test - public void it_should_create_exception_with_status() { - this.testExceptionWithStatus(this.getTestedClass(AbstractTestException.STATUS_CODE), MESSAGE, CODE); - } - - private WalletQueryException getTestedClass(String status) { - return new WalletQueryException(status, AbstractTestException.REASON_MESSAGE); - } -} \ No newline at end of file diff --git a/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java b/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java index a1807fd8..6f63cd82 100644 --- a/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java +++ b/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java @@ -6,8 +6,9 @@ import com.bitpay.sdk.Client; import com.bitpay.sdk.ConfigFilePath; +import com.bitpay.sdk.exceptions.BitPayApiException; import com.bitpay.sdk.exceptions.BitPayException; -import com.bitpay.sdk.exceptions.RateQueryException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.model.bill.Bill; import com.bitpay.sdk.model.bill.Item; import com.bitpay.sdk.model.Currency; @@ -68,7 +69,7 @@ public ClientFunctionalTest() throws BitPayException { * - GetRates(string currency) */ @Test - public void it_should_test_rate_requests() throws RateQueryException { + public void it_should_test_rate_requests() throws BitPayGenericException, BitPayApiException { Rate rate = this.client.getRate(Currency.BCH, Currency.USD); Assertions.assertTrue(rate.getValue() != 0); diff --git a/src/test/java/com/bitpay/sdk/model/rate/RatesTest.java b/src/test/java/com/bitpay/sdk/model/rate/RatesTest.java index e451f66a..b1730d99 100644 --- a/src/test/java/com/bitpay/sdk/model/rate/RatesTest.java +++ b/src/test/java/com/bitpay/sdk/model/rate/RatesTest.java @@ -4,7 +4,8 @@ package com.bitpay.sdk.model.rate; import com.bitpay.sdk.client.RateClient; -import com.bitpay.sdk.exceptions.RateQueryException; +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayGenericException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -61,7 +62,7 @@ public void it_should_get_specific_rate() { } @Test - public void it_should_update_rates() throws RateQueryException { + public void it_should_update_rates() throws BitPayGenericException, BitPayApiException { // given List rates = Arrays.asList(rate1, rate2); List expectedRates = Collections.singletonList(rate1); @@ -77,10 +78,10 @@ public void it_should_update_rates() throws RateQueryException { } @Test - public void it_should_throws_rateQueryException() throws RateQueryException { - Mockito.when(this.client.getRates()).thenThrow(RateQueryException.class); + public void it_should_throws_rateQueryException() throws BitPayGenericException, BitPayApiException { + Mockito.when(this.client.getRates()).thenThrow(BitPayApiException.class); - Assertions.assertThrows(RateQueryException.class, () -> { + Assertions.assertThrows(BitPayApiException.class, () -> { // given List rates = Arrays.asList(rate1, rate2); Rates testedClass = this.getTestedClass(rates); diff --git a/src/test/java/com/bitpay/sdk/util/KeyUtilsTest.java b/src/test/java/com/bitpay/sdk/util/KeyUtilsTest.java index d2ee6223..ce370f1b 100644 --- a/src/test/java/com/bitpay/sdk/util/KeyUtilsTest.java +++ b/src/test/java/com/bitpay/sdk/util/KeyUtilsTest.java @@ -7,10 +7,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.bitcoinj.core.ECKey; - -import com.bitpay.sdk.util.KeyUtils; +import com.bitpay.sdk.exceptions.BitPayGenericException; +import org.bitcoinj.core.ECKey; import org.junit.jupiter.api.Test; public class KeyUtilsTest { @@ -38,7 +37,7 @@ public void it_should_be_not_null() { } @Test - public void it_should_convert_hex_to_bytes() { + public void it_should_convert_hex_to_bytes() throws BitPayGenericException { String hex = "0123456789abcdef"; byte[] expectedBytes = { 1, 35, 69, 103, -119, -85, -51, -17 };