From 2c7577382495adb69e42507d178f3f45f51b2c41 Mon Sep 17 00:00:00 2001 From: Jean Aurambault Date: Fri, 23 Aug 2024 14:48:20 -0700 Subject: [PATCH] Add integrity checker for emails --- .../rest/entity/IntegrityCheckerType.java | 3 +- .../EmailIntegrityChecker.java | 24 ++++++++++++ .../EmailIntegrityCheckerException.java | 7 ++++ .../EmailIntegrityCheckerTest.java | 39 +++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityChecker.java create mode 100644 webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerException.java create mode 100644 webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerTest.java diff --git a/restclient/src/main/java/com/box/l10n/mojito/rest/entity/IntegrityCheckerType.java b/restclient/src/main/java/com/box/l10n/mojito/rest/entity/IntegrityCheckerType.java index 85aef06aeb..866d0f39cc 100644 --- a/restclient/src/main/java/com/box/l10n/mojito/rest/entity/IntegrityCheckerType.java +++ b/restclient/src/main/java/com/box/l10n/mojito/rest/entity/IntegrityCheckerType.java @@ -21,5 +21,6 @@ public enum IntegrityCheckerType { BACKQUOTE, EMPTY_TARGET_NOT_EMPTY_SOURCE, MARKDOWN_LINKS, - PYTHON_FPRINT; + PYTHON_FPRINT, + EMAIL; } diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityChecker.java b/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityChecker.java new file mode 100644 index 0000000000..bae2ed4482 --- /dev/null +++ b/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityChecker.java @@ -0,0 +1,24 @@ +package com.box.l10n.mojito.service.assetintegritychecker.integritychecker; + +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.regex.Matcher; + +public class EmailIntegrityChecker extends RegexIntegrityChecker { + + final static String EMAIL_REGEX = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"; + + @Override + public String getRegex() { + return EMAIL_REGEX; + } + + @Override + public void check(String content, String target) { + try { + super.check(content, target); + } catch (RegexCheckerException ex) { + throw new EmailIntegrityCheckerException("Emails are changed."); + } + } +} diff --git a/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerException.java b/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerException.java new file mode 100644 index 0000000000..07b78bcd2b --- /dev/null +++ b/webapp/src/main/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerException.java @@ -0,0 +1,7 @@ +package com.box.l10n.mojito.service.assetintegritychecker.integritychecker; + +public class EmailIntegrityCheckerException extends IntegrityCheckException { + public EmailIntegrityCheckerException(String message) { + super(message); + } +} diff --git a/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerTest.java b/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerTest.java new file mode 100644 index 0000000000..492df1082d --- /dev/null +++ b/webapp/src/test/java/com/box/l10n/mojito/service/assetintegritychecker/integritychecker/EmailIntegrityCheckerTest.java @@ -0,0 +1,39 @@ +package com.box.l10n.mojito.service.assetintegritychecker.integritychecker; + +import org.junit.Ignore; +import org.junit.Test; + +public class EmailIntegrityCheckerTest { + + EmailIntegrityChecker checker = new EmailIntegrityChecker(); + + @Test + public void testNoEmail() { + String source = "There is no email"; + String target = "Il n'y a pas d'email"; + checker.check(source, target); + } + + @Test(expected = EmailIntegrityCheckerException.class) + public void testMissingInTarget() { + String source = "There is an ja@test.com"; + String target = "Il n'y a pas d'email"; + checker.check(source, target); + } + + @Test(expected = EmailIntegrityCheckerException.class) + public void testAddedInTarget() { + String source = "There is no email"; + String target = "Il n'y a un email ja@test.com"; + checker.check(source, target); + } + + @Ignore // not supported yet + @Test(expected = EmailIntegrityCheckerException.class) + public void testDuplicates() { + String source = "There is an ja@test.com"; + String target = "Il n'y a un email ja@test.com ja@test.com"; + checker.check(source, target); + } + +} \ No newline at end of file