PreOrderValidationService { public boolean checkInstrumentNotAlreadyTraded(TradeableInstrument<N> instrument) { Collection<K> accIds = this.tradeInfoService.findAllAccountsWithInstrumentTrades(instrument); if (accIds.size() > 0) { LOG.warn(String.format("Rejecting trade with instrument %s as trade already exists", instrument.getInstrument())); return false; } else { Collection<Order<N, M>> pendingOrders = this.orderInfoService.pendingOrdersForInstrument(instrument); if (!pendingOrders.isEmpty()) { LOG.warn(String.format("Pending order with instrument %s already exists", instrument.getInstrument())); return false; } return true; } } PreOrderValidationService(TradeInfoService<M, N, K> tradeInfoService,
			MovingAverageCalculationService<N> movingAverageCalculationService, BaseTradingConfig baseTradingConfig,
			OrderInfoService<M, N, K> orderInfoService); boolean isInSafeZone(TradingSignal signal, double price, TradeableInstrument<N> instrument); boolean checkInstrumentNotAlreadyTraded(TradeableInstrument<N> instrument); boolean checkLimitsForCcy(TradeableInstrument<N> instrument, TradingSignal signal);  }
@Test public void instrumentNotAlreadyTradedTest() { TradeInfoService<M, N, Long> tradeInfoService = mock(TradeInfoService.class); OrderInfoService<M, N, Long> orderInfoService = mock(OrderInfoService.class); PreOrderValidationService<M, N, Long> service = new PreOrderValidationService<M, N, Long>(tradeInfoService, null, null, orderInfoService); Collection<Long> accountIds = Lists.newArrayList(); accountIds.add(TradingTestConstants.accountId); accountIds.add(TradingTestConstants.accountId2); TradeableInstrument<N> gbpusd = new TradeableInstrument<N>("GBP_USD"); TradeableInstrument<N> nzdjpy = new TradeableInstrument<N>("NZD_JPY"); TradeableInstrument<N> audchf = new TradeableInstrument<N>("AUD_CHF"); when(tradeInfoService.findAllAccountsWithInstrumentTrades(gbpusd)).thenReturn(accountIds); assertFalse(service.checkInstrumentNotAlreadyTraded(gbpusd)); Collection<Long> emptyCollectionIds = Collections.emptyList(); when(tradeInfoService.findAllAccountsWithInstrumentTrades(nzdjpy)).thenReturn(emptyCollectionIds); Collection<Order<N, M>> pendingOrders = Lists.newArrayList(); when(orderInfoService.pendingOrdersForInstrument(nzdjpy)).thenReturn(pendingOrders); pendingOrders.add(mock(Order.class)); pendingOrders.add(mock(Order.class)); assertFalse(service.checkInstrumentNotAlreadyTraded(nzdjpy)); Collection<Order<N, M>> emptyCollectionOrders = Collections.emptyList(); when(tradeInfoService.findAllAccountsWithInstrumentTrades(audchf)).thenReturn(emptyCollectionIds); when(orderInfoService.pendingOrdersForInstrument(audchf)).thenReturn(emptyCollectionOrders); assertTrue(service.checkInstrumentNotAlreadyTraded(audchf)); }