Skip to content

Commit

Permalink
SP-734 Java - Investigate HTTPResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarzybok-sumoheavy committed Nov 29, 2023
1 parent 90c7968 commit f42973e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/main/java/com/bitpay/sdk/client/HttpResponse.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2019 BitPay.
* All rights reserved.
*/

package com.bitpay.sdk.client;

import java.util.Map;
Expand Down Expand Up @@ -25,22 +30,22 @@ public HttpResponse(
}

public Integer getCode() {
return code;
return this.code;
}

public String getBody() {
return body;
return this.body;
}

public Map<String, String> getHeaders() {
return headers;
return this.headers;
}

public String getLocale() {
return locale;
return this.locale;
}

public String getHttpVersion() {
return httpVersion;
return this.httpVersion;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2019 BitPay.
* All rights reserved.
*/

package com.bitpay.sdk.client;

import com.bitpay.sdk.exceptions.BitPayApiException;
Expand All @@ -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 {
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/com/bitpay/sdk/client/HttpResponseTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

return new HttpResponse(CODE, BODY, headers, LOCALE, HTTP_VERSION);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f42973e

Please sign in to comment.