From fc857eb8990246b43e086f5e2b9640aa7cd4f3b0 Mon Sep 17 00:00:00 2001 From: Mujtaba Syed Date: Tue, 12 Sep 2023 18:25:26 +0530 Subject: [PATCH] Pull request SSS-35 Observations --- .../angelbroking/smartapi/SmartConnect.java | 17 +++- .../smartapi/sample/Examples.java | 9 +- .../smartapi/utils/Constants.java | 3 + .../smartapi/SmartConnectTest.java | 96 +++++++++++++++++-- 4 files changed, 111 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/angelbroking/smartapi/SmartConnect.java b/src/main/java/com/angelbroking/smartapi/SmartConnect.java index 2adf516..0046024 100644 --- a/src/main/java/com/angelbroking/smartapi/SmartConnect.java +++ b/src/main/java/com/angelbroking/smartapi/SmartConnect.java @@ -9,6 +9,7 @@ import com.angelbroking.smartapi.models.OrderParams; import com.angelbroking.smartapi.models.TokenSet; import com.angelbroking.smartapi.models.User; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.json.JSONArray; import org.json.JSONException; @@ -18,6 +19,9 @@ import java.net.Proxy; import java.util.List; +import static com.angelbroking.smartapi.utils.Constants.*; + +@Slf4j public class SmartConnect { public static SessionExpiryHook sessionExpiryHook = null; public static boolean ENABLE_LOGGING = false; @@ -709,14 +713,19 @@ public String candleData(JSONObject params) { * @param params is market data params. * @return returns the details of market data. */ - public JSONObject marketData(JSONObject params) { + public JSONObject marketData(JSONObject params) throws SmartAPIException { try{ String url = routes.get("api.market.data"); JSONObject response = smartAPIRequestHandler.postRequest(this.apiKey, url, params, accessToken); return response.getJSONObject("data"); - }catch (Exception | SmartAPIException e) { - System.out.println(e.getMessage()); - return null; + } + catch (SmartAPIException e) { + log.error("{} while marketData {}", SMART_API_EXCEPTION_OCCURRED, e.toString()); + throw new SmartAPIException(String.format("%s in marketData %s", SMART_API_EXCEPTION_ERROR_MSG, e)); + } + catch (Exception ex) { + log.error("{} Exception {}", EXCEPTION, ex.toString()); + throw new SmartAPIException(String.format("%s Exception %s", EXCEPTION, ex)); } } diff --git a/src/main/java/com/angelbroking/smartapi/sample/Examples.java b/src/main/java/com/angelbroking/smartapi/sample/Examples.java index a45a6c5..de8d152 100644 --- a/src/main/java/com/angelbroking/smartapi/sample/Examples.java +++ b/src/main/java/com/angelbroking/smartapi/sample/Examples.java @@ -273,7 +273,14 @@ public void getCandleData(SmartConnect smartConnect) throws SmartAPIException, I } /** Market Data */ - public void getMarketData(SmartConnect smartConnect) { + /** + * @param smartConnect describes functionalities for interacting with the Angel Broking Smart API to manage trading and market-related operations. + * This describes all the modes of market data + * e.g., payload.put("mode", "FULL"); + * payload.put("mode", "LTP"); + * payload.put("mode", "OHLC"); + */ + public void getMarketData(SmartConnect smartConnect) throws SmartAPIException, IOException { // Create the payload object JSONObject payload = new JSONObject(); payload.put("mode", "FULL"); diff --git a/src/main/java/com/angelbroking/smartapi/utils/Constants.java b/src/main/java/com/angelbroking/smartapi/utils/Constants.java index 6d3cb33..a7c0055 100644 --- a/src/main/java/com/angelbroking/smartapi/utils/Constants.java +++ b/src/main/java/com/angelbroking/smartapi/utils/Constants.java @@ -77,5 +77,8 @@ public class Constants { public static final int PRICE_OFFSET = 10; public static final int NUMBER_OF_ORDERS_OFFSET = 18; public static final int PRICE_CONVERSION_FACTOR = 100; + public static final String SMART_API_EXCEPTION_ERROR_MSG = "The operation failed to execute because of a SmartAPIException error"; + public static final String SMART_API_EXCEPTION_OCCURRED = "SmartAPIException occurred "; + public static final String EXCEPTION = "An error occurred due to "; } diff --git a/src/test/java/com/angelbroking/smartapi/SmartConnectTest.java b/src/test/java/com/angelbroking/smartapi/SmartConnectTest.java index 3d33505..75811f3 100644 --- a/src/test/java/com/angelbroking/smartapi/SmartConnectTest.java +++ b/src/test/java/com/angelbroking/smartapi/SmartConnectTest.java @@ -13,6 +13,7 @@ import java.io.IOException; +import static com.angelbroking.smartapi.utils.Constants.*; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -39,29 +40,34 @@ public void setup() { accessToken = System.getenv("accessToken"); } - +// Testing market data success for Full payload @Test public void testMarketData_Success() throws SmartAPIException, IOException { String url = routes.get("api.market.data"); - JSONObject params = getMarketDataRequest(); + JSONObject params = getMarketDataRequest("FULL"); when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))).thenReturn(createMarketDataResponse()); try{ JSONObject response = smartAPIRequestHandler.postRequest(this.apiKey, url, params, this.accessToken); response.getJSONObject("data"); - }catch (Exception | SmartAPIException e) { - e.printStackTrace(); - System.out.println(e.getMessage()); + } + catch (SmartAPIException e) { + log.error("{} while test Market Data Failure {}", SMART_API_EXCEPTION_OCCURRED, e.toString()); + throw new SmartAPIException(String.format("%s in test Market Data Failure %s", SMART_API_EXCEPTION_ERROR_MSG, e)); + } + catch (Exception ex) { + log.error("{} Exception {}", EXCEPTION, ex.toString()); + throw new SmartAPIException(String.format("%s Exception %s", EXCEPTION, ex)); } } - +// Testing market data failure for Full payload @Test public void testMarketData_Failure() throws SmartAPIException, IOException { // Stub the postRequest method String url = routes.get("api.market.data"); - JSONObject params = getMarketDataRequest(); + JSONObject params = getMarketDataRequest("FULL"); when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))) .thenThrow(new SmartAPIException("API request failed")); try { @@ -74,9 +80,9 @@ public void testMarketData_Failure() throws SmartAPIException, IOException { // If the exception is not thrown, the following assertion will fail } - private JSONObject getMarketDataRequest() { + private JSONObject getMarketDataRequest(String mode) { JSONObject payload = new JSONObject(); - payload.put("mode", "FULL"); + payload.put("mode", mode); JSONObject exchangeTokens = new JSONObject(); JSONArray nseTokens = new JSONArray(); nseTokens.put("3045"); @@ -84,6 +90,78 @@ private JSONObject getMarketDataRequest() { payload.put("exchangeTokens", exchangeTokens); return payload; } + // Testing market data success for LTP payload + @Test + public void testMarketDataLTP_Success() throws SmartAPIException, IOException { + String url = routes.get("api.market.data"); + JSONObject params = getMarketDataRequest("LTP"); + when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))).thenReturn(createMarketDataResponse()); + try{ + JSONObject response = smartAPIRequestHandler.postRequest(this.apiKey, url, params, this.accessToken); + response.getJSONObject("data"); + } + catch (SmartAPIException e) { + log.error("{} while test Market Data Failure {}", SMART_API_EXCEPTION_OCCURRED, e.toString()); + throw new SmartAPIException(String.format("%s in test Market Data Failure %s", SMART_API_EXCEPTION_ERROR_MSG, e)); + } + catch (Exception ex) { + log.error("{} Exception {}", EXCEPTION, ex.toString()); + throw new SmartAPIException(String.format("%s Exception %s", EXCEPTION, ex)); + } + } + // Testing market data failure for LTP payload + @Test + public void testMarketDataLTP_Failure() throws SmartAPIException, IOException { + // Stub the postRequest method + String url = routes.get("api.market.data"); + JSONObject params = getMarketDataRequest("LTP"); + when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))) + .thenThrow(new SmartAPIException("API request failed")); + try { + JSONObject response = smartAPIRequestHandler.postRequest(apiKey, url, params, accessToken); + // Perform assertions or further actions with the response + response.getJSONObject("data"); + } catch (Exception | SmartAPIException e) { + System.out.println(e.getMessage()); + } + // If the exception is not thrown, the following assertion will fail + } + // Testing market data success for OHLC payload + @Test + public void testMarketDataOHLC_Success() throws SmartAPIException, IOException { + String url = routes.get("api.market.data"); + JSONObject params = getMarketDataRequest("OHLC"); + when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))).thenReturn(createMarketDataResponse()); + try{ + JSONObject response = smartAPIRequestHandler.postRequest(this.apiKey, url, params, this.accessToken); + response.getJSONObject("data"); + } + catch (SmartAPIException e) { + log.error("{} while test Market Data Failure {}", SMART_API_EXCEPTION_OCCURRED, e.toString()); + throw new SmartAPIException(String.format("%s in test Market Data Failure %s", SMART_API_EXCEPTION_ERROR_MSG, e)); + } + catch (Exception ex) { + log.error("{} Exception {}", EXCEPTION, ex.toString()); + throw new SmartAPIException(String.format("%s Exception %s", EXCEPTION, ex)); + } + } + // Testing market data failure for OHLC payload + @Test + public void testMarketDataOHLC_Failure() throws SmartAPIException, IOException { + // Stub the postRequest method + String url = routes.get("api.market.data"); + JSONObject params = getMarketDataRequest("OHLC"); + when(smartAPIRequestHandler.postRequest(eq(this.apiKey), eq(url), eq(params), eq(this.accessToken))) + .thenThrow(new SmartAPIException("API request failed")); + try { + JSONObject response = smartAPIRequestHandler.postRequest(apiKey, url, params, accessToken); + // Perform assertions or further actions with the response + response.getJSONObject("data"); + } catch (Exception | SmartAPIException e) { + System.out.println(e.getMessage()); + } + // If the exception is not thrown, the following assertion will fail + } private static JSONObject createMarketDataResponse() { JSONObject jsonObject = new JSONObject();