ReportGenerator implements IReportGenerator { @Override public void orderEvent(OrderEvent event) { logger.info("Received order event: " + event); TradeOrder order = event.getOrder(); if (order.getCurrentStatus() == Status.FILLED) { TradeReferenceLine line = getTradeReferenceLine(order.getReference()); if (strategyName.equals(line.getStrategy())) { IRoundTrip roundTrip = roundTripMap.get(line.getCorrelationId()); if (roundTrip == null) { roundTrip = roundTripBuilder.buildRoundTrip(); roundTripMap.put(line.getCorrelationId(), roundTrip); } roundTrip.addTradeReference(order, line); if (roundTrip.isComplete()) { try { writeRoundTripToFile(roundTrip); deletePartial(roundTrip.getCorrelationId()); roundTripMap.remove(line.getCorrelationId()); } catch (IOException ex) { throw new IllegalStateException(ex); } } else { try { savePartial(line.getCorrelationId(), roundTrip); } catch (IOException ex) { throw new IllegalStateException(ex); } } } } } protected  ReportGenerator();  ReportGenerator(String strategyName, String dir, IRoundTripBuilder roundTripBuilder); @Override void loadPartialRoundTrips(); @Override void savePartial(String correlationId, IRoundTrip trip); @Override void deletePartial(String correlationId); @Override void orderEvent(OrderEvent event);  }
@Test public void testReportGeneratorEndToEnd() throws Exception { StockTicker longTicker = new StockTicker("ABC"); StockTicker shortTicker = new StockTicker("XYZ"); ZonedDateTime entryOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 18, 35, 0, ZoneId.systemDefault()); ZonedDateTime exitOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 19, 35, 0, ZoneId.systemDefault()); String directory = System.getProperty("java.io.tmpdir"); if (!directory.endsWith("/")) { directory += "/"; } Path reportPath = Paths.get(directory + "report.csv"); Files.deleteIfExists(reportPath); System.out.println("Creating directory at: " + directory); ReportGenerator generator = new ReportGenerator("EOD-Pair-Strategy", directory,pairRoundtripBuilder); TradeOrder longEntryOrder = new TradeOrder("123", longTicker, 100, TradeDirection.BUY); longEntryOrder.setFilledPrice(100.00); longEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Long*"); longEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); longEntryOrder.setOrderFilledTime(entryOrderTime); TradeOrder shortEntryOrder = new TradeOrder("234", shortTicker, 50, TradeDirection.SELL); shortEntryOrder.setFilledPrice(50.00); shortEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Short*"); shortEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortEntryOrder.setOrderFilledTime(entryOrderTime); generator.orderEvent(new OrderEvent(longEntryOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); assertFalse(Files.exists(reportPath)); TradeOrder longExitOrder = new TradeOrder("1234", longTicker, 100, TradeDirection.SELL); longExitOrder.setFilledPrice(105.00); longExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Long*"); longExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); longExitOrder.setOrderFilledTime(exitOrderTime); TradeOrder shortExitOrder = new TradeOrder("2345", shortTicker, 50, TradeDirection.BUY); shortExitOrder.setFilledPrice(40.00); shortExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Short*"); shortExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortExitOrder.setOrderFilledTime(exitOrderTime); generator.orderEvent(new OrderEvent(longExitOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); assertTrue(Files.exists(reportPath)); List<String> lines = Files.readAllLines(reportPath); assertEquals(1, lines.size()); String line = lines.get(0); String expected = "2016-03-25T06:18:35,Long,ABC,100,100.0,0,2016-03-25T06:19:35,105.0,0,Short,XYZ,50,50.0,0,40.0,0"; assertEquals(expected, line); generator.orderEvent(new OrderEvent(longEntryOrder, null)); generator.orderEvent(new OrderEvent(longExitOrder, null)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); lines = Files.readAllLines(reportPath); assertEquals(2, lines.size()); assertEquals(expected, lines.get(0)); assertEquals(expected, lines.get(1)); }