From 5e90d6ff7f949e4f80d989a5b6aeaaedf6309a46 Mon Sep 17 00:00:00 2001 From: Rishabh Joshi Date: Wed, 24 Oct 2018 18:52:16 +0530 Subject: [PATCH] Add UsptoxAuthService, update LoginService --- README.md | 6 +- build.gradle | 2 +- .../rishabh9/riko/upstox/common/Service.java | 15 +- .../riko/upstox/common/UpstoxAuthService.java | 45 +++ .../riko/upstox/feed/FeedService.java | 12 +- .../upstox/historical/HistoricalService.java | 12 +- .../riko/upstox/login/LoginService.java | 50 +--- .../riko/upstox/orders/OrderService.java | 12 +- .../riko/upstox/users/UserService.java | 12 +- .../upstox/websockets/WebSocketService.java | 12 +- .../riko/upstox/feed/FeedServiceTest.java | 135 ++------- .../historical/HistoricalServiceTest.java | 67 ++--- .../riko/upstox/login/LoginServiceTest.java | 129 ++++---- .../riko/upstox/orders/OrderServiceTest.java | 275 +++--------------- .../riko/upstox/users/UserServiceTest.java | 201 +++---------- 15 files changed, 313 insertions(+), 672 deletions(-) create mode 100644 src/main/java/com/github/rishabh9/riko/upstox/common/UpstoxAuthService.java diff --git a/README.md b/README.md index 909a2f1..dfeae90 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,18 @@ Checkout the [wiki](https://github.com/rishabh9/riko/wiki) for more details. com.github.rishabh9 riko - 1.0.2 + 2.0.0 ``` ### For Gradle based project ```groovy dependencies { - implementation 'com.github.rishabh9:riko:1.0.2' + implementation 'com.github.rishabh9:riko:2.0.0' } ``` ### For SBT based project ```scala -libraryDependencies += "com.github.rishabh9" % "riko" % "1.0.2" +libraryDependencies += "com.github.rishabh9" % "riko" % "2.0.0" ``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 5523985..9e35be2 100644 --- a/build.gradle +++ b/build.gradle @@ -47,7 +47,7 @@ dependencyManagement { // GroupId group = 'com.github.rishabh9' // Version -version = '1.0.2' +version = '2.0.0' archivesBaseName = 'riko' dependencies { diff --git a/src/main/java/com/github/rishabh9/riko/upstox/common/Service.java b/src/main/java/com/github/rishabh9/riko/upstox/common/Service.java index fbcbad6..a2df7f9 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/common/Service.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/common/Service.java @@ -40,23 +40,22 @@ public abstract class Service { private static final Logger log = LogManager.getLogger(Service.class); - protected final AccessToken accessToken; - protected final ApiCredentials credentials; + protected final UpstoxAuthService upstoxAuthService; /** - * @param accessToken The user's access token - * @param credentials The user's API credentials + * @param upstoxAuthService The service to retrieve authentication details */ - public Service(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + public Service(@Nonnull final UpstoxAuthService upstoxAuthService) { - this.accessToken = Objects.requireNonNull(accessToken); - this.credentials = Objects.requireNonNull(credentials); + this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService); } protected T prepareServiceApi(@Nonnull final Class type) { log.debug("Preparing service API: {}", type.getName()); + final AccessToken accessToken = upstoxAuthService.getAccessToken(); + final ApiCredentials credentials = upstoxAuthService.getApiCredentials(); + final String token = accessToken.getType() + " " + accessToken.getToken(); return ServiceGenerator.getInstance() .createService(type, new AuthHeaders(token, credentials.getApiKey())); diff --git a/src/main/java/com/github/rishabh9/riko/upstox/common/UpstoxAuthService.java b/src/main/java/com/github/rishabh9/riko/upstox/common/UpstoxAuthService.java new file mode 100644 index 0000000..ed5f5c8 --- /dev/null +++ b/src/main/java/com/github/rishabh9/riko/upstox/common/UpstoxAuthService.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2018 Rishabh Joshi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.rishabh9.riko.upstox.common; + +import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.login.models.AccessToken; + +/** + * The implementations of this interface are responsible to always return + * the latest version of the {@link ApiCredentials} and {@link AccessToken}. + */ +public interface UpstoxAuthService { + + /** + * @return The latest API Key and API Secret of an Upstox account. + */ + ApiCredentials getApiCredentials(); + + /** + * @return The latest access token + */ + AccessToken getAccessToken(); +} diff --git a/src/main/java/com/github/rishabh9/riko/upstox/feed/FeedService.java b/src/main/java/com/github/rishabh9/riko/upstox/feed/FeedService.java index 08ae6a3..cb9634f 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/feed/FeedService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/feed/FeedService.java @@ -25,27 +25,27 @@ package com.github.rishabh9.riko.upstox.feed; import com.github.rishabh9.riko.upstox.common.Service; -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.feed.models.Feed; import com.github.rishabh9.riko.upstox.feed.models.SubscriptionResponse; -import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.google.common.base.Strings; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.annotation.Nonnull; -import java.util.Objects; import java.util.concurrent.CompletableFuture; public class FeedService extends Service { private static final Logger log = LogManager.getLogger(FeedService.class); - public FeedService(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + /** + * @param upstoxAuthService The service to retrieve authentication details + */ + public FeedService(@Nonnull final UpstoxAuthService upstoxAuthService) { - super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials)); + super(upstoxAuthService); } /** diff --git a/src/main/java/com/github/rishabh9/riko/upstox/historical/HistoricalService.java b/src/main/java/com/github/rishabh9/riko/upstox/historical/HistoricalService.java index 029053e..ffa7280 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/historical/HistoricalService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/historical/HistoricalService.java @@ -25,27 +25,27 @@ package com.github.rishabh9.riko.upstox.historical; import com.github.rishabh9.riko.upstox.common.Service; -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.historical.models.Candle; -import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.google.common.base.Strings; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.annotation.Nonnull; import java.util.List; -import java.util.Objects; import java.util.concurrent.CompletableFuture; public class HistoricalService extends Service { private static final Logger log = LogManager.getLogger(HistoricalService.class); - public HistoricalService(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + /** + * @param upstoxAuthService The service to retrieve authentication details + */ + public HistoricalService(@Nonnull final UpstoxAuthService upstoxAuthService) { - super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials)); + super(upstoxAuthService); } /** diff --git a/src/main/java/com/github/rishabh9/riko/upstox/login/LoginService.java b/src/main/java/com/github/rishabh9/riko/upstox/login/LoginService.java index f8a9ded..3faf582 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/login/LoginService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/login/LoginService.java @@ -25,7 +25,7 @@ package com.github.rishabh9.riko.upstox.login; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.github.rishabh9.riko.upstox.login.models.TokenRequest; import com.google.common.base.Strings; @@ -35,66 +35,42 @@ import javax.annotation.Nonnull; import java.util.Objects; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; public class LoginService { private static final Logger log = LogManager.getLogger(LoginService.class); - private final TokenRequest request; - - private final ApiCredentials credentials; + private final UpstoxAuthService upstoxAuthService; /** - * @param request The TokenRequest object containing all fields, - * including the access code received after authenticating the user on Upstox site. - * @param credentials The API key and secret. + * @param upstoxAuthService The service to retrieve authentication details. */ - public LoginService(@Nonnull final TokenRequest request, - @Nonnull final ApiCredentials credentials) { + public LoginService(@Nonnull final UpstoxAuthService upstoxAuthService) { - this.request = Objects.requireNonNull(request); - this.credentials = Objects.requireNonNull(credentials); + this.upstoxAuthService = Objects.requireNonNull(upstoxAuthService); } /** * Retrieves the access code from Upstox Authorization URL through a synchronous call.
* + * @param request The TokenRequest object containing all fields, + * including the access code received after authenticating the user on Upstox site. * @return An 'optional' AccessToken. Return object is empty in case of error. */ - public AccessToken getAccessToken() throws ExecutionException, InterruptedException { + public CompletableFuture getAccessToken(@Nonnull final TokenRequest request) { - if (Strings.isNullOrEmpty(request.getCode())) { + if (Strings.isNullOrEmpty(Objects.requireNonNull(request).getCode())) { throw new IllegalArgumentException( "Missing value for authorization code. Code: " + request.getCode()); } // Create a very simple REST adapter which points the Upstox API endpoint. - LoginApi loginApi = + final LoginApi loginApi = ServiceGenerator.getInstance().createService( LoginApi.class, - credentials.getApiKey(), - credentials.getApiSecret()); - - CompletableFuture future = loginApi.getAccessToken(request); + upstoxAuthService.getApiCredentials().getApiKey(), + upstoxAuthService.getApiCredentials().getApiSecret()); - // Make a synchronous call. - return future -// .exceptionally( -// (throwable) -> { -// if (null != throwable) { -// if (throwable instanceof HttpException) { // Non 2XX HTTP Response Code -// log.error("Request to retrieve access code was unsuccessful", -// throwable); -// } else if (throwable instanceof IOError) { // Network Error -// log.error("A network error occurred while trying to retrieve access code", -// throwable); -// } else { -// log.error("Unexpected error has occurred", throwable); -// } -// } -// return null; -// }) - .get(); + return loginApi.getAccessToken(request); } } diff --git a/src/main/java/com/github/rishabh9/riko/upstox/orders/OrderService.java b/src/main/java/com/github/rishabh9/riko/upstox/orders/OrderService.java index f7b5466..af6c3a8 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/orders/OrderService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/orders/OrderService.java @@ -25,9 +25,8 @@ package com.github.rishabh9.riko.upstox.orders; import com.github.rishabh9.riko.upstox.common.Service; -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; -import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.github.rishabh9.riko.upstox.orders.models.Order; import com.github.rishabh9.riko.upstox.orders.models.OrderRequest; import com.github.rishabh9.riko.upstox.orders.models.Trade; @@ -37,7 +36,6 @@ import javax.annotation.Nonnull; import java.util.List; -import java.util.Objects; import java.util.concurrent.CompletableFuture; public class OrderService extends Service { @@ -45,13 +43,11 @@ public class OrderService extends Service { private static final Logger log = LogManager.getLogger(OrderService.class); /** - * @param accessToken The user's access token - * @param credentials The user's API credentials + * @param upstoxAuthService The service to retrieve authentication details */ - public OrderService(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + public OrderService(@Nonnull final UpstoxAuthService upstoxAuthService) { - super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials)); + super(upstoxAuthService); } /** diff --git a/src/main/java/com/github/rishabh9/riko/upstox/users/UserService.java b/src/main/java/com/github/rishabh9/riko/upstox/users/UserService.java index 71f4589..f1bd4d1 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/users/UserService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/users/UserService.java @@ -25,9 +25,8 @@ package com.github.rishabh9.riko.upstox.users; import com.github.rishabh9.riko.upstox.common.Service; -import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; -import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.github.rishabh9.riko.upstox.users.models.*; import com.google.common.base.Strings; import okhttp3.ResponseBody; @@ -38,7 +37,6 @@ import javax.annotation.Nullable; import java.io.InputStream; import java.util.List; -import java.util.Objects; import java.util.concurrent.CompletableFuture; public class UserService extends Service { @@ -46,13 +44,11 @@ public class UserService extends Service { private static final Logger log = LogManager.getLogger(UserService.class); /** - * @param accessToken The user's access token - * @param credentials The user's API credentials + * @param upstoxAuthService The service to retrieve authentication details */ - public UserService(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + public UserService(@Nonnull final UpstoxAuthService upstoxAuthService) { - super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials)); + super(upstoxAuthService); } /** diff --git a/src/main/java/com/github/rishabh9/riko/upstox/websockets/WebSocketService.java b/src/main/java/com/github/rishabh9/riko/upstox/websockets/WebSocketService.java index a3beb51..937cc29 100644 --- a/src/main/java/com/github/rishabh9/riko/upstox/websockets/WebSocketService.java +++ b/src/main/java/com/github/rishabh9/riko/upstox/websockets/WebSocketService.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.websockets; import com.github.rishabh9.riko.upstox.common.Service; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.login.models.AccessToken; @@ -38,7 +39,6 @@ import javax.annotation.Nonnull; import java.util.List; -import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -50,13 +50,11 @@ public class WebSocketService extends Service { private static final Logger log = LogManager.getLogger(WebSocketService.class); /** - * @param accessToken The user's access token - * @param credentials The user's API credentials + * @param upstoxAuthService The service to retrieve authentication details */ - public WebSocketService(@Nonnull final AccessToken accessToken, - @Nonnull final ApiCredentials credentials) { + public WebSocketService(@Nonnull final UpstoxAuthService upstoxAuthService) { - super(Objects.requireNonNull(accessToken), Objects.requireNonNull(credentials)); + super(upstoxAuthService); } private CompletableFuture> getWebsocketParameters() { @@ -135,6 +133,8 @@ private Request prepareRequest() { if (!Strings.isNullOrEmpty(port)) { urlBuilder.port(Integer.parseInt(port)); } + final AccessToken accessToken = upstoxAuthService.getAccessToken(); + final ApiCredentials credentials = upstoxAuthService.getApiCredentials(); urlBuilder .addQueryParameter("apiKey", credentials.getApiKey()) .addQueryParameter("token", accessToken.getToken()); diff --git a/src/test/java/com/github/rishabh9/riko/upstox/feed/FeedServiceTest.java b/src/test/java/com/github/rishabh9/riko/upstox/feed/FeedServiceTest.java index f801e82..28532cc 100644 --- a/src/test/java/com/github/rishabh9/riko/upstox/feed/FeedServiceTest.java +++ b/src/test/java/com/github/rishabh9/riko/upstox/feed/FeedServiceTest.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.feed; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.feed.models.Feed; @@ -47,6 +48,22 @@ class FeedServiceTest { private static final Logger log = LogManager.getLogger(FeedServiceTest.class); + private UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + AccessToken token = new AccessToken(); + token.setExpiresIn(86400L); + token.setType("Bearer"); + token.setToken("access_token_123456789"); + return token; + } + }; + @Test void liveFeed_success_whenAllParametersAreCorrect() throws IOException { MockWebServer server = new MockWebServer(); @@ -65,13 +82,7 @@ void liveFeed_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { UpstoxResponse serverResponse = @@ -102,13 +113,7 @@ void liveFeed_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(ExecutionException.class, service.liveFeed("NSE", "RELIANCE", "TYPE")::get); @@ -126,13 +131,7 @@ void liveFeed_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { service.liveFeed("NSE", "RELIANCE", "TYPE").get(); @@ -145,15 +144,7 @@ void liveFeed_failure_onNetworkError() throws IOException { @Test void liveFeed_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.liveFeed(null, null, null), @@ -191,21 +182,9 @@ void liveFeed_throwIAE_whenRequiredParametersAreMissing() { @Test void liveFeed_throwNPE_whenServiceParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - assertThrows(NullPointerException.class, - () -> new FeedService(null, credentials), - "Null check missing for 'AccessToken' from FeedService constructor"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - assertThrows(NullPointerException.class, - () -> new FeedService(token, null), - "Null check missing for 'ApiCredentials' from FeedService constructor"); + () -> new FeedService(null), + "Null check missing for 'UpstoxAuthService' from FeedService constructor"); } @Test @@ -226,13 +205,7 @@ void subscribe_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { UpstoxResponse subResponse = @@ -263,13 +236,7 @@ void subscribe_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(ExecutionException.class, service.subscribe("TYPE", "NSE", "ACC,RELIANCE")::get); @@ -287,13 +254,7 @@ void subscribe_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { service.subscribe("TYPE", "NSE", "RELIANCE,ACC").get(); @@ -306,15 +267,7 @@ void subscribe_failure_onNetworkError() throws IOException { @Test void subscribe_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.subscribe(null, null, null), @@ -367,13 +320,7 @@ void unsubscribe_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { UpstoxResponse unsubResponse = @@ -404,13 +351,7 @@ void unsubscribe_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(ExecutionException.class, service.unsubscribe("TYPE", "NSE", "ACC,RELIANCE")::get); @@ -428,13 +369,7 @@ void unsubscribe_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); try { service.unsubscribe("TYPE", "NSE", "RELIANCE,ACC").get(); @@ -447,15 +382,7 @@ void unsubscribe_failure_onNetworkError() throws IOException { @Test void unsubscribe_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - FeedService service = new FeedService(token, credentials); + FeedService service = new FeedService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.unsubscribe(null, null, null), diff --git a/src/test/java/com/github/rishabh9/riko/upstox/historical/HistoricalServiceTest.java b/src/test/java/com/github/rishabh9/riko/upstox/historical/HistoricalServiceTest.java index 8ced20a..ad1daa8 100644 --- a/src/test/java/com/github/rishabh9/riko/upstox/historical/HistoricalServiceTest.java +++ b/src/test/java/com/github/rishabh9/riko/upstox/historical/HistoricalServiceTest.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.historical; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.historical.models.Candle; @@ -48,6 +49,22 @@ class HistoricalServiceTest { private static final Logger log = LogManager.getLogger(HistoricalServiceTest.class); + private UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + AccessToken token = new AccessToken(); + token.setExpiresIn(86400L); + token.setType("Bearer"); + token.setToken("access_token_123456789"); + return token; + } + }; + @Test void getOhlc_success_whenAllParametersAreCorrect() throws IOException { MockWebServer server = new MockWebServer(); @@ -68,13 +85,7 @@ void getOhlc_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - HistoricalService service = new HistoricalService(token, credentials); + HistoricalService service = new HistoricalService(upstoxAuthService); try { UpstoxResponse> serverResponse = @@ -105,13 +116,7 @@ void getOhlc_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - HistoricalService service = new HistoricalService(token, credentials); + HistoricalService service = new HistoricalService(upstoxAuthService); assertThrows(ExecutionException.class, service.getOhlc("NSE", "RELIANCE", null, null, null)::get); @@ -129,13 +134,7 @@ void getOhlc_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - HistoricalService service = new HistoricalService(token, credentials); + HistoricalService service = new HistoricalService(upstoxAuthService); try { service.getOhlc("NSE", "RELIANCE", null, null, null).get(); @@ -148,15 +147,7 @@ void getOhlc_failure_onNetworkError() throws IOException { @Test void getOhlc_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - HistoricalService service = new HistoricalService(token, credentials); + HistoricalService service = new HistoricalService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.getOhlc(null, null, null, null, null), @@ -186,20 +177,8 @@ void getOhlc_throwIAE_whenRequiredParametersAreMissing() { @Test void getOhlc_throwNPE_whenServiceParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - assertThrows(NullPointerException.class, - () -> new HistoricalService(null, credentials), - "Null check missing for 'AccessToken' from HistoricalService constructor"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - assertThrows(NullPointerException.class, - () -> new HistoricalService(token, null), - "Null check missing for 'ApiCredentials' from HistoricalService constructor"); + () -> new HistoricalService(null), + "Null check missing for 'UpstoxAuthService' from HistoricalService constructor"); } } \ No newline at end of file diff --git a/src/test/java/com/github/rishabh9/riko/upstox/login/LoginServiceTest.java b/src/test/java/com/github/rishabh9/riko/upstox/login/LoginServiceTest.java index 0c0cf4c..34b908f 100644 --- a/src/test/java/com/github/rishabh9/riko/upstox/login/LoginServiceTest.java +++ b/src/test/java/com/github/rishabh9/riko/upstox/login/LoginServiceTest.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.login; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.login.models.AccessToken; import com.github.rishabh9.riko.upstox.login.models.TokenRequest; @@ -60,11 +61,21 @@ void getAccessToken_success_whenAllParametersAreCorrect() throws IOException { TokenRequest request = new TokenRequest("authorization_code_123456789", "authorization_code", "http://localhost:4567/hello"); - ApiCredentials credentials = new ApiCredentials("secretApiKey", "secret-secret"); + UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } - LoginService service = new LoginService(request, credentials); + @Override + public AccessToken getAccessToken() { + return null; + } + }; + + LoginService service = new LoginService(upstoxAuthService); try { - AccessToken accessToken = service.getAccessToken(); + AccessToken accessToken = service.getAccessToken(request).get(); assertNotNull(accessToken); assertEquals(Long.valueOf(86400L), accessToken.getExpiresIn()); @@ -98,11 +109,21 @@ void getAccessToken_failure_whenUpstoxReturnsError() throws IOException { TokenRequest request = new TokenRequest("authorization_code_123456789", "authorization_code", "http://localhost:4567/hello"); - ApiCredentials credentials = new ApiCredentials("secretApiKey", "secret-secret"); + UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + return null; + } + }; - LoginService service = new LoginService(request, credentials); + LoginService service = new LoginService(upstoxAuthService); - assertThrows(ExecutionException.class, service::getAccessToken); + assertThrows(ExecutionException.class, () -> service.getAccessToken(request).get()); // Shut down the server. Instances cannot be reused. server.shutdown(); @@ -121,12 +142,22 @@ void getAccessToken_failure_onNetworkError() throws IOException { TokenRequest request = new TokenRequest("authorization_code_123456789", "authorization_code", "http://localhost:4567/hello"); - ApiCredentials credentials = new ApiCredentials("secretApiKey", "secret-secret"); + UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + return null; + } + }; - LoginService service = new LoginService(request, credentials); + LoginService service = new LoginService(upstoxAuthService); try { - service.getAccessToken(); + service.getAccessToken(request).get(); } catch (ExecutionException | InterruptedException e) { assertTrue(e.getCause() instanceof IOException); } finally { @@ -136,67 +167,57 @@ void getAccessToken_failure_onNetworkError() throws IOException { } @Test - void getAccessToken_throwNPE_whenParametersAreNotProper() { - - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - + void getAccessToken_throwNPE_whenMissingTokenRequest() { + UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + return null; + } + }; + + LoginService service = new LoginService(upstoxAuthService); assertThrows(NullPointerException.class, - () -> new LoginService(null, credentials), + () -> service.getAccessToken(null), "Null check missing for 'TokenRequest' from LoginService constructor"); + } + @Test + void getAccessToken_throwNPE_whenMissingGrantType() { assertThrows(NullPointerException.class, - () -> new LoginService( - new TokenRequest( - "authorization_code_123456789", - null, - "http://localhost:4567/hello"), - credentials), + () -> new TokenRequest("authorization_code_123456789", null, "http://localhost:4567/hello"), "Null check missing for 'grantType' from TokenRequest constructor"); + } + @Test + void getAccessToken_throwNPE_whenMissingCode() { assertThrows(NullPointerException.class, - () -> new LoginService( - new TokenRequest( - null, - "authorization_code", - "http://localhost:4567/hello"), - credentials), + () -> new TokenRequest(null, "authorization_code", "http://localhost:4567/hello"), "Null check missing for 'code' from TokenRequest constructor"); + } + @Test + void getAccessToken_throwNPE_whenMissingRedirectUri() { assertThrows(NullPointerException.class, - () -> new LoginService( - new TokenRequest( - "authorization_code_123456789", - "authorization_code", - null), - credentials), + () -> new TokenRequest("authorization_code_123456789", "authorization_code", null), "Null check missing for 'redirectUri' from TokenRequest constructor"); + } - TokenRequest request = new TokenRequest( - "authorization_code_123456789", - "authorization_code", - "http://localhost:4567/hello"); - - assertThrows(NullPointerException.class, - () -> new LoginService( - request, - null), - "Null check missing for 'ApiCredentials' from LoginService constructor"); - + @Test + void getAccessToken_throwNPE_whenMissingApiKey() { assertThrows(NullPointerException.class, - () -> new LoginService( - request, - new ApiCredentials( - null, - "secret-secret")), + () -> new ApiCredentials(null, "secret-secret"), "Null check missing for 'apiKey' from ApiCredentials constructor"); + } + @Test + void getAccessToken_throwNPE_whenMissingApiSecret() { assertThrows(NullPointerException.class, - () -> new LoginService( - request, - new ApiCredentials( - "secretApiKey", - null)), + () -> new ApiCredentials("secretApiKey", null), "Null check missing for 'apiSecret' from ApiCredentials constructor"); } } \ No newline at end of file diff --git a/src/test/java/com/github/rishabh9/riko/upstox/orders/OrderServiceTest.java b/src/test/java/com/github/rishabh9/riko/upstox/orders/OrderServiceTest.java index e3394c9..afe440a 100644 --- a/src/test/java/com/github/rishabh9/riko/upstox/orders/OrderServiceTest.java +++ b/src/test/java/com/github/rishabh9/riko/upstox/orders/OrderServiceTest.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.orders; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.login.models.AccessToken; @@ -50,6 +51,22 @@ class OrderServiceTest { private static final Logger log = LogManager.getLogger(OrderServiceTest.class); + private UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + AccessToken token = new AccessToken(); + token.setExpiresIn(86400L); + token.setType("Bearer"); + token.setToken("access_token_123456789"); + return token; + } + }; + @Test void getOrderHistory_success_whenAllParametersAreCorrect() throws IOException { MockWebServer server = new MockWebServer(); @@ -70,13 +87,7 @@ void getOrderHistory_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse> serverResponse = service.getOrderHistory().get(); @@ -105,13 +116,7 @@ void getOrderHistory_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.getOrderHistory()::get); @@ -128,13 +133,7 @@ void getOrderHistory_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.getOrderHistory().get(); @@ -166,13 +165,7 @@ void getOrderDetails_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse> serverResponse = @@ -202,13 +195,7 @@ void getOrderDetails_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.getOrderDetails("ORD_ID_1")::get); @@ -225,13 +212,7 @@ void getOrderDetails_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.getOrderDetails("ORD_ID_1").get(); @@ -245,15 +226,7 @@ void getOrderDetails_failure_onNetworkError() throws IOException { @Test void getOrderDetails_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.getOrderDetails(null), @@ -284,13 +257,7 @@ void getTradeBook_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse> serverResponse = service.getTradeBook().get(); @@ -319,13 +286,7 @@ void getTradeBook_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.getTradeBook()::get); @@ -342,13 +303,7 @@ void getTradeBook_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.getTradeBook().get(); @@ -380,13 +335,7 @@ void getTradeHistory_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse> serverResponse = @@ -416,13 +365,7 @@ void getTradeHistory_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.getTradeHistory("ORD_ID_1")::get); @@ -439,13 +382,7 @@ void getTradeHistory_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.getTradeHistory("ORD_ID_1").get(); @@ -459,15 +396,7 @@ void getTradeHistory_failure_onNetworkError() throws IOException { @Test void getTradeHistory_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.getTradeHistory(null), @@ -496,13 +425,7 @@ void placeOrder_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); OrderRequest request = new OrderRequest(); request.setOrderId("ORD_ID_1"); @@ -534,13 +457,7 @@ void placeOrder_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.placeOrder(new OrderRequest())::get); @@ -557,13 +474,7 @@ void placeOrder_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.placeOrder(new OrderRequest()).get(); @@ -577,15 +488,7 @@ void placeOrder_failure_onNetworkError() throws IOException { @Test void placeOrder_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.placeOrder(null), @@ -610,13 +513,7 @@ void modifyOrder_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); OrderRequest request = new OrderRequest(); request.setOrderId("ORD_ID_1"); @@ -649,13 +546,7 @@ void modifyOrder_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.modifyOrder("ORD_ID_1", new OrderRequest())::get); @@ -673,13 +564,7 @@ void modifyOrder_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.modifyOrder("ORD_ID_1", new OrderRequest()).get(); @@ -693,15 +578,7 @@ void modifyOrder_failure_onNetworkError() throws IOException { @Test void modifyOrder_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.modifyOrder(null, new OrderRequest()), @@ -732,13 +609,7 @@ void cancelOrders_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse serverResponse = @@ -768,13 +639,7 @@ void cancelOrders_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.cancelOrders("ORDER_ID_1")::get); @@ -791,13 +656,7 @@ void cancelOrders_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.cancelOrders("ORD_ID_1,ORD_ID_2").get(); @@ -811,15 +670,7 @@ void cancelOrders_failure_onNetworkError() throws IOException { @Test void cancelOrders_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.cancelOrders(null), @@ -846,13 +697,7 @@ void cancelAllOrders_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { UpstoxResponse serverResponse = service.cancelAllOrders().get(); @@ -881,13 +726,7 @@ void cancelAllOrders_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); assertThrows(ExecutionException.class, service.cancelAllOrders()::get); @@ -904,13 +743,7 @@ void cancelAllOrders_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - OrderService service = new OrderService(token, credentials); + OrderService service = new OrderService(upstoxAuthService); try { service.cancelAllOrders().get(); @@ -924,20 +757,8 @@ void cancelAllOrders_failure_onNetworkError() throws IOException { @Test void cancelAllOrders_throwNPE_whenServiceParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - assertThrows(NullPointerException.class, - () -> new OrderService(null, credentials), - "Null check missing for 'AccessToken' from OrderService constructor"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - assertThrows(NullPointerException.class, - () -> new OrderService(token, null), - "Null check missing for 'ApiCredentials' from OrderService constructor"); + () -> new OrderService(null), + "Null check missing for 'UpstoxAuthService' from OrderService constructor"); } } \ No newline at end of file diff --git a/src/test/java/com/github/rishabh9/riko/upstox/users/UserServiceTest.java b/src/test/java/com/github/rishabh9/riko/upstox/users/UserServiceTest.java index a914949..298ae36 100644 --- a/src/test/java/com/github/rishabh9/riko/upstox/users/UserServiceTest.java +++ b/src/test/java/com/github/rishabh9/riko/upstox/users/UserServiceTest.java @@ -25,6 +25,7 @@ package com.github.rishabh9.riko.upstox.users; import com.github.rishabh9.riko.upstox.common.ServiceGenerator; +import com.github.rishabh9.riko.upstox.common.UpstoxAuthService; import com.github.rishabh9.riko.upstox.common.models.ApiCredentials; import com.github.rishabh9.riko.upstox.common.models.UpstoxResponse; import com.github.rishabh9.riko.upstox.login.models.AccessToken; @@ -55,6 +56,22 @@ class UserServiceTest { private static final Logger log = LogManager.getLogger(UserServiceTest.class); + private UpstoxAuthService upstoxAuthService = new UpstoxAuthService() { + @Override + public ApiCredentials getApiCredentials() { + return new ApiCredentials("secretApiKey", "secret-secret"); + } + + @Override + public AccessToken getAccessToken() { + AccessToken token = new AccessToken(); + token.setExpiresIn(86400L); + token.setType("Bearer"); + token.setToken("access_token_123456789"); + return token; + } + }; + @Test void getProfile_success_whenAllParametersAreCorrect() throws IOException { MockWebServer server = new MockWebServer(); @@ -73,13 +90,7 @@ void getProfile_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { UpstoxResponse serverResponse = service.getProfile().get(); @@ -108,13 +119,7 @@ void getProfile_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getProfile()::get); @@ -131,13 +136,7 @@ void getProfile_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getProfile().get(); @@ -168,13 +167,7 @@ void getProfileBalance_success_whenAllParametersAreCorrect() throws IOException ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { UpstoxResponse serverResponse = @@ -204,13 +197,7 @@ void getProfileBalance_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getProfileBalance(null)::get); @@ -227,13 +214,7 @@ void getProfileBalance_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getProfileBalance(null).get(); @@ -264,13 +245,7 @@ void getPositions_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { UpstoxResponse> serverResponse = service.getPositions().get(); @@ -299,13 +274,7 @@ void getPositions_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getPositions()::get); @@ -322,13 +291,7 @@ void getPositions_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getPositions().get(); @@ -359,13 +322,7 @@ void getHoldings_success_whenAllParametersAreCorrect() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { UpstoxResponse> serverResponse = service.getHoldings().get(); @@ -394,13 +351,7 @@ void getHoldings_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getHoldings()::get); @@ -417,13 +368,7 @@ void getHoldings_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getHoldings().get(); @@ -453,13 +398,7 @@ void getAllMasterContracts_success_whenAllParametersAreCorrect() throws IOExcept ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { InputStream inputStream = @@ -467,7 +406,8 @@ void getAllMasterContracts_success_whenAllParametersAreCorrect() throws IOExcept .get(); String stagedResponse = convert(inputStream, Charset.forName("UTF-8")); - Type fooType = new TypeToken>>() {}.getType(); + Type fooType = new TypeToken>>() { + }.getType(); UpstoxResponse> serverResponse = new Gson().fromJson(stagedResponse, fooType); System.err.println(stagedResponse); @@ -509,13 +449,7 @@ void getAllMasterContracts_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getAllMasterContracts("NSE")::get); @@ -533,13 +467,7 @@ void getAllMasterContracts_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getAllMasterContracts("NSE").get(); @@ -552,15 +480,7 @@ void getAllMasterContracts_failure_onNetworkError() throws IOException { @Test void getAllMasterContracts_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.getAllMasterContracts(null), @@ -589,13 +509,7 @@ void getMasterContract_success_whenAllParametersAreCorrect() throws IOException ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { UpstoxResponse serverResponse = @@ -626,13 +540,7 @@ void getMasterContract_failure_whenUpstoxReturnsError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(ExecutionException.class, service.getMasterContract("NSE", "RELIANCE", null)::get); @@ -650,13 +558,7 @@ void getMasterContract_failure_onNetworkError() throws IOException { ServiceGenerator.getInstance().rebuildWithUrl(server.url("/")); - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); try { service.getMasterContract("NSE", "RELIANCE", null).get(); @@ -669,15 +571,7 @@ void getMasterContract_failure_onNetworkError() throws IOException { @Test void getMasterContract_throwIAE_whenRequiredParametersAreMissing() { - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - - UserService service = new UserService(token, credentials); + UserService service = new UserService(upstoxAuthService); assertThrows(IllegalArgumentException.class, () -> service.getMasterContract(null, null, null), @@ -710,21 +604,8 @@ void getMasterContract_throwIAE_whenRequiredParametersAreMissing() { @Test void getMasterContract_throwNPE_whenServiceParametersAreMissing() { - - ApiCredentials credentials = - new ApiCredentials("secretApiKey", "secret-secret"); - - assertThrows(NullPointerException.class, - () -> new UserService(null, credentials), - "Null check missing for 'AccessToken' from UserService constructor"); - - AccessToken token = new AccessToken(); - token.setExpiresIn(86400L); - token.setType("Bearer"); - token.setToken("access_token_123456789"); - assertThrows(NullPointerException.class, - () -> new UserService(token, null), - "Null check missing for 'ApiCredentials' from UserService constructor"); + () -> new UserService(null), + "Null check missing for 'UpstoxAuthService' from UserService constructor"); } } \ No newline at end of file