1 | package fr.sii.ogham.testing.assertion.internal; | |
2 | ||
3 | import static fr.sii.ogham.testing.assertion.internal.helper.ImplementationFinder.findSender; | |
4 | import static fr.sii.ogham.testing.assertion.util.AssertionHelper.assertThat; | |
5 | import static org.apache.commons.lang3.reflect.FieldUtils.readField; | |
6 | ||
7 | import java.util.Map; | |
8 | ||
9 | import org.hamcrest.Matcher; | |
10 | ||
11 | import com.sendgrid.SendGrid; | |
12 | ||
13 | import fr.sii.ogham.core.service.MessagingService; | |
14 | import fr.sii.ogham.email.sendgrid.sender.SendGridSender; | |
15 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.SendGridV2Sender; | |
16 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.SendGridClient; | |
17 | import fr.sii.ogham.email.sendgrid.v4.sender.impl.SendGridV4Sender; | |
18 | import fr.sii.ogham.testing.util.HasParent; | |
19 | ||
20 | /** | |
21 | * Helper to make assertions on SendGrid instance created by Ogham. | |
22 | * | |
23 | * @author Aurélien Baudet | |
24 | * | |
25 | */ | |
26 | public class SendGridAssertions extends HasParent<MessagingServiceAssertions> { | |
27 | private static final String DELEGATE_FIELD = "delegate"; | |
28 | private final SendGridSender sendGridSender; | |
29 | ||
30 | public SendGridAssertions(MessagingServiceAssertions parent, SendGridSender sendGridSender) { | |
31 | super(parent); | |
32 | this.sendGridSender = sendGridSender; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Ensures that SendGrid apiKey is correctly configured. | |
37 | * | |
38 | * @param matcher | |
39 | * the matcher used to ensure that apiKey is correctly | |
40 | * configured. | |
41 | * @return this instance for fluent chaining | |
42 | */ | |
43 | public SendGridAssertions apiKey(Matcher<String> matcher) { | |
44 |
2
1. apiKey : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 2. apiKey : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED |
assertThat(getApiKey(sendGridSender), matcher); |
45 |
4
1. apiKey : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::apiKey → SURVIVED 2. apiKey : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::apiKey → NO_COVERAGE 3. apiKey : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::apiKey → KILLED 4. apiKey : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::apiKey → KILLED |
return this; |
46 | } | |
47 | ||
48 | /** | |
49 | * Ensures that SendGrid instance is correctly configured. | |
50 | * | |
51 | * @param matcher | |
52 | * the matcher used to ensure that instance is correctly | |
53 | * configured. | |
54 | * @return this instance for fluent chaining | |
55 | */ | |
56 | public SendGridAssertions client(Matcher<? super SendGrid> matcher) { | |
57 |
2
1. client : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 2. client : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED |
assertThat(getClient(sendGridSender), matcher); |
58 |
3
1. client : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::client → NO_COVERAGE 2. client : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::client → KILLED 3. client : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::client → KILLED |
return this; |
59 | } | |
60 | ||
61 | /** | |
62 | * Find the {@link SendGridSender} instance (one of {@link SendGridV2Sender} | |
63 | * or {@link SendGridV4Sender}). | |
64 | * | |
65 | * @param messagingService | |
66 | * the messaging service | |
67 | * @param senderClass | |
68 | * which of {@link SendGridV2Sender} or {@link SendGridV4Sender} | |
69 | * @return the found instance | |
70 | */ | |
71 | public static SendGridSender getSendGridSender(MessagingService messagingService, Class<? extends SendGridSender> senderClass) { | |
72 |
1
1. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → NO_COVERAGE |
return findSender(messagingService, senderClass); |
73 | } | |
74 | ||
75 | /** | |
76 | * Find the {@link SendGridSender} instance (one of {@link SendGridV2Sender} | |
77 | * or {@link SendGridV4Sender}) based on the classpath. If | |
78 | * {@link SendGridV4Sender} sender is found, return this instance. If not | |
79 | * found, tried to get {@link SendGridV2Sender} instance. | |
80 | * | |
81 | * @param messagingService the messaging service | |
82 | * @return the found instance | |
83 | */ | |
84 | public static SendGridSender getSendGridSender(MessagingService messagingService) { | |
85 | try { | |
86 |
3
1. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → NO_COVERAGE 2. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → KILLED 3. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → KILLED |
return findSender(messagingService, SendGridV4Sender.class); |
87 | } catch (IllegalStateException e) { // NOSONAR | |
88 | // skip | |
89 | } | |
90 | try { | |
91 |
2
1. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → NO_COVERAGE 2. getSendGridSender : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getSendGridSender → KILLED |
return findSender(messagingService, SendGridV2Sender.class); |
92 | } catch (IllegalStateException e) { // NOSONAR | |
93 | // skip | |
94 | } | |
95 | throw new IllegalStateException("No SendGridSender available"); | |
96 | } | |
97 | ||
98 | private static String getApiKey(SendGridSender sendGridSender) { | |
99 | SendGrid client = getClient(sendGridSender); | |
100 | try { | |
101 |
4
1. getApiKey : negated conditional → NO_COVERAGE 2. getApiKey : negated conditional → KILLED 3. getApiKey : negated conditional → KILLED 4. getApiKey : negated conditional → KILLED |
if (sendGridSender instanceof SendGridV2Sender) { |
102 |
2
1. getApiKey : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKey → NO_COVERAGE 2. getApiKey : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKey → TIMED_OUT |
return (String) readField(client, "password", true); |
103 | } | |
104 |
3
1. getApiKey : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKey → NO_COVERAGE 2. getApiKey : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKey → KILLED 3. getApiKey : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKey → KILLED |
return getApiKeyFromFieldOrHeaders(client); |
105 | } catch (IllegalAccessException e) { | |
106 | throw new IllegalStateException("Failed to read 'apiKey' of SendGrid", e); | |
107 | } | |
108 | } | |
109 | ||
110 | @SuppressWarnings({ "squid:S1166", "unchecked" }) | |
111 | private static String getApiKeyFromFieldOrHeaders(SendGrid client) throws IllegalAccessException { | |
112 | try { | |
113 |
1
1. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → NO_COVERAGE |
return (String) readField(client, "apiKey", true); |
114 | } catch (IllegalArgumentException e) { | |
115 | Map<String, String> requestHeaders = (Map<String, String>) readField(client, "requestHeaders", true); | |
116 | String authHeader = requestHeaders.get("Authorization"); | |
117 |
3
1. getApiKeyFromFieldOrHeaders : negated conditional → NO_COVERAGE 2. getApiKeyFromFieldOrHeaders : negated conditional → KILLED 3. getApiKeyFromFieldOrHeaders : negated conditional → KILLED |
if (authHeader == null) { |
118 |
1
1. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → NO_COVERAGE |
return null; |
119 | } | |
120 | String apiKey = authHeader.substring(7); | |
121 | // special case to be compatible with previous versions | |
122 |
3
1. getApiKeyFromFieldOrHeaders : negated conditional → NO_COVERAGE 2. getApiKeyFromFieldOrHeaders : negated conditional → KILLED 3. getApiKeyFromFieldOrHeaders : negated conditional → KILLED |
if ("null".equals(apiKey)) { |
123 |
2
1. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → NO_COVERAGE 2. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → KILLED |
return null; |
124 | } | |
125 |
3
1. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → NO_COVERAGE 2. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → KILLED 3. getApiKeyFromFieldOrHeaders : replaced return value with "" for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getApiKeyFromFieldOrHeaders → KILLED |
return apiKey; |
126 | } | |
127 | } | |
128 | ||
129 | private static SendGrid getClient(SendGridSender sendGridSender) { | |
130 | try { | |
131 |
4
1. getClient : negated conditional → NO_COVERAGE 2. getClient : negated conditional → KILLED 3. getClient : negated conditional → KILLED 4. getClient : negated conditional → KILLED |
if (sendGridSender instanceof SendGridV2Sender) { |
132 | SendGridClient wrapper = (SendGridClient) readField(sendGridSender, DELEGATE_FIELD, true); | |
133 |
2
1. getClient : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getClient → NO_COVERAGE 2. getClient : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getClient → KILLED |
return (SendGrid) readField(wrapper, DELEGATE_FIELD, true); |
134 |
3
1. getClient : negated conditional → NO_COVERAGE 2. getClient : negated conditional → KILLED 3. getClient : negated conditional → KILLED |
} else if (sendGridSender instanceof SendGridV4Sender) { |
135 | fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.client.SendGridClient wrapper = (fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.client.SendGridClient) readField( | |
136 | sendGridSender, DELEGATE_FIELD, true); | |
137 |
3
1. getClient : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getClient → NO_COVERAGE 2. getClient : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getClient → KILLED 3. getClient : replaced return value with null for fr/sii/ogham/testing/assertion/internal/SendGridAssertions::getClient → KILLED |
return (SendGrid) readField(wrapper, DELEGATE_FIELD, true); |
138 | } | |
139 | } catch (IllegalAccessException e) { | |
140 | throw new IllegalStateException("Failed to read 'delegate' of SendGridClient", e); | |
141 | } | |
142 | return null; | |
143 | } | |
144 | } | |
Mutations | ||
44 |
1.1 2.2 |
|
45 |
1.1 2.2 3.3 4.4 |
|
57 |
1.1 2.2 |
|
58 |
1.1 2.2 3.3 |
|
72 |
1.1 |
|
86 |
1.1 2.2 3.3 |
|
91 |
1.1 2.2 |
|
101 |
1.1 2.2 3.3 4.4 |
|
102 |
1.1 2.2 |
|
104 |
1.1 2.2 3.3 |
|
113 |
1.1 |
|
117 |
1.1 2.2 3.3 |
|
118 |
1.1 |
|
122 |
1.1 2.2 3.3 |
|
123 |
1.1 2.2 |
|
125 |
1.1 2.2 3.3 |
|
131 |
1.1 2.2 3.3 4.4 |
|
133 |
1.1 2.2 |
|
134 |
1.1 2.2 3.3 |
|
137 |
1.1 2.2 3.3 |