From 5804e3884266480196c752b4f2f1947503ee3fea Mon Sep 17 00:00:00 2001 From: gazbert Date: Sat, 9 Apr 2022 16:48:33 +0100 Subject: [PATCH] #137 : Enabled latest market price test --- .../exchanges/TestTryModeExchangeAdapter.java | 72 +++++-------------- 1 file changed, 16 insertions(+), 56 deletions(-) diff --git a/bxbot-exchanges/src/test/java/com/gazbert/bxbot/exchanges/TestTryModeExchangeAdapter.java b/bxbot-exchanges/src/test/java/com/gazbert/bxbot/exchanges/TestTryModeExchangeAdapter.java index dcb61388..52524613 100644 --- a/bxbot-exchanges/src/test/java/com/gazbert/bxbot/exchanges/TestTryModeExchangeAdapter.java +++ b/bxbot-exchanges/src/test/java/com/gazbert/bxbot/exchanges/TestTryModeExchangeAdapter.java @@ -88,15 +88,12 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter { private static final String OPEN_ORDERS_JSON_RESPONSE = "./src/test/exchange-data/bitstamp/open_orders.json"; - private static final String TICKER_JSON_RESPONSE = - "./src/test/exchange-data/bitstamp/ticker.json"; private static final String BUY_JSON_RESPONSE = "./src/test/exchange-data/bitstamp/buy.json"; private static final String SELL_JSON_RESPONSE = "./src/test/exchange-data/bitstamp/sell.json"; private static final String CANCEL_ORDER_JSON_RESPONSE = "./src/test/exchange-data/bitstamp/cancel_order.json"; private static final String OPEN_ORDERS = "open_orders/"; - private static final String TICKER = "ticker/"; private static final String BUY = "buy/"; private static final String SELL = "sell/"; private static final String CANCEL_ORDER = "cancel_order"; @@ -137,6 +134,8 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter { private static final BigDecimal ORDER_3_QUANTITY = new BigDecimal("0.03435344"); private static final BigDecimal ORDER_3_TOTAL = ORDER_3_PRICE.multiply(ORDER_3_QUANTITY); + private static final BigDecimal LATEST_MARKET_PRICE = new BigDecimal("20789.58"); + // -------------------------------------------------------------------------- // Mocked API Ops // -------------------------------------------------------------------------- @@ -151,12 +150,11 @@ public class TestTryModeExchangeAdapter extends AbstractExchangeAdapter { private static final String MOCKED_GET_TICKER_METHOD = "getTicker"; private static final String MOCKED_GET_BALANCE_INFO = "getBalanceInfo"; private static final String MOCKED_GET_MARKET_ORDERS = "getMarketOrders"; + private static final String MOCKED_GET_LATEST_MARKET_PRICE = "getLatestMarketPrice"; private static final String MOCKED_CREATE_REQUEST_PARAM_MAP_METHOD = "createRequestParamMap"; private static final String MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD = "sendAuthenticatedRequestToExchange"; - private static final String MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD = - "sendPublicRequestToExchange"; // -------------------------------------------------------------------------- // Delegate Exchange Adapter config @@ -653,68 +651,30 @@ public void testGettingYourOpenOrdersHandlesUnexpectedException() throws Excepti // Get Latest Market Price tests // -------------------------------------------------------------------------- - @Ignore("TODO: Enable test") @Test public void testGettingLatestMarketPriceSuccessfully() throws Exception { - final byte[] encoded = Files.readAllBytes(Paths.get(TICKER_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_PUBLIC_REQUEST_TO_EXCHANGE_METHOD); - PowerMock.expectPrivate( - exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID)) - .andReturn(exchangeResponse); + BitstampExchangeAdapter.class, MOCKED_GET_LATEST_MARKET_PRICE); - PowerMock.replayAll(); - exchangeAdapter.init(exchangeConfig); + PowerMock.expectPrivate(delegateExchangeAdapter, MOCKED_GET_LATEST_MARKET_PRICE, eq(MARKET_ID)) + .andReturn(LATEST_MARKET_PRICE); - final BigDecimal latestMarketPrice = - exchangeAdapter.getLatestMarketPrice(MARKET_ID).setScale(8, RoundingMode.HALF_UP); - assertEquals(0, latestMarketPrice.compareTo(new BigDecimal("230.33"))); - - PowerMock.verifyAll(); - } - - @Ignore("TODO: Enable test") - @Test(expected = ExchangeNetworkException.class) - public void testGettingLatestMarketPriceHandlesExchangeNetworkException() throws Exception { - final BitstampExchangeAdapter exchangeAdapter = + final TryModeExchangeAdapter tryModeExchangeAdapter = PowerMock.createPartialMockAndInvokeDefaultConstructor( - BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD); - PowerMock.expectPrivate( - exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID)) - .andThrow( - new ExchangeNetworkException( - "There are three basic types, Mr. Pizer: " - + "the Wills, the Won'ts, and the Can'ts. The Wills accomplish everything, " - + "the Won'ts oppose everything, and the Can'ts won't try anything.")); - - PowerMock.replayAll(); - exchangeAdapter.init(exchangeConfig); - - exchangeAdapter.getLatestMarketPrice(MARKET_ID); - PowerMock.verifyAll(); - } + TryModeExchangeAdapter.class, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER); - @Ignore("TODO: Enable test") - @Test(expected = TradingApiException.class) - public void testGettingLatestMarketPriceHandlesUnexpectedException() throws Exception { - final BitstampExchangeAdapter exchangeAdapter = - PowerMock.createPartialMockAndInvokeDefaultConstructor( - BitstampExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD); - PowerMock.expectPrivate( - exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, eq(TICKER + MARKET_ID)) - .andThrow( - new IllegalArgumentException( - "Every time I see one of those things I expect to spot some guy dressed " - + "in red with horns and a pitchfork.")); + PowerMock.expectPrivate(tryModeExchangeAdapter, MOCKED_CREATE_DELEGATE_EXCHANGE_ADAPTER) + .andReturn(delegateExchangeAdapter); PowerMock.replayAll(); - exchangeAdapter.init(exchangeConfig); + tryModeExchangeAdapter.init(exchangeConfig); + + final BigDecimal latestMarketPrice = + tryModeExchangeAdapter.getLatestMarketPrice(MARKET_ID).setScale(8, RoundingMode.HALF_UP); + assertEquals(0, latestMarketPrice.compareTo(LATEST_MARKET_PRICE)); - exchangeAdapter.getLatestMarketPrice(MARKET_ID); PowerMock.verifyAll(); }