From 6f0c3b2da98ee71f02212d71cac0d16fff254978 Mon Sep 17 00:00:00 2001 From: Craig Bryant Date: Fri, 3 Jun 2016 15:15:03 -0600 Subject: [PATCH] Allow QA teams to use the Top Level Domain .test QA teams have requested the ability to use the Top Level Domain (TLD) .test for webhook testing. Using .test is valid according to RFC-2606 but the Apache URL Validator does not allow it. Add an override for the .test TLD. Change-Id: Ie5801d0a84199a84b7121b002bda5398cfec3b89 --- .../validation/NotificationMethodValidation.java | 12 +++++++++--- .../app/command/CreateNotificationMethodTest.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/monasca/api/app/validation/NotificationMethodValidation.java b/java/src/main/java/monasca/api/app/validation/NotificationMethodValidation.java index bb08b5e41..e912fff9a 100644 --- a/java/src/main/java/monasca/api/app/validation/NotificationMethodValidation.java +++ b/java/src/main/java/monasca/api/app/validation/NotificationMethodValidation.java @@ -17,11 +17,19 @@ import monasca.api.domain.model.notificationmethod.NotificationMethodType; import monasca.api.resource.exception.Exceptions; import org.apache.commons.validator.routines.EmailValidator; +import org.apache.commons.validator.routines.RegexValidator; import org.apache.commons.validator.routines.UrlValidator; import java.util.List; public class NotificationMethodValidation { + private static final String[] SCHEMES = {"http","https"}; + // Allow QA to use the TLD .test. This is valid according to RFC-2606 + private static final RegexValidator TEST_TLD_VALIDATOR = new RegexValidator(".+\\.test$"); + private static final UrlValidator URL_VALIDATOR = + new UrlValidator(SCHEMES, + TEST_TLD_VALIDATOR, + UrlValidator.ALLOW_LOCAL_URLS | UrlValidator.ALLOW_2_SLASHES); public static void validate(NotificationMethodType type, String address, String period, List validPeriods) { @@ -34,9 +42,7 @@ public class NotificationMethodValidation { throw Exceptions.unprocessableEntity("Period can not be non zero for Email"); } break; case WEBHOOK : { - String[] schemes = {"http","https"}; - UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS | UrlValidator.ALLOW_2_SLASHES); - if (!urlValidator.isValid(address)) + if (!URL_VALIDATOR.isValid(address)) throw Exceptions.unprocessableEntity("Address %s is not of correct format", address); } break; case PAGERDUTY : { diff --git a/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java b/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java index 6c068404c..0253d6486 100644 --- a/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java +++ b/java/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java @@ -118,6 +118,19 @@ public class CreateNotificationMethodTest extends AbstractModelTest { newNotificationMethod.validate(validPeriods); } + public void testValidationTestDomainForWebhook() { + CreateNotificationMethodCommand newNotificationMethod = + new CreateNotificationMethodCommand("MyWebhook", NotificationMethodType.WEBHOOK, "http://test.test", "60"); + newNotificationMethod.validate(validPeriods); + } + + @Test(expectedExceptions = WebApplicationException.class) + public void testValidationInvalidDomainForWebhook() { + CreateNotificationMethodCommand newNotificationMethod = + new CreateNotificationMethodCommand("MyWebhook", NotificationMethodType.WEBHOOK, "http://test.fred", "60"); + newNotificationMethod.validate(validPeriods); + } + @Test(expectedExceptions = WebApplicationException.class) public void testValidationExceptionForWebhook() throws Exception { CreateNotificationMethodCommand newNotificationMethod =