Fixes validation of emails and webhooks. Fixed the incorrect indentation as well. Added unit tests as well.

Change-Id: I2d6f8e5323be2e927bf79deee11e65544cd25ac5
This commit is contained in:
Victor Ion Munteanu 2014-11-21 11:24:12 +01:00
parent b61cc600f9
commit b9a2173ada
3 changed files with 69 additions and 7 deletions

View File

@ -64,6 +64,11 @@
<artifactId>monasca-common-util</artifactId>
<version>${mon.common.version}</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>monasca-common</groupId>
<artifactId>monasca-common-kafka</artifactId>

View File

@ -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;
}
}
}

View File

@ -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();
}
}