DelegateSendGridClient.java

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 1 1. <init> : negated conditional → RUN_ERROR
		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 1 1. send : negated conditional → RUN_ERROR
		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 1 1. send : negated conditional → RUN_ERROR
		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 1 1. callApi : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::callApi → RUN_ERROR
			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 1 1. prepareRequest : removed call to com/sendgrid/Request::setMethod → RUN_ERROR
		request.setMethod(Method.POST);
80 1 1. prepareRequest : removed call to com/sendgrid/Request::setEndpoint → RUN_ERROR
		request.setEndpoint("mail/send");
81
		try {
82 1 1. prepareRequest : removed call to com/sendgrid/Request::setBody → RUN_ERROR
			request.setBody(email.build());
83
		} catch (IOException e) {
84
			throw new SendGridException("Preparing email for SendGrid failed", e);
85
		}
86 1 1. prepareRequest : replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::prepareRequest → RUN_ERROR
		return request;
87
	}
88
89
	private static boolean isSuccess(int statusCode) {
90 5 1. isSuccess : negated conditional → RUN_ERROR
2. isSuccess : replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::isSuccess → RUN_ERROR
3. isSuccess : negated conditional → RUN_ERROR
4. isSuccess : changed conditional boundary → RUN_ERROR
5. isSuccess : changed conditional boundary → RUN_ERROR
		return statusCode >= 200 && statusCode < 300;
91
	}
92
93
	private static String debug(EmailCompat address) {
94 1 1. debug : negated conditional → RUN_ERROR
		if (address == null) {
95 1 1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR
			return null;
96
		}
97 1 1. debug : negated conditional → RUN_ERROR
		if (address.getName() != null) {
98 1 1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR
			return address.getName() + "<" + address.getEmail() + ">";
99
		}
100 1 1. debug : replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR
		return address.getEmail();
101
	}
102
103
	private static List<String> debug(final MailCompat email) {
104 1 1. debug : negated conditional → RUN_ERROR
		if (email.getPersonalization() == null) {
105 1 1. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR
			return null; // NOSONAR
106
		}
107
		// @formatter:off
108 1 1. debug : replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR
		return email.getPersonalization()
109
				.stream()
110 2 1. lambda$debug$0 : negated conditional → RUN_ERROR
2. lambda$debug$0 : replaced return value with Stream.empty for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::lambda$debug$0 → RUN_ERROR
				.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
Location : <init>
Killed by :
negated conditional → RUN_ERROR

49

1.1
Location : send
Killed by :
negated conditional → RUN_ERROR

61

1.1
Location : send
Killed by :
negated conditional → RUN_ERROR

71

1.1
Location : callApi
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::callApi → RUN_ERROR

79

1.1
Location : prepareRequest
Killed by :
removed call to com/sendgrid/Request::setMethod → RUN_ERROR

80

1.1
Location : prepareRequest
Killed by :
removed call to com/sendgrid/Request::setEndpoint → RUN_ERROR

82

1.1
Location : prepareRequest
Killed by :
removed call to com/sendgrid/Request::setBody → RUN_ERROR

86

1.1
Location : prepareRequest
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::prepareRequest → RUN_ERROR

90

1.1
Location : isSuccess
Killed by :
negated conditional → RUN_ERROR

2.2
Location : isSuccess
Killed by :
replaced boolean return with true for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::isSuccess → RUN_ERROR

3.3
Location : isSuccess
Killed by :
negated conditional → RUN_ERROR

4.4
Location : isSuccess
Killed by :
changed conditional boundary → RUN_ERROR

5.5
Location : isSuccess
Killed by :
changed conditional boundary → RUN_ERROR

94

1.1
Location : debug
Killed by :
negated conditional → RUN_ERROR

95

1.1
Location : debug
Killed by :
replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR

97

1.1
Location : debug
Killed by :
negated conditional → RUN_ERROR

98

1.1
Location : debug
Killed by :
replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR

100

1.1
Location : debug
Killed by :
replaced return value with "" for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR

104

1.1
Location : debug
Killed by :
negated conditional → RUN_ERROR

105

1.1
Location : debug
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR

108

1.1
Location : debug
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::debug → RUN_ERROR

110

1.1
Location : lambda$debug$0
Killed by :
negated conditional → RUN_ERROR

2.2
Location : lambda$debug$0
Killed by :
replaced return value with Stream.empty for fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/client/DelegateSendGridClient::lambda$debug$0 → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1