EmailSentCounter extends AbstractEmailSenderCallback { @VisibleForTesting protected Pair<Integer, Integer> statistics(List<EventModel> events) { int success = 0, fail = 0; for(EventModel event : events) { String prop = event.getEventProperty(); Properties properties = JsonCodec.INSTANCE.decode(prop, Properties.class); boolean successOrNot = EmailService.DEFAULT.checkAsyncEmailResult(new EmailResponse() { @Override public Properties getProperties() { return properties; } }); if(successOrNot) { success ++; } else { logger.info("[statistics] email fail sent out: {}", event.getEventProperty()); fail ++; } } return new Pair<>(success, fail); }  @PostConstruct void scheduledCheckSentEmails(); @Override void success(); @Override void fail(Throwable throwable);  }
@Test public void testStatistics() throws Exception { Properties properties1 = new Properties(); properties1.put(EmailResponse.KEYS.CHECK_INFO.name(), "123456"); String property1 = JsonCodec.DEFAULT.encode(properties1); EventModel event1 = new EventModel().setEventProperty(property1); Properties properties2 = new Properties(); properties1.put(EmailResponse.KEYS.CHECK_INFO.name(), "1234567"); String property2 = JsonCodec.DEFAULT.encode(properties2); EventModel event2 = new EventModel().setEventProperty(property2); when(service.getLastHourAlertEvent()).thenReturn(Lists.newArrayList(event1, event2)); Pair<Integer, Integer> successAndFails = instance.statistics(service.getLastHourAlertEvent()); Assert.assertEquals(2, (int)successAndFails.getKey()); Assert.assertEquals(0, (int)successAndFails.getValue()); }