Skip to content

Commit

Permalink
Master: Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipeMata committed Sep 21, 2016
1 parent 2757192 commit 0f23859
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
15 changes: 11 additions & 4 deletions src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public class APIRequest {
private Request requester;
private Auth authenticator;
private String route;
private JSONObject body;


public APIRequest(String method, String route, Config config) throws Exception {
public APIRequest(String method, String route, JSONObject body, Config config) throws Exception {
this.route = route;
String authenticateRoute = config.getEndpoints().getJSONObject("authorize").getString("route");
String authenticateMethod = config.getEndpoints().getJSONObject("authorize").getString("method");
Expand All @@ -33,22 +34,24 @@ public APIRequest(String method, String route, Config config) throws Exception {
HttpURLConnection client = (HttpURLConnection) link.openConnection();

this.requester = new Request(method, client);
this.body = body;
}

public APIRequest(Auth auth, Request request){
public APIRequest(Auth auth, Request request, JSONObject body){
this.authenticator = auth;
this.requester = request;
this.body = body;
}

public JSONObject send(JSONObject body) throws AuthorizationException, GerencianetException, IOException{
public JSONObject send() throws AuthorizationException, GerencianetException, IOException{
Date expiredDate = this.authenticator.getExpires();
if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) {
this.authenticator.authorize();
}

this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
try {
return this.requester.send(body);
return this.requester.send(this.body);
} catch (AuthorizationException e) {
this.authenticator.authorize();
return this.requester.send(body);
Expand All @@ -62,4 +65,8 @@ public Request getRequester() {
public String getRoute() {
return route;
}

public JSONObject getBody() {
return body;
}
}
20 changes: 11 additions & 9 deletions src/main/java/br/com/gerencianet/gnsdk/Endpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,29 @@ public APIRequest getRequester() {
return requester;
}

public JSONObject call(String method, Map<String, String> params, JSONObject body) throws Exception{
return this.kernelCall(method, params, body);
public JSONObject call(String endpoint, Map<String, String> params, JSONObject body) throws Exception{
return kernelCall(endpoint, params, body);
}

public Map<String, Object> call(String method, Map<String, String> params, Map<String, Object> body) throws Exception{
JSONObject response= kernelCall(method, params, (JSONObject) JSONObject.wrap(body));
public Map<String, Object> call(String endpoint, Map<String, String> params, Map<String, Object> mapBody) throws Exception{
JSONObject body = (JSONObject) JSONObject.wrap(mapBody);
JSONObject response = kernelCall(endpoint, params, body);
return response.toMap();
}

private JSONObject kernelCall(String method, Map<String, String> params, JSONObject body) throws Exception{
private JSONObject kernelCall(String endpointName, Map<String, String> params, JSONObject body) throws Exception{
JSONObject endpoints = this.config.getEndpoints();
if(!endpoints.has(method))
if(!endpoints.has(endpointName))
throw new Exception("nonexistent endpoint");

JSONObject endpoint = (JSONObject)endpoints.get(method);
JSONObject endpoint = (JSONObject)endpoints.get(endpointName);
String route = getRoute(endpoint, params);
route += getQueryString(params);
if(this.requester == null)
requester = new APIRequest(endpoint.get("method").toString(), route, this.config);
JSONObject response = this.requester.send(body);
requester = new APIRequest(endpoint.get("method").toString(), route, body, this.config);
JSONObject response = this.requester.send();
this.requester = null;

return response;
}

Expand Down
33 changes: 23 additions & 10 deletions src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ public void shouldRequestSuccessfully() throws AuthorizationException, Gerencian
when(authenticator.getExpires()).thenReturn(new Date(new Date().getTime() + 500));
JSONObject options = new JSONObject();
options.put("baseUri", "https://sandbox.gerencianet.com.br");
JSONObject body = mock(JSONObject.class);

when(config.getOptions()).thenReturn(options);
apiRequester = new APIRequest(authenticator, requester);
apiRequester.send(body); // "post", "/v1/charge",

apiRequester = new APIRequest(authenticator, requester, body);
apiRequester.send(); // "post", "/v1/charge",
verify(requester, times(1)).send(body);

}
Expand All @@ -61,9 +64,12 @@ public void shouldReauthorizeExpiredToken() throws AuthorizationException, Geren
when(authenticator.getExpires()).thenReturn(new Date());
JSONObject options = new JSONObject();
options.put("baseUri", "https://sandbox.gerencianet.com.br");
JSONObject body = mock(JSONObject.class);

when(config.getOptions()).thenReturn(options);
apiRequester = new APIRequest(authenticator, requester);
apiRequester.send(body);

apiRequester = new APIRequest(authenticator, requester, body);
apiRequester.send();
verify(authenticator, times(1)).authorize();
verify(requester, times(1)).send(body);
}
Expand All @@ -73,23 +79,27 @@ public void shouldReauthorizeNullToken() throws AuthorizationException, Gerencia
when(authenticator.getExpires()).thenReturn(null);
JSONObject options = new JSONObject();
options.put("baseUri", "https://sandbox.gerencianet.com.br");
JSONObject body = mock(JSONObject.class);
when(config.getOptions()).thenReturn(options);
apiRequester = new APIRequest(authenticator, requester);
apiRequester.send(body);

apiRequester = new APIRequest(authenticator, requester, body);
apiRequester.send();
verify(authenticator, times(1)).authorize();
verify(requester, times(1)).send(body);
}

@Test
public void shouldReauthorizeWhenServerRespondsWithAuthError() throws GerencianetException, IOException, AuthorizationException{
JSONObject success = new JSONObject("{status: 200}");
JSONObject body = mock(JSONObject.class);

when(authenticator.getExpires()).thenReturn(new Date(new Date().getTime() + 500));
when(requester.send(body)).thenThrow(new AuthorizationException()).thenReturn(success);

JSONObject response = new JSONObject();
apiRequester = new APIRequest(authenticator, requester);
apiRequester = new APIRequest(authenticator, requester, body);
try{
response = apiRequester.send(body);
response = apiRequester.send();
}catch(AuthorizationException e){
verify(authenticator, times(1)).authorize();
verify(requester, times(2)).send(body);
Expand All @@ -99,21 +109,24 @@ public void shouldReauthorizeWhenServerRespondsWithAuthError() throws Gerenciane
}

@Test
public void test() throws Exception{
public void shouldSetPropertiesCorrectly() throws Exception{
JSONObject endpoints = new JSONObject();
JSONObject authorize = new JSONObject();
authorize.put("route", "/v1/authorize");
authorize.put("method", "post");
endpoints.put("authorize", authorize);
JSONObject body = new JSONObject("{\"item\": 12}");

JSONObject credentials = mock(JSONObject.class);
when(credentials.has("clientId")).thenReturn(true);
when(credentials.has("clientSecret")).thenReturn(true);
when(credentials.getString("baseUri")).thenReturn("https://sandbox.gerencianet.com.br");
when(config.getEndpoints()).thenReturn(endpoints);
when(config.getOptions()).thenReturn(credentials);
apiRequester = new APIRequest("post", "/v1/charge", config);
apiRequester = new APIRequest("post", "/v1/charge", body, config);
Assert.assertTrue(apiRequester.getRequester() != null);
Assert.assertTrue(apiRequester.getBody().has("item"));
Assert.assertTrue(apiRequester.getBody().getInt("item") == 12);
}

}
30 changes: 12 additions & 18 deletions src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -73,7 +70,7 @@ public void shouldGenerateRequestWithJsonBodySuccessfuly() throws Exception{
endpoints = new Endpoints(config, apiRequester);
JSONObject body = mock(JSONObject.class);
endpoints.call("charge", new HashMap<String, String>(), body);
verify(apiRequester, times(1)).send(body);
verify(apiRequester, times(1)).send();
}

@Test
Expand All @@ -89,28 +86,24 @@ public void shouldGenerateRequestWithMapBodySuccessfuly() throws Exception{

ep.put("authorize", authorize);
ep.put("charge", charge);

Map<String, Object> body = new HashMap<String,Object>();
body.put("item", 1);
JSONObject expectedResponse = new JSONObject("{\"status\": 200}");

JSONObject options = mock(JSONObject.class);
when(options.has("clientId")).thenReturn(true);
when(options.has("clientSecret")).thenReturn(true);
when(options.getString("baseUri")).thenReturn("https://sandbox.gerencianet.com.br");
Mockito.when(config.getEndpoints()).thenReturn(ep);
Mockito.when(config.getOptions()).thenReturn(options);
when(apiRequester.send()).thenReturn(expectedResponse);

endpoints = new Endpoints(config, apiRequester);
HashMap<String, Object> response = (HashMap<String, Object>) endpoints.call("charge", new HashMap<String, String>(), body);

HashMap<String, Object> body= new HashMap<String, Object>();
body.put("id", 1);
JSONObject jsonBody = (JSONObject) JSONObject.wrap(body);
ArgumentCaptor<JSONObject> bodyCaptor = ArgumentCaptor.forClass(JSONObject.class);

try{
endpoints.call("charge", new HashMap<String, String>(), body);
}catch(NullPointerException e){
verify(apiRequester).send(bodyCaptor.capture());
JSONObject jsonBodyExpected = bodyCaptor.getValue();
Assert.assertTrue(jsonBodyExpected.has("id") && jsonBody.has("id"));
Assert.assertTrue(jsonBodyExpected.getInt("id") == jsonBody.getInt("id"));
}
Assert.assertTrue(response.containsKey("status"));
Assert.assertTrue(response.get("status").equals(200));
}

@Test(expected=Exception.class)
Expand Down Expand Up @@ -208,6 +201,7 @@ public void shouldForwarWithQueryString() throws Exception {

ep.put("authorize", authorize);
ep.put("charge", charge);

JSONObject options = mock(JSONObject.class);
when(options.has("clientId")).thenReturn(true);
when(options.has("clientSecret")).thenReturn(true);
Expand All @@ -216,7 +210,7 @@ public void shouldForwarWithQueryString() throws Exception {
Mockito.when(config.getOptions()).thenReturn(options);
JSONObject body = mock(JSONObject.class);

endpoints = new Endpoints(config);
endpoints = new Endpoints(config, apiRequester);
HashMap<String, String> params = new HashMap<String, String>();
params.put("id", "1");
params.put("token", "45646546894621");
Expand Down

0 comments on commit 0f23859

Please sign in to comment.