AlertTemplateValidator implements Validator<AlertTemplate> { @Override public void validate(AlertTemplate template) throws ValidationException { if (template.getDestination() == null || template.getDestination().trim().isEmpty()) { throw new ValidationException("Alert target can't be empty"); } if (template.getDestination().length() > MAX_LENGTH_ALERT_DESTINATION) { throw new ValidationException( "Alert target must be less than " + MAX_LENGTH_ALERT_DESTINATION + " characters"); } if (template.getMedia() == null || template.getMedia().trim().isEmpty()) { throw new ValidationException("Alert media can't be empty"); } if (template.getMedia().length() > MAX_LENGTH_ALERT_MEDIA) { throw new ValidationException("Alert media must be less than " + MAX_LENGTH_ALERT_MEDIA + " characters"); } if (template.getBody() == null || template.getBody().trim().isEmpty()) { throw new ValidationException("Template body can't be empty"); } if (template.getTemplateName() == null || template.getTemplateName().trim().isEmpty()) { throw new ValidationException("Template name can't be empty"); } if (template.getThrottleDuration() < 1) { throw new ValidationException("Throttle duration can't be less than 1"); } if (template.getThrottleLimit() < 1) { throw new ValidationException("Throttle limit can't be less than 1"); } switch (template.getMedia()) { case "mail": case "http": case "slack": break; default: throw new ValidationException("Bad media type, only mail and http are allowed"); } if (template.getMedia().contains("mail")) { String[] emails = template.getDestination().split("\\s{0,1},"); for (String email : emails) { if (!EMAIL_PATTERN.matcher(email.trim()).matches()) { throw new ValidationException("Not a valid email address:" + email); } } } else if (template.getMedia().contains("http")) { if (!HTTP_PATTERN.matcher(template.getDestination()).matches()) { throw new ValidationException("Not a valid http address:" + template.getDestination()); } } else if (template.getMedia().contains("slack")) { if (!SLACK_PATTERN.matcher(template.getDestination()).matches()) { throw new ValidationException("Not a valid slack address:" + template.getDestination()); } } for (Validator<AlertTemplate> validator : alertTemplateValidator) { validator.validate(template); } } AlertTemplateValidator(); @SuppressWarnings("unchecked") @Override void configure(List<Validator<?>> validators); @Override void validate(AlertTemplate template);  }
@Test public void testMailAlertTemplatePositive() throws ValidationException { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); template.setMedia("mail"); template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("abc@xyz.com, efg@xyz.com"); validator.validate(template); }
@Test public void testMailAlertTemplateNegative() { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); try{ validator.validate(template); fail("Invalid template, can't pass test"); }catch(ValidationException e) { } template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("abc@xyz.com, efg@xyz.com"); template.setMedia("test"); try{ validator.validate(template); fail("Invalid template, can't pass test"); }catch(ValidationException e) { } template.setDestination("abc@xyz.com, efg@xy.c"); try{ validator.validate(template); fail("Invalid template, can't pass test"); }catch(ValidationException e) { } template.setDestination("abc@xyz.com,efg@xy.c"); try{ validator.validate(template); fail("Invalid template, can't pass test"); }catch(ValidationException e) { } template.setDestination("hello924jwjaksfsduufasidjfhkasdjfadsf9asfijklsdfoasdljfo9sdfpiojkasdfoasjdifaisdpfjasdl;fiaosdf9posdjklfasdfhaosdif9paidsujklfasdfa09wrjiwqjrwfjoifjlas@xyz.com"); try{ validator.validate(template); fail("Invalid template, can't pass test"); }catch(ValidationException e) { } }
@Test public void testHTTPAlertTemplatePostive() throws ValidationException { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); template.setMedia("http"); template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("https: validator.validate(template); template.setDestination("http: validator.validate(template); }
@Test public void testHTTPAlertTemplateNegative() { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); template.setMedia("http"); template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("htt: try { validator.validate(template); fail("Invalid template, can't pass test"); } catch (ValidationException e) { } template.setDestination("smtp: try { validator.validate(template); fail("Invalid template, can't pass test"); } catch (ValidationException e) { } }
@Test public void testSlackAlertTemplatePostive() throws ValidationException { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); template.setMedia("slack"); template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX@slack-channel"); validator.validate(template); template.setDestination("T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX@slack_channel"); validator.validate(template); }
@Test public void testSlackAlertTemplateNegative() { AlertTemplateValidator validator = new AlertTemplateValidator(); AlertTemplate template = new AlertTemplate((short)2); template.setMedia("slack"); template.setSubject("hello"); template.setBody("hello $x"); template.setTemplateName("Template"); template.setDestination("ABC@slack-channel"); try { validator.validate(template); fail("Invalid template, can't pass test"); } catch (ValidationException e) { } template.setDestination("ANV@slack#channel"); try { validator.validate(template); fail("Invalid template, can't pass test"); } catch (ValidationException e) { } }