TwilioNotificationService implements NotificationService { @Override public void sendNotification(Check check, Subscription subscription, List<Alert> alerts) throws NotificationFailedException { String twilioUrl = StringUtils.trimToNull(seyrenConfig.getTwilioUrl()); if (twilioUrl == null) { LOGGER.warn("Twilio URL needs to be set before sending notifications to Twilio"); return; } String body; if (check.getState() == AlertType.ERROR) { body = "ERROR Check "+check.getName()+" has exceeded its threshold."; } else if (check.getState() == AlertType.OK) { body = "OK Check "+check.getName()+" has been resolved."; } else { LOGGER.warn("Did not send notification to Twilio for check in state: {}", check.getState()); body = null; } List<NameValuePair> params=new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("To", subscription.getTarget())); params.add(new BasicNameValuePair("From", seyrenConfig.getTwilioPhoneNumber())); params.add(new BasicNameValuePair("Body", body)); HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(twilioUrl + "/"+seyrenConfig.getTwilioAccountSid()+"/Messages"); try { String credentials=seyrenConfig.getTwilioAccountSid()+":"+seyrenConfig.getTwilioAuthToken(); post.setHeader(new BasicHeader("Authorization", "Basic "+Base64.encodeBase64String(credentials.getBytes("UTF-8")))); HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); post.setEntity(entity); HttpResponse response=client.execute(post); if(response.getStatusLine().getStatusCode()/100 != 2) throw new IOException("API request failed: "+response.getStatusLine()); } catch (IOException e) { throw new NotificationFailedException("Sending notification to Twilio at " + twilioUrl + " failed.", e); } finally { HttpClientUtils.closeQuietly(client); } } @Inject  TwilioNotificationService(SeyrenConfig seyrenConfig); @Override void sendNotification(Check check, Subscription subscription, List<Alert> alerts); @Override boolean canHandle(SubscriptionType subscriptionType);  }
@Test public void checkingOutTheHappyPath() { String seyrenUrl = clientDriver.getBaseUrl() + "/seyren"; when(mockSeyrenConfig.getTwilioUrl()).thenReturn(clientDriver.getBaseUrl() + "/twilio"); when(mockSeyrenConfig.getTwilioAccountSid()).thenReturn(ACCOUNT_SID); when(mockSeyrenConfig.getTwilioAuthToken()).thenReturn("5678"); when(mockSeyrenConfig.getTwilioPhoneNumber()).thenReturn("+11234567890"); when(mockSeyrenConfig.getBaseUrl()).thenReturn(seyrenUrl); Check check = new Check() .withEnabled(true) .withName("check-name") .withState(AlertType.ERROR); Subscription subscription = new Subscription() .withType(SubscriptionType.TWILIO) .withTarget("+10987654321"); 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<String> bodyCapture = new StringBodyCapture(); clientDriver.addExpectation( onRequestTo("/twilio/"+ACCOUNT_SID+"/Messages") .withMethod(Method.POST) .capturingBodyIn(bodyCapture), giveResponse("Thanks for letting me know", "text/plain")); service.sendNotification(check, subscription, alerts); String body=bodyCapture.getContent(); assertThat(body, containsString("To=%2B10987654321")); assertThat(body, containsString("From=%2B11234567890")); assertThat(body, containsString("Body=ERROR+Check+check-name+has+exceeded+its+threshold")); verify(mockSeyrenConfig).getTwilioUrl(); }