@Test(expected = ExchangeNetworkException.class) public void testGettingMarketOrdersHandlesExchangeNetworkException() throws Exception { final ItBitExchangeAdapter exchangeAdapter = PowerMock.createPartialMock( ItBitExchangeAdapter.class, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD); PowerMock.expectPrivate( exchangeAdapter, MOCKED_SEND_PUBLIC_REQUEST_TO_EXCHANGE_METHOD, ORDER_BOOK) .andThrow( new ExchangeNetworkException( "There is an idea of a Patrick Bateman; some kind of " + "abstraction. But there is no real me: only an entity, something illusory. " + "And though I can hide my cold gaze, and you can shake my hand and feel " + "flesh gripping yours and maybe you can even sense our lifestyles are " + "probably comparable... I simply am not there.")); PowerMock.replayAll(); exchangeAdapter.getMarketOrders(MARKET_ID); PowerMock.verifyAll(); }
@Override public MarketOrderBook getMarketOrders(String marketId) throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { response = sendPublicRequestToExchange(MARKETS_RESOURCE + "/" + marketId + "/order_book"); if (LOG.isDebugEnabled()) { LOG.debug("Market Orders response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitOrderBookWrapper orderBook = gson.fromJson(response.getPayload(), ItBitOrderBookWrapper.class); final List<MarketOrder> buyOrders = new ArrayList<>(); for (ItBitMarketOrder itBitBuyOrder : orderBook.bids) { final MarketOrder buyOrder = new MarketOrderImpl( OrderType.BUY, itBitBuyOrder.get(0), itBitBuyOrder.get(1), itBitBuyOrder.get(0).multiply(itBitBuyOrder.get(1))); buyOrders.add(buyOrder); } final List<MarketOrder> sellOrders = new ArrayList<>(); for (ItBitMarketOrder itBitSellOrder : orderBook.asks) { final MarketOrder sellOrder = new MarketOrderImpl( OrderType.SELL, itBitSellOrder.get(0), itBitSellOrder.get(1), itBitSellOrder.get(0).multiply(itBitSellOrder.get(1))); sellOrders.add(sellOrder); } return new MarketOrderBookImpl(marketId, sellOrders, buyOrders); } else { final String errorMsg = "Failed to get market order book from exchange. Details: " + response; LOG.error(errorMsg); throw new TradingApiException(errorMsg); } } catch (ExchangeNetworkException | TradingApiException e) { throw e; } catch (Exception e) { if (isExchangeUndergoingMaintenance(response) && keepAliveDuringMaintenance) { LOG.warn(() -> UNDER_MAINTENANCE_WARNING_MESSAGE); throw new ExchangeNetworkException(UNDER_MAINTENANCE_WARNING_MESSAGE); } final String unexpectedErrorMsg = UNEXPECTED_ERROR_MSG + (response == null ? NULL_RESPONSE : response); LOG.error(unexpectedErrorMsg, e); throw new TradingApiException(unexpectedErrorMsg, e); } }
ItBitExchangeAdapter extends AbstractExchangeAdapter implements ExchangeAdapter { @Override public MarketOrderBook getMarketOrders(String marketId) throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { response = sendPublicRequestToExchange(MARKETS_RESOURCE + "/" + marketId + "/order_book"); if (LOG.isDebugEnabled()) { LOG.debug("Market Orders response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitOrderBookWrapper orderBook = gson.fromJson(response.getPayload(), ItBitOrderBookWrapper.class); final List<MarketOrder> buyOrders = new ArrayList<>(); for (ItBitMarketOrder itBitBuyOrder : orderBook.bids) { final MarketOrder buyOrder = new MarketOrderImpl( OrderType.BUY, itBitBuyOrder.get(0), itBitBuyOrder.get(1), itBitBuyOrder.get(0).multiply(itBitBuyOrder.get(1))); buyOrders.add(buyOrder); } final List<MarketOrder> sellOrders = new ArrayList<>(); for (ItBitMarketOrder itBitSellOrder : orderBook.asks) { final MarketOrder sellOrder = new MarketOrderImpl( OrderType.SELL, itBitSellOrder.get(0), itBitSellOrder.get(1), itBitSellOrder.get(0).multiply(itBitSellOrder.get(1))); sellOrders.add(sellOrder); } return new MarketOrderBookImpl(marketId, sellOrders, buyOrders); } else { final String errorMsg = "Failed to get market order book from exchange. Details: " + response; LOG.error(errorMsg); throw new TradingApiException(errorMsg); } } catch (ExchangeNetworkException | TradingApiException e) { throw e; } catch (Exception e) { if (isExchangeUndergoingMaintenance(response) && keepAliveDuringMaintenance) { LOG.warn(() -> UNDER_MAINTENANCE_WARNING_MESSAGE); throw new ExchangeNetworkException(UNDER_MAINTENANCE_WARNING_MESSAGE); } final String unexpectedErrorMsg = UNEXPECTED_ERROR_MSG + (response == null ? NULL_RESPONSE : response); LOG.error(unexpectedErrorMsg, e); throw new TradingApiException(unexpectedErrorMsg, e); } } }
ItBitExchangeAdapter extends AbstractExchangeAdapter implements ExchangeAdapter { @Override public MarketOrderBook getMarketOrders(String marketId) throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { response = sendPublicRequestToExchange(MARKETS_RESOURCE + "/" + marketId + "/order_book"); if (LOG.isDebugEnabled()) { LOG.debug("Market Orders response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitOrderBookWrapper orderBook = gson.fromJson(response.getPayload(), ItBitOrderBookWrapper.class); final List<MarketOrder> buyOrders = new ArrayList<>(); for (ItBitMarketOrder itBitBuyOrder : orderBook.bids) { final MarketOrder buyOrder = new MarketOrderImpl( OrderType.BUY, itBitBuyOrder.get(0), itBitBuyOrder.get(1), itBitBuyOrder.get(0).multiply(itBitBuyOrder.get(1))); buyOrders.add(buyOrder); } final List<MarketOrder> sellOrders = new ArrayList<>(); for (ItBitMarketOrder itBitSellOrder : orderBook.asks) { final MarketOrder sellOrder = new MarketOrderImpl( OrderType.SELL, itBitSellOrder.get(0), itBitSellOrder.get(1), itBitSellOrder.get(0).multiply(itBitSellOrder.get(1))); sellOrders.add(sellOrder); } return new MarketOrderBookImpl(marketId, sellOrders, buyOrders); } else { final String errorMsg = "Failed to get market order book from exchange. Details: " + response; LOG.error(errorMsg); throw new TradingApiException(errorMsg); } } catch (ExchangeNetworkException | TradingApiException e) { throw e; } catch (Exception e) { if (isExchangeUndergoingMaintenance(response) && keepAliveDuringMaintenance) { LOG.warn(() -> UNDER_MAINTENANCE_WARNING_MESSAGE); throw new ExchangeNetworkException(UNDER_MAINTENANCE_WARNING_MESSAGE); } final String unexpectedErrorMsg = UNEXPECTED_ERROR_MSG + (response == null ? NULL_RESPONSE : response); LOG.error(unexpectedErrorMsg, e); throw new TradingApiException(unexpectedErrorMsg, e); } }  }
ItBitExchangeAdapter extends AbstractExchangeAdapter implements ExchangeAdapter { @Override public MarketOrderBook getMarketOrders(String marketId) throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { response = sendPublicRequestToExchange(MARKETS_RESOURCE + "/" + marketId + "/order_book"); if (LOG.isDebugEnabled()) { LOG.debug("Market Orders response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitOrderBookWrapper orderBook = gson.fromJson(response.getPayload(), ItBitOrderBookWrapper.class); final List<MarketOrder> buyOrders = new ArrayList<>(); for (ItBitMarketOrder itBitBuyOrder : orderBook.bids) { final MarketOrder buyOrder = new MarketOrderImpl( OrderType.BUY, itBitBuyOrder.get(0), itBitBuyOrder.get(1), itBitBuyOrder.get(0).multiply(itBitBuyOrder.get(1))); buyOrders.add(buyOrder); } final List<MarketOrder> sellOrders = new ArrayList<>(); for (ItBitMarketOrder itBitSellOrder : orderBook.asks) { final MarketOrder sellOrder = new MarketOrderImpl( OrderType.SELL, itBitSellOrder.get(0), itBitSellOrder.get(1), itBitSellOrder.get(0).multiply(itBitSellOrder.get(1))); sellOrders.add(sellOrder); } return new MarketOrderBookImpl(marketId, sellOrders, buyOrders); } else { final String errorMsg = "Failed to get market order book from exchange. Details: " + response; LOG.error(errorMsg); throw new TradingApiException(errorMsg); } } catch (ExchangeNetworkException | TradingApiException e) { throw e; } catch (Exception e) { if (isExchangeUndergoingMaintenance(response) && keepAliveDuringMaintenance) { LOG.warn(() -> UNDER_MAINTENANCE_WARNING_MESSAGE); throw new ExchangeNetworkException(UNDER_MAINTENANCE_WARNING_MESSAGE); } final String unexpectedErrorMsg = UNEXPECTED_ERROR_MSG + (response == null ? NULL_RESPONSE : response); LOG.error(unexpectedErrorMsg, e); throw new TradingApiException(unexpectedErrorMsg, e); } }  @Override void init(ExchangeConfig config); @Override String createOrder(
      String marketId, OrderType orderType, BigDecimal quantity, BigDecimal price); @Override boolean cancelOrder(String orderId, String marketIdNotNeeded); @Override List<OpenOrder> getYourOpenOrders(String marketId); @Override MarketOrderBook getMarketOrders(String marketId); @Override BigDecimal getLatestMarketPrice(String marketId); @Override BalanceInfo getBalanceInfo(); @Override BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId); @Override BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId); @Override String getImplName(); @Override Ticker getTicker(String marketId); }
ItBitExchangeAdapter extends AbstractExchangeAdapter implements ExchangeAdapter { @Override public MarketOrderBook getMarketOrders(String marketId) throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { response = sendPublicRequestToExchange(MARKETS_RESOURCE + "/" + marketId + "/order_book"); if (LOG.isDebugEnabled()) { LOG.debug("Market Orders response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitOrderBookWrapper orderBook = gson.fromJson(response.getPayload(), ItBitOrderBookWrapper.class); final List<MarketOrder> buyOrders = new ArrayList<>(); for (ItBitMarketOrder itBitBuyOrder : orderBook.bids) { final MarketOrder buyOrder = new MarketOrderImpl( OrderType.BUY, itBitBuyOrder.get(0), itBitBuyOrder.get(1), itBitBuyOrder.get(0).multiply(itBitBuyOrder.get(1))); buyOrders.add(buyOrder); } final List<MarketOrder> sellOrders = new ArrayList<>(); for (ItBitMarketOrder itBitSellOrder : orderBook.asks) { final MarketOrder sellOrder = new MarketOrderImpl( OrderType.SELL, itBitSellOrder.get(0), itBitSellOrder.get(1), itBitSellOrder.get(0).multiply(itBitSellOrder.get(1))); sellOrders.add(sellOrder); } return new MarketOrderBookImpl(marketId, sellOrders, buyOrders); } else { final String errorMsg = "Failed to get market order book from exchange. Details: " + response; LOG.error(errorMsg); throw new TradingApiException(errorMsg); } } catch (ExchangeNetworkException | TradingApiException e) { throw e; } catch (Exception e) { if (isExchangeUndergoingMaintenance(response) && keepAliveDuringMaintenance) { LOG.warn(() -> UNDER_MAINTENANCE_WARNING_MESSAGE); throw new ExchangeNetworkException(UNDER_MAINTENANCE_WARNING_MESSAGE); } final String unexpectedErrorMsg = UNEXPECTED_ERROR_MSG + (response == null ? NULL_RESPONSE : response); LOG.error(unexpectedErrorMsg, e); throw new TradingApiException(unexpectedErrorMsg, e); } }  @Override void init(ExchangeConfig config); @Override String createOrder(
      String marketId, OrderType orderType, BigDecimal quantity, BigDecimal price); @Override boolean cancelOrder(String orderId, String marketIdNotNeeded); @Override List<OpenOrder> getYourOpenOrders(String marketId); @Override MarketOrderBook getMarketOrders(String marketId); @Override BigDecimal getLatestMarketPrice(String marketId); @Override BalanceInfo getBalanceInfo(); @Override BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId); @Override BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId); @Override String getImplName(); @Override Ticker getTicker(String marketId);  }