DatabaseMessageSender implements MessageSender { @Override @Transactional public void sendMessage(String message, String channel) { logger.info("Sending message {} to channel {}", message, channel); if (!Objects.equals(channel, Topics.APOLLO_RELEASE_TOPIC)) { logger.warn("Channel {} not supported by DatabaseMessageSender!", channel); return; } Tracer.logEvent("Apollo.AdminService.ReleaseMessage", message); Transaction transaction = Tracer.newTransaction("Apollo.AdminService", "sendMessage"); try { ReleaseMessage newMessage = releaseMessageRepository.save(new ReleaseMessage(message)); toClean.offer(newMessage.getId()); transaction.setStatus(Transaction.SUCCESS); } catch (Throwable ex) { logger.error("Sending message to database failed", ex); transaction.setStatus(ex); throw ex; } finally { transaction.complete(); } } DatabaseMessageSender(final ReleaseMessageRepository releaseMessageRepository); @Override @Transactional void sendMessage(String message, String channel);  }
@Test public void testSendMessage() throws Exception { String someMessage = "some-message"; long someId = 1; ReleaseMessage someReleaseMessage = mock(ReleaseMessage.class); when(someReleaseMessage.getId()).thenReturn(someId); when(releaseMessageRepository.save(any(ReleaseMessage.class))).thenReturn(someReleaseMessage); ArgumentCaptor<ReleaseMessage> captor = ArgumentCaptor.forClass(ReleaseMessage.class); messageSender.sendMessage(someMessage, Topics.APOLLO_RELEASE_TOPIC); verify(releaseMessageRepository, times(1)).save(captor.capture()); assertEquals(someMessage, captor.getValue().getMessage()); }
@Test public void testSendUnsupportedMessage() throws Exception { String someMessage = "some-message"; String someUnsupportedTopic = "some-invalid-topic"; messageSender.sendMessage(someMessage, someUnsupportedTopic); verify(releaseMessageRepository, never()).save(any(ReleaseMessage.class)); }
@Test(expected = RuntimeException.class) public void testSendMessageFailed() throws Exception { String someMessage = "some-message"; when(releaseMessageRepository.save(any(ReleaseMessage.class))).thenThrow(new RuntimeException()); messageSender.sendMessage(someMessage, Topics.APOLLO_RELEASE_TOPIC); }