IBRealTimeBarEngine implements IRealtimeBarEngine { @Override public void subscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener) { if( request.getTimeUnit() != BarData.LengthUnit.MINUTE ) { if( request.getTimeUnit() == BarData.LengthUnit.HOUR ) { if( request.getTimeInterval() != 1 ) { throw new IllegalStateException( "Only 1 hour and 1-60 minute bars are supported by IB realtime bar engine" ); } } else { throw new IllegalStateException( "Only 1 hour and 1-60 minute bars are supported by IB realtime bar engine" ); } } else { if( request.getTimeInterval() > 60 || request.getTimeInterval() < 1 ) { throw new IllegalStateException( "Only 1 hour and 1-60 minute bars are supported by IB realtime bar engine" ); } } Ticker ticker = request.getTicker(); IBarBuilder builder = barMap.get(request); if (builder == null) { builder = buildBarBuilder(schedulerFactory, request, historicalDataProvider); quoteEngine.subscribeLevel1(ticker, builder); barMap.put(request, builder); } builder.addBarListener(listener); } IBRealTimeBarEngine(IQuoteEngine quoteEngine, IHistoricalDataProvider historicalDataProvider); @Override boolean isConnected(); @Override void subscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener); @Override void unsubscribeRealtimeBars(RealtimeBarRequest request, RealtimeBarListener listener);  }
@Test public void testSubscribeRealtimeBars_ZeroMinuteRequest() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 0; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.testBarBuilder = mockBarBuilder; final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; try { engine.subscribeRealtimeBars(request, listener); fail(); } catch( IllegalStateException ex ) { } mockery.assertIsSatisfied(); }
@Test public void testSubscribeRealtimeBars_61MinuteRequest() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 61; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.testBarBuilder = mockBarBuilder; final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; try { engine.subscribeRealtimeBars(request, listener); fail(); } catch( IllegalStateException ex ) { } mockery.assertIsSatisfied(); }
@Test public void testSubscribeRealtimeBars() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 1; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; mockery.checking( new Expectations(){{ one(mockBarBuilder).addBarListener( listener ); }} ); engine.barMap.put(request, mockBarBuilder); engine.subscribeRealtimeBars(request, listener); mockery.assertIsSatisfied(); }
@Test public void testSubscribeRealtimeBars_OneHourRequest() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 1; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.HOUR); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; mockery.checking( new Expectations(){{ one(mockBarBuilder).addBarListener( listener ); }} ); engine.barMap.put(request, mockBarBuilder); engine.subscribeRealtimeBars(request, listener); mockery.assertIsSatisfied(); }
@Test public void testSubscribeRealtimeBars_NoBuilderExists() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 1; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.MINUTE); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.testBarBuilder = mockBarBuilder; final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; mockery.checking( new Expectations(){{ one(mockQuoteEngine).subscribeLevel1(ticker, mockBarBuilder); one(mockBarBuilder).addBarListener( listener ); }} ); engine.subscribeRealtimeBars(request, listener); assertEquals( mockBarBuilder, engine.barMap.get(request)); mockery.assertIsSatisfied(); }
@Test public void testSubscribeRealtimeBars_NonMinuteRequest() { final StockTicker ticker = new StockTicker("qqq"); int timeInterval = 2; final IQuoteEngine mockQuoteEngine = mockery.mock(IQuoteEngine.class); final IBarBuilder mockBarBuilder = mockery.mock( IBarBuilder.class ); IHistoricalDataProvider mockHistoricalData = mockery.mock(IHistoricalDataProvider.class); RealtimeBarRequest request = new RealtimeBarRequest(1, ticker, timeInterval, LengthUnit.HOUR); IBRealTimeBarEngine engine = new IBRealTimeBarEngine(mockQuoteEngine, mockHistoricalData); engine.testBarBuilder = mockBarBuilder; final RealtimeBarListener listener = new RealtimeBarListener() { public void realtimeBarReceived(int requestId, Ticker ticker, BarData bar) { } }; try { engine.subscribeRealtimeBars(request, listener); fail(); } catch( IllegalStateException ex ) { } mockery.assertIsSatisfied(); }