| 1 | package fr.sii.ogham.email.sendgrid.sender; | |
| 2 | ||
| 3 | import java.util.HashSet; | |
| 4 | import java.util.Set; | |
| 5 | ||
| 6 | import fr.sii.ogham.core.exception.InvalidMessageException; | |
| 7 | import fr.sii.ogham.email.message.Email; | |
| 8 | import fr.sii.ogham.email.message.Recipient; | |
| 9 | ||
| 10 | /** | |
| 11 | * Validate the email fields. | |
| 12 | * | |
| 13 | * @author Aurélien Baudet | |
| 14 | * | |
| 15 | */ | |
| 16 | public final class EmailValidator { | |
| 17 | /** | |
| 18 | * Ensure that email can be sent: | |
| 19 | * <ul> | |
| 20 | * <li>Content is required</li> | |
| 21 | * <li>Subject is required</li> | |
| 22 | * <li>Sender address is required</li> | |
| 23 | * <li>At least one recipient is required</li> | |
| 24 | * </ul> | |
| 25 | * | |
| 26 | * @param message | |
| 27 | * the email to validate | |
| 28 | * @throws InvalidMessageException | |
| 29 | * when the email is not valid | |
| 30 | */ | |
| 31 | public static void validate(final Email message) throws InvalidMessageException { | |
| 32 | final Set<String> violations = new HashSet<>(); | |
| 33 | ||
| 34 |
2
1. validate : negated conditional → NO_COVERAGE 2. validate : negated conditional → SURVIVED |
if (message.getContent() == null) { |
| 35 | violations.add("Missing content"); | |
| 36 | } | |
| 37 |
2
1. validate : negated conditional → SURVIVED 2. validate : negated conditional → NO_COVERAGE |
if (message.getSubject() == null) { |
| 38 | violations.add("Missing subject"); | |
| 39 | } | |
| 40 | ||
| 41 |
2
1. validate : negated conditional → NO_COVERAGE 2. validate : negated conditional → SURVIVED |
if (message.getFrom() == null) { |
| 42 | violations.add("Missing sender email address"); | |
| 43 | } | |
| 44 | ||
| 45 |
2
1. validate : negated conditional → NO_COVERAGE 2. validate : negated conditional → SURVIVED |
if (message.getRecipients().isEmpty()) { |
| 46 | violations.add("Missing recipients"); | |
| 47 | } | |
| 48 | ||
| 49 | for (Recipient recipient : message.getRecipients()) { | |
| 50 |
1
1. validate : negated conditional → NO_COVERAGE |
if (recipient.getAddress().getAddress() == null) { |
| 51 | violations.add("Missing recipient address " + recipient); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 |
2
1. validate : negated conditional → NO_COVERAGE 2. validate : negated conditional → KILLED |
if (!violations.isEmpty()) { |
| 56 | throw new InvalidMessageException("The provided email is invalid. (Violations: " + violations + ")", message, violations); | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | private EmailValidator() { | |
| 61 | super(); | |
| 62 | } | |
| 63 | ||
| 64 | } | |
Mutations | ||
| 34 |
1.1 2.2 |
|
| 37 |
1.1 2.2 |
|
| 41 |
1.1 2.2 |
|
| 45 |
1.1 2.2 |
|
| 50 |
1.1 |
|
| 55 |
1.1 2.2 |