@Test public void checkingOutTheHappyPath() { String seyrenUrl = clientDriver.getBaseUrl() + "/seyren"; when(mockSeyrenConfig.getHubotUrl()).thenReturn(clientDriver.getBaseUrl() + "/hubot"); when(mockSeyrenConfig.getBaseUrl()).thenReturn(seyrenUrl); Check check = new Check() .withEnabled(true) .withName("check-name") .withState(AlertType.ERROR); Subscription subscription = new Subscription() .withType(SubscriptionType.HUBOT) .withTarget("123,456"); Alert alert = new Alert() .withTarget("the.target.name") .withValue(BigDecimal.valueOf(12)) .withWarn(BigDecimal.valueOf(5)) .withError(BigDecimal.valueOf(10)) .withFromType(AlertType.WARN) .withToType(AlertType.ERROR); List<Alert> alerts = Arrays.asList(alert); BodyCapture<JsonNode> bodyCapture = new JsonBodyCapture(); clientDriver.addExpectation( onRequestTo("/hubot/seyren/alert") .withMethod(Method.POST) .capturingBodyIn(bodyCapture), giveResponse("Thanks for letting me know", "text/plain")); service.sendNotification(check, subscription, alerts); JsonNode node = bodyCapture.getContent(); assertThat(node, hasJsonPath("$.seyrenUrl", is(seyrenUrl))); assertThat(node, hasJsonPath("$.check.name", is("check-name"))); assertThat(node, hasJsonPath("$.check.state", is("ERROR"))); assertThat(node, hasJsonPath("$.alerts", hasSize(1))); assertThat(node, hasJsonPath("$.alerts[0].target", is("the.target.name"))); assertThat(node, hasJsonPath("$.alerts[0].value", is(12))); assertThat(node, hasJsonPath("$.alerts[0].warn", is(5))); assertThat(node, hasJsonPath("$.alerts[0].error", is(10))); assertThat(node, hasJsonPath("$.alerts[0].fromType", is("WARN"))); assertThat(node, hasJsonPath("$.alerts[0].toType", is("ERROR"))); assertThat(node, hasJsonPath("$.rooms", hasSize(2))); assertThat(node, hasJsonPath("$.rooms[0]", is("123"))); assertThat(node, hasJsonPath("$.rooms[1]", is("456"))); verify(mockSeyrenConfig).getHubotUrl(); verify(mockSeyrenConfig).getBaseUrl(); }
@Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String hubotUrl = StringUtils.trimToNull(seyrenConfig.getHubotUrl()); if (hubotUrl == null) { LOGGER.warn("Hubot URL needs to be set before sending notifications to Hubot"); return; } Map<String, Object> body = new HashMap<String, Object>(); body.put("seyrenUrl", seyrenConfig.getBaseUrl()); body.put("check", check); body.put("subscription", subscription); body.put("alerts", alerts); body.put("rooms", subscription.getTarget().split(",")); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(hubotUrl + "/seyren/alert"); try { HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); client.execute(post); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Hubot at " + hubotUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } }
HubotNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String hubotUrl = StringUtils.trimToNull(seyrenConfig.getHubotUrl()); if (hubotUrl == null) { LOGGER.warn("Hubot URL needs to be set before sending notifications to Hubot"); return; } Map<String, Object> body = new HashMap<String, Object>(); body.put("seyrenUrl", seyrenConfig.getBaseUrl()); body.put("check", check); body.put("subscription", subscription); body.put("alerts", alerts); body.put("rooms", subscription.getTarget().split(",")); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(hubotUrl + "/seyren/alert"); try { HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); client.execute(post); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Hubot at " + hubotUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } } }
HubotNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String hubotUrl = StringUtils.trimToNull(seyrenConfig.getHubotUrl()); if (hubotUrl == null) { LOGGER.warn("Hubot URL needs to be set before sending notifications to Hubot"); return; } Map<String, Object> body = new HashMap<String, Object>(); body.put("seyrenUrl", seyrenConfig.getBaseUrl()); body.put("check", check); body.put("subscription", subscription); body.put("alerts", alerts); body.put("rooms", subscription.getTarget().split(",")); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(hubotUrl + "/seyren/alert"); try { HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); client.execute(post); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Hubot at " + hubotUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } } @Inject  HubotNotificationService(SeyrenConfig seyrenConfig); }
HubotNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String hubotUrl = StringUtils.trimToNull(seyrenConfig.getHubotUrl()); if (hubotUrl == null) { LOGGER.warn("Hubot URL needs to be set before sending notifications to Hubot"); return; } Map<String, Object> body = new HashMap<String, Object>(); body.put("seyrenUrl", seyrenConfig.getBaseUrl()); body.put("check", check); body.put("subscription", subscription); body.put("alerts", alerts); body.put("rooms", subscription.getTarget().split(",")); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(hubotUrl + "/seyren/alert"); try { HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); client.execute(post); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Hubot at " + hubotUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } } @Inject  HubotNotificationService(SeyrenConfig seyrenConfig); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType); }
HubotNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String hubotUrl = StringUtils.trimToNull(seyrenConfig.getHubotUrl()); if (hubotUrl == null) { LOGGER.warn("Hubot URL needs to be set before sending notifications to Hubot"); return; } Map<String, Object> body = new HashMap<String, Object>(); body.put("seyrenUrl", seyrenConfig.getBaseUrl()); body.put("check", check); body.put("subscription", subscription); body.put("alerts", alerts); body.put("rooms", subscription.getTarget().split(",")); HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); HttpPost post = new HttpPost(hubotUrl + "/seyren/alert"); try { HttpEntity entity = new StringEntity(MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON); post.setEntity(entity); client.execute(post); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Hubot at " + hubotUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } } @Inject  HubotNotificationService(SeyrenConfig seyrenConfig); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType);  }