Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.dtone.dvs</groupId>
<artifactId>dvs-apiclient</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<packaging>jar</packaging>

<name>dvs-apiclient</name>
Expand Down Expand Up @@ -51,9 +51,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.6.1</version>
<scope>compile</scope>
</dependency>

<dependency>
Expand Down
38 changes: 17 additions & 21 deletions src/main/java/com/dtone/dvs/service/RestApiInvokeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;

import com.dtone.dvs.dto.ApiRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.StringEntity;

public class RestApiInvokeService {

Expand All @@ -30,9 +27,8 @@ public class RestApiInvokeService {
public RestApiInvokeService(String apiKey, String apiSecret) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;

CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.getApiKey(), this.getApiSecret()));
BasicCredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(null, null, -1,null, null), new UsernamePasswordCredentials(this.getApiKey(), this.getApiSecret().toCharArray()));
final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultCredentialsProvider(provider);
httpClient = httpClientBuilder.build();
Expand All @@ -44,12 +40,12 @@ public RestApiInvokeService(String apiKey, String apiSecret, HttpClient httpClie
this.httpClient = httpClient;
}

public HttpResponse executeGet(String url) throws IOException {
return httpClient.execute(getHttpGet(url));
public ClassicHttpResponse executeGet(String url) throws IOException {
return (ClassicHttpResponse) httpClient.execute(getHttpGet(url));
}

public HttpResponse executePost(String url, ApiRequest apiRequest) throws IOException {
return httpClient.execute(getHttpPost(url, apiRequest));
public ClassicHttpResponse executePost(String url, ApiRequest apiRequest) throws IOException {
return (ClassicHttpResponse) httpClient.execute(getHttpPost(url, apiRequest));
}

private static HttpPost getHttpPost(String url, ApiRequest apiRequest)
Expand All @@ -65,7 +61,7 @@ private static HttpPost getHttpPost(String url, ApiRequest apiRequest)
return httpPost;
}

private static HttpGet getHttpGet(String url) {
private static ClassicHttpRequest getHttpGet(String url) {
return new HttpGet(url);
}

Expand Down
49 changes: 27 additions & 22 deletions src/main/java/com/dtone/dvs/util/ApiResponseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import com.dtone.dvs.dto.ApiError;
import com.dtone.dvs.dto.ApiResponse;
import com.dtone.dvs.dto.ErrorResponse;
Expand All @@ -17,6 +13,10 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpResponse;

public class ApiResponseBuilder {

Expand All @@ -34,29 +34,34 @@ public class ApiResponseBuilder {
* @throws DvsApiException
*/
public <T> ApiResponse<T> prepareResponse(ApiResponse<T> apiResponse, TypeReference<T> typeReference,
HttpResponse response) throws IOException {
int statusCode = response.getStatusLine().getStatusCode();
setPageDetails(apiResponse, response.getAllHeaders());

apiResponse.setCode(statusCode);
apiResponse.setSuccess(true);
HttpEntity httpEntity = response.getEntity();
ClassicHttpResponse response) throws IOException {
try{
int statusCode = response.getCode();
setPageDetails(apiResponse, response.getHeaders());

if (statusCode < 400) {
apiResponse.setResult(extractResult(httpEntity, typeReference));
} else {
apiResponse.setSuccess(false);
apiResponse.setCode(statusCode);
apiResponse.setSuccess(true);
HttpEntity httpEntity = response.getEntity();

if (httpEntity != null && response.getEntity().getContent().available() != 0) {
apiResponse.setErrors(extractResult(httpEntity, new TypeReference<ErrorResponse>() {
}).getErrors());
if (statusCode < 400) {
apiResponse.setResult(extractResult(httpEntity, typeReference));
} else {
apiResponse.getErrors()
.add(new ApiError(String.valueOf(statusCode), response.getStatusLine().getReasonPhrase()));
}
apiResponse.setSuccess(false);

if (httpEntity != null && response.getEntity().getContent().available() != 0) {
apiResponse.setErrors(extractResult(httpEntity, new TypeReference<ErrorResponse>() {
}).getErrors());
} else {
apiResponse.getErrors()
.add(new ApiError(String.valueOf(statusCode), response.getReasonPhrase()));
}

}
return apiResponse;
}
finally {
response.close();
}
return apiResponse;
}

private static <T> void setPageDetails(ApiResponse<T> apiResponse, Header[] headers) {
Expand Down
34 changes: 13 additions & 21 deletions src/test/java/com/dtone/dvs/ApiResponseBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
import java.io.InputStream;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicHeader;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.message.BasicHeader;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.dtone.dvs.DvsApiClient;
import com.dtone.dvs.dto.ApiResponse;
import com.dtone.dvs.dto.Service;
import com.dtone.dvs.util.ApiResponseBuilder;
Expand All @@ -40,11 +38,10 @@ public class ApiResponseBuilderTest {
HttpGet mockHttpGet = mock(HttpGet.class);
DvsApiClient mockDvsClient = mock(DvsApiClient.class);
HttpResponse mockHttpResponse = mock(HttpResponse.class);
StatusLine mockStatusLine = mock(StatusLine.class);
HttpEntity mockHttpEntity = mock(HttpEntity.class);

@Mock
HttpResponse httpResponse = mock(CloseableHttpResponse.class);
ClassicHttpResponse httpResponse = mock(ClassicHttpResponse.class);

@Before
public void setUp() throws Exception {
Expand All @@ -54,10 +51,8 @@ public void setUp() throws Exception {
@Test
public void testHttpGetSuccessResponse() throws Exception {

when(httpResponse.getStatusLine()).thenReturn(mockStatusLine);
when(mockStatusLine.getStatusCode()).thenReturn(200);

when(httpResponse.getAllHeaders()).thenReturn(MockTestUtils.getMockHttpHeaders());
when(httpResponse.getCode()).thenReturn(200);
when(httpResponse.getHeaders()).thenReturn(MockTestUtils.getMockHttpHeaders());

when(httpResponse.getEntity()).thenReturn(mockHttpEntity);

Expand All @@ -74,11 +69,10 @@ public void testHttpGetSuccessResponse() throws Exception {

@Test
public void testHttpGetFailureResponse() throws Exception {
when(httpResponse.getStatusLine()).thenReturn(mockStatusLine);
when(mockStatusLine.getStatusCode()).thenReturn(404);
when(httpResponse.getCode()).thenReturn(404);

Header[] mockHeaders = new Header[] { new BasicHeader("a", "b") };
when(httpResponse.getAllHeaders()).thenReturn(mockHeaders);
when(httpResponse.getHeaders()).thenReturn(mockHeaders);

when(httpResponse.getEntity()).thenReturn(mockHttpEntity);

Expand All @@ -98,12 +92,10 @@ public void testHttpGetFailureResponse() throws Exception {

@Test
public void testHttpGetFailureResponse429() throws Exception {
when(httpResponse.getStatusLine()).thenReturn(mockStatusLine);
when(mockStatusLine.getStatusCode()).thenReturn(429);
when(mockStatusLine.getReasonPhrase()).thenReturn("Too many requests");
when(httpResponse.getCode()).thenReturn(429);

Header[] mockHeaders = new Header[] { new BasicHeader("a", "b") };
when(httpResponse.getAllHeaders()).thenReturn(mockHeaders);
when(httpResponse.getHeaders()).thenReturn(mockHeaders);

when(httpResponse.getEntity()).thenReturn(mockHttpEntity);

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/dtone/dvs/DvsApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/com/dtone/dvs/RestApiInvokeServiceMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
Expand All @@ -20,13 +20,13 @@
public class RestApiInvokeServiceMockTest {

@Mock
HttpClient mockHttpClient = mock(HttpClient.class);
HttpClient mockHttpClient = mock(HttpClient.class);

@InjectMocks
RestApiInvokeService mockRestApiInvokeService = new RestApiInvokeService("", "", mockHttpClient);

@Mock
HttpResponse mockHttpResponse = mock(HttpResponse.class);
ClassicHttpResponse mockHttpResponse = mock(ClassicHttpResponse.class);

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -62,5 +62,4 @@ public void testExecutePostException() throws Exception {
when(mockRestApiInvokeService.getHttpClient().execute(Mockito.any())).thenThrow(new ClientProtocolException());
mockRestApiInvokeService.executePost("", new TransactionRequest());
}

}
8 changes: 2 additions & 6 deletions src/test/java/com/dtone/dvs/util/MockTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import com.dtone.dvs.dto.ApiResponse;
import com.dtone.dvs.dto.Balance;
import com.dtone.dvs.dto.BenefitType;
Expand All @@ -22,10 +19,9 @@
import com.dtone.dvs.dto.Service;
import com.dtone.dvs.dto.TransactionRequest;
import com.dtone.dvs.dto.Transaction;
import com.dtone.dvs.util.ApiResponseBuilder;
import com.dtone.dvs.util.Constants;
import com.dtone.dvs.util.ErrorCodes;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.message.BasicHeader;

public class MockTestUtils {

Expand Down