Skip to content

Commit

Permalink
#137 : Created createDelegateExchangeAdapter method for mocking in tests
Browse files Browse the repository at this point in the history
Also enabled testGettingExchangeSellingFeeSuccessfully
  • Loading branch information
gazbert committed Mar 30, 2022
1 parent 9bb7599 commit 8180726
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class TryModeExchangeAdapter extends AbstractExchangeAdapter implements E
private BigDecimal counterCurrencyBalance;
private String delegateExchangeClassName;

private ExchangeAdapter delegateExchange;
private ExchangeAdapter delegateExchangeAdapter;

private OpenOrder currentOpenOrder;
private BigDecimal baseCurrencyBalance;
Expand All @@ -102,7 +102,7 @@ public MarketOrderBook getMarketOrders(String marketId)
throws ExchangeNetworkException, TradingApiException {
checkOpenOrderExecution(marketId);
LOG.info(() -> "Delegate 'getMarketOrders' to the configured delegation exchange adapter.");
return delegateExchange.getMarketOrders(marketId);
return delegateExchangeAdapter.getMarketOrders(marketId);
}

@Override
Expand Down Expand Up @@ -165,7 +165,7 @@ public BigDecimal getLatestMarketPrice(String marketId)
checkOpenOrderExecution(marketId);
LOG.info(
() -> "Delegate 'getLatestMarketPrice' to the configured delegation exchange adapter.");
return delegateExchange.getLatestMarketPrice(marketId);
return delegateExchangeAdapter.getLatestMarketPrice(marketId);
}

@Override
Expand All @@ -185,7 +185,7 @@ public BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId)
() ->
"Delegate 'getPercentageOfBuyOrderTakenForExchangeFee'"
+ "to the configured delegation exchange adapter.");
return delegateExchange.getPercentageOfBuyOrderTakenForExchangeFee(marketId);
return delegateExchangeAdapter.getPercentageOfBuyOrderTakenForExchangeFee(marketId);
}

@Override
Expand All @@ -195,14 +195,14 @@ public BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId)
() ->
"Delegate 'getPercentageOfSellOrderTakenForExchangeFee'"
+ "to the configured delegation exchange adapter.");
return delegateExchange.getPercentageOfSellOrderTakenForExchangeFee(marketId);
return delegateExchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(marketId);
}

@Override
public Ticker getTicker(String marketId) throws TradingApiException, ExchangeNetworkException {
checkOpenOrderExecution(marketId);
LOG.info(() -> "Delegate 'getTicker' to the configured delegation exchange adapter.");
return delegateExchange.getTicker(marketId);
return delegateExchangeAdapter.getTicker(marketId);
}

private void setOtherConfig(ExchangeConfig exchangeConfig) {
Expand Down Expand Up @@ -244,22 +244,26 @@ private void setOtherConfig(ExchangeConfig exchangeConfig) {
private void initializeAdapterDelegation(ExchangeConfig config) {
LOG.info(
() -> "Initializing the delegate exchange adapter '" + delegateExchangeClassName + "'...");
delegateExchangeAdapter = createDelegateExchangeAdapter();
delegateExchangeAdapter.init(config);
}

private ExchangeAdapter createDelegateExchangeAdapter() {
LOG.info(() -> "Creating the delegate exchange adapter '" + delegateExchangeClassName + "'...");
try {
final Class componentClass = Class.forName(delegateExchangeClassName);
final Object rawComponentObject = componentClass.getDeclaredConstructor().newInstance();
LOG.info(
() ->
"Successfully created the delegate exchange adapter class for: "
+ delegateExchangeClassName);
ExchangeAdapter loadedExchange = (ExchangeAdapter) rawComponentObject;
loadedExchange.init(config);
this.delegateExchange = loadedExchange;
return (ExchangeAdapter) rawComponentObject;
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| NoSuchMethodException
| InvocationTargetException e) {
final String errorMsg = "Failed to load and initialise delegate exchange adapter.";
final String errorMsg = "Failed to load and create delegate exchange adapter.";
LOG.error(errorMsg, e);
throw new IllegalStateException(errorMsg, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter {
private static final BigDecimal SELL_ORDER_QUANTITY = new BigDecimal("0.03");
private static final String ORDER_ID_TO_CANCEL = "80894263";

private static final BigDecimal PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE =
new BigDecimal("0.0025");
private static final String MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE =
"getPercentageOfSellOrderTakenForExchangeFee";
private static final String MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER =
"createDelegateExchangeAdapter";
private static final String MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD = "createRequestParamMap";
private static final String MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD =
"sendAuthenticatedRequestToExchange";
Expand Down Expand Up @@ -154,12 +160,9 @@ public void setupForEachTest() {
networkConfig = PowerMock.createMock(NetworkConfig.class);
expect(networkConfig.getConnectionTimeout()).andReturn(30);
expect(networkConfig.getNonFatalErrorCodes()).andReturn(nonFatalNetworkErrorCodes);

expect(networkConfig.getNonFatalErrorMessages()).andReturn(nonFatalNetworkErrorMessages);

final OtherConfig otherConfig = PowerMock.createMock(OtherConfig.class);

// Try-Mode adapter sim config
OtherConfig otherConfig = PowerMock.createMock(OtherConfig.class);
expect(otherConfig.getItem("simulatedBaseCurrency")).andReturn(BASE_CURRENCY).atLeastOnce();
expect(otherConfig.getItem("baseCurrencyStartingBalance"))
.andReturn(BASE_CURRENCY_STARTING_BALANCE)
Expand All @@ -174,10 +177,6 @@ public void setupForEachTest() {

expect(otherConfig.getItem("delegateAdapter")).andReturn(DELEGATE_ADAPTER).atLeastOnce();

// // Bitstamp config
// expect(otherConfig.getItem("buy-fee")).andReturn("0.25");
// expect(otherConfig.getItem("sell-fee")).andReturn("0.25");

authenticationConfig = PowerMock.createMock(AuthenticationConfig.class);
expect(authenticationConfig.getItem("client-id")).andReturn(CLIENT_ID);
expect(authenticationConfig.getItem("key")).andReturn(KEY);
Expand Down Expand Up @@ -858,29 +857,33 @@ public void testGettingExchangeBuyingFeeHandlesUnexpectedException() throws Exce
// Get Exchange Fees for Sell orders tests
// --------------------------------------------------------------------------

@Ignore("TODO: Enable test")
@Test
public void testGettingExchangeSellingFeeSuccessfully() throws Exception {
final byte[] encoded = Files.readAllBytes(Paths.get(BALANCE_JSON_RESPONSE));
final ExchangeHttpResponse exchangeResponse =
new ExchangeHttpResponse(200, "OK", new String(encoded, StandardCharsets.UTF_8));

final BitstampExchangeAdapter exchangeAdapter =
final BitstampExchangeAdapter delegateExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
BitstampExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD);
BitstampExchangeAdapter.class,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

PowerMock.expectPrivate(
exchangeAdapter,
MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD,
eq(BALANCE),
eq(null))
.andReturn(exchangeResponse);
delegateExchangeAdapter,
MOCKED_GET_PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE,
eq(MARKET_ID))
.andReturn(PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE);

final TryModeExchangeAdapter tryModeExchangeAdapter =
PowerMock.createPartialMockAndInvokeDefaultConstructor(
TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER);

PowerMock.expectPrivate(tryModeExchangeAdapter, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER)
.andReturn(delegateExchangeAdapter);

PowerMock.replayAll();
exchangeAdapter.init(exchangeConfig);

tryModeExchangeAdapter.init(exchangeConfig);

final BigDecimal sellPercentageFee =
exchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(MARKET_ID);
assertEquals(0, sellPercentageFee.compareTo(new BigDecimal("0.0025")));
tryModeExchangeAdapter.getPercentageOfSellOrderTakenForExchangeFee(MARKET_ID);
assertEquals(0, sellPercentageFee.compareTo(PERCENTAGE_OF_SELL_ORDER_TAKEN_FOR_EXCHANGE_FEE));

PowerMock.verifyAll();
}
Expand Down

0 comments on commit 8180726

Please sign in to comment.