diff --git a/pom.xml b/pom.xml
index 8dcb701bf..ef1428c40 100644
--- a/pom.xml
+++ b/pom.xml
@@ -64,6 +64,11 @@
monasca-common-util
${mon.common.version}
+
+ commons-validator
+ commons-validator
+ 1.4.0
+
monasca-common
monasca-common-kafka
diff --git a/src/main/java/monasca/api/app/command/CreateNotificationMethodCommand.java b/src/main/java/monasca/api/app/command/CreateNotificationMethodCommand.java
index 3355a1827..92a1c032d 100644
--- a/src/main/java/monasca/api/app/command/CreateNotificationMethodCommand.java
+++ b/src/main/java/monasca/api/app/command/CreateNotificationMethodCommand.java
@@ -15,7 +15,8 @@ package monasca.api.app.command;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
-
+import org.apache.commons.validator.routines.EmailValidator;
+import org.apache.commons.validator.routines.UrlValidator;
import org.hibernate.validator.constraints.NotEmpty;
import monasca.api.domain.model.notificationmethod.NotificationMethodType;
@@ -64,10 +65,17 @@ public class CreateNotificationMethodCommand {
}
public void validate() {
- int atPos = address.indexOf("@");
- int commaPos = address.indexOf(",");
- if (type == NotificationMethodType.EMAIL
- && (atPos <= 0 || atPos == address.length() - 1 || commaPos >= 0))
- throw Exceptions.unprocessableEntity("Address %s is not of correct format", address);
+ switch (type) {
+ case EMAIL : {
+ if (!EmailValidator.getInstance().isValid(address))
+ throw Exceptions.unprocessableEntity("Address %s is not of correct format", address);
+ }; break;
+ case WEBHOOK : {
+ String[] schemes = {"http","https"};
+ UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS | UrlValidator.ALLOW_2_SLASHES);
+ if (!urlValidator.isValid(address))
+ throw Exceptions.unprocessableEntity("Address %s is not of correct format", address);
+ }; break;
+ }
}
}
diff --git a/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java b/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java
index 74ec4b6af..6cc1754c7 100644
--- a/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java
+++ b/src/test/java/monasca/api/app/command/CreateNotificationMethodTest.java
@@ -17,9 +17,12 @@ package monasca.api.app.command;
import static monasca.common.dropwizard.JsonHelpers.jsonFixture;
import static org.testng.Assert.assertEquals;
+import javax.ws.rs.WebApplicationException;
+
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.JsonMappingException;
+
import monasca.api.app.command.CreateNotificationMethodCommand;
import monasca.api.domain.model.AbstractModelTest;
import monasca.api.domain.model.notificationmethod.NotificationMethodType;
@@ -53,4 +56,50 @@ public class CreateNotificationMethodTest extends AbstractModelTest {
CreateNotificationMethodCommand other = fromJson(json, CreateNotificationMethodCommand.class);
assertEquals(other, newNotificationMethod);
}
-}
+
+ public void testValidationForEmail() {
+ CreateNotificationMethodCommand newNotificationMethod =
+ new CreateNotificationMethodCommand("MyEmail", NotificationMethodType.EMAIL, "name@domain.com");
+
+ Exception ex = null;
+
+ try {
+ newNotificationMethod.validate();
+ } catch (Exception e) {
+ ex = e;
+ }
+
+ assertEquals(null, ex);
+ }
+
+ @Test(expectedExceptions = WebApplicationException.class)
+ public void testValidationExceptionForEmail() throws Exception {
+ CreateNotificationMethodCommand newNotificationMethod =
+ new CreateNotificationMethodCommand("MyEmail", NotificationMethodType.EMAIL, "name@domain");
+
+ newNotificationMethod.validate();
+ }
+
+ public void testValidationForWebhook() {
+ CreateNotificationMethodCommand newNotificationMethod =
+ new CreateNotificationMethodCommand("MyEmail", NotificationMethodType.WEBHOOK, "http://somedomain.com");
+
+ Exception ex = null;
+
+ try {
+ newNotificationMethod.validate();
+ } catch (Exception e) {
+ ex = e;
+ }
+
+ assertEquals(null, ex);
+ }
+
+ @Test(expectedExceptions = WebApplicationException.class)
+ public void testValidationExceptionForWebhook() throws Exception {
+ CreateNotificationMethodCommand newNotificationMethod =
+ new CreateNotificationMethodCommand("MyWebhook", NotificationMethodType.WEBHOOK, "ftp://localhost");
+
+ newNotificationMethod.validate();
+ }
+}
\ No newline at end of file