OandaPositionManagementProvider implements PositionManagementProvider<String, Long> { @Override public boolean closePosition(Long accountId, TradeableInstrument<String> instrument) { CloseableHttpClient httpClient = getHttpClient(); try { HttpDelete httpDelete = new HttpDelete(getPositionForInstrumentUrl(accountId, instrument)); httpDelete.setHeader(authHeader); LOG.info(TradingUtils.executingRequestMsg(httpDelete)); HttpResponse resp = httpClient.execute(httpDelete); int httpCode = resp.getStatusLine().getStatusCode(); if (httpCode == HttpStatus.SC_OK) { LOG.info(String.format("Position successfully closed for instrument %s and account %d", instrument .getInstrument(), accountId)); return true; } else { LOG.warn(String.format( "Position for instrument %s and account %d not closed. Encountered error code=%d", instrument .getInstrument(), accountId, httpCode)); } } catch (Exception ex) { LOG.error(String.format("error encountered whilst closing position for instrument %s and account %d", instrument.getInstrument(), accountId), ex); } finally { TradingUtils.closeSilently(httpClient); } return false; } OandaPositionManagementProvider(String url, String accessToken); @Override Collection<Position<String>> getPositionsForAccount(Long accountId); @Override boolean closePosition(Long accountId, TradeableInstrument<String> instrument); @Override Position<String> getPositionForInstrument(Long accountId, TradeableInstrument<String> instrument);  }
@Test public void closePositionTest() throws Exception { OandaPositionManagementProvider service = new OandaPositionManagementProvider(OandaTestConstants.url, OandaTestConstants.accessToken); OandaPositionManagementProvider spy = doMockStuff("src/test/resources/positionForInstrument.txt", service); boolean success = spy.closePosition(OandaTestConstants.accountId, new TradeableInstrument<String>("AUD_NZD")); assertTrue(success); verify(spy.getHttpClient(), times(1)).execute(any(HttpDelete.class)); }