Notifier implements ApplicationListener<ApplicationReadyEvent> { @Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { if (!running.compareAndSet(false, true)) return; Thread notifyLoop = new Thread(this::loopUntilShutdown); notifyLoop.setName("adaptivealerting notify loop"); notifyLoop.setDaemon(true); notifyLoop.start(); } @Autowired  Notifier(NotifierConfig notifierConfig, ObjectMapper objectMapper, RestTemplate restTemplate); @PreDestroy void stopLooperThread(); @Override void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent);  }
@Test public void applicationReadyEvent_startsNotifier() { Assertions.assertThat(notifier.running).isFalse(); notifier.onApplicationEvent(mock(ApplicationReadyEvent.class)); Assertions.assertThat(notifier.running).isTrue(); }
@Test public void context_close_stopsNotifier() { notifier.onApplicationEvent(mock(ApplicationReadyEvent.class)); context.close(); Assertions.assertThat(notifier.running).isFalse(); }
@Test public void message_invokesWebhook() throws Exception { notifier.onApplicationEvent(mock(ApplicationReadyEvent.class)); webhook.enqueue(new MockResponse()); String json = notifierConfig.objectMapper().writeValueAsString(newMappedMetricData()); kafka.helper().produceStrings(notifierConfig.getKafkaTopic(), json); RecordedRequest webhookRequest = webhook.takeRequest(); Assertions.assertThat(webhookRequest.getMethod()) .isEqualTo("POST"); Assertions.assertThat(webhookRequest.getBody().readUtf8()) .isEqualTo(json); }