From 90c79684fd023d9eef932e68246eeb3fbcd0d241 Mon Sep 17 00:00:00 2001 From: Marcin Warzybok Date: Wed, 29 Nov 2023 13:47:22 +0100 Subject: [PATCH 1/3] SP-734 Java - Investigate HTTPResponse --- .../sdk/client/AuthorizationClient.java | 10 +- .../com/bitpay/sdk/client/BillClient.java | 23 ++- .../com/bitpay/sdk/client/BitPayClient.java | 116 +++++-------- .../com/bitpay/sdk/client/CurrencyClient.java | 19 +- .../com/bitpay/sdk/client/HttpResponse.java | 46 +++++ .../sdk/client/HttpResponseProvider.java | 34 ++++ .../com/bitpay/sdk/client/InvoiceClient.java | 40 +++-- .../com/bitpay/sdk/client/LedgerClient.java | 6 +- .../com/bitpay/sdk/client/PayoutClient.java | 15 +- .../bitpay/sdk/client/PayoutGroupClient.java | 6 +- .../sdk/client/PayoutRecipientsClient.java | 20 ++- .../com/bitpay/sdk/client/RateClient.java | 15 +- .../com/bitpay/sdk/client/RefundClient.java | 27 ++- .../bitpay/sdk/client/SettlementClient.java | 11 +- .../com/bitpay/sdk/client/WalletClient.java | 3 +- .../sdk/model/settlement/PayoutInfo.java | 6 +- src/test/java/com/bitpay/sdk/ClientTest.java | 163 +++++++++++------- .../sdk/functional/ClientFunctionalTest.java | 25 ++- .../sdk/model/settlement/PayoutInfoTest.java | 2 +- 19 files changed, 366 insertions(+), 221 deletions(-) create mode 100644 src/main/java/com/bitpay/sdk/client/HttpResponse.java create mode 100644 src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java diff --git a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java index 542576ff..3c11fe5b 100644 --- a/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java +++ b/src/main/java/com/bitpay/sdk/client/AuthorizationClient.java @@ -73,7 +73,9 @@ public void authorizeClient(String pairingCode) throws BitPayApiException, BitPa "Failed to serialize Token object : " + e.getMessage()); } - String jsonResponse = this.bitPayClient.post("tokens", json); + HttpResponse response = this.bitPayClient.post("tokens", json); + + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); List tokens = null; @@ -117,12 +119,14 @@ public String authorizeClient(Facade facade) throws BitPayApiException, BitPayGe BitPayExceptionProvider.throwSerializeResourceException("Token", e.getMessage()); } - String response = this.bitPayClient.post("tokens", json); + HttpResponse response = this.bitPayClient.post("tokens", json); + + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); List tokens = null; try { - tokens = Arrays.asList(mapper.readValue(response, Token[].class)); + tokens = Arrays.asList(mapper.readValue(jsonResponse, Token[].class)); // Expecting a single token resource. if (tokens.size() != 1) { diff --git a/src/main/java/com/bitpay/sdk/client/BillClient.java b/src/main/java/com/bitpay/sdk/client/BillClient.java index a0775559..0612aca1 100644 --- a/src/main/java/com/bitpay/sdk/client/BillClient.java +++ b/src/main/java/com/bitpay/sdk/client/BillClient.java @@ -95,7 +95,8 @@ public Bill create( BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage()); } - String jsonResponse = this.bitPayClient.post("bills", json, signRequest); + HttpResponse response = this.bitPayClient.post("bills", json, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { bill = mapper.readerForUpdating(bill).readValue(jsonResponse); @@ -130,7 +131,9 @@ public Bill get( ParameterAdder.execute(params, "token", token); Bill bill = null; - String jsonResponse = this.bitPayClient.get("bills/" + billId, params, signRequest); + + HttpResponse response = this.bitPayClient.get("bills/" + billId, params, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { bill = JsonMapperFactory.create().readValue(jsonResponse, Bill.class); @@ -157,7 +160,9 @@ public List getBills(String status) throws BitPayGenericException, BitPayA List bills = null; try { - String jsonResponse = this.bitPayClient.get("bills", params); + HttpResponse response = this.bitPayClient.get("bills", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + bills = Arrays.asList( JsonMapperFactory.create().readValue(jsonResponse, Bill[].class)); } catch (JsonProcessingException e) { @@ -179,7 +184,8 @@ public List getBills() throws BitPayGenericException, BitPayApiException { ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); List bills = null; - String jsonResponse = this.bitPayClient.get("bills", params); + HttpResponse response = this.bitPayClient.get("bills", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { bills = Arrays.asList( @@ -218,7 +224,8 @@ public Bill update( BitPayExceptionProvider.throwSerializeResourceException("Bill", e.getMessage()); } - String jsonResponse = this.bitPayClient.update("bills/" + billId, json); + HttpResponse response = this.bitPayClient.update("bills/" + billId, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { bill = mapper.readerForUpdating(bill).readValue(jsonResponse); @@ -260,7 +267,9 @@ public String deliver( BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest); - return response.replace("\"", ""); + HttpResponse response = this.bitPayClient.post("bills/" + billId + "/deliveries", json, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + + return jsonResponse.replace("\"", ""); } } diff --git a/src/main/java/com/bitpay/sdk/client/BitPayClient.java b/src/main/java/com/bitpay/sdk/client/BitPayClient.java index ec906245..2b076db3 100644 --- a/src/main/java/com/bitpay/sdk/client/BitPayClient.java +++ b/src/main/java/com/bitpay/sdk/client/BitPayClient.java @@ -16,8 +16,6 @@ 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.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; @@ -25,8 +23,8 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.message.AbstractHttpMessage; import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; import org.bitcoinj.core.ECKey; /** @@ -68,7 +66,7 @@ public BitPayClient( * @throws BitPayGenericException BitPayGenericException class * @throws BitPayApiException BitPayApiException class */ - public String get( + public HttpResponse get( final String uri, final List parameters ) throws BitPayApiException, BitPayGenericException { @@ -85,13 +83,11 @@ public String get( * @throws BitPayGenericException BitPayGenericException * @throws BitPayApiException BitPayApiException */ - public String get( + public HttpResponse get( final String uri, final List parameters, final boolean signatureRequired ) throws BitPayApiException, BitPayGenericException { - String jsonResponse = null; - try { String fullUrl = this.baseUrl + uri; final HttpGet httpGet = this.httpRequestFactory.createHttpGet(fullUrl); @@ -101,32 +97,22 @@ public String get( httpGet.setURI(new URI(fullUrl)); } + this.addDefaultHeaders(httpGet); if (signatureRequired) { - httpGet.addHeader("x-signature", KeyUtils.sign(this.ecKey, fullUrl)); - httpGet.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); + this.addSignatureRequiredHeaders(httpGet, fullUrl); } - 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); + HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpGet)); - final HttpEntity entity = response.getEntity(); - String jsonString = EntityUtils.toString(entity, "UTF-8"); - - LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, jsonString); - - jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, fullUrl, response.getBody()); + return response; } catch (IOException | URISyntaxException e) { BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + throw new BitPayApiException(e.getMessage(), null); } - - return jsonResponse; } /** @@ -137,7 +123,7 @@ public String get( * @throws BitPayApiException BitPayApiException * @throws BitPayGenericException BitPayGenericException */ - public String get(final String uri) throws BitPayApiException, BitPayGenericException { + public HttpResponse get(final String uri) throws BitPayApiException, BitPayGenericException { return this.get(uri, null, false); } @@ -150,12 +136,10 @@ public String get(final String uri) throws BitPayApiException, BitPayGenericExce * @throws BitPayApiException BitPayApiException * @throws BitPayGenericException BitPayGenericException */ - public String delete( + public HttpResponse delete( final String uri, final List parameters ) throws BitPayApiException, BitPayGenericException { - String jsonResponse = null; - try { String fullUrl = this.baseUrl + uri; final HttpDelete httpDelete = this.httpRequestFactory.createHttpDelete(fullUrl); @@ -165,28 +149,20 @@ public String delete( 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())); + this.addDefaultHeaders(httpDelete); + this.addSignatureRequiredHeaders(httpDelete, fullUrl); 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"); + HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpDelete)); - LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, jsonString); + LoggerProvider.getLogger().logResponse(HttpDelete.METHOD_NAME, fullUrl, response.getBody()); - jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + return response; } catch (IOException | URISyntaxException e) { BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + throw new BitPayApiException(e.getMessage(), null); } - - return jsonResponse; } /** @@ -198,7 +174,7 @@ public String delete( * @throws BitPayApiException BitPayApiException * @throws BitPayGenericException BitPayGenericException */ - public String post( + public HttpResponse post( final String uri, final String json ) throws BitPayApiException, BitPayGenericException { @@ -215,45 +191,33 @@ public String post( * @throws BitPayApiException BitPayApiException * @throws BitPayGenericException BitPayGenericException */ - public String post( + public HttpResponse post( final String uri, final String json, final boolean signatureRequired ) throws BitPayApiException, BitPayGenericException { - String jsonResponse = null; - try { final String endpoint = this.baseUrl + uri; final HttpPost httpPost = this.httpRequestFactory.createHttpPost(endpoint); - httpPost.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8))); + this.addDefaultHeaders(httpPost); + httpPost.addHeader("Content-Type", "application/json"); if (signatureRequired) { - httpPost.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json)); - httpPost.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); + this.addSignatureRequiredHeaders(httpPost, endpoint + json); } - httpPost.addHeader("x-accept-version", Config.BITPAY_API_VERSION); - httpPost.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); - httpPost.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); - httpPost.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); - httpPost.addHeader("Content-Type", "application/json"); - 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"); + HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPost)); - LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, jsonString); + LoggerProvider.getLogger().logResponse(HttpGet.METHOD_NAME, endpoint, response.getBody()); - jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + return response; } catch (IOException e) { BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + throw new BitPayApiException(e.getMessage(), null); } - - return jsonResponse; } /** @@ -265,7 +229,7 @@ public String post( * @throws BitPayApiException BitPayApiException * @throws BitPayGenericException BitPayGenericException */ - public String update( + public HttpResponse update( final String uri, final String json ) throws BitPayApiException, BitPayGenericException { @@ -277,8 +241,7 @@ public String update( httpPut.setEntity(new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8))); - httpPut.addHeader("x-signature", KeyUtils.sign(this.ecKey, this.baseUrl + uri + json)); - httpPut.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); + this.addSignatureRequiredHeaders(httpPut, endpoint + json); 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"); @@ -287,18 +250,27 @@ public String update( LoggerProvider.getLogger().logRequest(HttpPut.METHOD_NAME, endpoint, httpPut.toString()); - HttpResponse response = this.httpClient.execute(httpPut); - - final HttpEntity entity = response.getEntity(); - String jsonString = EntityUtils.toString(entity, "UTF-8"); + HttpResponse response = HttpResponseProvider.fromApacheHttpResponse(this.httpClient.execute(httpPut)); - LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, jsonString); + LoggerProvider.getLogger().logResponse(HttpPut.METHOD_NAME, endpoint, response.getBody()); - jsonResponse = ResponseParser.getJsonDataFromJsonResponse(jsonString); + return response; } catch (IOException e) { BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + throw new BitPayApiException(e.getMessage(), null); } + } + + private void addDefaultHeaders(AbstractHttpMessage httpMessage) { + httpMessage.addHeader("x-accept-version", Config.BITPAY_API_VERSION); + httpMessage.addHeader("x-bitpay-api-frame", Config.BITPAY_API_FRAME); + httpMessage.addHeader("x-bitpay-api-frame-version", Config.BITPAY_API_FRAME_VERSION); + httpMessage.addHeader("X-BitPay-Plugin-Info", Config.BITPAY_PLUGIN_INFO); + } - return jsonResponse; + private void addSignatureRequiredHeaders(AbstractHttpMessage httpMessage, String uri) + throws BitPayGenericException { + httpMessage.addHeader("x-signature", KeyUtils.sign(this.ecKey, uri)); + httpMessage.addHeader("x-identity", KeyUtils.bytesToHex(this.ecKey.getPubKey())); } } diff --git a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java index e48c765e..046661e4 100644 --- a/src/main/java/com/bitpay/sdk/client/CurrencyClient.java +++ b/src/main/java/com/bitpay/sdk/client/CurrencyClient.java @@ -22,20 +22,20 @@ public class CurrencyClient implements ResourceClient { private static CurrencyClient instance; - private final BitPayClient client; + private final BitPayClient bitPayClient; private List> currenciesInfo; /** * Instantiates a new Currency client. * - * @param client the client + * @param bitPayClient the client * @throws BitPayGenericException BitPayGenericException class */ - private CurrencyClient(BitPayClient client) throws BitPayGenericException { - if (Objects.isNull(client)) { + private CurrencyClient(BitPayClient bitPayClient) throws BitPayGenericException { + if (Objects.isNull(bitPayClient)) { BitPayExceptionProvider.throwGenericExceptionWithMessage("Failed init Currency Client"); } - this.client = client; + this.bitPayClient = bitPayClient; } /** @@ -84,19 +84,20 @@ public Map getInfo(String currencyCode) throws BitPayGenericExce */ private void loadCurrencies() { try { - String jsonString = this.client.get("currencies"); + HttpResponse response = this.bitPayClient.get("currencies"); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); JsonMapper mapper = JsonMapperFactory.create(); - JsonNode rootNode = mapper.readTree(jsonString); + JsonNode rootNode = mapper.readTree(jsonResponse); JsonNode node = rootNode.get("data"); if (node != null) { - jsonString = node.toString(); + jsonResponse = node.toString(); } this.currenciesInfo = new ArrayList(Arrays.asList( - JsonMapperFactory.create().readValue(jsonString, Map[].class)) + JsonMapperFactory.create().readValue(jsonResponse, Map[].class)) ); } catch (Exception e) { diff --git a/src/main/java/com/bitpay/sdk/client/HttpResponse.java b/src/main/java/com/bitpay/sdk/client/HttpResponse.java new file mode 100644 index 00000000..f99ff9fa --- /dev/null +++ b/src/main/java/com/bitpay/sdk/client/HttpResponse.java @@ -0,0 +1,46 @@ +package com.bitpay.sdk.client; + +import java.util.Map; + +public class HttpResponse { + + private final Integer code; + private final String body; + private final Map headers; + private final String locale; + private final String httpVersion; + + public HttpResponse( + Integer code, + String body, + Map headers, + String locale, + String httpVersion + ) { + this.code = code; + this.body = body; + this.headers = headers; + this.locale = locale; + this.httpVersion = httpVersion; + } + + public Integer getCode() { + return code; + } + + public String getBody() { + return body; + } + + public Map getHeaders() { + return headers; + } + + public String getLocale() { + return locale; + } + + public String getHttpVersion() { + return httpVersion; + } +} diff --git a/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java b/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java new file mode 100644 index 00000000..f8bc83a0 --- /dev/null +++ b/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java @@ -0,0 +1,34 @@ +package com.bitpay.sdk.client; + +import com.bitpay.sdk.exceptions.BitPayApiException; +import com.bitpay.sdk.exceptions.BitPayExceptionProvider; +import java.io.IOException; +import java.util.Arrays; +import java.util.stream.Collectors; +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.util.EntityUtils; + +class HttpResponseProvider { + + public static HttpResponse fromApacheHttpResponse(org.apache.http.HttpResponse apacheHttpResponse) + throws BitPayApiException { + try { + final HttpEntity entity = apacheHttpResponse.getEntity(); + String body = EntityUtils.toString(entity, "UTF-8"); + + return new HttpResponse( + apacheHttpResponse.getStatusLine().getStatusCode(), + body, + Arrays.stream(apacheHttpResponse.getAllHeaders()) + .collect(Collectors.toMap(Header::getName, Header::getValue)), + apacheHttpResponse.getLocale().toString(), + apacheHttpResponse.getStatusLine().getProtocolVersion().toString() + ); + + } catch (IOException e) { + BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage()); + throw new RuntimeException(e.getMessage()); + } + } +} diff --git a/src/main/java/com/bitpay/sdk/client/InvoiceClient.java b/src/main/java/com/bitpay/sdk/client/InvoiceClient.java index 3a7d9d63..4c32ff25 100644 --- a/src/main/java/com/bitpay/sdk/client/InvoiceClient.java +++ b/src/main/java/com/bitpay/sdk/client/InvoiceClient.java @@ -105,7 +105,8 @@ public Invoice create( BitPayExceptionProvider.throwSerializeResourceException("Invoice", e.getMessage()); } - String jsonResponse = this.bitPayClient.post("invoices", json, signRequest); + HttpResponse response = this.bitPayClient.post("invoices", json, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = mapper.readerForUpdating(invoice).readValue(jsonResponse); @@ -139,7 +140,8 @@ public Invoice get( Invoice invoice = null; - String jsonResponse = this.bitPayClient.get("invoices/" + invoiceId, params, signRequest); + HttpResponse response = this.bitPayClient.get("invoices/" + invoiceId, params, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); @@ -175,7 +177,8 @@ public Invoice getByGuid( Invoice invoice = null; ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(facade)); - String jsonResponse = this.bitPayClient.get("invoices/guid/" + guid, params, signRequest); + HttpResponse response = this.bitPayClient.get("invoices/guid/" + guid, params, signRequest); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); @@ -225,7 +228,8 @@ public List getInvoices( } List invoices = null; - String jsonResponse = this.bitPayClient.get("invoices", params); + HttpResponse response = this.bitPayClient.get("invoices", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoices = Arrays.asList(JsonMapperFactory.create().readValue(jsonResponse, Invoice[].class)); @@ -248,7 +252,8 @@ public InvoiceEventToken getInvoiceEventToken(String invoiceId) throws BitPayApi final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - String jsonResponse = this.bitPayClient.get("invoices/" + invoiceId + "/events", params); + HttpResponse response = this.bitPayClient.get("invoices/" + invoiceId + "/events", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); InvoiceEventToken result = null; try { @@ -312,11 +317,12 @@ public Invoice update( BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String response = this.bitPayClient.update("invoices/" + invoiceId, json); + HttpResponse response = this.bitPayClient.update("invoices/" + invoiceId, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = - JsonMapperFactory.create().readValue(response, Invoice.class); + JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } @@ -352,10 +358,12 @@ public Invoice pay( BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String response = this.bitPayClient.update("invoices/pay/" + invoiceId, json); + HttpResponse response = this.bitPayClient.update("invoices/pay/" + invoiceId, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + try { invoice = - JsonMapperFactory.create().readValue(response, Invoice.class); + JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } @@ -395,11 +403,12 @@ public Invoice cancel( } Invoice invoice = null; - String response = this.bitPayClient.delete("invoices/" + invoiceId, params); + HttpResponse response = this.bitPayClient.delete("invoices/" + invoiceId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = - JsonMapperFactory.create().readValue(response, Invoice.class); + JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } @@ -428,11 +437,12 @@ public Invoice cancelByGuid(String guid, Boolean forceCancel) throws BitPayApiEx } Invoice invoice = null; - String response = this.bitPayClient.delete("invoices/guid/" + guid, params); + HttpResponse response = this.bitPayClient.delete("invoices/guid/" + guid, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { invoice = - JsonMapperFactory.create().readValue(response, Invoice.class); + JsonMapperFactory.create().readValue(jsonResponse, Invoice.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Invoice", e.getMessage()); } @@ -461,7 +471,9 @@ public Boolean requestInvoiceWebhookToBeResent(String invoiceId) throws BitPayAp BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String jsonResponse = this.bitPayClient.post("invoices/" + invoiceId + "/notifications", json); + HttpResponse response = this.bitPayClient.post("invoices/" + invoiceId + "/notifications", json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + return jsonResponse.replace("\"", "").toLowerCase(Locale.ROOT).equals("success"); } diff --git a/src/main/java/com/bitpay/sdk/client/LedgerClient.java b/src/main/java/com/bitpay/sdk/client/LedgerClient.java index a80d9452..5ab50d72 100644 --- a/src/main/java/com/bitpay/sdk/client/LedgerClient.java +++ b/src/main/java/com/bitpay/sdk/client/LedgerClient.java @@ -86,7 +86,8 @@ public List getEntries( ParameterAdder.execute(params, "startDate", dateStart); ParameterAdder.execute(params, "endDate", dateEnd); - String jsonResponse = this.bitPayClient.get("ledgers/" + currency, params); + HttpResponse response = this.bitPayClient.get("ledgers/" + currency, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); List entries = null; @@ -114,7 +115,8 @@ public List getLedgers() throws BitPayApiException, BitPayGenericExcepti List ledgers = null; - String jsonResponse = this.bitPayClient.get("ledgers", params); + HttpResponse response = this.bitPayClient.get("ledgers", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { ledgers = Arrays diff --git a/src/main/java/com/bitpay/sdk/client/PayoutClient.java b/src/main/java/com/bitpay/sdk/client/PayoutClient.java index 5b8793c8..ef136ec9 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutClient.java @@ -85,7 +85,8 @@ public Payout submit(Payout payout) throws BitPayApiException, BitPayGenericExce BitPayExceptionProvider.throwSerializeResourceException("Payout", e.getMessage()); } - String jsonResponse = this.bitPayClient.post("payouts", json, true); + HttpResponse response = this.bitPayClient.post("payouts", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { payout = JsonMapperFactory.create() @@ -117,7 +118,8 @@ public Payout get(String payoutId) throws BitPayApiException, BitPayGenericExcep Payout payout = null; - String jsonResponse = this.bitPayClient.get("payouts/" + payoutId, params, true); + HttpResponse response = this.bitPayClient.get("payouts/" + payoutId, params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { payout = JsonMapperFactory.create() @@ -149,7 +151,8 @@ public Boolean cancel(String payoutId) boolean result = false; JsonMapper mapper = JsonMapperFactory.create(); - String jsonResponse = this.bitPayClient.delete("payouts/" + payoutId, params); + HttpResponse response = this.bitPayClient.delete("payouts/" + payoutId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { JsonNode rootNode = mapper.readTree(jsonResponse); @@ -204,7 +207,8 @@ public List getPayouts( List payouts = null; - String jsonResponse = this.bitPayClient.get("payouts", params, true); + HttpResponse response = this.bitPayClient.get("payouts", params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { payouts = Arrays @@ -245,7 +249,8 @@ public Boolean requestNotification(String payoutId) BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String jsonResponse = this.bitPayClient.post("payouts/" + payoutId + "/notifications", json, true); + HttpResponse response = this.bitPayClient.post("payouts/" + payoutId + "/notifications", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { JsonNode rootNode = mapper.readTree(jsonResponse); diff --git a/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java b/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java index b510aee7..feb0e95d 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutGroupClient.java @@ -86,7 +86,8 @@ public PayoutGroup submit(Collection payouts) throws BitPayApiException, BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String jsonResponse = this.bitPayClient.post("payouts/group", json, true); + HttpResponse response = this.bitPayClient.post("payouts/group", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { result = JsonMapperFactory.create() @@ -117,7 +118,8 @@ public PayoutGroup cancel(String groupId) final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.PAYOUT)); - String jsonResponse = this.bitPayClient.delete("payouts/group/" + groupId, params); + HttpResponse response = this.bitPayClient.delete("payouts/group/" + groupId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { result = JsonMapperFactory.create() diff --git a/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java b/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java index 8cc6e735..97950a06 100644 --- a/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java +++ b/src/main/java/com/bitpay/sdk/client/PayoutRecipientsClient.java @@ -100,7 +100,8 @@ public List submit(PayoutRecipients recipients) throws BitPayAp List recipientsList = null; - String jsonResponse = this.bitPayClient.post("recipients", json, true); + HttpResponse response = this.bitPayClient.post("recipients", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { recipientsList = Arrays @@ -139,7 +140,8 @@ public List getRecipientsByFilters(String status, Integer limit List recipientsList = null; - String jsonResponse = this.bitPayClient.get("recipients", params, true); + HttpResponse response = this.bitPayClient.get("recipients", params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { recipientsList = Arrays @@ -172,7 +174,8 @@ public PayoutRecipient get(String recipientId) PayoutRecipient recipient = null; - String jsonResponse = this.bitPayClient.get("recipients/" + recipientId, params, true); + HttpResponse response = this.bitPayClient.get("recipients/" + recipientId, params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { recipient = JsonMapperFactory.create() @@ -213,11 +216,12 @@ public PayoutRecipient update(String recipientId, PayoutRecipient recipient) PayoutRecipient updateRecipient = null; - String response = this.bitPayClient.update("recipients/" + recipientId, json); + HttpResponse response = this.bitPayClient.update("recipients/" + recipientId, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { updateRecipient = JsonMapperFactory.create() - .readValue(response, PayoutRecipient.class); + .readValue(jsonResponse, PayoutRecipient.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Payout Recipient", e.getMessage()); } @@ -243,7 +247,8 @@ public Boolean delete(String recipientId) throws BitPayApiException, BitPayGener JsonMapper mapper = JsonMapperFactory.create(); Boolean result = null; - String jsonResponse = this.bitPayClient.delete("recipients/" + recipientId, params); + HttpResponse response = this.bitPayClient.delete("recipients/" + recipientId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { JsonNode rootNode = mapper.readTree(jsonResponse); @@ -282,7 +287,8 @@ public Boolean requestNotification(String recipientId) throws BitPayApiException BitPayExceptionProvider.throwSerializeParamsException(e.getMessage()); } - String jsonResponse = this.bitPayClient.post("recipients/" + recipientId + "/notifications", json, true); + HttpResponse response = this.bitPayClient.post("recipients/" + recipientId + "/notifications", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); JsonNode rootNode = null; try { diff --git a/src/main/java/com/bitpay/sdk/client/RateClient.java b/src/main/java/com/bitpay/sdk/client/RateClient.java index 479680ce..2c1b8023 100644 --- a/src/main/java/com/bitpay/sdk/client/RateClient.java +++ b/src/main/java/com/bitpay/sdk/client/RateClient.java @@ -63,7 +63,8 @@ public Rate get( String baseCurrency, String currency ) throws BitPayGenericException, BitPayApiException { - String jsonResponse = this.bitPayClient.get("rates/" + baseCurrency + "/" + currency); + HttpResponse response = this.bitPayClient.get("rates/" + baseCurrency + "/" + currency); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); Rate rate = null; @@ -87,10 +88,12 @@ public Rate get( public Rates getRates() throws BitPayGenericException, BitPayApiException { List rates = null; - String response = this.bitPayClient.get("rates"); + HttpResponse response = this.bitPayClient.get("rates"); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + try { rates = Arrays.asList( - JsonMapperFactory.create().readValue(response, Rate[].class) + JsonMapperFactory.create().readValue(jsonResponse, Rate[].class) ); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Rates", e.getMessage()); @@ -113,10 +116,12 @@ public Rates getRates() throws BitPayGenericException, BitPayApiException { public Rates getRates(String baseCurrency) throws BitPayGenericException, BitPayApiException { List rates = null; - String response = this.bitPayClient.get("rates/" + baseCurrency); + HttpResponse response = this.bitPayClient.get("rates/" + baseCurrency); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); + try { rates = Arrays.asList( - JsonMapperFactory.create().readValue(response, Rate[].class) + JsonMapperFactory.create().readValue(jsonResponse, Rate[].class) ); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Rates", e.getMessage()); diff --git a/src/main/java/com/bitpay/sdk/client/RefundClient.java b/src/main/java/com/bitpay/sdk/client/RefundClient.java index 77e7899f..0c46590e 100644 --- a/src/main/java/com/bitpay/sdk/client/RefundClient.java +++ b/src/main/java/com/bitpay/sdk/client/RefundClient.java @@ -98,7 +98,8 @@ public Refund create(final Refund refund) throws BitPayApiException, BitPayGener BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - final String jsonResponse = this.bitPayClient.post("refunds/", json, true); + final HttpResponse response = this.bitPayClient.post("refunds/", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { result = JsonMapperFactory.create() @@ -126,7 +127,8 @@ public Refund getById(final String refundId) throws BitPayApiException, BitPayGe final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - final String jsonResponse = this.bitPayClient.get("refunds/" + refundId, params, true); + final HttpResponse response = this.bitPayClient.get("refunds/" + refundId, params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() @@ -153,7 +155,8 @@ public Refund getByGuid(final String guid) throws BitPayApiException, BitPayGene 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); + final HttpResponse response = this.bitPayClient.get("refunds/guid/" + guid, params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() @@ -182,7 +185,8 @@ public List getRefundsByInvoiceId(final String invoiceId) ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); ParameterAdder.execute(params, "invoiceId", invoiceId); - String jsonResponse = this.bitPayClient.get("refunds/", params, true); + HttpResponse response = this.bitPayClient.get("refunds/", params, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refunds = Arrays.asList( @@ -216,7 +220,8 @@ public Refund update( final String json = getUpdateRefundJson(status); Refund refund = null; - final String jsonResponse = this.bitPayClient.update("refunds/" + refundId, json); + final HttpResponse response = this.bitPayClient.update("refunds/" + refundId, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() @@ -250,7 +255,8 @@ public Refund updateByGuid( final String json = getUpdateRefundJson(status); Refund refund = null; - String jsonResponse = this.bitPayClient.update("refunds/guid/" + guid, json); + HttpResponse response = this.bitPayClient.update("refunds/guid/" + guid, json); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() @@ -288,7 +294,8 @@ public Boolean sendRefundNotification(final String refundId) throws BitPayApiExc BitPayExceptionProvider.throwEncodeException(e.getMessage()); } - String jsonResponse = this.bitPayClient.post("refunds/" + refundId + "/notifications", json, true); + HttpResponse response = this.bitPayClient.post("refunds/" + refundId + "/notifications", json, true); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { final JsonNode rootNode = mapper.readTree(jsonResponse); @@ -319,7 +326,8 @@ public Refund cancel(final String refundId) throws BitPayApiException, BitPayGen final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - String jsonResponse = this.bitPayClient.delete("refunds/" + refundId, params); + HttpResponse response = this.bitPayClient.delete("refunds/" + refundId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() @@ -350,7 +358,8 @@ public Refund cancelByGuid(final String guid) throws BitPayApiException, BitPayG final List params = new ArrayList(); ParameterAdder.execute(params, "token", this.accessTokens.getAccessToken(Facade.MERCHANT)); - String jsonResponse = this.bitPayClient.delete("refunds/guid/" + guid, params); + HttpResponse response = this.bitPayClient.delete("refunds/guid/" + guid, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { refund = JsonMapperFactory.create() diff --git a/src/main/java/com/bitpay/sdk/client/SettlementClient.java b/src/main/java/com/bitpay/sdk/client/SettlementClient.java index fa13fb61..9194d404 100644 --- a/src/main/java/com/bitpay/sdk/client/SettlementClient.java +++ b/src/main/java/com/bitpay/sdk/client/SettlementClient.java @@ -100,7 +100,8 @@ public List getSettlements( List settlements = null; - String jsonResponse = this.bitPayClient.get("settlements", params); + HttpResponse response = this.bitPayClient.get("settlements", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { settlements = @@ -132,11 +133,12 @@ public Settlement get(String settlementId) throws BitPayApiException, BitPayGene final List params = new ArrayList(); ParameterAdder.execute(params, "token", token); - String response = this.bitPayClient.get("settlements/" + settlementId, params); + HttpResponse response = this.bitPayClient.get("settlements/" + settlementId, params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { settlement = - objectMapper.readValue(response, Settlement.class); + objectMapper.readValue(jsonResponse, Settlement.class); } catch (JsonProcessingException e) { BitPayExceptionProvider.throwDeserializeResourceException("Settlement", e.getMessage()); } @@ -166,7 +168,8 @@ public Settlement getSettlementReconciliationReport( Settlement reconciliationReport = null; - String jsonResponse = this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params); + HttpResponse response = this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { reconciliationReport = JsonMapperFactory.create() diff --git a/src/main/java/com/bitpay/sdk/client/WalletClient.java b/src/main/java/com/bitpay/sdk/client/WalletClient.java index e3bb5d83..7ef294b5 100644 --- a/src/main/java/com/bitpay/sdk/client/WalletClient.java +++ b/src/main/java/com/bitpay/sdk/client/WalletClient.java @@ -56,7 +56,8 @@ public static WalletClient getInstance(BitPayClient bitPayClient) { public List getSupportedWallets() throws BitPayApiException, BitPayGenericException { List wallets = null; - String jsonResponse = this.bitPayClient.get("supportedwallets"); + HttpResponse response = this.bitPayClient.get("supportedwallets"); + String jsonResponse = ResponseParser.getJsonDataFromJsonResponse(response.getBody()); try { wallets = Arrays.asList( diff --git a/src/main/java/com/bitpay/sdk/model/settlement/PayoutInfo.java b/src/main/java/com/bitpay/sdk/model/settlement/PayoutInfo.java index 212ae5cf..6b201a4f 100644 --- a/src/main/java/com/bitpay/sdk/model/settlement/PayoutInfo.java +++ b/src/main/java/com/bitpay/sdk/model/settlement/PayoutInfo.java @@ -29,7 +29,7 @@ public class PayoutInfo { private String city; private String postal; private String sort; - private String wire; + private boolean wire; private String bankName; private String bankAddress; private String bankAddress2; @@ -309,7 +309,7 @@ public void setSort(String sort) { * @return the wire */ @JsonIgnore - public String getWire() { + public boolean getWire() { return this.wire; } @@ -322,7 +322,7 @@ public String getWire() { * @param wire the wire */ @JsonProperty("wire") - public void setWire(String wire) { + public void setWire(boolean wire) { this.wire = wire; } diff --git a/src/test/java/com/bitpay/sdk/ClientTest.java b/src/test/java/com/bitpay/sdk/ClientTest.java index 64354d0d..71b53edd 100644 --- a/src/test/java/com/bitpay/sdk/ClientTest.java +++ b/src/test/java/com/bitpay/sdk/ClientTest.java @@ -5,6 +5,7 @@ package com.bitpay.sdk; import com.bitpay.sdk.client.BitPayClient; +import com.bitpay.sdk.client.HttpResponse; import com.bitpay.sdk.exceptions.BitPayApiException; import com.bitpay.sdk.exceptions.BitPayGenericException; import com.bitpay.sdk.exceptions.BitPayValidationException; @@ -28,15 +29,14 @@ import com.bitpay.sdk.model.wallet.Wallet; import com.bitpay.sdk.util.GuidGenerator; import com.bitpay.sdk.util.TokenContainer; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; @@ -144,7 +144,7 @@ public void it_should_authorize_client_by_pairing_code() throws BitPayApiExcepti "[{\"policies\":[{\"policy\":\"id\",\"method\":\"active\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"t0k3n\",\"facade\":\"merchant\",\"dateCreated\":1668425446554,\"pairingExpiration\":1668511846554,\"pairingCode\":\"123123123\"}]"; Mockito.when(this.bitPayClient.post("tokens", "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) - .thenReturn(responseString); + .thenReturn(this.getHttpResponseWithSpecificBody(responseString)); Client testedClass = this.getTestedClass(); @@ -164,7 +164,7 @@ public void it_should_authorize_client_by_facade() throws BitPayApiException, Bi "[{\"policies\":[{\"policy\":\"id\",\"method\":\"inactive\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"G7XM9fcM1gtCN7DUr8ZWtPGVFLTKiYWanHR4kvqsnjP3\",\"facade\":\"merchant\",\"label\":\"merchantwebsite.com\",\"dateCreated\":1621340364865,\"pairingExpiration\":1621426764865,\"pairingCode\":\"C4Lg7oW\"}]"; Mockito.when(this.bitPayClient.post("tokens", "{\"count\":1,\"facade\":\"merchant\",\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"}")) - .thenReturn(responseString); + .thenReturn(this.getHttpResponseWithSpecificBody(responseString)); Client testedClass = this.getTestedClass(); @@ -185,7 +185,7 @@ public void it_should_test_requestClientAuthorization() throws BitPayApiExceptio "[{\"policies\":[{\"policy\":\"id\",\"method\":\"active\",\"params\":[\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\"]}],\"token\":\"t0k3n\",\"facade\":\"merchant\",\"dateCreated\":1668425446554,\"pairingExpiration\":1668511846554,\"pairingCode\":\"\"}]"; Mockito.when(this.bitPayClient.post("tokens", "{\"guid\":\"37bd36bd-6fcb-409c-a907-47f9244302aa\",\"id\":\"Tf2yXsY49iFyDfxt3b2kf9VPRMwPxxAyCRW\",\"pairingCode\":\"123123123\"}")) - .thenReturn(responseString); + .thenReturn(this.getHttpResponseWithSpecificBody(responseString)); Client testedClass = this.getTestedClass(); @@ -212,9 +212,8 @@ public void it_should_test_getAccessToken() throws BitPayGenericException { @Test 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(response); + Mockito.when(this.bitPayClient.get("currencies")).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("currencies.json"))); Client testedClass = this.getTestedClass(); @@ -234,7 +233,8 @@ public void it_should_test_create_invoice_by_merchant() Invoice invoice = getInvoiceExample(); Mockito.when(this.bitPayClient.post("invoices", getPreparedJsonDataFromFile("createInvoiceRequest.json"), true)) - .thenReturn(getPreparedJsonDataFromFile("createInvoiceResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createInvoiceResponse.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)) .thenReturn("someToken"); @@ -262,7 +262,8 @@ public void it_should_test_createInvoice_by_pos() throws BitPayGenericException, Mockito .when(this.bitPayClient.post("invoices", getPreparedJsonDataFromFile("createInvoiceRequest.json"), false)) - .thenReturn(getPreparedJsonDataFromFile("createInvoiceResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createInvoiceResponse.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn("someToken"); Client testedClass = this.getTestedClass(); @@ -292,7 +293,8 @@ public void it_should_test_getInvoice_by_merchant() throws BitPayApiException, B Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(true))) - .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoice.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)) .thenReturn(facadeToken); @@ -317,7 +319,8 @@ public void it_should_test_getInvoice_by_pos() throws BitPayApiException, BitPay Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/" + id), ArgumentMatchers.eq(params), ArgumentMatchers.eq(false))) - .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoice.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(facadeToken); Client testedClass = this.getTestedClass(); @@ -341,7 +344,8 @@ public void it_should_test_getInvoiceByGuid() throws BitPayApiException, BitPayG Mockito.when(this.bitPayClient .get(ArgumentMatchers.eq("invoices/guid/" + guid), ArgumentMatchers.eq(expectedParams), ArgumentMatchers.eq(true))) - .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -364,7 +368,8 @@ public void it_should_test_getInvoices() throws BitPayApiException, BitPayGeneri expectedParams.add(new BasicNameValuePair("status", "complete")); expectedParams.add(new BasicNameValuePair("limit", "1")); Mockito.when(this.bitPayClient.get(ArgumentMatchers.eq("invoices"), ArgumentMatchers.eq(expectedParams))) - .thenReturn(getPreparedJsonDataFromFile("getInvoices.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoices.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -390,7 +395,8 @@ public void it_should_get_invoice_event_token() throws BitPayApiException, BitPa List expectedParams = new ArrayList(); expectedParams.add(new BasicNameValuePair("token", merchantToken)); Mockito.when(this.bitPayClient.get(ArgumentMatchers.eq("invoices/GZRP3zgNHTDf8F5BmdChKz/events"), ArgumentMatchers.eq(expectedParams))) - .thenReturn(getPreparedJsonDataFromFile("getInvoiceEventToken.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoiceEventToken.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -434,7 +440,8 @@ public void it_should_test_updateInvoice() throws BitPayApiException, BitPayGene Mockito.when(this.bitPayClient.update(ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq("{\"buyerSms\":\"+12223334444\",\"token\":\"merchantToken\"}"))) - .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -462,7 +469,7 @@ public void it_should_test_payInvoice() throws BitPayApiException, BitPayGeneric Mockito.when(this.bitPayClient.update(ArgumentMatchers.eq("invoices/pay/" + invoiceId), ArgumentMatchers.eq("{\"token\":\"merchantToken\",\"status\":\"complete\"}"))) - .thenReturn(getPreparedJsonDataFromFile("getInvoice.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -488,7 +495,7 @@ public void it_should_force_cancel_invoice() throws BitPayApiException, BitPayGe Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getCancelledInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -511,7 +518,7 @@ public void it_should_cancel_invoice() throws BitPayApiException, BitPayGenericE Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/" + invoiceId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getCancelledInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -534,7 +541,8 @@ public void it_should_cancel_invoice_by_guid() throws BitPayApiException, BitPay Mockito.when(this.bitPayClient.delete( ArgumentMatchers.eq("invoices/guid/" + guidId), ArgumentMatchers.eq(expectedParams)) - ).thenReturn(getPreparedJsonDataFromFile("getCancelledInvoice.json")); + ).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getCancelledInvoice.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -588,7 +596,7 @@ public void it_should_request_invoice_webhook_to_be_resent() throws BitPayApiExc Mockito.when(this.bitPayClient.post( ArgumentMatchers.eq("invoices/" + invoiceId + "/notifications"), ArgumentMatchers.eq(requestJson)) - ).thenReturn("\"Success\""); + ).thenReturn(this.getHttpResponseWithSpecificBody("{\"data\": \"Success\"}")); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -609,7 +617,8 @@ public void it_should_create_refund() throws BitPayApiException, BitPayGenericEx final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createRefundResponse.json"))); Mockito.when(this.guidGenerator.execute()).thenReturn(EXAMPLE_UUID); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -643,7 +652,8 @@ public void it_should_create_refund_with_guid() throws BitPayApiException, BitPa final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createRefundResponse.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -678,7 +688,8 @@ public void it_should_create_refund_using_refund_object() throws BitPayApiExcept final String createRefundJsonRequest = getPreparedJsonDataFromFile("createRefundRequest.json"); Mockito.when(this.bitPayClient.post("refunds/", createRefundJsonRequest, true)) - .thenReturn(getPreparedJsonDataFromFile("createRefundResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createRefundResponse.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); Refund refundRequestObject = new Refund(); @@ -852,7 +863,8 @@ public void it_should_get_refund_by_id() throws BitPayApiException, BitPayGeneri params.add(new BasicNameValuePair("token", "merchantToken")); Mockito.when(this.bitPayClient.get("refunds/" + refundId, params, true)) - .thenReturn(getPreparedJsonDataFromFile("getRefund.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getRefund.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -879,7 +891,8 @@ public void it_should_get_refund_by_guid() throws BitPayApiException, BitPayGene params.add(new BasicNameValuePair("token", "merchantToken")); Mockito.when(this.bitPayClient.get("refunds/guid/" + EXAMPLE_UUID, params, true)) - .thenReturn(getPreparedJsonDataFromFile("getRefund.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getRefund.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -909,7 +922,7 @@ public void it_should_test_get_refunds() throws BitPayApiException, BitPayGeneri params.add(new BasicNameValuePair("invoiceId", invoiceId)); Mockito.when(this.bitPayClient.get("refunds/", params, true)) - .thenReturn(getRefundsJsonConvertedResponse); + .thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -942,7 +955,7 @@ public void it_should_update_refund_by_id() throws BitPayApiException, BitPayGen Mockito.when(this.bitPayClient.update( "refunds/" + refundId, requestedJson - )).thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -971,7 +984,7 @@ public void it_should_update_refund_by_guid() throws BitPayApiException, BitPayG Mockito.when(this.bitPayClient.update( "refunds/guid/" + guid, requestedJson - )).thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -999,7 +1012,7 @@ public void it_should_test_sendRefundNotification() throws BitPayApiException, B "refunds/" + refundId + "/notifications", requestedJson, true - )).thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1029,7 +1042,7 @@ public void it_should_cancel_refund_by_id() throws BitPayApiException, BitPayGen Mockito.when(this.bitPayClient.delete( "refunds/" + refundId, params - )).thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1056,7 +1069,7 @@ public void it_should_cancel_refund_by_guid() throws BitPayApiException, BitPayG Mockito.when(this.bitPayClient.delete( "refunds/guid/" + guid, params - )).thenReturn(getRefundsJsonConvertedResponse); + )).thenReturn(this.getHttpResponseWithSpecificBody(getRefundsJsonConvertedResponse)); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Client testedClass = this.getTestedClass(); @@ -1081,7 +1094,8 @@ public void it_should_test_createBill_by_merchant_facade() throws BitPayApiExcep "bills", createBillApiRequest, true - )).thenReturn(getPreparedJsonDataFromFile("createBillResponse.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createBillResponse.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(merchantToken); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); @@ -1107,7 +1121,8 @@ public void it_should_test_createBill_by_pos_facade() throws BitPayApiException, "bills", createBillApiRequest, false - )).thenReturn(getPreparedJsonDataFromFile("createBillPosResponse.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("createBillPosResponse.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(merchantToken); @@ -1131,7 +1146,8 @@ public void it_should_test_getBill_by_merchant_facade() throws BitPayApiExceptio params.add(new BasicNameValuePair("token", facadeToken)); Mockito.when(this.bitPayClient.get("bills/" + BILL_ID, params, true)) - .thenReturn(getPreparedJsonDataFromFile("getBill.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getBill.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(facadeToken); @@ -1155,7 +1171,8 @@ public void it_should_test_getBill_by_pos_facade() throws BitPayApiException, Bi params.add(new BasicNameValuePair("token", facadeToken)); Mockito.when(this.bitPayClient.get("bills/" + BILL_ID, params, false)) - .thenReturn(getPreparedJsonDataFromFile("getBill.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getBill.json"))); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Mockito.when(this.accessTokens.getAccessToken(Facade.POS)).thenReturn(facadeToken); @@ -1181,7 +1198,8 @@ public void it_should_test_getBills() throws BitPayApiException, BitPayGenericEx Mockito.when(this.bitPayClient.get( "bills", params - )).thenReturn(getPreparedJsonDataFromFile("getBills.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getBills.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Client testedClass = this.getTestedClass(); @@ -1208,7 +1226,8 @@ public void it_should_test_getBills_by_status() throws BitPayApiException, BitPa Mockito.when(this.bitPayClient.get( "bills", params - )).thenReturn(getPreparedJsonDataFromFile("getBills.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getBills.json"))); Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Client testedClass = this.getTestedClass(); @@ -1236,7 +1255,8 @@ public void it_should_test_updateBill() throws BitPayApiException, BitPayGeneric Mockito.when(this.bitPayClient.update( "bills/" + BILL_ID, updateBillApiRequest - )).thenReturn(getPreparedJsonDataFromFile("getBill.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getBill.json"))); Client testedClass = this.getTestedClass(); @@ -1256,7 +1276,7 @@ public void it_should_test_deliverBill_by_merchant_facade() throws BitPayApiExce "bills/" + BILL_ID + "/deliveries", "{\"token\":\"billToken\"}", true - )).thenReturn("Success"); + )).thenReturn(this.getHttpResponseWithSpecificBody("{\"data\": \"Success\"}")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(true); Client testedClass = this.getTestedClass(); @@ -1281,7 +1301,7 @@ public void it_should_test_deliverBill_by_pos_facade() throws BitPayApiException "bills/" + BILL_ID + "/deliveries", "{\"token\":\"billToken\"}", false - )).thenReturn("Success"); + )).thenReturn(this.getHttpResponseWithSpecificBody("{\"data\": \"Success\"}")); Mockito.when(this.accessTokens.tokenExists(Facade.MERCHANT)).thenReturn(false); Client testedClass = this.getTestedClass(); @@ -1302,8 +1322,8 @@ public void it_should_test_deliverBill_by_pos_facade() throws BitPayApiException public void it_should_return_rate() throws BitPayApiException, BitPayGenericException { // given Mockito.when(this.bitPayClient.get("rates/BCH/USD")) - .thenReturn("{\"code\": \"USD\", \"name\": \"US Dollar\", \"rate\": 100.99}"); - + .thenReturn(this.getHttpResponseWithSpecificBody("{\"data\": {\"code\": \"USD\",\"name\": \"US Dollar\"," + + "\"rate\": 100.99 }}")); Client testedClass = this.getTestedClass(); // when @@ -1317,7 +1337,8 @@ public void it_should_return_rate() throws BitPayApiException, BitPayGenericExce @Test public void it_should_test_get_rates() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("rates")).thenReturn(getPreparedJsonDataFromFile("getRates.json")); + Mockito.when(this.bitPayClient.get("rates")) + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getRates.json"))); Client testedClass = this.getTestedClass(); @@ -1332,7 +1353,8 @@ public void it_should_test_get_rates() throws BitPayApiException, BitPayGenericE @Test public void it_should_get_rates_by_base_currency() throws BitPayApiException, BitPayGenericException { // given - Mockito.when(this.bitPayClient.get("rates/USD")).thenReturn(getPreparedJsonDataFromFile("getRates.json")); + Mockito.when(this.bitPayClient.get("rates/USD")).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getRates.json"))); Client testedClass = this.getTestedClass(); @@ -1358,7 +1380,7 @@ public void it_should_get_ledger_entries() throws BitPayApiException, BitPayGene Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("ledgers/" + currency, params)) - .thenReturn(getPreparedJsonDataFromFile("getLedgers.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getLedgers.json"))); Client testedClass = this.getTestedClass(); @@ -1380,7 +1402,8 @@ public void it_should_test_getLedgers() throws BitPayApiException, BitPayGeneric Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("ledgers", params)) - .thenReturn(getPreparedJsonDataFromFile("getLedgerBalances.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getLedgerBalances.json"))); Client testedClass = this.getTestedClass(); @@ -1403,7 +1426,8 @@ public void it_should_test_submitPayoutRecipients() throws BitPayApiException, B "recipients", getPreparedJsonDataFromFile("submitPayoutRecipientsRequest.json"), true - )).thenReturn(getPreparedJsonDataFromFile("submitPayoutRecipientsResponse.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("submitPayoutRecipientsResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1440,7 +1464,8 @@ public void it_should_test_getPayoutRecipients() throws BitPayApiException, BitP params.add(new BasicNameValuePair("offset", offset.toString())); Mockito.when(this.bitPayClient.get("recipients", params, true)) - .thenReturn(getPreparedJsonDataFromFile("retrieveRecipientsResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("retrieveRecipientsResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1465,7 +1490,8 @@ public void it_should_test_getPayoutRecipient() throws BitPayApiException, BitPa Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.get("recipients/" + RECIPIENT_ID, params, true)) - .thenReturn(getPreparedJsonDataFromFile("retrieveRecipientResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("retrieveRecipientResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1495,7 +1521,8 @@ public void it_should_test_updatePayoutRecipient() throws BitPayApiException, Bi Mockito.when(this.bitPayClient.update( "recipients/" + RECIPIENT_ID, getPreparedJsonDataFromFile("updatePayoutRecipientRequest.json") - )).thenReturn(getPreparedJsonDataFromFile("retrieveRecipientResponse.json")); + )).thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("retrieveRecipientResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1521,7 +1548,7 @@ public void it_should_test_deletePayoutRecipient() throws BitPayApiException, Bi Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.delete("recipients/" + RECIPIENT_ID, params)) - .thenReturn(getPreparedJsonDataFromFile("success.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("success.json"))); Client testedClass = this.getTestedClass(); @@ -1547,7 +1574,7 @@ public void it_should_test_requestPayoutRecipientNotification() throws BitPayApi "recipients/" + RECIPIENT_ID + "/notifications", requestJson, true - )).thenReturn(getPreparedJsonDataFromFile("success.json")); + )).thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("success.json"))); Client testedClass = this.getTestedClass(); @@ -1573,7 +1600,7 @@ public void it_should_test_submitPayout() throws BitPayApiException, BitPayGener Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts", requestJson, true)) - .thenReturn(getPreparedJsonDataFromFile("submitPayoutResponse.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("submitPayoutResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1598,7 +1625,8 @@ public void it_should_test_submitPayoutGroup() throws BitPayApiException, BitPay Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts/group", requestJson, true)) - .thenReturn(getPreparedJsonDataFromFile("submitPayoutGroupResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("submitPayoutGroupResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1655,7 +1683,7 @@ public void it_should_test_getPayout() throws BitPayApiException, BitPayGenericE Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.get("payouts/" + PAYOUT_ID, params, true)) - .thenReturn(getPreparedJsonDataFromFile("submitPayoutResponse.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("submitPayoutResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1679,7 +1707,7 @@ public void it_should_test_cancelPayout() throws BitPayApiException, BitPayGener Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.delete("payouts/" + PAYOUT_ID, params)) - .thenReturn(getPreparedJsonDataFromFile("success.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("success.json"))); Client testedClass = this.getTestedClass(); @@ -1701,7 +1729,8 @@ public void it_should_test_cancelPayoutGroup() throws BitPayApiException, BitPay Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.delete("payouts/group/" + PAYOUT_ID, params)) - .thenReturn(getPreparedJsonDataFromFile("cancelPayoutGroupResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("cancelPayoutGroupResponse.json"))); Client testedClass = this.getTestedClass(); // when @@ -1737,7 +1766,7 @@ public void it_should_test_getPayouts() throws BitPayApiException, BitPayGeneric Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.get("payouts", params, true)) - .thenReturn(getPreparedJsonDataFromFile("getPayouts.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getPayouts.json"))); Client testedClass = this.getTestedClass(); @@ -1768,7 +1797,7 @@ public void it_should_test_requestPayoutNotification() throws BitPayApiException Mockito.when(this.accessTokens.getAccessToken(Facade.PAYOUT)).thenReturn(PAYOUT_ACCESS_TOKEN); Mockito.when(this.bitPayClient.post("payouts/" + PAYOUT_ID + "/notifications", requestJson, true)) - .thenReturn(getPreparedJsonDataFromFile("success.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("success.json"))); Client testedClass = this.getTestedClass(); @@ -1806,7 +1835,7 @@ public void it_should_test_getSettlements() throws BitPayApiException, BitPayGen Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("settlements", params)) - .thenReturn(getPreparedJsonDataFromFile("getSettlementsResponse.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getSettlementsResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1837,7 +1866,7 @@ public void it_should_test_getSettlement() throws BitPayApiException, BitPayGene Mockito.when(this.accessTokens.getAccessToken(Facade.MERCHANT)).thenReturn(MERCHANT_TOKEN); Mockito.when(this.bitPayClient.get("settlements/" + settlementId, params)) - .thenReturn(getPreparedJsonDataFromFile("getSettlementResponse.json")); + .thenReturn(this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getSettlementResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1860,7 +1889,8 @@ public void it_should_test_getSettlementReconciliationReport() throws BitPayApiE params.add(new BasicNameValuePair("token", settlementToken)); Mockito.when(this.bitPayClient.get("settlements/" + settlementId + "/reconciliationreport", params)) - .thenReturn(getPreparedJsonDataFromFile("getSettlementReconciliationReportResponse.json")); + .thenReturn(this.getHttpResponseWithSpecificBody( + getPreparedJsonDataFromFile("getSettlementReconciliationReportResponse.json"))); Client testedClass = this.getTestedClass(); @@ -1878,7 +1908,8 @@ public void it_should_test_getSettlementReconciliationReport() throws BitPayApiE public void it_should_test_getSupportedWallets() throws BitPayApiException, BitPayGenericException { // given Mockito.when(this.bitPayClient.get("supportedwallets")) - .thenReturn(getPreparedJsonDataFromFile("getSupportedWalletsResponse.json")); + .thenReturn( + this.getHttpResponseWithSpecificBody(getPreparedJsonDataFromFile("getSupportedWalletsResponse.json"))); Client testedClass = this.getTestedClass(); @@ -2017,6 +2048,10 @@ protected String getPreparedJsonDataFromFile(String fileName) { return data; } + protected HttpResponse getHttpResponseWithSpecificBody(String body) { + return new HttpResponse(200, body, new HashMap<>(), "en_US", "HTTP/1.1"); + } + private Client getTestedClass() { return new Client( this.bitPayClient, diff --git a/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java b/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java index 6f63cd82..3deb6e99 100644 --- a/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java +++ b/src/test/java/com/bitpay/sdk/functional/ClientFunctionalTest.java @@ -9,10 +9,9 @@ import com.bitpay.sdk.exceptions.BitPayApiException; import com.bitpay.sdk.exceptions.BitPayException; import com.bitpay.sdk.exceptions.BitPayGenericException; +import com.bitpay.sdk.model.Currency; import com.bitpay.sdk.model.bill.Bill; import com.bitpay.sdk.model.bill.Item; -import com.bitpay.sdk.model.Currency; -import com.bitpay.sdk.model.payout.PayoutGroup; import com.bitpay.sdk.model.invoice.Buyer; import com.bitpay.sdk.model.invoice.Invoice; import com.bitpay.sdk.model.invoice.InvoiceEventToken; @@ -20,6 +19,7 @@ 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.PayoutRecipient; import com.bitpay.sdk.model.payout.PayoutRecipients; import com.bitpay.sdk.model.rate.Rate; @@ -80,6 +80,16 @@ public void it_should_test_rate_requests() throws BitPayGenericException, BitPay Assertions.assertTrue(rateUsd.getRate(Currency.BCH) != 0); } + /** + * Tested wallet requests: + * - GetSupportedWallets() + */ + @Test + public void it_should_test_wallet_requests() throws BitPayException { + List supportedWallets = this.client.getSupportedWallets(); + Assertions.assertFalse(supportedWallets.isEmpty()); + } + /** * Tested currency requests: * - GetCurrencyInfo(string currencyCode) @@ -408,17 +418,6 @@ public void it_should_test_bills_requests() throws BitPayException { Assertions.assertEquals("Success", deliverBill); } - /** - * Tested wallet requests: - * - * - GetSupportedWallets() - */ - @Test - public void it_should_test_wallet_requests() throws BitPayException { - List supportedWallets = this.client.getSupportedWallets(); - Assertions.assertFalse(supportedWallets.isEmpty()); - } - private Invoice getExampledInvoice() throws BitPayException { Invoice invoice = new Invoice(); invoice.setPrice(10.00); diff --git a/src/test/java/com/bitpay/sdk/model/settlement/PayoutInfoTest.java b/src/test/java/com/bitpay/sdk/model/settlement/PayoutInfoTest.java index 351843c1..8ce32322 100644 --- a/src/test/java/com/bitpay/sdk/model/settlement/PayoutInfoTest.java +++ b/src/test/java/com/bitpay/sdk/model/settlement/PayoutInfoTest.java @@ -168,7 +168,7 @@ public void it_should_change_sort() { @Test public void it_should_change_wire() { // given - String expected = "expectedString"; + boolean expected = false; PayoutInfo testedClass = this.getTestedClass(); // when From f42973e6e68dd4a0aeecf1fd4bddadf707df0727 Mon Sep 17 00:00:00 2001 From: Marcin Warzybok Date: Wed, 29 Nov 2023 15:29:20 +0100 Subject: [PATCH 2/3] SP-734 Java - Investigate HTTPResponse --- .../com/bitpay/sdk/client/HttpResponse.java | 15 ++++-- .../sdk/client/HttpResponseProvider.java | 7 ++- .../bitpay/sdk/client/HttpResponseTest.java | 49 +++++++++++++++++++ .../client/HttpResponseProviderTest.java | 35 +++++++++++++ 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/bitpay/sdk/client/HttpResponseTest.java create mode 100644 src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java diff --git a/src/main/java/com/bitpay/sdk/client/HttpResponse.java b/src/main/java/com/bitpay/sdk/client/HttpResponse.java index f99ff9fa..1821f97e 100644 --- a/src/main/java/com/bitpay/sdk/client/HttpResponse.java +++ b/src/main/java/com/bitpay/sdk/client/HttpResponse.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + package com.bitpay.sdk.client; import java.util.Map; @@ -25,22 +30,22 @@ public HttpResponse( } public Integer getCode() { - return code; + return this.code; } public String getBody() { - return body; + return this.body; } public Map getHeaders() { - return headers; + return this.headers; } public String getLocale() { - return locale; + return this.locale; } public String getHttpVersion() { - return httpVersion; + return this.httpVersion; } } diff --git a/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java b/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java index f8bc83a0..1d1bee48 100644 --- a/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java +++ b/src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) 2019 BitPay. + * All rights reserved. + */ + package com.bitpay.sdk.client; import com.bitpay.sdk.exceptions.BitPayApiException; @@ -9,7 +14,7 @@ import org.apache.http.HttpEntity; import org.apache.http.util.EntityUtils; -class HttpResponseProvider { +public class HttpResponseProvider { public static HttpResponse fromApacheHttpResponse(org.apache.http.HttpResponse apacheHttpResponse) throws BitPayApiException { diff --git a/src/test/java/com/bitpay/sdk/client/HttpResponseTest.java b/src/test/java/com/bitpay/sdk/client/HttpResponseTest.java new file mode 100644 index 00000000..da719e73 --- /dev/null +++ b/src/test/java/com/bitpay/sdk/client/HttpResponseTest.java @@ -0,0 +1,49 @@ +package com.bitpay.sdk.client; + +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class HttpResponseTest { + + private static final int CODE = 200; + private static final String BODY = "anyBody"; + private static final String LOCALE = "en_US"; + private static final String HTTP_VERSION = "HTTP/1.1"; + + @Test + public void it_should_returns_code() { + Assertions.assertEquals(CODE, this.getTestedClass().getCode()); + } + + @Test + public void it_should_returns_body() { + Assertions.assertSame(BODY, this.getTestedClass().getBody()); + } + + @Test + public void it_should_returns_headers() { + Assertions.assertSame("application/json", this.getTestedClass().getHeaders().get("Content-Type")); + } + + @Test + public void it_should_returns_locale() { + Assertions.assertSame(LOCALE, this.getTestedClass().getLocale()); + } + + @Test + public void it_should_returns_httpVersion() { + Assertions.assertSame(HTTP_VERSION, this.getTestedClass().getHttpVersion()); + } + + private HttpResponse getTestedClass() { + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + + return new HttpResponse(CODE, BODY, headers, LOCALE, HTTP_VERSION); + } +} diff --git a/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java b/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java new file mode 100644 index 00000000..bc0bc31b --- /dev/null +++ b/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java @@ -0,0 +1,35 @@ +package com.bitpay.sdk.functional.client; + +import com.bitpay.sdk.client.HttpResponse; +import com.bitpay.sdk.client.HttpResponseProvider; +import com.bitpay.sdk.exceptions.BitPayApiException; +import java.io.IOException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class HttpResponseProviderTest { + + @Test + public void it_should_correct_transfer_apache_response_to_bitpay_response() throws IOException, BitPayApiException { + + HttpClient apacheClient = HttpClientBuilder.create().build(); + HttpGet httpGet = new HttpGet("https://jsonplaceholder.typicode.com/posts/1"); + org.apache.http.HttpResponse apacheResponse = apacheClient.execute(httpGet); + + HttpResponse httpResponse = HttpResponseProvider.fromApacheHttpResponse(apacheResponse); + + Assertions.assertEquals(20, httpResponse.getCode()); + Assertions.assertEquals("{\n" + + " \"userId\": 1,\n" + + " \"id\": 1,\n" + + " \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n" + + " \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n" + + "}", httpResponse.getBody()); + } +} From eba15f19d3d18c04f08fc2fcb061355707356d63 Mon Sep 17 00:00:00 2001 From: Marcin Warzybok Date: Wed, 6 Dec 2023 14:53:36 +0100 Subject: [PATCH 3/3] SP-734 Java - Investigate HTTPResponse --- .../bitpay/sdk/functional/client/HttpResponseProviderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java b/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java index bc0bc31b..571470ad 100644 --- a/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java +++ b/src/test/java/com/bitpay/sdk/functional/client/HttpResponseProviderTest.java @@ -24,7 +24,7 @@ public void it_should_correct_transfer_apache_response_to_bitpay_response() thro HttpResponse httpResponse = HttpResponseProvider.fromApacheHttpResponse(apacheResponse); - Assertions.assertEquals(20, httpResponse.getCode()); + Assertions.assertEquals(200, httpResponse.getCode()); Assertions.assertEquals("{\n" + " \"userId\": 1,\n" + " \"id\": 1,\n" +