Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/main/java/com/angelbroking/smartapi/SmartConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/angelbroking/smartapi/sample/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/angelbroking/smartapi/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";

}
96 changes: 87 additions & 9 deletions src/test/java/com/angelbroking/smartapi/SmartConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -74,16 +80,88 @@ 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");
exchangeTokens.put("NSE", nseTokens);
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();

Expand Down