diff --git a/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java b/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java index 7b474fa..2852376 100644 --- a/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java +++ b/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java @@ -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"); @@ -33,14 +34,16 @@ 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(); @@ -48,7 +51,7 @@ public JSONObject send(JSONObject body) throws AuthorizationException, Gerencian 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); @@ -62,4 +65,8 @@ public Request getRequester() { public String getRoute() { return route; } + + public JSONObject getBody() { + return body; + } } diff --git a/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java b/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java index ad22678..3d149a8 100644 --- a/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java +++ b/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java @@ -57,27 +57,29 @@ public APIRequest getRequester() { return requester; } - public JSONObject call(String method, Map params, JSONObject body) throws Exception{ - return this.kernelCall(method, params, body); + public JSONObject call(String endpoint, Map params, JSONObject body) throws Exception{ + return kernelCall(endpoint, params, body); } - public Map call(String method, Map params, Map body) throws Exception{ - JSONObject response= kernelCall(method, params, (JSONObject) JSONObject.wrap(body)); + public Map call(String endpoint, Map params, Map mapBody) throws Exception{ + JSONObject body = (JSONObject) JSONObject.wrap(mapBody); + JSONObject response = kernelCall(endpoint, params, body); return response.toMap(); } - private JSONObject kernelCall(String method, Map params, JSONObject body) throws Exception{ + private JSONObject kernelCall(String endpointName, Map 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; } diff --git a/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java b/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java index ac63460..94a28ca 100644 --- a/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java +++ b/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java @@ -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); } @@ -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); } @@ -73,9 +79,11 @@ 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); } @@ -83,13 +91,15 @@ public void shouldReauthorizeNullToken() throws AuthorizationException, Gerencia @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); @@ -99,12 +109,13 @@ 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); @@ -112,8 +123,10 @@ public void test() throws Exception{ 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); } } diff --git a/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java b/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java index 0c3cd4e..e7a99ca 100644 --- a/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java +++ b/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java @@ -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; @@ -73,7 +70,7 @@ public void shouldGenerateRequestWithJsonBodySuccessfuly() throws Exception{ endpoints = new Endpoints(config, apiRequester); JSONObject body = mock(JSONObject.class); endpoints.call("charge", new HashMap(), body); - verify(apiRequester, times(1)).send(body); + verify(apiRequester, times(1)).send(); } @Test @@ -89,28 +86,24 @@ public void shouldGenerateRequestWithMapBodySuccessfuly() throws Exception{ ep.put("authorize", authorize); ep.put("charge", charge); + + Map body = new HashMap(); + 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 response = (HashMap) endpoints.call("charge", new HashMap(), body); - HashMap body= new HashMap(); - body.put("id", 1); - JSONObject jsonBody = (JSONObject) JSONObject.wrap(body); - ArgumentCaptor bodyCaptor = ArgumentCaptor.forClass(JSONObject.class); - - try{ - endpoints.call("charge", new HashMap(), 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) @@ -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); @@ -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 params = new HashMap(); params.put("id", "1"); params.put("token", "45646546894621");