From e87bb61e2634bd4d1cce3372935c628a25a07e3b Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 10:56:23 -0300 Subject: [PATCH 01/16] add intellij file to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b3f16c1..6a439c8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .settings/ target/ mockapi/node_modules +/.idea/ +/sdk.iml From 2b1e58b42ce24b9c44572ab6bee4b7d1569d7bb9 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 11:08:54 -0300 Subject: [PATCH 02/16] code format --- mockapi/app.js | 104 ++-- mockapi/package.json | 12 +- pom.xml | 104 ++-- .../sdk/AuthorizationFailure.java | 7 +- src/main/java/com/mercadolibre/sdk/Meli.java | 564 +++++++++--------- .../com/mercadolibre/sdk/MeliException.java | 3 +- .../java/com/mercadolibre/sdk/MeliTest.java | 135 ++--- 7 files changed, 478 insertions(+), 451 deletions(-) diff --git a/mockapi/app.js b/mockapi/app.js index b39715b..fcf197a 100644 --- a/mockapi/app.js +++ b/mockapi/app.js @@ -3,69 +3,91 @@ var fs = require('fs'); var app = express.createServer(); -app.configure(function(){ +app.configure(function () { app.use(express.methodOverride()); app.use(express.bodyParser()); }); -app.post('/oauth/token', function(req, res) { - if(req.query["grant_type"]=="authorization_code") { - if(req.query["code"]=="bad code") { - res.send({"message":"Error validando el parámetro code","error":"invalid_grant","status":400,"cause":[]}, 400); - } else if(req.query["code"]=="valid code without refresh token") { +app.post('/oauth/token', function (req, res) { + if (req.query["grant_type"] == "authorization_code") { + if (req.query["code"] == "bad code") { res.send({ - "access_token" : "valid token", - "token_type" : "bearer", - "expires_in" : 10800, - "scope" : "write read" + "message": "Error validando el parámetro code", + "error": "invalid_grant", + "status": 400, + "cause": [] + }, 400); + } else if (req.query["code"] == "valid code without refresh token") { + res.send({ + "access_token": "valid token", + "token_type": "bearer", + "expires_in": 10800, + "scope": "write read" }); - } else if(req.query["code"]=="valid code with refresh token") { + } else if (req.query["code"] == "valid code with refresh token") { res.send({ - "access_token" : "valid token", - "token_type" : "bearer", - "expires_in" : 10800, - "refresh_token" : "valid refresh token", - "scope" : "write read" + "access_token": "valid token", + "token_type": "bearer", + "expires_in": 10800, + "refresh_token": "valid refresh token", + "scope": "write read" }); } else { res.send(404); } - } else if(req.query['grant_type']=='refresh_token') { - if(req.query['refresh_token']=='valid refresh token') { + } else if (req.query['grant_type'] == 'refresh_token') { + if (req.query['refresh_token'] == 'valid refresh token') { res.send({ - "access_token" : "valid token", - "token_type" : "bearer", - "expires_in" : 10800, - "scope" : "write read" + "access_token": "valid token", + "token_type": "bearer", + "expires_in": 10800, + "scope": "write read" }); } } }); -app.get('/sites', function(req, res) { - res.send([{"id":"MLA","name":"Argentina"},{"id":"MLB","name":"Brasil"},{"id":"MCO","name":"Colombia"},{"id":"MCR","name":"Costa Rica"},{"id":"MEC","name":"Ecuador"},{"id":"MLC","name":"Chile"},{"id":"MLM","name":"Mexico"},{"id":"MLU","name":"Uruguay"},{"id":"MLV","name":"Venezuela"},{"id":"MPA","name":"Panamá"},{"id":"MPE","name":"Perú"},{"id":"MPT","name":"Portugal"},{"id":"MRD","name":"Dominicana"}]); +app.get('/sites', function (req, res) { + res.send([{"id": "MLA", "name": "Argentina"}, {"id": "MLB", "name": "Brasil"}, { + "id": "MCO", + "name": "Colombia" + }, {"id": "MCR", "name": "Costa Rica"}, {"id": "MEC", "name": "Ecuador"}, { + "id": "MLC", + "name": "Chile" + }, {"id": "MLM", "name": "Mexico"}, {"id": "MLU", "name": "Uruguay"}, { + "id": "MLV", + "name": "Venezuela" + }, {"id": "MPA", "name": "Panamá"}, {"id": "MPE", "name": "Perú"}, {"id": "MPT", "name": "Portugal"}, { + "id": "MRD", + "name": "Dominicana" + }]); }); -app.get('/users/me', function(req, res) { - if(req.query['access_token']=='valid token') { - res.send({"id":123456,"nickname":"foobar"}); - } else if(req.query['access_token']=='expired token') { +app.get('/users/me', function (req, res) { + if (req.query['access_token'] == 'valid token') { + res.send({"id": 123456, "nickname": "foobar"}); + } else if (req.query['access_token'] == 'expired token') { res.send(404); } else { - res.send({"message":"The User ID must match the consultant's","error":"forbidden","status":403,"cause":[]}, 403); + res.send({ + "message": "The User ID must match the consultant's", + "error": "forbidden", + "status": 403, + "cause": [] + }, 403); } }); -app.post('/items', function(req, res) { - if(req.query['access_token']=='valid token') { - if(req.body && req.body.foo == "bar") { +app.post('/items', function (req, res) { + if (req.query['access_token'] == 'valid token') { + if (req.body && req.body.foo == "bar") { res.send(201); } else { res.send(400); } - } else if(req.query['access_token']=='expired token') { + } else if (req.query['access_token'] == 'expired token') { res.send(404); } else { res.send(403); @@ -73,31 +95,31 @@ app.post('/items', function(req, res) { }); -app.put('/items/123', function(req, res) { - if(req.query['access_token']=='valid token') { - if(req.body && req.body.foo == "bar") { +app.put('/items/123', function (req, res) { + if (req.query['access_token'] == 'valid token') { + if (req.body && req.body.foo == "bar") { res.send(200); } else { res.send(400); } - } else if(req.query['access_token']=='expired token') { + } else if (req.query['access_token'] == 'expired token') { res.send(404); } else { res.send(403); } }); -app.delete('/items/123', function(req, res) { - if(req.query['access_token']=='valid token') { +app.delete('/items/123', function (req, res) { + if (req.query['access_token'] == 'valid token') { res.send(200); - } else if(req.query['access_token']=='expired token') { + } else if (req.query['access_token'] == 'expired token') { res.send(404); } else { res.send(403); } }); -app.get('/echo/user_agent',function(req,res) { +app.get('/echo/user_agent', function (req, res) { if (req.headers['user-agent'].match(/MELI-JAVA-SDK-.*/)) res.send(200); else diff --git a/mockapi/package.json b/mockapi/package.json index e3497bb..3788de5 100644 --- a/mockapi/package.json +++ b/mockapi/package.json @@ -1,8 +1,8 @@ { - "name": "mockapi", - "version": "1.0.0", - "dependencies": { - "express" : "2.5.x" - }, - "engine": "node ~> 0.8.x" + "name": "mockapi", + "version": "1.0.0", + "dependencies": { + "express": "2.5.x" + }, + "engine": "node ~> 0.8.x" } diff --git a/pom.xml b/pom.xml index d6261c7..c1a0b1f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,58 +1,58 @@ - 4.0.0 - com.mercadolibre - sdk - 0.0.3-SNAPSHOT - - - repo - https://github.com/mercadolibre/java-sdk-repo/raw/master/releases - - - snapshot-repo - https://github.com/mercadolibre/java-sdk-repo/raw/master/snapshots - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.mercadolibre + sdk + 0.0.3-SNAPSHOT + + + repo + https://github.com/mercadolibre/java-sdk-repo/raw/master/releases + + + snapshot-repo + https://github.com/mercadolibre/java-sdk-repo/raw/master/snapshots + + - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.6 - 1.6 - - + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + - - org.apache.maven.plugins - maven-surefire-plugin - - true - - + + org.apache.maven.plugins + maven-surefire-plugin + + true + + - - + + - - - junit - junit - 4.10 - - - com.ning - async-http-client - 1.7.4 - - - com.google.code.gson - gson - 2.2.2 - - + + + junit + junit + 4.10 + + + com.ning + async-http-client + 1.7.4 + + + com.google.code.gson + gson + 2.2.2 + + diff --git a/src/main/java/com/mercadolibre/sdk/AuthorizationFailure.java b/src/main/java/com/mercadolibre/sdk/AuthorizationFailure.java index 38a06ae..e4be785 100644 --- a/src/main/java/com/mercadolibre/sdk/AuthorizationFailure.java +++ b/src/main/java/com/mercadolibre/sdk/AuthorizationFailure.java @@ -1,14 +1,15 @@ package com.mercadolibre.sdk; public class AuthorizationFailure extends Exception { + private static final long serialVersionUID = 8688100047490895706L; public AuthorizationFailure(String message) { - super(message); + super(message); - } + } public AuthorizationFailure(Throwable cause) { - super(cause); + super(cause); } } diff --git a/src/main/java/com/mercadolibre/sdk/Meli.java b/src/main/java/com/mercadolibre/sdk/Meli.java index 4a39897..fed83fb 100644 --- a/src/main/java/com/mercadolibre/sdk/Meli.java +++ b/src/main/java/com/mercadolibre/sdk/Meli.java @@ -16,316 +16,318 @@ import com.ning.http.client.Response; public class Meli { - - public static String apiUrl = "https://api.mercadolibre.com"; - - /** - * Availables auth sites. One user - application can only operate in one site - * - */ - - public static enum AuthUrls { - MLA("https://auth.mercadolibre.com.ar"), // Argentina - MLB("https://auth.mercadolivre.com.br"), // Brasil - MCO("https://auth.mercadolibre.com.co"), // Colombia - MCR("https://auth.mercadolibre.com.cr"), // Costa Rica - MEC("https://auth.mercadolibre.com.ec"), // Ecuador - MLC("https://auth.mercadolibre.cl"), // Chile - MLM("https://auth.mercadolibre.com.mx"), // Mexico - MLU("https://auth.mercadolibre.com.uy"), // Uruguay - MLV("https://auth.mercadolibre.com.ve"), // Venezuela - MPA("https://auth.mercadolibre.com.pa"), // Panama - MPE("https://auth.mercadolibre.com.pe"), // Peru - MPT("https://auth.mercadolibre.com.pt"), // Portugal - MRD("https://auth.mercadolibre.com.do"); // Dominicana - - private String value; - - - private AuthUrls(String value) { - this.value = value; - } - - public String getValue() { - return value; - } - }; - - private String accessToken; - private String refreshToken; - private Long clientId; - private String clientSecret; - private AsyncHttpClient http; - /** news **/ - private Long expiresIn; - private String scope; - private String userId; - private String tokenType; - - - - { - AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder() - .setUserAgent("MELI-JAVA-SDK-0.0.4").build(); - http = new AsyncHttpClient(cf); - } - - public Meli(Long clientId, String clientSecret) { - this.clientId = clientId; - this.clientSecret = clientSecret; - } - public Meli(Long clientId, String clientSecret, String accessToken) { - this.accessToken = accessToken; - this.clientId = clientId; - this.clientSecret = clientSecret; - } + public static String apiUrl = "https://api.mercadolibre.com"; + + /** + * Availables auth sites. One user - application can only operate in one site + */ + + public static enum AuthUrls { + MLA("https://auth.mercadolibre.com.ar"), // Argentina + MLB("https://auth.mercadolivre.com.br"), // Brasil + MCO("https://auth.mercadolibre.com.co"), // Colombia + MCR("https://auth.mercadolibre.com.cr"), // Costa Rica + MEC("https://auth.mercadolibre.com.ec"), // Ecuador + MLC("https://auth.mercadolibre.cl"), // Chile + MLM("https://auth.mercadolibre.com.mx"), // Mexico + MLU("https://auth.mercadolibre.com.uy"), // Uruguay + MLV("https://auth.mercadolibre.com.ve"), // Venezuela + MPA("https://auth.mercadolibre.com.pa"), // Panama + MPE("https://auth.mercadolibre.com.pe"), // Peru + MPT("https://auth.mercadolibre.com.pt"), // Portugal + MRD("https://auth.mercadolibre.com.do"); // Dominicana + + private String value; - public Meli(Long clientId, String clientSecret, String accessToken, String refreshToken) { - this.accessToken = accessToken; - this.clientId = clientId; - this.clientSecret = clientSecret; - this.refreshToken = refreshToken; - } - public String getAccessToken() { - return this.accessToken; + private AuthUrls(String value) { + this.value = value; } - public String getRefreshToken() { - return this.refreshToken; + public String getValue() { + return value; } - /** news **/ - public Long getExpiresIn() { - return this.expiresIn; + } + + ; + + private String accessToken; + private String refreshToken; + private Long clientId; + private String clientSecret; + private AsyncHttpClient http; + /** + * news + **/ + private Long expiresIn; + private String scope; + private String userId; + private String tokenType; + + + { + AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder() + .setUserAgent("MELI-JAVA-SDK-0.0.4").build(); + http = new AsyncHttpClient(cf); + } + + public Meli(Long clientId, String clientSecret) { + this.clientId = clientId; + this.clientSecret = clientSecret; + } + + public Meli(Long clientId, String clientSecret, String accessToken) { + this.accessToken = accessToken; + this.clientId = clientId; + this.clientSecret = clientSecret; + } + + public Meli(Long clientId, String clientSecret, String accessToken, String refreshToken) { + this.accessToken = accessToken; + this.clientId = clientId; + this.clientSecret = clientSecret; + this.refreshToken = refreshToken; + } + + public String getAccessToken() { + return this.accessToken; + } + + public String getRefreshToken() { + return this.refreshToken; + } + + /** + * news + **/ + public Long getExpiresIn() { + return this.expiresIn; + } + + public String getScope() { + return this.scope; + } + + public String getUserId() { + return this.userId; + } + + public String getTokenType() { + return this.tokenType; + } + + public Response get(String path) throws MeliException { + return get(path, new FluentStringsMap()); + } + + private BoundRequestBuilder prepareGet(String path, FluentStringsMap params) { + return http.prepareGet(apiUrl + path) + .addHeader("Accept", "application/json") + .setQueryParameters(params); + } + + + private BoundRequestBuilder prepareDelete(String path, + FluentStringsMap params) { + return http.prepareDelete(apiUrl + path) + .addHeader("Accept", "application/json") + .setQueryParameters(params); + } + + private BoundRequestBuilder preparePost(String path, + FluentStringsMap params, String body) { + return http.preparePost(apiUrl + path) + .addHeader("Accept", "application/json") + .setQueryParameters(params) + .setHeader("Content-Type", "application/json").setBody(body) + .setBodyEncoding("UTF-8"); + } + + private BoundRequestBuilder preparePut(String path, + FluentStringsMap params, String body) { + return http.preparePut(apiUrl + path) + .addHeader("Accept", "application/json") + .setQueryParameters(params) + .setHeader("Content-Type", "application/json").setBody(body) + .setBodyEncoding("UTF-8"); + } + + private BoundRequestBuilder preparePost(String path, FluentStringsMap params) { + return http.preparePost(apiUrl + path) + .addHeader("Accept", "application/json") + .setQueryParameters(params); + } + + + public Response get(String path, FluentStringsMap params) throws MeliException { + + BoundRequestBuilder r = prepareGet(path, params); + + Response response; + try { + response = r.execute().get(); + } catch (Exception e) { + throw new MeliException(e); } - public String getScope() { - return this.scope; - } - public String getUserId() { - return this.userId; + return response; + } + + public void refreshAccessToken() throws AuthorizationFailure { + FluentStringsMap params = new FluentStringsMap(); + params.add("grant_type", "refresh_token"); + params.add("client_id", String.valueOf(this.clientId)); + params.add("client_secret", this.clientSecret); + params.add("refresh_token", this.refreshToken); + try { + BoundRequestBuilder req = preparePost("/oauth/token", params); + parseToken(req); + } catch (AuthorizationFailure e1) { + System.out.println(e1.getMessage()); + } catch (Exception e) { + System.out.println(e.getMessage()); } - public String getTokenType() { - return this.tokenType; + } + + /** + * @param callback: The callback URL. Must be the applications redirect URI + * @param authUrl: The authorization URL. Get from Meli.AuthUrls + * @return the authorization URL + */ + public String getAuthUrl(String callback, AuthUrls authUrl) { + try { + return authUrl.getValue() + "/authorization?response_type=code&client_id=" + + clientId + + "&redirect_uri=" + + URLEncoder.encode(callback, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return authUrl + "/authorization?response_type=code&client_id=" + + clientId + "&redirect_uri=" + callback; } - - public Response get(String path) throws MeliException { - return get(path, new FluentStringsMap()); + } + + public void authorize(String code, String redirectUri) throws AuthorizationFailure { + FluentStringsMap params = new FluentStringsMap(); + + params.add("grant_type", "authorization_code"); + params.add("client_id", String.valueOf(clientId)); + params.add("client_secret", clientSecret); + params.add("code", code); + params.add("redirect_uri", redirectUri); + + BoundRequestBuilder r = preparePost("/oauth/token", params); + + parseToken(r); + } + + private void parseToken(BoundRequestBuilder r) throws AuthorizationFailure { + Response response = null; + String responseBody = ""; + try { + response = r.execute().get(); + responseBody = response.getResponseBody(); + } catch (InterruptedException e) { + throw new AuthorizationFailure(e); + } catch (ExecutionException e) { + throw new AuthorizationFailure(e); + } catch (IOException e) { + throw new AuthorizationFailure(e); } - private BoundRequestBuilder prepareGet(String path, FluentStringsMap params) { - return http.prepareGet(apiUrl + path) - .addHeader("Accept", "application/json") - .setQueryParameters(params); + JsonParser p = new JsonParser(); + JsonObject object; + + try { + object = p.parse(responseBody).getAsJsonObject(); + } catch (JsonSyntaxException e) { + throw new AuthorizationFailure(responseBody); } + if (response.getStatusCode() == 200) { - private BoundRequestBuilder prepareDelete(String path, - FluentStringsMap params) { - return http.prepareDelete(apiUrl + path) - .addHeader("Accept", "application/json") - .setQueryParameters(params); - } - - private BoundRequestBuilder preparePost(String path, - FluentStringsMap params, String body) { - return http.preparePost(apiUrl + path) - .addHeader("Accept", "application/json") - .setQueryParameters(params) - .setHeader("Content-Type", "application/json").setBody(body) - .setBodyEncoding("UTF-8"); - } - - private BoundRequestBuilder preparePut(String path, - FluentStringsMap params, String body) { - return http.preparePut(apiUrl + path) - .addHeader("Accept", "application/json") - .setQueryParameters(params) - .setHeader("Content-Type", "application/json").setBody(body) - .setBodyEncoding("UTF-8"); - } - - private BoundRequestBuilder preparePost(String path, FluentStringsMap params) { - return http.preparePost(apiUrl + path) - .addHeader("Accept", "application/json") - .setQueryParameters(params); - } - - - - public Response get(String path, FluentStringsMap params) throws MeliException { - - BoundRequestBuilder r = prepareGet(path, params); - - Response response; - try { - response = r.execute().get(); - } catch (Exception e) { - throw new MeliException(e); - } - - - - return response; - } - - public void refreshAccessToken() throws AuthorizationFailure { - FluentStringsMap params = new FluentStringsMap(); - params.add("grant_type", "refresh_token"); - params.add("client_id", String.valueOf(this.clientId)); - params.add("client_secret", this.clientSecret); - params.add("refresh_token", this.refreshToken); - try { - BoundRequestBuilder req = preparePost("/oauth/token", params); - parseToken(req); - } catch (AuthorizationFailure e1) { - System.out.println(e1.getMessage()); - }catch (Exception e){ - System.out.println(e.getMessage()); - } + this.accessToken = object.get("access_token").getAsString(); - } + JsonElement jsonElement = object.get("refresh_token"); + this.refreshToken = jsonElement != null ? object.get( + "refresh_token").getAsString() : null; + /** News **/ + JsonElement jsonElementExpires = object.get("expires_in"); + this.expiresIn = jsonElementExpires != null ? Long.parseLong(object.get( + "expires_in").getAsString()) : null; + + JsonElement jsonElementScope = object.get("scope"); + this.scope = jsonElementScope != null ? object.get( + "scope").getAsString() : null; + + JsonElement jsonElementUserID = object.get("user_id"); + this.userId = jsonElementUserID != null ? object.get( + "user_id").getAsString() : null; - /** - * - * @param callback: The callback URL. Must be the applications redirect URI - * @param authUrl: The authorization URL. Get from Meli.AuthUrls - * @return the authorization URL - */ - public String getAuthUrl(String callback, AuthUrls authUrl) { - try { - return authUrl.getValue() + "/authorization?response_type=code&client_id=" - + clientId - + "&redirect_uri=" - + URLEncoder.encode(callback, "UTF-8"); - } catch (UnsupportedEncodingException e) { - return authUrl+"/authorization?response_type=code&client_id=" - + clientId + "&redirect_uri=" + callback; - } + JsonElement jsonElementToken = object.get("token_type"); + this.tokenType = jsonElementToken != null ? object.get( + "token_type").getAsString() : null; + + } else { + throw new AuthorizationFailure(object.get("message").getAsString()); } - public void authorize(String code, String redirectUri) throws AuthorizationFailure { - FluentStringsMap params = new FluentStringsMap(); + } - params.add("grant_type", "authorization_code"); - params.add("client_id", String.valueOf(clientId)); - params.add("client_secret", clientSecret); - params.add("code", code); - params.add("redirect_uri", redirectUri); + private boolean hasRefreshToken() { + return this.refreshToken != null && !this.refreshToken.isEmpty(); + } - BoundRequestBuilder r = preparePost("/oauth/token", params); + public Response post(String path, FluentStringsMap params, String body) throws MeliException { - parseToken(r); - } + BoundRequestBuilder r = preparePost(path, params, body); - private void parseToken(BoundRequestBuilder r) throws AuthorizationFailure { - Response response = null; - String responseBody = ""; - try { - response = r.execute().get(); - responseBody = response.getResponseBody(); - } catch (InterruptedException e) { - throw new AuthorizationFailure(e); - } catch (ExecutionException e) { - throw new AuthorizationFailure(e); - } catch (IOException e) { - throw new AuthorizationFailure(e); - } - - JsonParser p = new JsonParser(); - JsonObject object; - - try { - object = p.parse(responseBody).getAsJsonObject(); - } catch (JsonSyntaxException e) { - throw new AuthorizationFailure(responseBody); - } - - if (response.getStatusCode() == 200) { - - this.accessToken = object.get("access_token").getAsString(); - - JsonElement jsonElement = object.get("refresh_token"); - this.refreshToken = jsonElement != null ? object.get( - "refresh_token").getAsString() : null; - /** News **/ - JsonElement jsonElementExpires = object.get("expires_in"); - this.expiresIn = jsonElementExpires != null ? Long.parseLong(object.get( - "expires_in").getAsString()): null; - - JsonElement jsonElementScope = object.get("scope"); - this.scope = jsonElementScope != null ? object.get( - "scope").getAsString() : null; - - JsonElement jsonElementUserID = object.get("user_id"); - this.userId = jsonElementUserID != null ? object.get( - "user_id").getAsString() : null; - - JsonElement jsonElementToken = object.get("token_type"); - this.tokenType = jsonElementToken != null ? object.get( - "token_type").getAsString() : null; - - } else { - throw new AuthorizationFailure(object.get("message").getAsString()); - } - - } - - private boolean hasRefreshToken() { - return this.refreshToken != null && !this.refreshToken.isEmpty(); + Response response; + try { + response = r.execute().get(); + } catch (Exception e) { + throw new MeliException(e); } - public Response post(String path, FluentStringsMap params, String body) throws MeliException { - - BoundRequestBuilder r = preparePost(path, params, body); - - Response response; - try { - response = r.execute().get(); - } catch (Exception e) { - throw new MeliException(e); - } - - - return response; - } - - public Response put(String path, FluentStringsMap params, String body) throws MeliException { - - BoundRequestBuilder r = preparePut(path, params, body); - - Response response; - try { - response = r.execute().get(); - } catch (Exception e) { - throw new MeliException(e); - } - - return response; - } - - public Response delete(String path, FluentStringsMap params) throws MeliException { - BoundRequestBuilder r = prepareDelete(path, params); - - Response response; - try { - response = r.execute().get(); - } catch (Exception e) { - throw new MeliException(e); - } - - return response; - } - - public BoundRequestBuilder head(String path) { - return null; + + return response; + } + + public Response put(String path, FluentStringsMap params, String body) throws MeliException { + + BoundRequestBuilder r = preparePut(path, params, body); + + Response response; + try { + response = r.execute().get(); + } catch (Exception e) { + throw new MeliException(e); } - public BoundRequestBuilder options(String path) { - return null; + return response; + } + + public Response delete(String path, FluentStringsMap params) throws MeliException { + BoundRequestBuilder r = prepareDelete(path, params); + + Response response; + try { + response = r.execute().get(); + } catch (Exception e) { + throw new MeliException(e); } + + return response; + } + + public BoundRequestBuilder head(String path) { + return null; + } + + public BoundRequestBuilder options(String path) { + return null; + } } diff --git a/src/main/java/com/mercadolibre/sdk/MeliException.java b/src/main/java/com/mercadolibre/sdk/MeliException.java index 9650965..4544ea3 100644 --- a/src/main/java/com/mercadolibre/sdk/MeliException.java +++ b/src/main/java/com/mercadolibre/sdk/MeliException.java @@ -1,8 +1,9 @@ package com.mercadolibre.sdk; public class MeliException extends Exception { + public MeliException(Throwable cause) { - super(cause); + super(cause); } private static final long serialVersionUID = 7263275678852231779L; diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index b8fe0dc..567064d 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -9,12 +9,13 @@ import com.ning.http.client.Response; public class MeliTest extends Assert { + @Test public void testGetAuthUrl() { - assertEquals( - "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com", - new Meli(123456l, "client secret") - .getAuthUrl("http://someurl.com", Meli.AuthUrls.MLA)); + assertEquals( + "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com", + new Meli(123456l, "client secret") + .getAuthUrl("http://someurl.com", Meli.AuthUrls.MLA)); } @Test(expected = AuthorizationFailure.class) @@ -22,126 +23,126 @@ public void testAuthorizationFailure() throws AuthorizationFailure { Meli.apiUrl = "https://api.mercadolibre.com"; - new Meli(123456l, "client secret").authorize("bad code", - "http://someurl.com"); + new Meli(123456l, "client secret").authorize("bad code", + "http://someurl.com"); } @Test public void testAuthorizationSuccess() throws AuthorizationFailure { - Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret"); - m.authorize("valid code with refresh token", "http://someurl.com"); + Meli.apiUrl = "https://api.mercadolibre.com"; + Meli m = new Meli(123456l, "client secret"); + m.authorize("valid code with refresh token", "http://someurl.com"); - assertEquals("valid token", m.getAccessToken()); - assertEquals("valid refresh token", m.getRefreshToken()); + assertEquals("valid token", m.getAccessToken()); + assertEquals("valid refresh token", m.getRefreshToken()); } @Test public void testGet() throws MeliException, IOException { - Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret", "valid token"); + Meli.apiUrl = "https://api.mercadolibre.com"; + Meli m = new Meli(123456l, "client secret", "valid token"); - Response response = m.get("/sites"); + Response response = m.get("/sites"); - assertEquals(200, response.getStatusCode()); - assertFalse(response.getResponseBody().isEmpty()); + assertEquals(200, response.getStatusCode()); + assertFalse(response.getResponseBody().isEmpty()); } @Test public void testGetWithRefreshToken() throws MeliException, IOException { - Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); + Meli.apiUrl = "https://api.mercadolibre.com"; + Meli m = new Meli(123456l, "client secret", "expired token", + "valid refresh token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/users/me", params); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response response = m.get("/users/me", params); - assertEquals(200, response.getStatusCode()); - assertFalse(response.getResponseBody().isEmpty()); + assertEquals(200, response.getStatusCode()); + assertFalse(response.getResponseBody().isEmpty()); } @Test public void testErrorHandling() throws IOException, MeliException { - Meli m = new Meli(123456l, "client secret", "invalid token"); + Meli m = new Meli(123456l, "client secret", "invalid token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/users/me", params); - assertEquals(403, response.getStatusCode()); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response response = m.get("/users/me", params); + assertEquals(403, response.getStatusCode()); } @Test public void testUserAgent() throws IOException, MeliException { - Meli m = new Meli(123456l, "client secret", "invalid token"); + Meli m = new Meli(123456l, "client secret", "invalid token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/echo/user_agent", params); - assertEquals(200, response.getStatusCode()); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response response = m.get("/echo/user_agent", params); + assertEquals(200, response.getStatusCode()); } public void testPost() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); + Meli m = new Meli(123456l, "client secret", "valid token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); - assertEquals(201, r.getStatusCode()); + assertEquals(201, r.getStatusCode()); } public void testPostWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); + Meli m = new Meli(123456l, "client secret", "expired token", + "valid refresh token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); - assertEquals(201, r.getStatusCode()); + assertEquals(201, r.getStatusCode()); } public void testPut() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); + Meli m = new Meli(123456l, "client secret", "valid token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); - assertEquals(200, r.getStatusCode()); + assertEquals(200, r.getStatusCode()); } public void testPutWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); + Meli m = new Meli(123456l, "client secret", "expired token", + "valid refresh token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); - assertEquals(200, r.getStatusCode()); + assertEquals(200, r.getStatusCode()); } public void testDelete() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); + Meli m = new Meli(123456l, "client secret", "valid token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.delete("/items/123", params); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.delete("/items/123", params); - assertEquals(200, r.getStatusCode()); + assertEquals(200, r.getStatusCode()); } public void testDeleteWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); + Meli m = new Meli(123456l, "client secret", "expired token", + "valid refresh token"); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.delete("/items/123", params); + FluentStringsMap params = new FluentStringsMap(); + params.add("access_token", m.getAccessToken()); + Response r = m.delete("/items/123", params); - assertEquals(200, r.getStatusCode()); + assertEquals(200, r.getStatusCode()); } } \ No newline at end of file From 5855754e4f5d907c87949a9e67727d9943b5e6a7 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 14:24:02 -0300 Subject: [PATCH 03/16] added mockito --- pom.xml | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index c1a0b1f..9e8a3a5 100644 --- a/pom.xml +++ b/pom.xml @@ -39,11 +39,6 @@ - - junit - junit - 4.10 - com.ning async-http-client @@ -54,5 +49,24 @@ gson 2.2.2 + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + commons-io + commons-io + 2.6 + test + + From 63ee2d7ae906daf6501bd18e9a3bf5724c2aea6c Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 14:24:24 -0300 Subject: [PATCH 04/16] added setter for http client --- src/main/java/com/mercadolibre/sdk/Meli.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/mercadolibre/sdk/Meli.java b/src/main/java/com/mercadolibre/sdk/Meli.java index fed83fb..5bbfcbd 100644 --- a/src/main/java/com/mercadolibre/sdk/Meli.java +++ b/src/main/java/com/mercadolibre/sdk/Meli.java @@ -117,6 +117,10 @@ public String getTokenType() { return this.tokenType; } + public void setHttp(AsyncHttpClient http) { + this.http = http; + } + public Response get(String path) throws MeliException { return get(path, new FluentStringsMap()); } From 30beeb333e1b13d1b267d7f2422452ffdd81aaf3 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 14:25:01 -0300 Subject: [PATCH 05/16] meli request mocked in test for authorization failure --- .../java/com/mercadolibre/sdk/MeliTest.java | 51 ++++++++++++++++--- .../authorization_bad_request.json | 6 +++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/api_responses/authorization_bad_request.json diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 567064d..9e8ad59 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -1,12 +1,22 @@ package com.mercadolibre.sdk; -import java.io.IOException; - +import com.ning.http.client.AsyncHttpClient; +import com.ning.http.client.FluentStringsMap; +import com.ning.http.client.ListenableFuture; +import com.ning.http.client.Response; +import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; -import com.ning.http.client.FluentStringsMap; -import com.ning.http.client.Response; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.concurrent.ExecutionException; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; public class MeliTest extends Assert { @@ -19,12 +29,13 @@ public void testGetAuthUrl() { } @Test(expected = AuthorizationFailure.class) - public void testAuthorizationFailure() throws AuthorizationFailure { - + public void testAuthorizationFailure() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_bad_request.json"); Meli.apiUrl = "https://api.mercadolibre.com"; + Meli meli = new Meli(123456l, "client secret"); + mockHttpRequest(meli, jsonResponse); - new Meli(123456l, "client secret").authorize("bad code", - "http://someurl.com"); + meli.authorize("bad code", "http://someurl.com"); } @Test @@ -145,4 +156,28 @@ public void testDeleteWithRefreshToken() throws MeliException { assertEquals(200, r.getStatusCode()); } + + private String getFileContent(String filePath) throws IOException { + InputStream inputStream = new FileInputStream(filePath); + return IOUtils.toString(inputStream, "UTF-8"); + } + + private void mockHttpRequest(Meli meli, String jsonResponse) throws IOException, ExecutionException, InterruptedException { + Response responseMock = mock(Response.class); + given(responseMock.getResponseBody()).willReturn(jsonResponse); + + ListenableFuture listenableFutureMock = mock(ListenableFuture.class); + given(listenableFutureMock.get()).willReturn(responseMock); + + AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); + given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); + + AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); + given(asyncHttpClientMock.preparePost(anyString())).willReturn(boundRequestBuilderMock); + + meli.setHttp(asyncHttpClientMock); + } + } \ No newline at end of file diff --git a/src/test/resources/api_responses/authorization_bad_request.json b/src/test/resources/api_responses/authorization_bad_request.json new file mode 100644 index 0000000..01c5abe --- /dev/null +++ b/src/test/resources/api_responses/authorization_bad_request.json @@ -0,0 +1,6 @@ +{ + "message": "invalid client_id or client_secret.", + "error": "invalid_client", + "status": 400, + "cause": [] +} \ No newline at end of file From 25bdb8f4a64a312276e43924676915da8e497885 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 14:40:30 -0300 Subject: [PATCH 06/16] meli request mocked in test for authorization successfull --- .../java/com/mercadolibre/sdk/MeliTest.java | 22 ++++++++++++------- .../api_responses/authorization_success.json | 7 ++++++ 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/api_responses/authorization_success.json diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 9e8ad59..54be9e5 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -2,6 +2,7 @@ import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.FluentStringsMap; +import com.ning.http.client.HttpResponseStatus; import com.ning.http.client.ListenableFuture; import com.ning.http.client.Response; import org.apache.commons.io.IOUtils; @@ -31,22 +32,26 @@ public void testGetAuthUrl() { @Test(expected = AuthorizationFailure.class) public void testAuthorizationFailure() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_bad_request.json"); + int statusCode = 400; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(123456l, "client secret"); - mockHttpRequest(meli, jsonResponse); + mockHttpRequest(meli, jsonResponse, statusCode); meli.authorize("bad code", "http://someurl.com"); } @Test - public void testAuthorizationSuccess() throws AuthorizationFailure { - + public void testAuthorizationSuccess() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_success.json"); + int statusCode = 200; Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret"); - m.authorize("valid code with refresh token", "http://someurl.com"); + Meli meli = new Meli(123456l, "client secret"); + mockHttpRequest(meli, jsonResponse, statusCode); + + meli.authorize("valid code with refresh token", "http://someurl.com"); - assertEquals("valid token", m.getAccessToken()); - assertEquals("valid refresh token", m.getRefreshToken()); + assertEquals("APP_USR-6092-3246532-cb45c82853f6e620bb0deda096b128d3-8035443", meli.getAccessToken()); + assertEquals("TG-5005b6b3e4b07e60756a3353", meli.getRefreshToken()); } @Test @@ -162,8 +167,9 @@ private String getFileContent(String filePath) throws IOException { return IOUtils.toString(inputStream, "UTF-8"); } - private void mockHttpRequest(Meli meli, String jsonResponse) throws IOException, ExecutionException, InterruptedException { + private void mockHttpRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { Response responseMock = mock(Response.class); + given(responseMock.getStatusCode()).willReturn(statusCode); given(responseMock.getResponseBody()).willReturn(jsonResponse); ListenableFuture listenableFutureMock = mock(ListenableFuture.class); diff --git a/src/test/resources/api_responses/authorization_success.json b/src/test/resources/api_responses/authorization_success.json new file mode 100644 index 0000000..6720277 --- /dev/null +++ b/src/test/resources/api_responses/authorization_success.json @@ -0,0 +1,7 @@ +{ + "access_token" : "APP_USR-6092-3246532-cb45c82853f6e620bb0deda096b128d3-8035443", + "token_type" : "bearer", + "expires_in" : 10800, + "refresh_token" : "TG-5005b6b3e4b07e60756a3353", + "scope" : "write read" +} \ No newline at end of file From d8813b526579e8c730c5dcc509e7bce716bda308 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 14:58:20 -0300 Subject: [PATCH 07/16] meli request mocked in test for get sites --- .../java/com/mercadolibre/sdk/MeliTest.java | 36 ++++++-- .../api_responses/get_sites_success.json | 82 +++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 src/test/resources/api_responses/get_sites_success.json diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 54be9e5..0a573c1 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -35,7 +35,7 @@ public void testAuthorizationFailure() throws AuthorizationFailure, IOException, int statusCode = 400; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(123456l, "client secret"); - mockHttpRequest(meli, jsonResponse, statusCode); + mockHttpPostRequest(meli, jsonResponse, statusCode); meli.authorize("bad code", "http://someurl.com"); } @@ -46,7 +46,7 @@ public void testAuthorizationSuccess() throws AuthorizationFailure, IOException, int statusCode = 200; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(123456l, "client secret"); - mockHttpRequest(meli, jsonResponse, statusCode); + mockHttpPostRequest(meli, jsonResponse, statusCode); meli.authorize("valid code with refresh token", "http://someurl.com"); @@ -55,14 +55,17 @@ public void testAuthorizationSuccess() throws AuthorizationFailure, IOException, } @Test - public void testGet() throws MeliException, IOException { + public void testGet() throws MeliException, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("src/test/resources/api_responses/get_sites_success.json"); Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret", "valid token"); + Meli meli = new Meli(123456l, "client secret", "valid token"); + int statusCode = 200; + mockHttpGetRequest(meli, jsonResponse, statusCode); - Response response = m.get("/sites"); + Response response = meli.get("/sites"); assertEquals(200, response.getStatusCode()); - assertFalse(response.getResponseBody().isEmpty()); + assertEquals(jsonResponse, response.getResponseBody()); } @Test @@ -167,7 +170,7 @@ private String getFileContent(String filePath) throws IOException { return IOUtils.toString(inputStream, "UTF-8"); } - private void mockHttpRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + private void mockHttpPostRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { Response responseMock = mock(Response.class); given(responseMock.getStatusCode()).willReturn(statusCode); given(responseMock.getResponseBody()).willReturn(jsonResponse); @@ -186,4 +189,23 @@ private void mockHttpRequest(Meli meli, String jsonResponse, int statusCode) thr meli.setHttp(asyncHttpClientMock); } + private void mockHttpGetRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + Response responseMock = mock(Response.class); + given(responseMock.getStatusCode()).willReturn(statusCode); + given(responseMock.getResponseBody()).willReturn(jsonResponse); + + ListenableFuture listenableFutureMock = mock(ListenableFuture.class); + given(listenableFutureMock.get()).willReturn(responseMock); + + AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); + given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); + + AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); + given(asyncHttpClientMock.prepareGet(anyString())).willReturn(boundRequestBuilderMock); + + meli.setHttp(asyncHttpClientMock); + } + } \ No newline at end of file diff --git a/src/test/resources/api_responses/get_sites_success.json b/src/test/resources/api_responses/get_sites_success.json new file mode 100644 index 0000000..358bd3b --- /dev/null +++ b/src/test/resources/api_responses/get_sites_success.json @@ -0,0 +1,82 @@ +[ + { + "id": "MLA", + "name": "Argentina" + }, + { + "id": "MLC", + "name": "Chile" + }, + { + "id": "MLU", + "name": "Uruguay" + }, + { + "id": "MPY", + "name": "Paraguay" + }, + { + "id": "MBO", + "name": "Bolivia" + }, + { + "id": "MHN", + "name": "Honduras" + }, + { + "id": "MCU", + "name": "Cuba" + }, + { + "id": "MGT", + "name": "Guatemala" + }, + { + "id": "MCO", + "name": "Colombia" + }, + { + "id": "MCR", + "name": "Costa Rica" + }, + { + "id": "MRD", + "name": "Dominicana" + }, + { + "id": "MSV", + "name": "El Salvador" + }, + { + "id": "MEC", + "name": "Ecuador" + }, + { + "id": "MPA", + "name": "Panamá" + }, + { + "id": "MLV", + "name": "Venezuela" + }, + { + "id": "MNI", + "name": "Nicaragua" + }, + { + "id": "MPE", + "name": "Perú" + }, + { + "id": "MPT", + "name": "Portugal" + }, + { + "id": "MLB", + "name": "Brasil" + }, + { + "id": "MLM", + "name": "Mexico" + } +] \ No newline at end of file From 4d0b381f3c426c41393353c1b3fd189e766ee112 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 15:09:40 -0300 Subject: [PATCH 08/16] refactor test names --- .../java/com/mercadolibre/sdk/MeliTest.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 0a573c1..97f40a6 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -22,26 +22,16 @@ public class MeliTest extends Assert { @Test - public void testGetAuthUrl() { - assertEquals( - "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com", - new Meli(123456l, "client secret") - .getAuthUrl("http://someurl.com", Meli.AuthUrls.MLA)); - } + public void getAuthUrl_returnsAuthUrl() { + Meli meli = new Meli(123456L, "client secret"); - @Test(expected = AuthorizationFailure.class) - public void testAuthorizationFailure() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { - String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_bad_request.json"); - int statusCode = 400; - Meli.apiUrl = "https://api.mercadolibre.com"; - Meli meli = new Meli(123456l, "client secret"); - mockHttpPostRequest(meli, jsonResponse, statusCode); + String authUrl = meli.getAuthUrl("http://someurl.com", Meli.AuthUrls.MLA); - meli.authorize("bad code", "http://someurl.com"); + assertEquals("https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com", authUrl); } @Test - public void testAuthorizationSuccess() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { + public void authorize_withValidCode_returnsAccessToken() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_success.json"); int statusCode = 200; Meli.apiUrl = "https://api.mercadolibre.com"; @@ -54,11 +44,22 @@ public void testAuthorizationSuccess() throws AuthorizationFailure, IOException, assertEquals("TG-5005b6b3e4b07e60756a3353", meli.getRefreshToken()); } + @Test(expected = AuthorizationFailure.class) + public void authorize_withInvalidCode_throwsAuthorizationFailureException() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_bad_request.json"); + int statusCode = 400; + Meli.apiUrl = "https://api.mercadolibre.com"; + Meli meli = new Meli(1234561L, "client secret"); + mockHttpPostRequest(meli, jsonResponse, statusCode); + + meli.authorize("bad code", "http://someurl.com"); + } + @Test - public void testGet() throws MeliException, IOException, ExecutionException, InterruptedException { + public void get_withExistingEndpoint_returnsCorrectResponse() throws MeliException, IOException, ExecutionException, InterruptedException { String jsonResponse = getFileContent("src/test/resources/api_responses/get_sites_success.json"); Meli.apiUrl = "https://api.mercadolibre.com"; - Meli meli = new Meli(123456l, "client secret", "valid token"); + Meli meli = new Meli(1234561L, "client secret", "valid token"); int statusCode = 200; mockHttpGetRequest(meli, jsonResponse, statusCode); @@ -71,12 +72,12 @@ public void testGet() throws MeliException, IOException, ExecutionException, Int @Test public void testGetWithRefreshToken() throws MeliException, IOException { Meli.apiUrl = "https://api.mercadolibre.com"; - Meli m = new Meli(123456l, "client secret", "expired token", + Meli meli = new Meli(123456l, "client secret", "expired token", "valid refresh token"); FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/users/me", params); + params.add("access_token", meli.getAccessToken()); + Response response = meli.get("/users/me", params); assertEquals(200, response.getStatusCode()); assertFalse(response.getResponseBody().isEmpty()); From 5a8f4c33ebc751fbb8011e337d24164ca3334899 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 15:19:21 -0300 Subject: [PATCH 09/16] deleted useless test --- src/test/java/com/mercadolibre/sdk/MeliTest.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 97f40a6..ea90898 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -69,20 +69,6 @@ public void get_withExistingEndpoint_returnsCorrectResponse() throws MeliExcepti assertEquals(jsonResponse, response.getResponseBody()); } - @Test - public void testGetWithRefreshToken() throws MeliException, IOException { - Meli.apiUrl = "https://api.mercadolibre.com"; - Meli meli = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); - - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", meli.getAccessToken()); - Response response = meli.get("/users/me", params); - - assertEquals(200, response.getStatusCode()); - assertFalse(response.getResponseBody().isEmpty()); - } - @Test public void testErrorHandling() throws IOException, MeliException { Meli m = new Meli(123456l, "client secret", "invalid token"); From ba6c54d5b329522ee8c41a2ba8cc9cea772c34c6 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 15:47:53 -0300 Subject: [PATCH 10/16] mocked post item request --- src/main/java/com/mercadolibre/sdk/Meli.java | 3 +- .../java/com/mercadolibre/sdk/MeliTest.java | 75 +++++----- .../api_responses/post_item_success.json | 139 ++++++++++++++++++ 3 files changed, 182 insertions(+), 35 deletions(-) create mode 100644 src/test/resources/api_responses/post_item_success.json diff --git a/src/main/java/com/mercadolibre/sdk/Meli.java b/src/main/java/com/mercadolibre/sdk/Meli.java index 5bbfcbd..9159874 100644 --- a/src/main/java/com/mercadolibre/sdk/Meli.java +++ b/src/main/java/com/mercadolibre/sdk/Meli.java @@ -144,7 +144,8 @@ private BoundRequestBuilder preparePost(String path, return http.preparePost(apiUrl + path) .addHeader("Accept", "application/json") .setQueryParameters(params) - .setHeader("Content-Type", "application/json").setBody(body) + .setHeader("Content-Type", "application/json") + .setBody(body) .setBodyEncoding("UTF-8"); } diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index ea90898..659ec16 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -2,7 +2,6 @@ import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.FluentStringsMap; -import com.ning.http.client.HttpResponseStatus; import com.ning.http.client.ListenableFuture; import com.ning.http.client.Response; import org.apache.commons.io.IOUtils; @@ -32,11 +31,11 @@ public void getAuthUrl_returnsAuthUrl() { @Test public void authorize_withValidCode_returnsAccessToken() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { - String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_success.json"); + String jsonResponse = getFileContent("authorization_success.json"); int statusCode = 200; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(123456l, "client secret"); - mockHttpPostRequest(meli, jsonResponse, statusCode); + mockHttpPostRequestWithoutBody(meli, jsonResponse, statusCode); meli.authorize("valid code with refresh token", "http://someurl.com"); @@ -46,18 +45,18 @@ public void authorize_withValidCode_returnsAccessToken() throws AuthorizationFai @Test(expected = AuthorizationFailure.class) public void authorize_withInvalidCode_throwsAuthorizationFailureException() throws AuthorizationFailure, IOException, ExecutionException, InterruptedException { - String jsonResponse = getFileContent("src/test/resources/api_responses/authorization_bad_request.json"); + String jsonResponse = getFileContent("authorization_bad_request.json"); int statusCode = 400; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(1234561L, "client secret"); - mockHttpPostRequest(meli, jsonResponse, statusCode); + mockHttpPostRequestWithoutBody(meli, jsonResponse, statusCode); meli.authorize("bad code", "http://someurl.com"); } @Test - public void get_withExistingEndpoint_returnsCorrectResponse() throws MeliException, IOException, ExecutionException, InterruptedException { - String jsonResponse = getFileContent("src/test/resources/api_responses/get_sites_success.json"); + public void get_withExistingEndpoint_returnsSuccessfulResponse() throws MeliException, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("get_sites_success.json"); Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(1234561L, "client secret", "valid token"); int statusCode = 200; @@ -70,33 +69,19 @@ public void get_withExistingEndpoint_returnsCorrectResponse() throws MeliExcepti } @Test - public void testErrorHandling() throws IOException, MeliException { - Meli m = new Meli(123456l, "client secret", "invalid token"); - - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/users/me", params); - assertEquals(403, response.getStatusCode()); - } - - @Test - public void testUserAgent() throws IOException, MeliException { - Meli m = new Meli(123456l, "client secret", "invalid token"); - + public void post_withExistingEndpoint_returnsSuccessfulResponse() throws MeliException, IOException, ExecutionException, InterruptedException { + String jsonResponse = getFileContent("post_item_success.json"); + Meli meli = new Meli(1234561L, "client secret", "valid token"); FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response response = m.get("/echo/user_agent", params); - assertEquals(200, response.getStatusCode()); - } + params.add("access_token", meli.getAccessToken()); + int statusCode = 201; + String body = "{\"foo\":\"bar\"}"; + mockHttpPostRequestWithBody(meli, jsonResponse, statusCode, body); - public void testPost() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); + Response response = meli.post("/items", params, body); - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); - - assertEquals(201, r.getStatusCode()); + assertEquals(201, response.getStatusCode()); + assertEquals(jsonResponse, response.getResponseBody()); } public void testPostWithRefreshToken() throws MeliException { @@ -152,12 +137,31 @@ public void testDeleteWithRefreshToken() throws MeliException { assertEquals(200, r.getStatusCode()); } - private String getFileContent(String filePath) throws IOException { - InputStream inputStream = new FileInputStream(filePath); + private String getFileContent(String filename) throws IOException { + InputStream inputStream = new FileInputStream("src/test/resources/api_responses/" + filename); return IOUtils.toString(inputStream, "UTF-8"); } - private void mockHttpPostRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + private void mockHttpPostRequestWithoutBody(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + Response responseMock = mock(Response.class); + given(responseMock.getStatusCode()).willReturn(statusCode); + given(responseMock.getResponseBody()).willReturn(jsonResponse); + + ListenableFuture listenableFutureMock = mock(ListenableFuture.class); + given(listenableFutureMock.get()).willReturn(responseMock); + + AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); + given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); + + AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); + given(asyncHttpClientMock.preparePost(anyString())).willReturn(boundRequestBuilderMock); + + meli.setHttp(asyncHttpClientMock); + } + + private void mockHttpPostRequestWithBody(Meli meli, String jsonResponse, int statusCode, String body) throws IOException, ExecutionException, InterruptedException { Response responseMock = mock(Response.class); given(responseMock.getStatusCode()).willReturn(statusCode); given(responseMock.getResponseBody()).willReturn(jsonResponse); @@ -168,6 +172,9 @@ private void mockHttpPostRequest(Meli meli, String jsonResponse, int statusCode) AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBody(body)).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBodyEncoding(anyString())).willReturn(boundRequestBuilderMock); given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); diff --git a/src/test/resources/api_responses/post_item_success.json b/src/test/resources/api_responses/post_item_success.json new file mode 100644 index 0000000..89e62ca --- /dev/null +++ b/src/test/resources/api_responses/post_item_success.json @@ -0,0 +1,139 @@ +{ + "id": "MLA600190449", + "site_id": "MLA", + "title": "Iphone 6 64gb Space Gray Liberado", + "subtitle": null, + "seller_id": 118617944, + "category_id": "MLA352543", + "official_store_id": null, + "price": 16550, + "base_price": 16550, + "original_price": null, + "currency_id": "ARS", + "initial_quantity": 2, + "available_quantity": 2, + "sold_quantity": 0, + "buying_mode": "buy_it_now", + "listing_type_id": "bronze", + "start_time": "2016-01-13T18:10:29.000Z", + "stop_time": "2016-03-13T18:10:29.000Z", + "condition": "new", + "permalink": "http://articulo.mercadolibre.com.ar/MLA-600190449-iphone-6-64gb-space-gray-liberado-_JM", + "thumbnail": "http://mla-s1-p.mlstatic.com/873411-MLA20547233702_012016-I.jpg", + "secure_thumbnail": "https://a248.e.akamai.net/mla-s1-p.mlstatic.com/873411-MLA20547233702_012016-I.jpg", + "pictures": [ + { + "id": "873411-MLA20547233702_012016", + "url": "http://mla-s1-p.mlstatic.com/873411-MLA20547233702_012016-O.jpg", + "secure_url": "https://a248.e.akamai.net/mla-s1-p.mlstatic.com/873411-MLA20547233702_012016-O.jpg", + "size": "225x225", + "max_size": "225x225", + "quality": "" + }, + { + "id": "234411-MLA20547233720_012016", + "url": "http://mla-s1-p.mlstatic.com/234411-MLA20547233720_012016-O.jpg", + "secure_url": "https://a248.e.akamai.net/mla-s1-p.mlstatic.com/234411-MLA20547233720_012016-O.jpg", + "size": "259x194", + "max_size": "259x194", + "quality": "" + }, + { + "id": "768311-MLA20547233735_012016", + "url": "http://mla-s1-p.mlstatic.com/768311-MLA20547233735_012016-O.jpg", + "secure_url": "https://a248.e.akamai.net/mla-s1-p.mlstatic.com/768311-MLA20547233735_012016-O.jpg", + "size": "300x168", + "max_size": "300x168", + "quality": "" + } + ], + "video_id": null, + "descriptions": [ + { + "id": "MLA600190449-1007729488" + } + ], + "accepts_mercadopago": true, + "non_mercado_pago_payment_methods": [ + ], + "shipping": { + "mode": "me2", + "local_pick_up": true, + "free_shipping": true, + "free_methods": [ + { + "id": 73328, + "rule": { + "free_mode": "country", + "value": null + } + } + ], + "dimensions": null, + "tags": [ + ] + }, + "international_delivery_mode": "none", + "seller_address": { + "id": 138834162, + "comment": "", + "address_line": "", + "zip_code": "", + "city": { + "id": "TUxBQ05FVXF1ZW4", + "name": "Neuquén" + }, + "state": { + "id": "AR-Q", + "name": "Neuquén" + }, + "country": { + "id": "AR", + "name": "Argentina" + }, + "latitude": -38.95628353, + "longitude": -68.12749595, + "search_location": { + "neighborhood": { + "id": "", + "name": "" + }, + "city": { + "id": "TUxBQ05FVXF1ZW4", + "name": "Neuquén" + }, + "state": { + "id": "TUxBUE5FVW4xMzMzNQ", + "name": "Neuquén" + } + } + }, + "seller_contact": null, + "location": { + }, + "geolocation": { + "latitude": -38.96055205, + "longitude": -68.12525497 + }, + "coverage_areas": [ + ], + "attributes": [ + ], + "listing_source": "", + "variations": [ + ], + "status": "active", + "sub_status": [ + ], + "tags": [ + ], + "warranty": null, + "catalog_product_id": null, + "parent_item_id": null, + "differential_pricing": null, + "deal_ids": [ + ], + "automatic_relist": false, + "date_created": "2016-01-13T18:10:29.000Z", + "last_updated": "2016-01-13T18:26:54.000Z" +} \ No newline at end of file From 246bc3facaa2daf188b1958c8d3f890e42ef2655 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:07:57 -0300 Subject: [PATCH 11/16] mocked put item request --- .../java/com/mercadolibre/sdk/MeliTest.java | 68 +++++++++---------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 659ec16..336d45c 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -84,36 +84,19 @@ public void post_withExistingEndpoint_returnsSuccessfulResponse() throws MeliExc assertEquals(jsonResponse, response.getResponseBody()); } - public void testPostWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); - - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.post("/items", params, "{\"foo\":\"bar\"}"); - - assertEquals(201, r.getStatusCode()); - } - - public void testPut() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); - - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); - - assertEquals(200, r.getStatusCode()); - } + @Test + public void put_withExistingItem_returnsSuccessfulResponse() throws MeliException, InterruptedException, ExecutionException, IOException { + int statusCode = 200; + Meli meli = new Meli(1234561L, "client secret", "valid token"); + String body = "{\"tags\":[\"immediate_payment\"]}"; - public void testPutWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); + mockHttpPutRequestWithBody(meli, "", statusCode, body); FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.put("/items/123", params, "{\"foo\":\"bar\"}"); + params.add("access_token", meli.getAccessToken()); + Response response = meli.put("/items/123", params, "{\"tags\":[\"immediate_payment\"]}"); - assertEquals(200, r.getStatusCode()); + assertEquals(200, response.getStatusCode()); } public void testDelete() throws MeliException { @@ -126,17 +109,6 @@ public void testDelete() throws MeliException { assertEquals(200, r.getStatusCode()); } - public void testDeleteWithRefreshToken() throws MeliException { - Meli m = new Meli(123456l, "client secret", "expired token", - "valid refresh token"); - - FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.delete("/items/123", params); - - assertEquals(200, r.getStatusCode()); - } - private String getFileContent(String filename) throws IOException { InputStream inputStream = new FileInputStream("src/test/resources/api_responses/" + filename); return IOUtils.toString(inputStream, "UTF-8"); @@ -202,4 +174,26 @@ private void mockHttpGetRequest(Meli meli, String jsonResponse, int statusCode) meli.setHttp(asyncHttpClientMock); } + private void mockHttpPutRequestWithBody(Meli meli, String jsonResponse, int statusCode, String body) throws IOException, ExecutionException, InterruptedException { + Response responseMock = mock(Response.class); + given(responseMock.getStatusCode()).willReturn(statusCode); + given(responseMock.getResponseBody()).willReturn(jsonResponse); + + ListenableFuture listenableFutureMock = mock(ListenableFuture.class); + given(listenableFutureMock.get()).willReturn(responseMock); + + AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); + given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBody(body)).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBodyEncoding(anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); + + AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); + given(asyncHttpClientMock.preparePut(anyString())).willReturn(boundRequestBuilderMock); + + meli.setHttp(asyncHttpClientMock); + } + } \ No newline at end of file From bd787c7f5fb7af347beebd9e83e92a575b64e05d Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:12:30 -0300 Subject: [PATCH 12/16] mocked delete item request --- .../java/com/mercadolibre/sdk/MeliTest.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index 336d45c..aecfff9 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -17,6 +17,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockingDetails; public class MeliTest extends Assert { @@ -89,7 +90,6 @@ public void put_withExistingItem_returnsSuccessfulResponse() throws MeliExceptio int statusCode = 200; Meli meli = new Meli(1234561L, "client secret", "valid token"); String body = "{\"tags\":[\"immediate_payment\"]}"; - mockHttpPutRequestWithBody(meli, "", statusCode, body); FluentStringsMap params = new FluentStringsMap(); @@ -99,14 +99,17 @@ public void put_withExistingItem_returnsSuccessfulResponse() throws MeliExceptio assertEquals(200, response.getStatusCode()); } - public void testDelete() throws MeliException { - Meli m = new Meli(123456l, "client secret", "valid token"); - + @Test + public void delete_WithExistingItem_returnsSuccessfulResponse() throws MeliException, InterruptedException, ExecutionException, IOException { + int statusCode = 200; + Meli meli = new Meli(1234561L, "client secret", "valid token"); + mockHttpDeleteRequest(meli, "", statusCode); FluentStringsMap params = new FluentStringsMap(); - params.add("access_token", m.getAccessToken()); - Response r = m.delete("/items/123", params); + params.add("access_token", meli.getAccessToken()); + + Response response = meli.delete("/items/123", params); - assertEquals(200, r.getStatusCode()); + assertEquals(200, response.getStatusCode()); } private String getFileContent(String filename) throws IOException { @@ -196,4 +199,23 @@ private void mockHttpPutRequestWithBody(Meli meli, String jsonResponse, int stat meli.setHttp(asyncHttpClientMock); } + private void mockHttpDeleteRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + Response responseMock = mock(Response.class); + given(responseMock.getStatusCode()).willReturn(statusCode); + given(responseMock.getResponseBody()).willReturn(jsonResponse); + + ListenableFuture listenableFutureMock = mock(ListenableFuture.class); + given(listenableFutureMock.get()).willReturn(responseMock); + + AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); + given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); + + AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); + given(asyncHttpClientMock.prepareDelete(anyString())).willReturn(boundRequestBuilderMock); + + meli.setHttp(asyncHttpClientMock); + } + } \ No newline at end of file From a3575a6017e154b305dc78bdc9a3c9ace8a7a946 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:30:49 -0300 Subject: [PATCH 13/16] refactor of the method that creates the mocks --- .../java/com/mercadolibre/sdk/MeliTest.java | 123 +++++------------- 1 file changed, 32 insertions(+), 91 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index aecfff9..f3b8d00 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -17,10 +17,13 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockingDetails; public class MeliTest extends Assert { + private enum HttpMethod { + GET, POST, PUT, DELETE + } + @Test public void getAuthUrl_returnsAuthUrl() { Meli meli = new Meli(123456L, "client secret"); @@ -35,8 +38,8 @@ public void authorize_withValidCode_returnsAccessToken() throws AuthorizationFai String jsonResponse = getFileContent("authorization_success.json"); int statusCode = 200; Meli.apiUrl = "https://api.mercadolibre.com"; - Meli meli = new Meli(123456l, "client secret"); - mockHttpPostRequestWithoutBody(meli, jsonResponse, statusCode); + Meli meli = new Meli(1234561L, "client secret"); + mockHttpRequest(meli, jsonResponse, statusCode, HttpMethod.POST, null); meli.authorize("valid code with refresh token", "http://someurl.com"); @@ -50,7 +53,7 @@ public void authorize_withInvalidCode_throwsAuthorizationFailureException() thro int statusCode = 400; Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(1234561L, "client secret"); - mockHttpPostRequestWithoutBody(meli, jsonResponse, statusCode); + mockHttpRequest(meli, jsonResponse, statusCode, HttpMethod.POST, null); meli.authorize("bad code", "http://someurl.com"); } @@ -61,7 +64,7 @@ public void get_withExistingEndpoint_returnsSuccessfulResponse() throws MeliExce Meli.apiUrl = "https://api.mercadolibre.com"; Meli meli = new Meli(1234561L, "client secret", "valid token"); int statusCode = 200; - mockHttpGetRequest(meli, jsonResponse, statusCode); + mockHttpRequest(meli, jsonResponse, statusCode, HttpMethod.GET, null); Response response = meli.get("/sites"); @@ -77,7 +80,7 @@ public void post_withExistingEndpoint_returnsSuccessfulResponse() throws MeliExc params.add("access_token", meli.getAccessToken()); int statusCode = 201; String body = "{\"foo\":\"bar\"}"; - mockHttpPostRequestWithBody(meli, jsonResponse, statusCode, body); + mockHttpRequest(meli, jsonResponse, statusCode, HttpMethod.POST, body); Response response = meli.post("/items", params, body); @@ -90,11 +93,11 @@ public void put_withExistingItem_returnsSuccessfulResponse() throws MeliExceptio int statusCode = 200; Meli meli = new Meli(1234561L, "client secret", "valid token"); String body = "{\"tags\":[\"immediate_payment\"]}"; - mockHttpPutRequestWithBody(meli, "", statusCode, body); + mockHttpRequest(meli, "", statusCode, HttpMethod.PUT, body); FluentStringsMap params = new FluentStringsMap(); params.add("access_token", meli.getAccessToken()); - Response response = meli.put("/items/123", params, "{\"tags\":[\"immediate_payment\"]}"); + Response response = meli.put("/items/123", params, "{\"tags\":[\"immediate_payment\"]}"); assertEquals(200, response.getStatusCode()); } @@ -103,9 +106,9 @@ public void put_withExistingItem_returnsSuccessfulResponse() throws MeliExceptio public void delete_WithExistingItem_returnsSuccessfulResponse() throws MeliException, InterruptedException, ExecutionException, IOException { int statusCode = 200; Meli meli = new Meli(1234561L, "client secret", "valid token"); - mockHttpDeleteRequest(meli, "", statusCode); FluentStringsMap params = new FluentStringsMap(); params.add("access_token", meli.getAccessToken()); + mockHttpRequest(meli, "", statusCode, HttpMethod.DELETE, null); Response response = meli.delete("/items/123", params); @@ -117,7 +120,7 @@ private String getFileContent(String filename) throws IOException { return IOUtils.toString(inputStream, "UTF-8"); } - private void mockHttpPostRequestWithoutBody(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { + private void mockHttpRequest(Meli meli, String jsonResponse, int statusCode, HttpMethod httpMethod, String body) throws IOException, ExecutionException, InterruptedException { Response responseMock = mock(Response.class); given(responseMock.getStatusCode()).willReturn(statusCode); given(responseMock.getResponseBody()).willReturn(jsonResponse); @@ -128,92 +131,30 @@ private void mockHttpPostRequestWithoutBody(Meli meli, String jsonResponse, int AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); - - AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); - given(asyncHttpClientMock.preparePost(anyString())).willReturn(boundRequestBuilderMock); - - meli.setHttp(asyncHttpClientMock); - } - private void mockHttpPostRequestWithBody(Meli meli, String jsonResponse, int statusCode, String body) throws IOException, ExecutionException, InterruptedException { - Response responseMock = mock(Response.class); - given(responseMock.getStatusCode()).willReturn(statusCode); - given(responseMock.getResponseBody()).willReturn(jsonResponse); + if (body != null) { + given(boundRequestBuilderMock.setHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBody(body)).willReturn(boundRequestBuilderMock); + given(boundRequestBuilderMock.setBodyEncoding(anyString())).willReturn(boundRequestBuilderMock); + } - ListenableFuture listenableFutureMock = mock(ListenableFuture.class); - given(listenableFutureMock.get()).willReturn(responseMock); - - AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); - given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setBody(body)).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setBodyEncoding(anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); - - AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); - given(asyncHttpClientMock.preparePost(anyString())).willReturn(boundRequestBuilderMock); - - meli.setHttp(asyncHttpClientMock); - } - - private void mockHttpGetRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { - Response responseMock = mock(Response.class); - given(responseMock.getStatusCode()).willReturn(statusCode); - given(responseMock.getResponseBody()).willReturn(jsonResponse); - - ListenableFuture listenableFutureMock = mock(ListenableFuture.class); - given(listenableFutureMock.get()).willReturn(responseMock); - - AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); - given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); - - AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); - given(asyncHttpClientMock.prepareGet(anyString())).willReturn(boundRequestBuilderMock); - - meli.setHttp(asyncHttpClientMock); - } - - private void mockHttpPutRequestWithBody(Meli meli, String jsonResponse, int statusCode, String body) throws IOException, ExecutionException, InterruptedException { - Response responseMock = mock(Response.class); - given(responseMock.getStatusCode()).willReturn(statusCode); - given(responseMock.getResponseBody()).willReturn(jsonResponse); - - ListenableFuture listenableFutureMock = mock(ListenableFuture.class); - given(listenableFutureMock.get()).willReturn(responseMock); - - AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); - given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setBody(body)).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setBodyEncoding(anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); - - AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); - given(asyncHttpClientMock.preparePut(anyString())).willReturn(boundRequestBuilderMock); - - meli.setHttp(asyncHttpClientMock); - } - - private void mockHttpDeleteRequest(Meli meli, String jsonResponse, int statusCode) throws IOException, ExecutionException, InterruptedException { - Response responseMock = mock(Response.class); - given(responseMock.getStatusCode()).willReturn(statusCode); - given(responseMock.getResponseBody()).willReturn(jsonResponse); - - ListenableFuture listenableFutureMock = mock(ListenableFuture.class); - given(listenableFutureMock.get()).willReturn(responseMock); - - AsyncHttpClient.BoundRequestBuilder boundRequestBuilderMock = mock(AsyncHttpClient.BoundRequestBuilder.class); - given(boundRequestBuilderMock.addHeader(anyString(), anyString())).willReturn(boundRequestBuilderMock); - given(boundRequestBuilderMock.setQueryParameters(any(FluentStringsMap.class))).willReturn(boundRequestBuilderMock); given(boundRequestBuilderMock.execute()).willReturn(listenableFutureMock); AsyncHttpClient asyncHttpClientMock = mock(AsyncHttpClient.class); - given(asyncHttpClientMock.prepareDelete(anyString())).willReturn(boundRequestBuilderMock); + switch (httpMethod) { + case GET: + given(asyncHttpClientMock.prepareGet(anyString())).willReturn(boundRequestBuilderMock); + break; + case POST: + given(asyncHttpClientMock.preparePost(anyString())).willReturn(boundRequestBuilderMock); + break; + case PUT: + given(asyncHttpClientMock.preparePut(anyString())).willReturn(boundRequestBuilderMock); + break; + case DELETE: + given(asyncHttpClientMock.prepareDelete(anyString())).willReturn(boundRequestBuilderMock); + break; + } meli.setHttp(asyncHttpClientMock); } From 05c8061525640c65c79093ed82fbb0420ae7a92f Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:31:54 -0300 Subject: [PATCH 14/16] asserts by static import --- src/test/java/com/mercadolibre/sdk/MeliTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/mercadolibre/sdk/MeliTest.java b/src/test/java/com/mercadolibre/sdk/MeliTest.java index f3b8d00..2f5ece4 100644 --- a/src/test/java/com/mercadolibre/sdk/MeliTest.java +++ b/src/test/java/com/mercadolibre/sdk/MeliTest.java @@ -5,7 +5,6 @@ import com.ning.http.client.ListenableFuture; import com.ning.http.client.Response; import org.apache.commons.io.IOUtils; -import org.junit.Assert; import org.junit.Test; import java.io.FileInputStream; @@ -13,12 +12,13 @@ import java.io.InputStream; import java.util.concurrent.ExecutionException; +import static org.junit.Assert.assertEquals; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; -public class MeliTest extends Assert { +public class MeliTest { private enum HttpMethod { GET, POST, PUT, DELETE From 70d5198f0e886bcc633e74f0df13eca667c6d9a1 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:35:35 -0300 Subject: [PATCH 15/16] pom updated --- pom.xml | 70 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index 9e8a3a5..707c45d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,42 @@ 4.0.0 + com.mercadolibre sdk - 0.0.3-SNAPSHOT + 0.0.4-SNAPSHOT + + + + com.ning + async-http-client + 1.7.4 + + + com.google.code.gson + gson + 2.8.2 + + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + commons-io + commons-io + 2.6 + test + + + repo @@ -20,13 +53,12 @@ org.apache.maven.plugins maven-compiler-plugin - 2.0.2 + 3.7.0 1.6 1.6 - org.apache.maven.plugins maven-surefire-plugin @@ -34,39 +66,7 @@ true - - - - com.ning - async-http-client - 1.7.4 - - - com.google.code.gson - gson - 2.2.2 - - - junit - junit - 4.12 - test - - - org.mockito - mockito-all - 1.10.19 - test - - - commons-io - commons-io - 2.6 - test - - - From b69d751fe2ef4a3c867e6d440fcb64766eb10ba6 Mon Sep 17 00:00:00 2001 From: cernadasjuan Date: Tue, 14 Nov 2017 16:45:29 -0300 Subject: [PATCH 16/16] added configuration for maven site and cobertura --- pom.xml | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 707c45d..17b6ea1 100644 --- a/pom.xml +++ b/pom.xml @@ -61,12 +61,47 @@ org.apache.maven.plugins - maven-surefire-plugin + maven-site-plugin + 3.6 + + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.9 - true + false + + + + + index + summary + + + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.5 + + + org.codehaus.mojo + cobertura-maven-plugin + 2.7 + + + html + xml + - +