OrderFilledEventHandler implements EventHandler<JSONObject, OrderEventPayLoad>,
		EmailContentGenerator<JSONObject> { @Override public EmailPayLoad generate(EventPayLoad<JSONObject> payLoad) { JSONObject jsonPayLoad = payLoad.getPayLoad(); TradeableInstrument<String> instrument = new TradeableInstrument<String>(jsonPayLoad .containsKey(OandaJsonKeys.instrument) ? jsonPayLoad.get(OandaJsonKeys.instrument).toString() : "N/A"); final String type = jsonPayLoad.get(OandaJsonKeys.type).toString(); final long accountId = (Long) jsonPayLoad.get(OandaJsonKeys.accountId); final double accountBalance = jsonPayLoad.containsKey(OandaJsonKeys.accountBalance) ? ((Number) jsonPayLoad .get(OandaJsonKeys.accountBalance)).doubleValue() : 0.0; final long orderId = (Long) jsonPayLoad.get(OandaJsonKeys.id); final String emailMsg = String.format( "Order event %s received on account %d. Order id=%d. Account balance after the event=%5.2f", type, accountId, orderId, accountBalance); final String subject = String.format("Order event %s for %s", type, instrument.getInstrument()); return new EmailPayLoad(subject, emailMsg); } OrderFilledEventHandler(TradeInfoService<Long, String, Long> tradeInfoService); @Override @Subscribe @AllowConcurrentEvents void handleEvent(OrderEventPayLoad payLoad); @Override EmailPayLoad generate(EventPayLoad<JSONObject> payLoad);  }
@Test public void generatePayLoad() { OrderFilledEventHandler eventHandler = new OrderFilledEventHandler(null); JSONObject jsonPayLoad = mock(JSONObject.class); EventPayLoad<JSONObject> payLoad = new OrderEventPayLoad(OrderEvents.ORDER_FILLED, jsonPayLoad); when(jsonPayLoad.containsKey(OandaJsonKeys.instrument)).thenReturn(true); when(jsonPayLoad.get(OandaJsonKeys.instrument)).thenReturn("GBP_CHF"); when(jsonPayLoad.get(OandaJsonKeys.type)).thenReturn(OrderEvents.ORDER_FILLED); when(jsonPayLoad.get(OandaJsonKeys.accountId)).thenReturn(OandaTestConstants.accountId); when(jsonPayLoad.containsKey(OandaJsonKeys.accountBalance)).thenReturn(true); when(jsonPayLoad.get(OandaJsonKeys.accountBalance)).thenReturn(178.95); when(jsonPayLoad.get(OandaJsonKeys.id)).thenReturn(1002L); EmailPayLoad emailPayLoad = eventHandler.generate(payLoad); assertEquals("Order event ORDER_FILLED for GBP_CHF", emailPayLoad.getSubject()); assertEquals( "Order event ORDER_FILLED received on account 123456. Order id=1002. Account balance after the event=178.95", emailPayLoad.getBody()); }