@Test(expected = ExchangeNetworkException.class) public void testGettingBalanceInfoHandlesExchangeNetworkException() throws Exception { final ItBitExchangeAdapter exchangeAdapter = PowerMock.createPartialMock( ItBitExchangeAdapter.class, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD); PowerMock.expectPrivate( exchangeAdapter, MOCKED_SEND_AUTHENTICATED_REQUEST_TO_EXCHANGE_METHOD, eq("GET"), eq(WALLETS), anyObject(Map.class)) .andThrow(new ExchangeNetworkException("You were in a 4g inverted dive with a MiG28?")); PowerMock.replayAll(); exchangeAdapter.getBalanceInfo(); PowerMock.verifyAll(); }
@Override public BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { final Map<String, String> params = createRequestParamMap(); params.put(USER_ID_PROPERTY_NAME, userId); response = sendAuthenticatedRequestToExchange("GET", WALLETS_RESOURCE, params); if (LOG.isDebugEnabled()) { LOG.debug("Balance Info response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitWallet[] itBitWallets = gson.fromJson(response.getPayload(), ItBitWallet[].class); return adaptItBitBalanceInfo(itBitWallets); } else { final String errorMsg = "Failed to get your wallet balance info 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 BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { final Map<String, String> params = createRequestParamMap(); params.put(USER_ID_PROPERTY_NAME, userId); response = sendAuthenticatedRequestToExchange("GET", WALLETS_RESOURCE, params); if (LOG.isDebugEnabled()) { LOG.debug("Balance Info response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitWallet[] itBitWallets = gson.fromJson(response.getPayload(), ItBitWallet[].class); return adaptItBitBalanceInfo(itBitWallets); } else { final String errorMsg = "Failed to get your wallet balance info 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 BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { final Map<String, String> params = createRequestParamMap(); params.put(USER_ID_PROPERTY_NAME, userId); response = sendAuthenticatedRequestToExchange("GET", WALLETS_RESOURCE, params); if (LOG.isDebugEnabled()) { LOG.debug("Balance Info response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitWallet[] itBitWallets = gson.fromJson(response.getPayload(), ItBitWallet[].class); return adaptItBitBalanceInfo(itBitWallets); } else { final String errorMsg = "Failed to get your wallet balance info 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 BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { final Map<String, String> params = createRequestParamMap(); params.put(USER_ID_PROPERTY_NAME, userId); response = sendAuthenticatedRequestToExchange("GET", WALLETS_RESOURCE, params); if (LOG.isDebugEnabled()) { LOG.debug("Balance Info response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitWallet[] itBitWallets = gson.fromJson(response.getPayload(), ItBitWallet[].class); return adaptItBitBalanceInfo(itBitWallets); } else { final String errorMsg = "Failed to get your wallet balance info 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 BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException { ExchangeHttpResponse response = null; try { final Map<String, String> params = createRequestParamMap(); params.put(USER_ID_PROPERTY_NAME, userId); response = sendAuthenticatedRequestToExchange("GET", WALLETS_RESOURCE, params); if (LOG.isDebugEnabled()) { LOG.debug("Balance Info response: {}", response); } if (response.getStatusCode() == HttpURLConnection.HTTP_OK) { final ItBitWallet[] itBitWallets = gson.fromJson(response.getPayload(), ItBitWallet[].class); return adaptItBitBalanceInfo(itBitWallets); } else { final String errorMsg = "Failed to get your wallet balance info 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);  }