ChannelChain { public NotifyStatistics send(Subscriber subscriber, List<NotificationEventTO> events) { NotifyStatistics statistics = new NotifyStatistics(); if (events == null || events.isEmpty()) { return statistics; } for (Channel channel : channels) { boolean delivered = false; if (!subscriber.getChannels().contains(channel.getChannelName())) continue; try { channel.send(subscriber, events); delivered = true; } catch (Exception e) { log.error("Could not send via the channel [" + channel.getChannelName() + "] for the subscriber [" + subscriber.getId() + "] because of: " + e); } finally { statistics.addChannelResponse(channel.getChannelName(), delivered); } } return statistics; }  @PostConstruct void init(); List<Channel> getChannels(); Channel getMailChannel(); void setMailChannel(MailChannel mailChannel); Channel getDummyChannel(); void setDummyChannel(DummyChannel dummyChannel); NotifyStatistics send(Subscriber subscriber, List<NotificationEventTO> events);  }
@Test public void testNotifyStatistics_send_successfulForMail() { NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals(0, statistics.getFailedCounter()); assertEquals(2, statistics.getDeliveredCounter()); assertEquals(2, statistics.getTotalCounter()); }
@Test public void testNotifyStatistics_send_failedForMail() throws Exception { doNothing().when(dummyChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); doThrow(new MailSendException("")).when(mailChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals(1, statistics.getFailedCounter()); assertEquals(1, statistics.getDeliveredCounter()); assertEquals(2, statistics.getTotalCounter()); }
@Test public void send_UserIsConfiguredForMailChannelAndDummyChannel_SucessForBoth() throws Exception { doNothing().when(mailChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); doNothing().when(dummyChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals("Wrong number of the size", 2, statistics.getChannel2StatusMap().size()); assertTrue(statistics.getChannel2StatusMap().get(Subscriber.Channel.EMAIL)); assertTrue(statistics.getChannel2StatusMap().get(null)); }
@Test public void send_UserIsConfiguredForMailChannelAndDummyChannel_FailedForBoth() throws Exception { doThrow(new MailSendException("")).when(mailChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); doThrow(new MailSendException("")).when(dummyChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals("Wrong number of the size", 2, statistics.getChannel2StatusMap().size()); assertFalse(statistics.getChannel2StatusMap().get(Subscriber.Channel.EMAIL)); assertFalse(statistics.getChannel2StatusMap().get(null)); }
@Test public void send_UserIsConfiguredForMailChannelAndDummyChannel_SucessForMail_FaildForDummy() throws Exception { doNothing().when(mailChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); doThrow(new MailSendException("")).when(dummyChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals("Wrong number of the size", 2, statistics.getChannel2StatusMap().size()); assertTrue(statistics.getChannel2StatusMap().get(Subscriber.Channel.EMAIL)); assertFalse(statistics.getChannel2StatusMap().get(null)); }
@Test public void send_UserIsConfiguredForMailChannelAndDummyChannel_SucessForDummy_FaildForMail() throws Exception { doNothing().when(dummyChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); doThrow(new MailSendException("")).when(mailChannelMock).send(any(Subscriber.class), anyListOf(NotificationEventTO.class)); NotifyStatistics statistics = channelChainTestObject.send(subscriber, eventTOs); assertEquals("Wrong number of the size", 2, statistics.getChannel2StatusMap().size()); assertFalse(statistics.getChannel2StatusMap().get(Subscriber.Channel.EMAIL)); assertTrue(statistics.getChannel2StatusMap().get(null)); }