diff --git a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManager.java b/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManager.java deleted file mode 100644 index 1985176c..00000000 --- a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManager.java +++ /dev/null @@ -1,216 +0,0 @@ -package it.fattureincloud.sdk.auth; - -import com.google.gson.Gson; -import io.vavr.control.Either; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URL; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.stream.Collectors; -import okhttp3.*; - -public class OAuth2AuthorizationCodeManager { - static final String DEFAULT_BASE_URI = "https://api-v2.fattureincloud.it"; - - private String clientId; - private String clientSecret; - private String redirectUri; - private Optional baseUri; - private OkHttpClient httpClient; - private final Gson gson; - - private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); - - public OAuth2AuthorizationCodeManager(String clientId, String clientSecret, String redirectUri) { - this(clientId, clientSecret, redirectUri, null, null); - } - - public OAuth2AuthorizationCodeManager( - String clientId, String clientSecret, String redirectUri, String baseUri) { - this(clientId, clientSecret, redirectUri, baseUri, null); - } - - public OAuth2AuthorizationCodeManager( - String clientId, String clientSecret, String redirectUri, OkHttpClient httpClient) { - this(clientId, clientSecret, redirectUri, null, httpClient); - } - - public OAuth2AuthorizationCodeManager( - String clientId, - String clientSecret, - String redirectUri, - String baseUri, - OkHttpClient httpClient) { - this.clientId = clientId; - this.clientSecret = clientSecret; - this.redirectUri = redirectUri; - this.baseUri = Optional.ofNullable(baseUri); - if (httpClient != null) { - this.httpClient = httpClient; - } else { - this.httpClient = new OkHttpClient(); - } - this.gson = new Gson(); - } - - public String getClientId() { - return clientId; - } - - public void setClientId(String clientId) { - this.clientId = clientId; - } - - public String getClientSecret() { - return clientSecret; - } - - public void setClientSecret(String clientSecret) { - this.clientSecret = clientSecret; - } - - public String getRedirectUri() { - return redirectUri; - } - - public void setRedirectUri(String redirectUri) { - this.redirectUri = redirectUri; - } - - public String getBaseUri() { - return baseUri.orElse(DEFAULT_BASE_URI); - } - - public void setBaseUri(String baseUri) { - this.baseUri = Optional.ofNullable(baseUri); - } - - public void setBaseUri(Optional baseUri) { - this.baseUri = baseUri; - } - - public OkHttpClient getHttpClient() { - return httpClient; - } - - public void setHttpClient(OkHttpClient httpClient) { - this.httpClient = httpClient; - } - - public String getAuthorizationUrl(Collection scopes, String state) { - String authUri = this.getCompleteUri("/oauth/authorize?"); - - String scopesString = getScopesString(scopes); - - HashMap map = new HashMap<>(); - map.put("response_type", "code"); - map.put("client_id", this.clientId); - map.put("redirect_uri", this.redirectUri); - map.put("scope", scopesString); - if (state != null) { - map.put("state", state); - } - - String qs = - map.entrySet().stream() - .map(e -> e.getKey() + "=" + encodeValue(e.getValue())) - .collect(Collectors.joining("&")); - - return authUri.concat(qs); - } - - public OAuth2AuthorizationCodeParams getParamsFromUrl(String url) throws Exception { - String query = new URL(url).getQuery(); - - String[] params = query.split("&"); - Map map = new HashMap<>(); - for (String param : params) { - String[] kv = param.split("="); - map.put(kv[0], decodeValue(kv[1])); - } - return new OAuth2AuthorizationCodeParams(map.get("state"), map.get("code")); - } - - public Either fetchToken( - String authCode) throws IOException { - String tokenUri = this.getCompleteUri("/oauth/token"); - - HashMap map = new HashMap<>(); - map.put("grant_type", "authorization_code"); - map.put("client_id", this.clientId); - map.put("client_secret", this.clientSecret); - map.put("redirect_uri", this.redirectUri); - map.put("code", authCode); - - String json = mapToJson(map); - - return this.post(tokenUri, json); - } - - public Either refreshToken( - String refreshToken) throws IOException { - String tokenUri = this.getCompleteUri("/oauth/token"); - - HashMap map = new HashMap<>(); - map.put("grant_type", "refresh_token"); - map.put("client_id", this.clientId); - map.put("client_secret", this.clientSecret); - map.put("refresh_token", refreshToken); - - String json = mapToJson(map); - - return this.post(tokenUri, json); - } - - private String getCompleteUri(String path) { - return this.getBaseUri().concat(path); - } - - private String mapToJson(Map inputMap) { - return gson.toJson(inputMap); - } - - private Either post( - String url, String json) throws IOException { - RequestBody body = RequestBody.create(json, JSON); - Request request = new Request.Builder().url(url).post(body).build(); - try (Response response = this.httpClient.newCall(request).execute()) { - String responseBody = Objects.requireNonNull(response.body()).string(); - int code = response.code(); - - if (code != 200) { - OAuth2AuthorizationCodeError err = - gson.fromJson(responseBody, OAuth2AuthorizationCodeError.class); - err.setCode(code); - return Either.left(err); - } else { - OAuth2AuthorizationCodeResponse oauthRes = - gson.fromJson(responseBody, OAuth2AuthorizationCodeResponse.class); - return Either.right(oauthRes); - } - } - } - - private String encodeValue(String value) { - try { - return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); - } catch (UnsupportedEncodingException e) { - return ""; - } - } - - private String decodeValue(String value) { - try { - return URLDecoder.decode(value, StandardCharsets.UTF_8.toString()); - } catch (UnsupportedEncodingException e) { - return ""; - } - } - - private String getScopesString(Collection scopes) { - return scopes.stream().map(Scope::getScope).collect(Collectors.joining(" ")); - } -} diff --git a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeError.java b/src/main/java/it/fattureincloud/sdk/auth/OAuth2Error.java similarity index 87% rename from src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeError.java rename to src/main/java/it/fattureincloud/sdk/auth/OAuth2Error.java index 02648930..4ce20c0b 100644 --- a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeError.java +++ b/src/main/java/it/fattureincloud/sdk/auth/OAuth2Error.java @@ -3,7 +3,7 @@ import com.google.gson.annotations.SerializedName; import java.util.Objects; -public class OAuth2AuthorizationCodeError { +public class OAuth2Error { @SerializedName(value = "error") private String error; @@ -13,7 +13,7 @@ public class OAuth2AuthorizationCodeError { @SerializedName(value = "code") private Integer code; - public OAuth2AuthorizationCodeError(String error, String errorDescription, Integer code) { + public OAuth2Error(String error, String errorDescription, Integer code) { this.error = error; this.errorDescription = errorDescription; this.code = code; @@ -47,7 +47,7 @@ public void setCode(Integer code) { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - OAuth2AuthorizationCodeError that = (OAuth2AuthorizationCodeError) o; + OAuth2Error that = (OAuth2Error) o; return Objects.equals(error, that.error) && Objects.equals(errorDescription, that.errorDescription) && Objects.equals(code, that.code); diff --git a/src/main/java/it/fattureincloud/sdk/auth/OAuth2Manager.java b/src/main/java/it/fattureincloud/sdk/auth/OAuth2Manager.java new file mode 100644 index 00000000..083b157d --- /dev/null +++ b/src/main/java/it/fattureincloud/sdk/auth/OAuth2Manager.java @@ -0,0 +1,136 @@ +package it.fattureincloud.sdk.auth; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import io.vavr.control.Either; +import okhttp3.*; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.stream.Collectors; + +public abstract class OAuth2Manager { + static final String DEFAULT_BASE_URI = "https://api-v2.fattureincloud.it"; + + protected String clientId; + protected Optional baseUri; + protected OkHttpClient httpClient; + protected Gson gson; + + protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + + public OAuth2Manager(String clientId) { + this(clientId, null, null); + } + + public OAuth2Manager( + String clientId, String baseUri) { + this(clientId, baseUri, null); + } + + public OAuth2Manager( + String clientId, OkHttpClient httpClient) { + this(clientId, null, httpClient); + } + + public OAuth2Manager( + String clientId, + String baseUri, + OkHttpClient httpClient) { + this.clientId = clientId; + this.baseUri = Optional.ofNullable(baseUri); + if (httpClient != null) { + this.httpClient = httpClient; + } else { + this.httpClient = new OkHttpClient(); + } + this.gson = new Gson(); + } + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getBaseUri() { + return baseUri.orElse(DEFAULT_BASE_URI); + } + + public void setBaseUri(String baseUri) { + this.baseUri = Optional.ofNullable(baseUri); + } + + public void setBaseUri(Optional baseUri) { + this.baseUri = baseUri; + } + + public OkHttpClient getHttpClient() { + return httpClient; + } + + public void setHttpClient(OkHttpClient httpClient) { + this.httpClient = httpClient; + } + + protected abstract Either fetchToken( + String code) throws IOException; + + protected abstract Either refreshToken( + String refreshToken) throws IOException; + + protected String getCompleteUri(String path) { + return this.getBaseUri().concat(path); + } + + protected String mapToJson(Map inputMap) { + return gson.toJson(inputMap); + } + + protected Either post( + String url, String json, Class typeOfT) throws IOException { + RequestBody body = RequestBody.create(json, JSON); + Request request = new Request.Builder().url(url).post(body).build(); + try (Response response = this.httpClient.newCall(request).execute()) { + String responseBody = Objects.requireNonNull(response.body()).string(); + int code = response.code(); + + if (code != 200) { + OAuth2Error err = + gson.fromJson(responseBody, OAuth2Error.class); + err.setCode(code); + return Either.left(err); + } else { + T res = gson.fromJson(responseBody, typeOfT); + return Either.right(res); + } + } + } + + protected String encodeValue(String value) { + try { + return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); + } catch (UnsupportedEncodingException e) { + return ""; + } + } + + protected String decodeValue(String value) { + try { + return URLDecoder.decode(value, StandardCharsets.UTF_8.toString()); + } catch (UnsupportedEncodingException e) { + return ""; + } + } + + protected String getScopesString(Collection scopes) { + return scopes.stream().map(Scope::getScope).collect(Collectors.joining(" ")); + } +} diff --git a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponse.java b/src/main/java/it/fattureincloud/sdk/auth/OAuth2TokenResponse.java similarity index 92% rename from src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponse.java rename to src/main/java/it/fattureincloud/sdk/auth/OAuth2TokenResponse.java index 8cb7ed63..f2af92ce 100644 --- a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponse.java +++ b/src/main/java/it/fattureincloud/sdk/auth/OAuth2TokenResponse.java @@ -3,7 +3,7 @@ import com.google.gson.annotations.SerializedName; import java.util.Objects; -public class OAuth2AuthorizationCodeResponse { +public class OAuth2TokenResponse { @SerializedName(value = "token_type") private String tokenType; @@ -16,7 +16,7 @@ public class OAuth2AuthorizationCodeResponse { @SerializedName(value = "expires_in") private int expiresIn; - public OAuth2AuthorizationCodeResponse( + public OAuth2TokenResponse( String tokenType, String accessToken, String refreshToken, int expiresIn) { this.tokenType = tokenType; this.accessToken = accessToken; @@ -60,7 +60,7 @@ public void setExpiresIn(int expiresIn) { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - OAuth2AuthorizationCodeResponse that = (OAuth2AuthorizationCodeResponse) o; + OAuth2TokenResponse that = (OAuth2TokenResponse) o; return expiresIn == that.expiresIn && Objects.equals(tokenType, that.tokenType) && Objects.equals(accessToken, that.accessToken) diff --git a/src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManager.java b/src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManager.java new file mode 100644 index 00000000..5f83b857 --- /dev/null +++ b/src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManager.java @@ -0,0 +1,124 @@ +package it.fattureincloud.sdk.auth.authorization; + +import io.vavr.control.Either; +import java.io.IOException; +import java.net.URL; +import java.util.*; +import java.util.stream.Collectors; + +import it.fattureincloud.sdk.auth.OAuth2Error; +import it.fattureincloud.sdk.auth.OAuth2Manager; +import it.fattureincloud.sdk.auth.OAuth2TokenResponse; +import it.fattureincloud.sdk.auth.Scope; +import okhttp3.*; + +public class OAuth2AuthorizationCodeManager extends OAuth2Manager { + private String clientSecret; + private String redirectUri; + + public OAuth2AuthorizationCodeManager(String clientId, String clientSecret, String redirectUri) { + this(clientId, clientSecret, redirectUri, null, null); + } + + public OAuth2AuthorizationCodeManager( + String clientId, String clientSecret, String redirectUri, String baseUri) { + this(clientId, clientSecret, redirectUri, baseUri, null); + } + + public OAuth2AuthorizationCodeManager( + String clientId, String clientSecret, String redirectUri, OkHttpClient httpClient) { + this(clientId, clientSecret, redirectUri, null, httpClient); + } + + public OAuth2AuthorizationCodeManager( + String clientId, + String clientSecret, + String redirectUri, + String baseUri, + OkHttpClient httpClient) { + super(clientId, baseUri, httpClient); + this.clientSecret = clientSecret; + this.redirectUri = redirectUri; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public String getRedirectUri() { + return redirectUri; + } + + public void setRedirectUri(String redirectUri) { + this.redirectUri = redirectUri; + } + + public String getAuthorizationUrl(Collection scopes, String state) { + String authUri = this.getCompleteUri("/oauth/authorize?"); + + String scopesString = getScopesString(scopes); + + HashMap map = new HashMap<>(); + map.put("response_type", "code"); + map.put("client_id", this.clientId); + map.put("redirect_uri", this.redirectUri); + map.put("scope", scopesString); + if (state != null) { + map.put("state", state); + } + + String qs = + map.entrySet().stream() + .map(e -> e.getKey() + "=" + encodeValue(e.getValue())) + .collect(Collectors.joining("&")); + + return authUri.concat(qs); + } + + public OAuth2AuthorizationCodeParams getParamsFromUrl(String url) throws Exception { + String query = new URL(url).getQuery(); + + String[] params = query.split("&"); + Map map = new HashMap<>(); + for (String param : params) { + String[] kv = param.split("="); + map.put(kv[0], decodeValue(kv[1])); + } + return new OAuth2AuthorizationCodeParams(map.get("state"), map.get("code")); + } + + public Either fetchToken( + String code) throws IOException { + String tokenUri = this.getCompleteUri("/oauth/token"); + + HashMap map = new HashMap<>(); + map.put("grant_type", "authorization_code"); + map.put("client_id", this.clientId); + map.put("client_secret", this.clientSecret); + map.put("redirect_uri", this.redirectUri); + map.put("code", code); + + String json = mapToJson(map); + + return this.post(tokenUri, json, OAuth2TokenResponse.class); + } + + public Either refreshToken( + String refreshToken) throws IOException { + String tokenUri = this.getCompleteUri("/oauth/token"); + + HashMap map = new HashMap<>(); + map.put("grant_type", "refresh_token"); + map.put("client_id", this.clientId); + map.put("client_secret", this.clientSecret); + map.put("refresh_token", refreshToken); + + String json = mapToJson(map); + + return this.post(tokenUri, json, OAuth2TokenResponse.class); + } +} diff --git a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParams.java b/src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParams.java similarity index 96% rename from src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParams.java rename to src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParams.java index 6138cf3b..35b0ceca 100644 --- a/src/main/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParams.java +++ b/src/main/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParams.java @@ -1,4 +1,4 @@ -package it.fattureincloud.sdk.auth; +package it.fattureincloud.sdk.auth.authorization; import com.google.gson.annotations.SerializedName; import java.util.Objects; diff --git a/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManager.java b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManager.java new file mode 100644 index 00000000..53663038 --- /dev/null +++ b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManager.java @@ -0,0 +1,83 @@ +package it.fattureincloud.sdk.auth.device; + +import io.vavr.control.Either; +import it.fattureincloud.sdk.auth.OAuth2Error; +import it.fattureincloud.sdk.auth.OAuth2Manager; +import it.fattureincloud.sdk.auth.OAuth2TokenResponse; +import it.fattureincloud.sdk.auth.Scope; +import okhttp3.OkHttpClient; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; + +public class OAuth2DeviceCodeManager extends OAuth2Manager { + + public OAuth2DeviceCodeManager(String clientId) { + this(clientId, null, null); + } + + public OAuth2DeviceCodeManager( + String clientId, String baseUri) { + this(clientId, baseUri, null); + } + + public OAuth2DeviceCodeManager( + String clientId, OkHttpClient httpClient) { + this(clientId, null, httpClient); + } + + public OAuth2DeviceCodeManager( + String clientId, + String baseUri, + OkHttpClient httpClient) { + super(clientId, baseUri, httpClient); + } + + public Either getDeviceCode( + Collection scopes) throws IOException { + String scopesString = getScopesString(scopes); + + String tokenUri = this.getCompleteUri("/oauth/device"); + + HashMap map = new HashMap<>(); + map.put("client_id", this.clientId); + map.put("scope", scopesString); + + String json = mapToJson(map); + + Either response = this.post(tokenUri, json, OAuth2DeviceCodeResponseData.class); + if(response.isRight()) + return Either.right(response.get().getData()); + else + return Either.left(response.getLeft()); + } + + public Either fetchToken( + String code) throws IOException { + String tokenUri = this.getCompleteUri("/oauth/token"); + + HashMap map = new HashMap<>(); + map.put("grant_type", "urn:ietf:params:oauth:grant-type:device_code"); + map.put("client_id", this.clientId); + map.put("device_code", code); + + String json = mapToJson(map); + + return this.post(tokenUri, json, OAuth2TokenResponse.class); + } + + public Either refreshToken( + String refreshToken) throws IOException { + String tokenUri = this.getCompleteUri("/oauth/token"); + + HashMap map = new HashMap<>(); + map.put("grant_type", "refresh_token"); + map.put("client_id", this.clientId); + map.put("refresh_token", refreshToken); + + String json = mapToJson(map); + + return this.post(tokenUri, json, OAuth2TokenResponse.class); + } +} diff --git a/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponse.java b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponse.java new file mode 100644 index 00000000..be41a4d8 --- /dev/null +++ b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponse.java @@ -0,0 +1,108 @@ +package it.fattureincloud.sdk.auth.device; + +import com.google.gson.annotations.SerializedName; + +import java.util.Map; +import java.util.Objects; + +public class OAuth2DeviceCodeResponse { + @SerializedName(value = "device_code") + private String deviceCode; + + @SerializedName(value = "user_code") + private String userCode; + + @SerializedName(value = "scope") + private Map scope; + + @SerializedName(value = "verification_uri") + private String verificationUri; + + @SerializedName(value = "interval") + private int interval; + + @SerializedName(value = "expires_in") + private int expiresIn; + + public OAuth2DeviceCodeResponse(String deviceCode, String userCode, Map scope, String verificationUri, int interval, int expiresIn) { + this.deviceCode = deviceCode; + this.userCode = userCode; + this.scope = scope; + this.verificationUri = verificationUri; + this.interval = interval; + this.expiresIn = expiresIn; + } + + public String getDeviceCode() { + return deviceCode; + } + + public void setDeviceCode(String deviceCode) { + this.deviceCode = deviceCode; + } + + public String getUserCode() { + return userCode; + } + + public void setUserCode(String userCode) { + this.userCode = userCode; + } + + public Map getScope() { + return scope; + } + + public void setScope(Map scope) { + this.scope = scope; + } + + public String getVerificationUri() { + return verificationUri; + } + + public void setVerificationUri(String verificationUri) { + this.verificationUri = verificationUri; + } + + public int getInterval() { + return interval; + } + + public void setInterval(int interval) { + this.interval = interval; + } + + public int getExpiresIn() { + return expiresIn; + } + + public void setExpiresIn(int expiresIn) { + this.expiresIn = expiresIn; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + OAuth2DeviceCodeResponse that = (OAuth2DeviceCodeResponse) o; + return interval == that.interval && expiresIn == that.expiresIn && Objects.equals(deviceCode, that.deviceCode) && Objects.equals(userCode, that.userCode) && Objects.equals(scope, that.scope) && Objects.equals(verificationUri, that.verificationUri); + } + + @Override + public int hashCode() { + return Objects.hash(deviceCode, userCode, scope, verificationUri, interval, expiresIn); + } + + @Override + public String toString() { + return "OAuth2DeviceCodeResponse{" + + "deviceCode='" + deviceCode + '\'' + + ", userCode='" + userCode + '\'' + + ", scope=" + scope + + ", verificationUri='" + verificationUri + '\'' + + ", interval=" + interval + + ", expiresIn=" + expiresIn + + '}'; + } +} diff --git a/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseData.java b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseData.java new file mode 100644 index 00000000..6385ad75 --- /dev/null +++ b/src/main/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseData.java @@ -0,0 +1,16 @@ +package it.fattureincloud.sdk.auth.device; + +import com.google.gson.annotations.SerializedName; + +public class OAuth2DeviceCodeResponseData { + @SerializedName(value = "data") + private OAuth2DeviceCodeResponse data; + + public OAuth2DeviceCodeResponseData(OAuth2DeviceCodeResponse data) { + this.data = data; + } + + public OAuth2DeviceCodeResponse getData() { + return data; + } +} diff --git a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeErrorTest.java b/src/test/java/it/fattureincloud/sdk/auth/OAuth2ErrorTest.java similarity index 72% rename from src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeErrorTest.java rename to src/test/java/it/fattureincloud/sdk/auth/OAuth2ErrorTest.java index 7fe63d17..44f0e458 100644 --- a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeErrorTest.java +++ b/src/test/java/it/fattureincloud/sdk/auth/OAuth2ErrorTest.java @@ -9,20 +9,20 @@ import org.junit.jupiter.api.Test; /** Tests for OAuth2AuthorizationCodeError */ -public class OAuth2AuthorizationCodeErrorTest { +public class OAuth2ErrorTest { - private OAuth2AuthorizationCodeError err; + private OAuth2Error err; @BeforeEach public void init() { - err = new OAuth2AuthorizationCodeError("I am a teapot", "I am a teapot", 418); + err = new OAuth2Error("I am a teapot", "I am a teapot", 418); } /** Model tests for OAuth2AuthorizationCodeError */ @Test public void testOAuth2AuthorizationCodeError() { - OAuth2AuthorizationCodeError err = - new OAuth2AuthorizationCodeError("something_bad_happened", "it is you, not me", 401); + OAuth2Error err = + new OAuth2Error("something_bad_happened", "it is you, not me", 401); JSON jsonManager = new JSON(); Gson gson = jsonManager.getGson(); @@ -31,15 +31,15 @@ public void testOAuth2AuthorizationCodeError() { "{\"error\":\"something_bad_happened\",\"error_description\":\"it is you, not" + " me\",\"code\":401}"; assertEquals(str, json); - OAuth2AuthorizationCodeError generated = gson.fromJson(str, OAuth2AuthorizationCodeError.class); + OAuth2Error generated = gson.fromJson(str, OAuth2Error.class); assertEquals(err, generated); - OAuth2AuthorizationCodeError err2 = - new OAuth2AuthorizationCodeError("something_bad_happened", "it is you, not me", null); + OAuth2Error err2 = + new OAuth2Error("something_bad_happened", "it is you, not me", null); String str2 = "{\"error\":\"something_bad_happened\",\"error_description\":\"it is you, not me\"}"; - OAuth2AuthorizationCodeError generated2 = - gson.fromJson(str2, OAuth2AuthorizationCodeError.class); + OAuth2Error generated2 = + gson.fromJson(str2, OAuth2Error.class); assertEquals(err2, generated2); Object o = err; diff --git a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponseTest.java b/src/test/java/it/fattureincloud/sdk/auth/OAuth2TokenResponseTest.java similarity index 78% rename from src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponseTest.java rename to src/test/java/it/fattureincloud/sdk/auth/OAuth2TokenResponseTest.java index 98729db6..b04b621d 100644 --- a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeResponseTest.java +++ b/src/test/java/it/fattureincloud/sdk/auth/OAuth2TokenResponseTest.java @@ -4,27 +4,25 @@ import com.google.gson.Gson; import it.fattureincloud.sdk.JSON; -import it.fattureincloud.sdk.model.*; -import okhttp3.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** Tests for OAuth2AuthorizationCodeResponse */ -public class OAuth2AuthorizationCodeResponseTest { +public class OAuth2TokenResponseTest { - private OAuth2AuthorizationCodeResponse response; + private OAuth2TokenResponse response; @BeforeEach public void init() { response = - new OAuth2AuthorizationCodeResponse("bearer", "a/ACCESS_TOKEN", "r/REFRESH_TOKEN", 86400); + new OAuth2TokenResponse("bearer", "a/ACCESS_TOKEN", "r/REFRESH_TOKEN", 86400); } /** Model tests for OAuth2AuthorizationCodeResponse */ @Test public void testOAuth2AuthorizationCodeResponse() { - OAuth2AuthorizationCodeResponse response = - new OAuth2AuthorizationCodeResponse("bearer", "a/ACCESS_TOKEN", "r/REFRESH_TOKEN", 86400); + OAuth2TokenResponse response = + new OAuth2TokenResponse("bearer", "a/ACCESS_TOKEN", "r/REFRESH_TOKEN", 86400); JSON jsonManager = new JSON(); Gson gson = jsonManager.getGson(); @@ -32,8 +30,8 @@ public void testOAuth2AuthorizationCodeResponse() { String str = "{\"token_type\":\"bearer\",\"access_token\":\"a/ACCESS_TOKEN\",\"refresh_token\":\"r/REFRESH_TOKEN\",\"expires_in\":86400}"; assertEquals(str, json); - OAuth2AuthorizationCodeResponse generated = - gson.fromJson(str, OAuth2AuthorizationCodeResponse.class); + OAuth2TokenResponse generated = + gson.fromJson(str, OAuth2TokenResponse.class); assertEquals(response, generated); Object o = response; diff --git a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManagerTest.java b/src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManagerTest.java similarity index 91% rename from src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManagerTest.java rename to src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManagerTest.java index 22359b44..0c9a8e0c 100644 --- a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeManagerTest.java +++ b/src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeManagerTest.java @@ -1,4 +1,4 @@ -package it.fattureincloud.sdk.auth; +package it.fattureincloud.sdk.auth.authorization; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -9,6 +9,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Optional; + +import it.fattureincloud.sdk.auth.OAuth2Error; +import it.fattureincloud.sdk.auth.OAuth2TokenResponse; +import it.fattureincloud.sdk.auth.Scope; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeManager; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeParams; import okhttp3.*; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; @@ -151,11 +157,11 @@ public void fetchTokenTest() throws Exception { manager.setHttpClient(okHttpClient); - Either res = + Either res = manager.fetchToken("c/GIMME_A_TOKEN"); assert (res.isRight()); - OAuth2AuthorizationCodeResponse exp = - new OAuth2AuthorizationCodeResponse( + OAuth2TokenResponse exp = + new OAuth2TokenResponse( "bearer", "a/THIS_IS_A_TOKEN", "r/RINFRESCA_IL_TOKEN", 86400); assertEquals(exp, res.get()); Mockito.verify(mockCall, Mockito.only()).execute(); @@ -185,11 +191,11 @@ public void fetchTokenErrorTest() throws Exception { manager.setHttpClient(okHttpClient); - Either res = + Either res = manager.fetchToken("c/GIMME_GIMME_GIMME_A_TOKEN"); assert (res.isLeft()); - OAuth2AuthorizationCodeError exp = - new OAuth2AuthorizationCodeError("wtf", "ma perché è gialla?", 401); + OAuth2Error exp = + new OAuth2Error("wtf", "ma perché è gialla?", 401); assertEquals(exp, res.getLeft()); Mockito.verify(mockCall, Mockito.only()).execute(); } @@ -211,11 +217,11 @@ public void fetchTokenTestWithMockServer() throws Exception { assertEquals(0, mockWebServer.getRequestCount()); - Either res = + Either res = manager.fetchToken("c/GIMME_A_TOKEN"); assert (res.isRight()); - OAuth2AuthorizationCodeResponse exp = - new OAuth2AuthorizationCodeResponse( + OAuth2TokenResponse exp = + new OAuth2TokenResponse( "bearer", "a/THIS_IS_A_TOKEN", "r/RINFRESCA_IL_TOKEN", 86400); assertEquals(exp, res.get()); assertEquals(1, mockWebServer.getRequestCount()); @@ -254,11 +260,11 @@ public void refreshTokenTest() throws Exception { manager.setHttpClient(okHttpClient); - Either res = + Either res = manager.refreshToken("r/REFRESH_ME"); assert (res.isRight()); - OAuth2AuthorizationCodeResponse exp = - new OAuth2AuthorizationCodeResponse("bearer", "a/THIS_IS_A_NEW_TOKEN", "r/SO_FRESH", 86400); + OAuth2TokenResponse exp = + new OAuth2TokenResponse("bearer", "a/THIS_IS_A_NEW_TOKEN", "r/SO_FRESH", 86400); assertEquals(exp, res.get()); Mockito.verify(mockCall, Mockito.only()).execute(); } @@ -287,11 +293,11 @@ public void refreshTokenErrorTest() throws Exception { manager.setHttpClient(okHttpClient); - Either res = + Either res = manager.refreshToken("r/WHY_IS_IT_SO_HOT_IN_HERE"); assert (res.isLeft()); - OAuth2AuthorizationCodeError exp = - new OAuth2AuthorizationCodeError("so_hot", "I_AM_MELTING", 418); + OAuth2Error exp = + new OAuth2Error("so_hot", "I_AM_MELTING", 418); assertEquals(exp, res.getLeft()); Mockito.verify(mockCall, Mockito.only()).execute(); } @@ -313,11 +319,11 @@ public void refreshTokenTestWithMockServer() throws Exception { assertEquals(0, mockWebServer.getRequestCount()); - Either res = + Either res = manager.refreshToken("r/REFRESH_ME"); assert (res.isRight()); - OAuth2AuthorizationCodeResponse exp = - new OAuth2AuthorizationCodeResponse("bearer", "a/THIS_IS_A_NEW_TOKEN", "r/SO_FRESH", 86400); + OAuth2TokenResponse exp = + new OAuth2TokenResponse("bearer", "a/THIS_IS_A_NEW_TOKEN", "r/SO_FRESH", 86400); assertEquals(exp, res.get()); assertEquals(1, mockWebServer.getRequestCount()); diff --git a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParamsTest.java b/src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParamsTest.java similarity index 93% rename from src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParamsTest.java rename to src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParamsTest.java index 61732219..6fd39d1d 100644 --- a/src/test/java/it/fattureincloud/sdk/auth/OAuth2AuthorizationCodeParamsTest.java +++ b/src/test/java/it/fattureincloud/sdk/auth/authorization/OAuth2AuthorizationCodeParamsTest.java @@ -1,10 +1,11 @@ -package it.fattureincloud.sdk.auth; +package it.fattureincloud.sdk.auth.authorization; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import com.google.gson.Gson; import it.fattureincloud.sdk.JSON; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeParams; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManagerTest.java b/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManagerTest.java new file mode 100644 index 00000000..a66aee82 --- /dev/null +++ b/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeManagerTest.java @@ -0,0 +1,312 @@ +package it.fattureincloud.sdk.auth.device; + +import com.google.gson.internal.LinkedTreeMap; +import io.vavr.control.Either; +import it.fattureincloud.sdk.auth.OAuth2Error; +import it.fattureincloud.sdk.auth.OAuth2TokenResponse; +import it.fattureincloud.sdk.auth.Scope; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeManager; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeParams; +import okhttp3.*; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** Tests for OAuth2DeviceCodeManager */ +public class OAuth2DeviceCodeManagerTest { + private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + + private OAuth2DeviceCodeManager manager; + private MockWebServer mockWebServer; + + @BeforeEach + public void init() throws IOException { + manager = + new OAuth2DeviceCodeManager("SALENTO"); + mockWebServer = new MockWebServer(); + mockWebServer.start(); + } + + @AfterEach + public void tearDown() throws IOException { + mockWebServer.shutdown(); + } + + /** Test the property 'clientId' */ + @Test + public void clientIdTest() { + assertEquals("SALENTO", manager.getClientId()); + manager.setClientId("TIERRA BOMBA"); + assertEquals("TIERRA BOMBA", manager.getClientId()); + } + + /** Test the property 'baseUri' */ + @Test + public void baseUriTest() { + assertEquals("https://api-v2.fattureincloud.it", manager.getBaseUri()); + manager.setBaseUri("https://www.mock.it"); + assertEquals("https://www.mock.it", manager.getBaseUri()); + manager.setBaseUri(Optional.of("https://www.google.com")); + assertEquals("https://www.google.com", manager.getBaseUri()); + manager.setBaseUri(Optional.empty()); + assertEquals("https://api-v2.fattureincloud.it", manager.getBaseUri()); + } + + /** Test the method 'getDeviceCode' */ + @Test + public void getDeviceCodeTest() throws Exception { + final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class); + Call mockCall = Mockito.mock(Call.class); + + String jsonResponse = + "{\"data\":{\"device_code\":\"d/MEDELLIN\",\"user_code\":\"CALI\",\"scope\":{\"situation\":\"r\",\"settings\":\"a\"},\"verification_uri\":\"https://fattureincloud.it/connetti\",\"interval\":5,\"expires_in\":300}}"; + + Response.Builder builder = + new Response.Builder() + .request(new Request.Builder().url("https://api-v2.fattureincloud.it").build()) + .protocol(Protocol.HTTP_1_1) + .code(200) + .message(""); + + builder = builder.body(ResponseBody.create(jsonResponse, MediaType.parse("application/json"))); + + final Response response = builder.build(); + + Mockito.when(mockCall.execute()).thenReturn(response); + Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(mockCall); + + manager.setHttpClient(okHttpClient); + + Map scopeMap = new HashMap<>(); + scopeMap.put("settings", "a"); + scopeMap.put("situation", "r"); + + List scopes = + Arrays.asList(Scope.SETTINGS_ALL, Scope.SITUATION_READ); + + Either res = + manager.getDeviceCode(scopes); + assert (res.isRight()); + OAuth2DeviceCodeResponse exp = + new OAuth2DeviceCodeResponse( + "d/MEDELLIN", "CALI", scopeMap, "https://fattureincloud.it/connetti", 5, 300); + + OAuth2DeviceCodeResponse right = res.get(); + assertEquals(exp, right); + Mockito.verify(mockCall, Mockito.only()).execute(); + } + + /** Test the method 'fetchToken' */ + @Test + public void fetchTokenTest() throws Exception { + final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class); + Call mockCall = Mockito.mock(Call.class); + + String jsonResponse = + "{\"token_type\":\"bearer\",\"access_token\":\"a/VILLAVICENCIO\",\"refresh_token\":\"r/PEREIRA\",\"expires_in\":86400}"; + + Response.Builder builder = + new Response.Builder() + .request(new Request.Builder().url("https://api-v2.fattureincloud.it").build()) + .protocol(Protocol.HTTP_1_1) + .code(200) + .message(""); + + builder = builder.body(ResponseBody.create(jsonResponse, MediaType.parse("application/json"))); + + final Response response = builder.build(); + + Mockito.when(mockCall.execute()).thenReturn(response); + Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(mockCall); + + manager.setHttpClient(okHttpClient); + + Either res = + manager.fetchToken("c/TEUSAQUILLO"); + assert (res.isRight()); + OAuth2TokenResponse exp = + new OAuth2TokenResponse( + "bearer", "a/VILLAVICENCIO", "r/PEREIRA", 86400); + assertEquals(exp, res.get()); + Mockito.verify(mockCall, Mockito.only()).execute(); + } + + /** Test the method 'fetchToken' - error case */ + @Test + public void fetchTokenErrorTest() throws Exception { + final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class); + Call mockCall = Mockito.mock(Call.class); + + String jsonResponse = "{\"error\":\"wtf\",\"error_description\":\"maracaibo no es en colombia\"}"; + + Response.Builder builder = + new Response.Builder() + .request(new Request.Builder().url("https://api-v2.fattureincloud.it").build()) + .protocol(Protocol.HTTP_1_1) + .code(401) + .message(""); + + builder = builder.body(ResponseBody.create(jsonResponse, MediaType.parse("application/json"))); + + final Response response = builder.build(); + + Mockito.when(mockCall.execute()).thenReturn(response); + Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(mockCall); + + manager.setHttpClient(okHttpClient); + + Either res = + manager.fetchToken("c/MARACAIBO"); + assert (res.isLeft()); + OAuth2Error exp = + new OAuth2Error("wtf", "maracaibo no es en colombia", 401); + assertEquals(exp, res.getLeft()); + Mockito.verify(mockCall, Mockito.only()).execute(); + } + + /** Test the method 'fetchToken' - mock server */ + @Test + public void fetchTokenTestWithMockServer() throws Exception { + String httpUrl = mockWebServer.url("").toString(); + httpUrl = httpUrl.substring(0, httpUrl.length() - 1); + manager.setBaseUri(httpUrl); + + MockResponse mockedResponse = + new MockResponse() + .setBody( + "{\"token_type\":\"bearer\",\"access_token\":\"a/BOGOTA\",\"refresh_token\":\"r/EJECAFETERO\",\"expires_in\":86400}") + .addHeader("Content-Type", "application/json"); + + mockWebServer.enqueue(mockedResponse); + + assertEquals(0, mockWebServer.getRequestCount()); + + Either res = + manager.fetchToken("c/VILLADELEYVA"); + assert (res.isRight()); + OAuth2TokenResponse exp = + new OAuth2TokenResponse( + "bearer", "a/BOGOTA", "r/EJECAFETERO", 86400); + assertEquals(exp, res.get()); + assertEquals(1, mockWebServer.getRequestCount()); + + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + assertEquals("/oauth/token", recordedRequest.getPath()); + assertEquals("POST", recordedRequest.getMethod()); + assertEquals( + "{\"grant_type\":\"urn:ietf:params:oauth:grant-type:device_code\",\"device_code\":\"c/VILLADELEYVA\",\"client_id\":\"SALENTO\"}", + recordedRequest.getBody().readString(StandardCharsets.UTF_8)); + assertEquals("application/json; charset=utf-8", recordedRequest.getHeader("Content-Type")); + } + + /** Test the method 'refreshToken' */ + @Test + public void refreshTokenTest() throws Exception { + final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class); + Call mockCall = Mockito.mock(Call.class); + + String jsonResponse = + "{\"token_type\":\"bearer\",\"access_token\":\"a/BOGOTA\",\"refresh_token\":\"r/EJECAFETERO\",\"expires_in\":86400}"; + + Response.Builder builder = + new Response.Builder() + .request(new Request.Builder().url("https://api-v2.fattureincloud.it").build()) + .protocol(Protocol.HTTP_1_1) + .code(200) + .message(""); + + builder = builder.body(ResponseBody.create(jsonResponse, MediaType.parse("application/json"))); + + final Response response = builder.build(); + + Mockito.when(mockCall.execute()).thenReturn(response); + Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(mockCall); + + manager.setHttpClient(okHttpClient); + + Either res = + manager.refreshToken("r/VILLADELEYVA"); + assert (res.isRight()); + OAuth2TokenResponse exp = + new OAuth2TokenResponse("bearer", "a/BOGOTA", "r/EJECAFETERO", 86400); + assertEquals(exp, res.get()); + Mockito.verify(mockCall, Mockito.only()).execute(); + } + + /** Test the method 'refreshToken' - error case */ + @Test + public void refreshTokenErrorTest() throws Exception { + final OkHttpClient okHttpClient = Mockito.mock(OkHttpClient.class); + Call mockCall = Mockito.mock(Call.class); + + String jsonResponse = "{\"error\":\"so_hot\",\"error_description\":\"I_AM_MELTING\"}"; + + Response.Builder builder = + new Response.Builder() + .request(new Request.Builder().url("https://api-v2.fattureincloud.it").build()) + .protocol(Protocol.HTTP_1_1) + .code(418) + .message(""); + + builder = builder.body(ResponseBody.create(jsonResponse, MediaType.parse("application/json"))); + + final Response response = builder.build(); + + Mockito.when(mockCall.execute()).thenReturn(response); + Mockito.when(okHttpClient.newCall(Mockito.any())).thenReturn(mockCall); + + manager.setHttpClient(okHttpClient); + + Either res = + manager.refreshToken("r/WHY_IS_IT_SO_HOT_IN_HERE"); + assert (res.isLeft()); + OAuth2Error exp = + new OAuth2Error("so_hot", "I_AM_MELTING", 418); + assertEquals(exp, res.getLeft()); + Mockito.verify(mockCall, Mockito.only()).execute(); + } + + /** Test the method 'refreshToken' - mock server */ + @Test + public void refreshTokenTestWithMockServer() throws Exception { + String httpUrl = mockWebServer.url("").toString(); + httpUrl = httpUrl.substring(0, httpUrl.length() - 1); + manager.setBaseUri(httpUrl); + + MockResponse mockedResponse = + new MockResponse() + .setBody( + "{\"token_type\":\"bearer\",\"access_token\":\"a/BOGOTA\",\"refresh_token\":\"r/EJECAFETERO\",\"expires_in\":86400}") + .addHeader("Content-Type", "application/json"); + + mockWebServer.enqueue(mockedResponse); + + assertEquals(0, mockWebServer.getRequestCount()); + + Either res = + manager.refreshToken("r/VILLAVICENCIO"); + assert (res.isRight()); + OAuth2TokenResponse exp = + new OAuth2TokenResponse("bearer", "a/BOGOTA", "r/EJECAFETERO", 86400); + assertEquals(exp, res.get()); + assertEquals(1, mockWebServer.getRequestCount()); + + RecordedRequest recordedRequest = mockWebServer.takeRequest(); + assertEquals("/oauth/token", recordedRequest.getPath()); + assertEquals("POST", recordedRequest.getMethod()); + assertEquals( + "{\"refresh_token\":\"r/VILLAVICENCIO\",\"grant_type\":\"refresh_token\",\"client_id\":\"SALENTO\"}", + recordedRequest.getBody().readString(StandardCharsets.UTF_8)); + assertEquals("application/json; charset=utf-8", recordedRequest.getHeader("Content-Type")); + } +} diff --git a/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseTest.java b/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseTest.java new file mode 100644 index 00000000..1a877337 --- /dev/null +++ b/src/test/java/it/fattureincloud/sdk/auth/device/OAuth2DeviceCodeResponseTest.java @@ -0,0 +1,107 @@ +package it.fattureincloud.sdk.auth.device; + +import com.google.gson.Gson; +import it.fattureincloud.sdk.JSON; +import it.fattureincloud.sdk.auth.authorization.OAuth2AuthorizationCodeParams; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** Tests for OAuth2DeviceCodeResponse */ +public class OAuth2DeviceCodeResponseTest { + + private OAuth2DeviceCodeResponse res; + + @BeforeEach + public void init() { + HashMap scope = new HashMap<>(); + scope.put("situation", "r"); + scope.put("settings", "a"); + res = new OAuth2DeviceCodeResponse("d/CARTAGENA", "BOGOTA", scope, "https://fic.api.it", 5,300); + } + + /** Model tests for OAuth2DeviceCodeResponse */ + @Test + public void testOAuth2DeviceCodeResponse() { + HashMap scope = new HashMap<>(); + scope.put("situation", "r"); + scope.put("settings", "a"); + + OAuth2DeviceCodeResponse res = + new OAuth2DeviceCodeResponse("d/CARTAGENA", "BOGOTA", scope, "https://fic.api.it", 5,300); + + JSON jsonManager = new JSON(); + Gson gson = jsonManager.getGson(); + String json = gson.toJson(res); + String str = "{\"device_code\":\"d/CARTAGENA\",\"user_code\":\"BOGOTA\",\"scope\":{\"settings\":\"a\",\"situation\":\"r\"},\"verification_uri\":\"https://fic.api.it\",\"interval\":5,\"expires_in\":300}"; + assertEquals(str, json); + OAuth2DeviceCodeResponse generated = + gson.fromJson(str, OAuth2DeviceCodeResponse.class); + assertEquals(res, generated); + + Object o = res; + assertEquals(res, o); + assertFalse(res.equals(null)); + assertFalse(res.equals(Integer.getInteger("5"))); + } + + /** Test the property 'deviceCode' */ + @Test + public void deviceCodeTest() { + assertEquals("d/CARTAGENA", res.getDeviceCode()); + res.setDeviceCode("d/EJECAFETERO"); + assertEquals("d/EJECAFETERO", res.getDeviceCode()); + } + + /** Test the property 'userCode' */ + @Test + public void userCodeTest() { + assertEquals("BOGOTA", res.getUserCode()); + res.setUserCode("VILLADELEYVA"); + assertEquals("VILLADELEYVA", res.getUserCode()); + } + + /** Test the property 'scope' */ + @Test + public void scopeTest() { + HashMap scope = new HashMap<>(); + scope.put("settings", "a"); + scope.put("situation", "r"); + assertEquals(scope, res.getScope()); + + HashMap scope2 = new HashMap<>(); + scope.put("stock", "a"); + scope.put("issued_documents.credit_notes", "a"); + scope.put("taxes", "r"); + res.setScope(scope2); + assertEquals(scope2, res.getScope()); + } + + /** Test the property 'verificationUri' */ + @Test + public void verificationUriTest() { + assertEquals("https://fic.api.it", res.getVerificationUri()); + res.setVerificationUri("https://colombia.turism.co"); + assertEquals("https://colombia.turism.co", res.getVerificationUri()); + } + + /** Test the property 'interval' */ + @Test + public void intervalTest() { + assertEquals(5, res.getInterval()); + res.setInterval(22); + assertEquals(22, res.getInterval()); + } + + /** Test the property 'expiresIn' */ + @Test + public void expiresInTest() { + assertEquals(300, res.getExpiresIn()); + res.setExpiresIn(99); + assertEquals(99, res.getExpiresIn()); + } +}