Permalink
Browse files

Fixed removing finished orders from the order book

  • Loading branch information...
1 parent 1a35231 commit 04bf34994622b75cec9a203d61dbc9ba86ca9d97 @jettro jettro committed Jan 27, 2012
View
14 ...src/main/java/org/axonframework/samples/trader/app/query/orderbook/OrderBookListener.java
@@ -86,21 +86,30 @@ public void handleTradeExecuted(TradeExecutedEvent event) {
tradeExecutedRepository.save(tradeExecutedEntry);
// TODO find a better solution or maybe pull them apart
+ OrderEntry foundBuyOrder = null;
for (OrderEntry order : orderBookEntry.buyOrders()) {
if (order.getIdentifier().equals(buyOrderId.asString())) {
long itemsRemaining = order.getItemsRemaining();
order.setItemsRemaining(itemsRemaining - event.getTradeCount());
+ foundBuyOrder = order;
break;
}
}
-
+ if (null != foundBuyOrder && foundBuyOrder.getItemsRemaining() == 0) {
+ orderBookEntry.buyOrders().remove(foundBuyOrder);
+ }
+ OrderEntry foundSellOrder = null;
for (OrderEntry order : orderBookEntry.sellOrders()) {
if (order.getIdentifier().equals(sellOrderId.asString())) {
long itemsRemaining = order.getItemsRemaining();
order.setItemsRemaining(itemsRemaining - event.getTradeCount());
+ foundSellOrder = order;
break;
}
}
+ if (null != foundSellOrder && foundSellOrder.getItemsRemaining() == 0) {
+ orderBookEntry.sellOrders().remove(foundSellOrder);
+ }
orderBookRepository.save(orderBookEntry);
}
@@ -116,16 +125,19 @@ private OrderEntry createPlacedOrder(AbstractOrderPlacedEvent event, String type
return entry;
}
+ @SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public void setOrderBookRepository(OrderBookQueryRepository orderBookRepository) {
this.orderBookRepository = orderBookRepository;
}
+ @SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public void setCompanyRepository(CompanyQueryRepository companyRepository) {
this.companyRepository = companyRepository;
}
+ @SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
public void setTradeExecutedRepository(TradeExecutedQueryRepository tradeExecutedRepository) {
this.tradeExecutedRepository = tradeExecutedRepository;
View
12 ...rg/axonframework/samples/trader/app/query/orderbook/OrderBookListenerIntegrationTest.java
@@ -42,6 +42,7 @@
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:META-INF/spring/persistence-infrastructure-context.xml"})
+@SuppressWarnings("SpringJavaAutowiringInspection")
public class OrderBookListenerIntegrationTest {
private OrderBookListener orderBookListener;
@@ -51,6 +52,7 @@
@Autowired
private TradeExecutedQueryRepository tradeExecutedRepository;
+
@Autowired
private CompanyQueryRepository companyRepository;
@@ -133,7 +135,7 @@ public void testHandleTradeExecuted() throws Exception {
AggregateIdentifier userIdentifier = new UUIDAggregateIdentifier();
AggregateIdentifier sellOrderId = new UUIDAggregateIdentifier();
AggregateIdentifier sellTransactionId = new UUIDAggregateIdentifier();
- SellOrderPlacedEvent sellOrderPlacedEvent = new SellOrderPlacedEvent(sellOrderId, sellTransactionId, 300, 100, userIdentifier);
+ SellOrderPlacedEvent sellOrderPlacedEvent = new SellOrderPlacedEvent(sellOrderId, sellTransactionId, 400, 100, userIdentifier);
DomainEventUtils.setAggregateIdentifier(sellOrderPlacedEvent, new UUIDAggregateIdentifier(orderBook.getIdentifier()));
orderBookListener.handleSellOrderPlaced(sellOrderPlacedEvent);
@@ -163,6 +165,14 @@ public void testHandleTradeExecuted() throws Exception {
assertEquals("Test Company", tradeExecutedEntry.getCompanyName());
assertEquals(300, tradeExecutedEntry.getTradeCount());
assertEquals(125, tradeExecutedEntry.getTradePrice());
+
+ all = orderBookRepository.findAll();
+ orderBookEntry = all.iterator().next();
+ assertNotNull("The first item of the iterator for orderbooks should not be null", orderBookEntry);
+ assertEquals("Test Company", orderBookEntry.getCompanyName());
+ assertEquals(1, orderBookEntry.sellOrders().size());
+ assertEquals(0, orderBookEntry.buyOrders().size());
+
}

0 comments on commit 04bf349

Please sign in to comment.