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()); + } +}