| 1 | package fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.client; | |
| 2 | ||
| 3 | import static java.util.stream.Collectors.toList; | |
| 4 | ||
| 5 | import java.io.IOException; | |
| 6 | import java.util.List; | |
| 7 | import java.util.stream.Stream; | |
| 8 | ||
| 9 | import org.slf4j.Logger; | |
| 10 | import org.slf4j.LoggerFactory; | |
| 11 | ||
| 12 | import com.sendgrid.Method; | |
| 13 | import com.sendgrid.Request; | |
| 14 | import com.sendgrid.Response; | |
| 15 | import com.sendgrid.SendGrid; | |
| 16 | import com.sendgrid.SendGridAPI; | |
| 17 | ||
| 18 | import fr.sii.ogham.email.sendgrid.sender.exception.SendGridException; | |
| 19 | import fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.compat.EmailCompat; | |
| 20 | import fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.compat.MailCompat; | |
| 21 | ||
| 22 | /** | |
| 23 | * Facade wrapping the {@link SendGrid} object. | |
| 24 | */ | |
| 25 | public final class DelegateSendGridClient implements SendGridClient { | |
| 26 | ||
| 27 | private static final Logger LOG = LoggerFactory.getLogger(DelegateSendGridClient.class); | |
| 28 | ||
| 29 | private SendGridAPI delegate; | |
| 30 | ||
| 31 | /** | |
| 32 | * Constructor. | |
| 33 | * | |
| 34 | * @param delegate | |
| 35 | * the entry point to the SendGrid library | |
| 36 | * @throws IllegalArgumentException | |
| 37 | * if provided delegate is null | |
| 38 | */ | |
| 39 | public DelegateSendGridClient(final SendGridAPI delegate) { | |
| 40 |
4
1. <init> : negated conditional → NO_COVERAGE 2. <init> : negated conditional → KILLED 3. <init> : negated conditional → KILLED 4. <init> : negated conditional → KILLED |
if (delegate == null) { |
| 41 | throw new IllegalArgumentException("[delegate] cannot be null"); | |
| 42 | } | |
| 43 | ||
| 44 | this.delegate = delegate; | |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | public void send(final MailCompat email) throws SendGridException { | |
| 49 |
2
1. send : negated conditional → NO_COVERAGE 2. send : negated conditional → KILLED |
if (email == null) { |
| 50 | throw new IllegalArgumentException("[email] cannot be null"); | |
| 51 | } | |
| 52 | ||
| 53 | if (LOG.isDebugEnabled()) { | |
| 54 | LOG.debug("Sending to SendGrid client: FROM {}", debug(email.getFrom())); | |
| 55 | LOG.debug("Sending to SendGrid client: TO {}", debug(email)); | |
| 56 | LOG.debug("Sending to SendGrid client: SUBJECT {}", email.getSubject()); | |
| 57 | } | |
| 58 | ||
| 59 | final Response response = callApi(email); | |
| 60 | ||
| 61 |
2
1. send : negated conditional → NO_COVERAGE 2. send : negated conditional → KILLED |
if (isSuccess(response.getStatusCode())) { |
| 62 | LOG.debug("Response from SendGrid client: ({}) {}", response.getStatusCode(), response.getBody()); | |
| 63 | } else { | |
| 64 | throw new SendGridException(new IOException("Sending to SendGrid failed: (" + response.getStatusCode() + ") " + response.getBody())); | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | private Response callApi(final MailCompat email) throws SendGridException { | |
| 69 | try { | |
| 70 | Request request = prepareRequest(email); | |
| 71 |
2
1. callApi : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::callApi → NO_COVERAGE 2. callApi : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::callApi → KILLED |
return delegate.api(request); |
| 72 | } catch (IOException e) { | |
| 73 | throw new SendGridException("Sending email to SendGrid failed", e); | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | private static Request prepareRequest(final MailCompat email) throws SendGridException { | |
| 78 | Request request = new Request(); | |
| 79 |
2
1. prepareRequest : removed call to com/sendgrid/Request::setMethod → NO_COVERAGE 2. prepareRequest : removed call to com/sendgrid/Request::setMethod → KILLED |
request.setMethod(Method.POST); |
| 80 |
2
1. prepareRequest : removed call to com/sendgrid/Request::setEndpoint → NO_COVERAGE 2. prepareRequest : removed call to com/sendgrid/Request::setEndpoint → KILLED |
request.setEndpoint("mail/send"); |
| 81 | try { | |
| 82 |
2
1. prepareRequest : removed call to com/sendgrid/Request::setBody → NO_COVERAGE 2. prepareRequest : removed call to com/sendgrid/Request::setBody → KILLED |
request.setBody(email.build()); |
| 83 | } catch (IOException e) { | |
| 84 | throw new SendGridException("Preparing email for SendGrid failed", e); | |
| 85 | } | |
| 86 |
2
1. prepareRequest : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::prepareRequest → NO_COVERAGE 2. prepareRequest : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::prepareRequest → KILLED |
return request; |
| 87 | } | |
| 88 | ||
| 89 | private static boolean isSuccess(int statusCode) { | |
| 90 |
10
1. isSuccess : replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::isSuccess → NO_COVERAGE 2. isSuccess : changed conditional boundary → NO_COVERAGE 3. isSuccess : changed conditional boundary → SURVIVED 4. isSuccess : changed conditional boundary → NO_COVERAGE 5. isSuccess : negated conditional → NO_COVERAGE 6. isSuccess : negated conditional → NO_COVERAGE 7. isSuccess : replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::isSuccess → KILLED 8. isSuccess : changed conditional boundary → KILLED 9. isSuccess : negated conditional → KILLED 10. isSuccess : negated conditional → KILLED |
return statusCode >= 200 && statusCode < 300; |
| 91 | } | |
| 92 | ||
| 93 | private static String debug(EmailCompat address) { | |
| 94 |
2
1. debug : negated conditional → NO_COVERAGE 2. debug : negated conditional → KILLED |
if (address == null) { |
| 95 |
2
1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → SURVIVED 2. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → NO_COVERAGE |
return null; |
| 96 | } | |
| 97 |
2
1. debug : negated conditional → NO_COVERAGE 2. debug : negated conditional → SURVIVED |
if (address.getName() != null) { |
| 98 |
2
1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → NO_COVERAGE 2. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → SURVIVED |
return address.getName() + "<" + address.getEmail() + ">"; |
| 99 | } | |
| 100 |
2
1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → SURVIVED 2. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → NO_COVERAGE |
return address.getEmail(); |
| 101 | } | |
| 102 | ||
| 103 | private static List<String> debug(final MailCompat email) { | |
| 104 |
2
1. debug : negated conditional → NO_COVERAGE 2. debug : negated conditional → KILLED |
if (email.getPersonalization() == null) { |
| 105 |
2
1. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → SURVIVED 2. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → NO_COVERAGE |
return null; // NOSONAR |
| 106 | } | |
| 107 | // @formatter:off | |
| 108 |
2
1. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → SURVIVED 2. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → NO_COVERAGE |
return email.getPersonalization() |
| 109 | .stream() | |
| 110 |
4
1. lambda$debug$0 : negated conditional → NO_COVERAGE 2. lambda$debug$0 : negated conditional → SURVIVED 3. lambda$debug$0 : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::lambda$debug$0 → SURVIVED 4. lambda$debug$0 : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::lambda$debug$0 → NO_COVERAGE |
.flatMap(p -> p.getTos() == null ? Stream.empty() : p.getTos().stream()) |
| 111 | .map(DelegateSendGridClient::debug) | |
| 112 | .collect(toList()); | |
| 113 | // @formatter:on | |
| 114 | } | |
| 115 | } | |
Mutations | ||
| 40 |
1.1 2.2 3.3 4.4 |
|
| 49 |
1.1 2.2 |
|
| 61 |
1.1 2.2 |
|
| 71 |
1.1 2.2 |
|
| 79 |
1.1 2.2 |
|
| 80 |
1.1 2.2 |
|
| 82 |
1.1 2.2 |
|
| 86 |
1.1 2.2 |
|
| 90 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 |
|
| 94 |
1.1 2.2 |
|
| 95 |
1.1 2.2 |
|
| 97 |
1.1 2.2 |
|
| 98 |
1.1 2.2 |
|
| 100 |
1.1 2.2 |
|
| 104 |
1.1 2.2 |
|
| 105 |
1.1 2.2 |
|
| 108 |
1.1 2.2 |
|
| 110 |
1.1 2.2 3.3 4.4 |