OrderPayLoadToTweet implements EventPayLoadToTweet<JSONObject, OrderEventPayLoad> { @Override public String toTweet(OrderEventPayLoad payLoad) { if (!orderEventsSupported.contains(payLoad.getEvent())) { return null; } final JSONObject jsonPayLoad = payLoad.getPayLoad(); final String instrument = jsonPayLoad.get(OandaJsonKeys.instrument).toString(); final String instrumentAsHashtag = OandaUtils.oandaToHashTagCcy(instrument); final long tradeUnits = (Long) jsonPayLoad.get(OandaJsonKeys.units); final double price = ((Number) jsonPayLoad.get(OandaJsonKeys.price)).doubleValue(); final String side = jsonPayLoad.get(OandaJsonKeys.side).toString(); TradingSignal signal = OandaUtils.toTradingSignal(side); switch (payLoad.getEvent()) { case ORDER_FILLED: return String.format("Opened %s position of %d units for %s@%2.5f", signal.name(), tradeUnits, instrumentAsHashtag, price); case LIMIT_ORDER_CREATE: return String.format("%s LIMIT order of %d units for %s@%2.5f", signal.name(), tradeUnits, instrumentAsHashtag, price); default: return null; } }  @Override String toTweet(OrderEventPayLoad payLoad);  }
@Test public void payLoadToTweet() { OrderPayLoadToTweet payLoadToTweet = new OrderPayLoadToTweet(); JSONObject jsonPayLoad = mock(JSONObject.class); TradeableInstrument<String> eurchf = new TradeableInstrument<String>("EUR_CHF"); when(jsonPayLoad.get(OandaJsonKeys.instrument)).thenReturn(eurchf.getInstrument()); when(jsonPayLoad.get(OandaJsonKeys.units)).thenReturn(200l); when(jsonPayLoad.get(OandaJsonKeys.price)).thenReturn(1.10325); when(jsonPayLoad.get(OandaJsonKeys.side)).thenReturn("sell"); OrderEventPayLoad payLoad = new OrderEventPayLoad(OrderEvents.ORDER_FILLED, jsonPayLoad); String tweet = payLoadToTweet.toTweet(payLoad); assertNotNull(tweet); assertEquals("Opened SHORT position of 200 units for #EURCHF@1.10325", tweet); payLoad = new OrderEventPayLoad(OrderEvents.LIMIT_ORDER_CREATE, jsonPayLoad); tweet = payLoadToTweet.toTweet(payLoad); assertNotNull(tweet); assertEquals("SHORT LIMIT order of 200 units for #EURCHF@1.10325", tweet); payLoad = new OrderEventPayLoad(OrderEvents.ORDER_CANCEL, jsonPayLoad); tweet = payLoadToTweet.toTweet(payLoad); assertNull(tweet); }