1 | package fr.sii.ogham.testing.assertion.email; | |
2 | ||
3 | import static fr.sii.ogham.testing.assertion.util.AssertionHelper.assertThat; | |
4 | import static fr.sii.ogham.testing.assertion.util.AssertionHelper.usingContext; | |
5 | import static fr.sii.ogham.testing.assertion.util.EmailUtils.getAlternativePart; | |
6 | import static fr.sii.ogham.testing.assertion.util.EmailUtils.getAttachments; | |
7 | import static fr.sii.ogham.testing.assertion.util.EmailUtils.getBodyPart; | |
8 | import static java.util.Arrays.asList; | |
9 | import static javax.mail.Message.RecipientType.CC; | |
10 | import static javax.mail.Message.RecipientType.TO; | |
11 | import static org.hamcrest.Matchers.hasSize; | |
12 | import static org.hamcrest.Matchers.lessThanOrEqualTo; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.Arrays; | |
16 | import java.util.Collection; | |
17 | import java.util.List; | |
18 | import java.util.function.Predicate; | |
19 | ||
20 | import javax.mail.BodyPart; | |
21 | import javax.mail.Message; | |
22 | import javax.mail.MessagingException; | |
23 | import javax.mail.Part; | |
24 | import javax.mail.internet.InternetAddress; | |
25 | ||
26 | import org.hamcrest.Matcher; | |
27 | ||
28 | import fr.sii.ogham.testing.assertion.context.SingleMessageContext; | |
29 | import fr.sii.ogham.testing.assertion.filter.FileNamePredicate; | |
30 | import fr.sii.ogham.testing.assertion.util.AssertionRegistry; | |
31 | import fr.sii.ogham.testing.assertion.util.EmailUtils; | |
32 | import fr.sii.ogham.testing.util.HasParent; | |
33 | ||
34 | @SuppressWarnings("squid:S1192") | |
35 | public class FluentEmailAssert<P> extends HasParent<P> { | |
36 | /** | |
37 | * The list of messages that will be used for assertions | |
38 | */ | |
39 | private final List<? extends Message> actual; | |
40 | private int index; | |
41 | /** | |
42 | * Registry to register assertions | |
43 | */ | |
44 | private final AssertionRegistry registry; | |
45 | ||
46 | public FluentEmailAssert(Message actual, int index, P parent, AssertionRegistry registry) { | |
47 | this(Arrays.asList(actual), parent, registry); | |
48 | this.index = index; | |
49 | } | |
50 | ||
51 | public FluentEmailAssert(List<? extends Message> actual, P parent, AssertionRegistry registry) { | |
52 | super(parent); | |
53 | this.actual = actual; | |
54 | this.registry = registry; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Make assertions on the subject of the message(s). | |
59 | * | |
60 | * <pre> | |
61 | * .receivedMessages().message(0) | |
62 | * .subject(is("foobar")) | |
63 | * </pre> | |
64 | * | |
65 | * Will check if the subject of the first message is exactly "foobar". | |
66 | * | |
67 | * <pre> | |
68 | * .receivedMessages().every() | |
69 | * .subject(is("foobar")) | |
70 | * </pre> | |
71 | * | |
72 | * Will check that the subject of every message is exactly "foobar". | |
73 | * | |
74 | * @param matcher | |
75 | * the assertion to apply on subject | |
76 | * @return the fluent API for chaining assertions on received message(s) | |
77 | */ | |
78 | public FluentEmailAssert<P> subject(Matcher<? super String> matcher) { | |
79 | try { | |
80 | String desc = "subject of message ${messageIndex}"; | |
81 | int msgIdx = this.index; | |
82 | for (Message message : actual) { | |
83 | final int idx = msgIdx; | |
84 |
6
1. lambda$subject$0 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 2. lambda$subject$0 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 3. subject : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 4. subject : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → SURVIVED 5. lambda$subject$0 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → KILLED 6. subject : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED |
registry.register(() -> assertThat(message.getSubject(), usingContext(desc, new SingleMessageContext(idx), matcher))); |
85 |
3
1. subject : Changed increment from 1 to -1 → SURVIVED 2. subject : Changed increment from 1 to -1 → NO_COVERAGE 3. subject : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
86 | } | |
87 |
4
1. subject : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::subject → NO_COVERAGE 2. subject : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::subject → KILLED 3. subject : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::subject → KILLED 4. subject : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::subject → KILLED |
return this; |
88 | } catch (MessagingException e) { | |
89 | throw new AssertionError("Failed to get subject of messsage", e); | |
90 | } | |
91 | } | |
92 | ||
93 | /** | |
94 | * Make assertions on the body of the message(s) using fluent API. The body | |
95 | * of the message is either: | |
96 | * <ul> | |
97 | * <li>The part of the message when it contains only one part</li> | |
98 | * <li>The part with text or HTML mimetype if only one part with one of that | |
99 | * mimetype</li> | |
100 | * <li>The second part with text or HTML mimetype if there are two text or | |
101 | * HTML parts</li> | |
102 | * </ul> | |
103 | * | |
104 | * <pre> | |
105 | * .receivedMessages().message(0).body() | |
106 | * .contentAsString(is("email body")) | |
107 | * .contentType(is("text/plain")) | |
108 | * </pre> | |
109 | * | |
110 | * Will check if the body content of the first message is "email body" and | |
111 | * content-type of the first message is "text/plain". | |
112 | * | |
113 | * <pre> | |
114 | * .receivedMessages().every().body() | |
115 | * .contentAsString(is("email body")) | |
116 | * .contentType(is("text/plain")) | |
117 | * </pre> | |
118 | * | |
119 | * Will check that the body content of every message is "email body" and | |
120 | * content-type of every message is "text/plain". | |
121 | * | |
122 | * @return the fluent API for chaining assertions on received message(s) | |
123 | */ | |
124 | public FluentPartAssert<FluentEmailAssert<P>> body() { | |
125 | try { | |
126 | int msgIdx = this.index; | |
127 | List<PartWithContext> bodies = new ArrayList<>(); | |
128 | for (Message message : actual) { | |
129 | bodies.add(new PartWithContext(getBodyPart(message), "body", new SingleMessageContext(msgIdx))); | |
130 |
4
1. body : Changed increment from 1 to -1 → SURVIVED 2. body : Changed increment from 1 to -1 → NO_COVERAGE 3. body : Changed increment from 1 to -1 → TIMED_OUT 4. body : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
131 | } | |
132 |
5
1. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → NO_COVERAGE 2. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → TIMED_OUT 3. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → KILLED 4. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → KILLED 5. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → KILLED |
return new FluentPartAssert<>(bodies, this, registry); |
133 | } catch (MessagingException e) { | |
134 | throw new AssertionError("Failed to get body of messsage", e); | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Make assertions on the alternative part of the message(s) using fluent | |
140 | * API. The alternative is useful when sending HTML email that may be | |
141 | * unreadable on some email clients. For example, a smartphone will display | |
142 | * the 2 or 3 first lines as a summary. Many smartphones will take the HTML | |
143 | * message as-is and will display HTML tags instead of content of email. | |
144 | * Alternative is used to provide a textual visualization of the message | |
145 | * that will be readable by any system. | |
146 | * | |
147 | * <p> | |
148 | * The alternative of the message is either: | |
149 | * <ul> | |
150 | * <li>null if there is only one part</li> | |
151 | * <li>null if there is only one text or HTML part</li> | |
152 | * <li>the first part if there are more than one text or HTML part</li> | |
153 | * </ul> | |
154 | * | |
155 | * <pre> | |
156 | * .receivedMessages().message(0).alternative() | |
157 | * .contentAsString(is("email alternative")) | |
158 | * .contentType(is("text/plain")) | |
159 | * </pre> | |
160 | * | |
161 | * Will check if the body content of the first message is "email | |
162 | * alternative" and content-type of the first message is "text/plain". | |
163 | * | |
164 | * <pre> | |
165 | * .receivedMessages().every().alternative() | |
166 | * .contentAsString(is("email alternative")) | |
167 | * .contentType(is("text/plain")) | |
168 | * </pre> | |
169 | * | |
170 | * Will check that the body content of every message is "email alternative" | |
171 | * and content-type of every message is "text/plain". | |
172 | * | |
173 | * @return the fluent API for chaining assertions on received message(s) | |
174 | */ | |
175 | public FluentPartAssert<FluentEmailAssert<P>> alternative() { | |
176 | try { | |
177 | int msgIdx = this.index; | |
178 | List<PartWithContext> bodies = new ArrayList<>(); | |
179 | for (Message message : actual) { | |
180 | bodies.add(new PartWithContext(getAlternativePart(message), "alternative", new SingleMessageContext(msgIdx))); | |
181 |
3
1. alternative : Changed increment from 1 to -1 → NO_COVERAGE 2. alternative : Changed increment from 1 to -1 → SURVIVED 3. alternative : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
182 | } | |
183 |
3
1. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → NO_COVERAGE 2. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → KILLED 3. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → KILLED |
return new FluentPartAssert<>(bodies, this, registry); |
184 | } catch (MessagingException e) { | |
185 | throw new AssertionError("Failed to get body of messsage", e); | |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * Make assertions on the body of the message(s). The body of the message is | |
191 | * either: | |
192 | * <ul> | |
193 | * <li>The part of the message when it contains only one part</li> | |
194 | * <li>The part with text or HTML mimetype if only one part with one of that | |
195 | * mimetype</li> | |
196 | * <li>The second part with text or HTML mimetype if there are two text or | |
197 | * HTML parts</li> | |
198 | * </ul> | |
199 | * | |
200 | * <pre> | |
201 | * .receivedMessages().message(0) | |
202 | * .body(allOf(notNullValue(), instanceOf(MimeBodyPart.class)) | |
203 | * </pre> | |
204 | * | |
205 | * Will check if the body of the first message is not null and is a | |
206 | * MimeBodyPart instance. | |
207 | * | |
208 | * <pre> | |
209 | * .receivedMessages().every() | |
210 | * .body(allOf(notNullValue(), instanceOf(MimeBodyPart.class)) | |
211 | * </pre> | |
212 | * | |
213 | * Will check that the body of every message is not null and is a | |
214 | * MimeBodyPart instance. | |
215 | * | |
216 | * <p> | |
217 | * You can use the {@link #body()} variant to make more powerful assertions. | |
218 | * | |
219 | * @param matcher | |
220 | * the assertion to apply on body | |
221 | * @param <T> | |
222 | * the type used for the matcher | |
223 | * @return the fluent API for chaining assertions on received message(s) | |
224 | */ | |
225 | public <T extends Part> FluentEmailAssert<P> body(Matcher<? super Part> matcher) { // NOSONAR | |
226 | try { | |
227 | String desc = "body of message ${messageIndex}"; | |
228 | int msgIdx = this.index; | |
229 | for (Message message : actual) { | |
230 | final int idx = msgIdx; | |
231 |
6
1. body : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → SURVIVED 2. body : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 3. lambda$body$1 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 4. lambda$body$1 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 5. body : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 6. lambda$body$1 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → KILLED |
registry.register(() -> assertThat(getBodyPart(message), usingContext(desc, new SingleMessageContext(idx), matcher))); |
232 |
3
1. body : Changed increment from 1 to -1 → SURVIVED 2. body : Changed increment from 1 to -1 → NO_COVERAGE 3. body : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
233 | } | |
234 |
3
1. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → NO_COVERAGE 2. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → KILLED 3. body : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::body → KILLED |
return this; |
235 | } catch (MessagingException e) { | |
236 | throw new AssertionError("Failed to access attachments of messsage", e); | |
237 | } | |
238 | } | |
239 | ||
240 | /** | |
241 | * Make assertions on the alternative part of the message(s). The | |
242 | * alternative is useful when sending HTML email that may be unreadable on | |
243 | * some email clients. For example, a smartphone will display the 2 or 3 | |
244 | * first lines as a summary. Many smartphones will take the HTML message | |
245 | * as-is and will display HTML tags instead of content of email. Alternative | |
246 | * is used to provide a textual visualization of the message that will be | |
247 | * readable by any system. | |
248 | * | |
249 | * <p> | |
250 | * The alternative of the message is either: | |
251 | * <ul> | |
252 | * <li>null if there is only one part</li> | |
253 | * <li>null if there is only one text or HTML part</li> | |
254 | * <li>the first part if there are more than one text or HTML part</li> | |
255 | * </ul> | |
256 | * | |
257 | * <pre> | |
258 | * .receivedMessages().message(0) | |
259 | * .alternative(allOf(notNullValue(), instanceOf(MimeBodyPart.class)) | |
260 | * </pre> | |
261 | * | |
262 | * Will check if the alternative of the first message is not null and is a | |
263 | * MimeBodyPart instance. | |
264 | * | |
265 | * <pre> | |
266 | * .receivedMessages().every() | |
267 | * .alternative(allOf(notNullValue(), instanceOf(MimeBodyPart.class)) | |
268 | * </pre> | |
269 | * | |
270 | * Will check that the alternative of every message is not null and is a | |
271 | * MimeBodyPart instance. | |
272 | * | |
273 | * <p> | |
274 | * You can use the {@link #alternative()} variant to make more powerful | |
275 | * assertions. | |
276 | * | |
277 | * @param matcher | |
278 | * the assertion to apply on alternative | |
279 | * @param <T> | |
280 | * the type used for the matcher | |
281 | * @return the fluent API for chaining assertions on received message(s) | |
282 | */ | |
283 | public <T extends Part> FluentEmailAssert<P> alternative(Matcher<? super Part> matcher) { // NOSONAR | |
284 | try { | |
285 | String desc = "alternative of message ${messageIndex}"; | |
286 | int msgIdx = this.index; | |
287 | for (Message message : actual) { | |
288 | final int idx = msgIdx; | |
289 |
6
1. alternative : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. alternative : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → SURVIVED 3. lambda$alternative$2 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 4. lambda$alternative$2 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 5. alternative : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 6. lambda$alternative$2 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → KILLED |
registry.register(() -> assertThat(getAlternativePart(message), usingContext(desc, new SingleMessageContext(idx), matcher))); |
290 |
3
1. alternative : Changed increment from 1 to -1 → SURVIVED 2. alternative : Changed increment from 1 to -1 → NO_COVERAGE 3. alternative : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
291 | } | |
292 |
4
1. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → NO_COVERAGE 2. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → KILLED 3. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → KILLED 4. alternative : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::alternative → KILLED |
return this; |
293 | } catch (MessagingException e) { | |
294 | throw new AssertionError("Failed to access attachments of messsage", e); | |
295 | } | |
296 | } | |
297 | ||
298 | /** | |
299 | * Make assertions on the sender of the message(s) using fluent API. | |
300 | * | |
301 | * <pre> | |
302 | * .receivedMessages().message(0).from() | |
303 | * .address(hasItems("noreply@sii.fr")) | |
304 | * .personal(hasItems("Groupe SII")) | |
305 | * </pre> | |
306 | * | |
307 | * Will check if the sender email address of the first message is exactly | |
308 | * "noreply@sii.fr" and sender displayed address of the first message is | |
309 | * exactly "Groupe SII". | |
310 | * | |
311 | * <pre> | |
312 | * .receivedMessages().every().from() | |
313 | * .address(hasItems("noreply@sii.fr")) | |
314 | * .personal(hasItems("Groupe SII")) | |
315 | * </pre> | |
316 | * | |
317 | * Will check if the sender email address of every message is exactly | |
318 | * "noreply@sii.fr" and sender displayed address of every message is exactly | |
319 | * "Groupe SII". | |
320 | * | |
321 | * @return the fluent API for chaining assertions on received message(s) | |
322 | */ | |
323 | public FluentAddressListAssert<FluentEmailAssert<P>> from() { | |
324 | try { | |
325 | int msgIdx = this.index; | |
326 | List<AddressesWithContext> addresses = new ArrayList<>(); | |
327 | for (Message message : actual) { | |
328 | addresses.add(new AddressesWithContext(asList((InternetAddress[]) message.getFrom()), "from", new SingleMessageContext(msgIdx))); | |
329 |
3
1. from : Changed increment from 1 to -1 → SURVIVED 2. from : Changed increment from 1 to -1 → NO_COVERAGE 3. from : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
330 | } | |
331 |
4
1. from : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::from → NO_COVERAGE 2. from : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::from → KILLED 3. from : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::from → KILLED 4. from : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::from → KILLED |
return new FluentAddressListAssert<>(addresses, this, registry); |
332 | } catch (MessagingException e) { | |
333 | throw new AssertionError("Failed to get from field of messsage", e); | |
334 | } | |
335 | } | |
336 | ||
337 | /** | |
338 | * Make assertions on the sender of the message(s) using fluent API. | |
339 | * | |
340 | * <pre> | |
341 | * .receivedMessages().message(0).to() | |
342 | * .address(hasItems("recipient1@sii.fr", "recipient2@sii.fr")) | |
343 | * .personal(hasItems("Foo", "Bar")) | |
344 | * </pre> | |
345 | * | |
346 | * Will check if the list of email addresses of direct recipients (TO) of | |
347 | * the first message are exactly "recipient1@sii.fr", "recipient2@sii.fr" | |
348 | * and the list of displayed address of direct recipients (TO) of the first | |
349 | * message are exactly "Foo", "Bar". | |
350 | * | |
351 | * <pre> | |
352 | * .receivedMessages().every().to() | |
353 | * .address(hasItems("recipient1@sii.fr", "recipient2@sii.fr")) | |
354 | * .personal(hasItems("Foo", "Bar")) | |
355 | * </pre> | |
356 | * | |
357 | * Will check if the list of email addresses of direct recipients (TO) of | |
358 | * every message are exactly "recipient1@sii.fr", "recipient2@sii.fr" and | |
359 | * the list of displayed address of direct recipients (TO) of every message | |
360 | * are exactly "Foo", "Bar". | |
361 | * | |
362 | * @return the fluent API for chaining assertions on received message(s) | |
363 | */ | |
364 | public FluentAddressListAssert<FluentEmailAssert<P>> to() { | |
365 | try { | |
366 | int msgIdx = this.index; | |
367 | List<AddressesWithContext> addresses = new ArrayList<>(); | |
368 | for (Message message : actual) { | |
369 | addresses.add(new AddressesWithContext(asList((InternetAddress[]) message.getRecipients(TO)), "to", new SingleMessageContext(msgIdx))); | |
370 |
3
1. to : Changed increment from 1 to -1 → NO_COVERAGE 2. to : Changed increment from 1 to -1 → SURVIVED 3. to : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
371 | } | |
372 |
4
1. to : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::to → NO_COVERAGE 2. to : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::to → KILLED 3. to : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::to → KILLED 4. to : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::to → KILLED |
return new FluentAddressListAssert<>(addresses, this, registry); |
373 | } catch (MessagingException e) { | |
374 | throw new AssertionError("Failed to get to field of messsage", e); | |
375 | } | |
376 | } | |
377 | ||
378 | /** | |
379 | * Make assertions on the sender of the message(s) using fluent API. | |
380 | * | |
381 | * <pre> | |
382 | * .receivedMessages().message(0).cc() | |
383 | * .address(hasItems("recipient1@sii.fr", "recipient2@sii.fr")) | |
384 | * .personal(hasItems("Foo", "Bar")) | |
385 | * </pre> | |
386 | * | |
387 | * Will check if the list of email addresses of copy recipients (CC) of the | |
388 | * first message are exactly "recipient1@sii.fr", "recipient2@sii.fr" and | |
389 | * the list of displayed address of copy recipients (CC) of the first | |
390 | * message are exactly "Foo", "Bar". | |
391 | * | |
392 | * <pre> | |
393 | * .receivedMessages().every().cc() | |
394 | * .address(hasItems("recipient1@sii.fr", "recipient2@sii.fr")) | |
395 | * .personal(hasItems("Foo", "Bar")) | |
396 | * </pre> | |
397 | * | |
398 | * Will check if the list of email addresses of copy recipients (CC) of | |
399 | * every message are exactly "recipient1@sii.fr", "recipient2@sii.fr" and | |
400 | * the list of displayed address of copy recipients (CC) of every message | |
401 | * are exactly "Foo", "Bar". | |
402 | * | |
403 | * @return the fluent API for chaining assertions on received message(s) | |
404 | */ | |
405 | public FluentAddressListAssert<FluentEmailAssert<P>> cc() { | |
406 | try { | |
407 | int msgIdx = this.index; | |
408 | List<AddressesWithContext> addresses = new ArrayList<>(); | |
409 | for (Message message : actual) { | |
410 | addresses.add(new AddressesWithContext(asList((InternetAddress[]) message.getRecipients(CC)), "cc", new SingleMessageContext(msgIdx))); | |
411 |
3
1. cc : Changed increment from 1 to -1 → NO_COVERAGE 2. cc : Changed increment from 1 to -1 → SURVIVED 3. cc : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
412 | } | |
413 |
3
1. cc : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::cc → NO_COVERAGE 2. cc : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::cc → KILLED 3. cc : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::cc → KILLED |
return new FluentAddressListAssert<>(addresses, this, registry); |
414 | } catch (MessagingException e) { | |
415 | throw new AssertionError("Failed to get cc field of messsage", e); | |
416 | } | |
417 | } | |
418 | ||
419 | /** | |
420 | * Make assertions on the list of attachments of the message(s). | |
421 | * | |
422 | * <pre> | |
423 | * .receivedMessages().message(0) | |
424 | * .attachments(hasSize(1)) | |
425 | * </pre> | |
426 | * | |
427 | * Will check if the number of attachments of the first message is exactly | |
428 | * 1. | |
429 | * | |
430 | * <pre> | |
431 | * .receivedMessages().every() | |
432 | * .attachments(hasSize(1)) | |
433 | * </pre> | |
434 | * | |
435 | * Will check that the number of attachments of every message is exactly 1. | |
436 | * | |
437 | * <p> | |
438 | * You can use the {@link #attachment(String)} or | |
439 | * {@link #attachments(Predicate)} variants to make more powerful assertions | |
440 | * on a particular attachment. | |
441 | * | |
442 | * @param matcher | |
443 | * the assertion to apply on list of attachments | |
444 | * @param <T> | |
445 | * the type used for the matcher | |
446 | * @return the fluent API for chaining assertions on received message(s) | |
447 | */ | |
448 | public <T extends Collection<? extends BodyPart>> FluentEmailAssert<P> attachments(Matcher<? super Collection<? extends BodyPart>> matcher) { // NOSONAR | |
449 | try { | |
450 | String desc = "attachments of message ${messageIndex}"; | |
451 | int msgIdx = this.index; | |
452 | for (Message message : actual) { | |
453 | final int idx = msgIdx; | |
454 |
6
1. attachments : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → SURVIVED 2. attachments : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 3. lambda$attachments$3 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 4. lambda$attachments$3 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 5. attachments : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 6. lambda$attachments$3 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → KILLED |
registry.register(() -> assertThat(getAttachments(message), usingContext(desc, new SingleMessageContext(idx), matcher))); |
455 |
3
1. attachments : Changed increment from 1 to -1 → NO_COVERAGE 2. attachments : Changed increment from 1 to -1 → SURVIVED 3. attachments : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
456 | } | |
457 |
4
1. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → NO_COVERAGE 2. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED 3. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED 4. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED |
return this; |
458 | } catch (MessagingException e) { | |
459 | throw new AssertionError("Failed to access attachments of messsage", e); | |
460 | } | |
461 | } | |
462 | ||
463 | /** | |
464 | * Make assertions on a particular attachment of the message(s) using fluent | |
465 | * API. The attachment is identified by its filename. | |
466 | * | |
467 | * <pre> | |
468 | * .receivedMessages().message(0).attachment("foo.pdf") | |
469 | * .contentType(is("application/pdf")) | |
470 | * </pre> | |
471 | * | |
472 | * Will check if the content-type of the attachment named "foo.pdf" of the | |
473 | * first message is exactly "application/pdf". | |
474 | * | |
475 | * <pre> | |
476 | * .receivedMessages().every().attachment("foo.pdf") | |
477 | * .contentType(is("application/pdf")) | |
478 | * </pre> | |
479 | * | |
480 | * Will check that the content-type of attachment named "foo.pdf" of every | |
481 | * message is exactly "application/pdf". | |
482 | * | |
483 | * <p> | |
484 | * This is a shortcut to {@link #attachments(Predicate)} with | |
485 | * {@link FileNamePredicate}; | |
486 | * | |
487 | * @param filename | |
488 | * the name of the attachment to make assertions on it | |
489 | * @return the fluent API for chaining assertions on received message(s) | |
490 | */ | |
491 | public FluentPartAssert<FluentEmailAssert<P>> attachment(String filename) { | |
492 |
4
1. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → NO_COVERAGE 2. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED 3. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED 4. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED |
return attachment(By.filename(filename)); |
493 | } | |
494 | ||
495 | /** | |
496 | * Make assertions on a particular attachment of the message(s). | |
497 | * | |
498 | * <pre> | |
499 | * .receivedMessages().message(0).attachment(0) | |
500 | * .contentType(is("application/pdf")) | |
501 | * </pre> | |
502 | * | |
503 | * Will check if the content-type of the first attachment of the first | |
504 | * message is exactly "application/pdf". | |
505 | * | |
506 | * <pre> | |
507 | * .receivedMessages().every().attachment(0) | |
508 | * .contentType(is("application/pdf")) | |
509 | * </pre> | |
510 | * | |
511 | * Will check if the content-type of the first attachment of every message | |
512 | * is exactly "application/pdf". | |
513 | * | |
514 | * @param index | |
515 | * the index of the attachment | |
516 | * @return the fluent API for chaining assertions on received message(s) | |
517 | */ | |
518 | public FluentPartAssert<FluentEmailAssert<P>> attachment(int index) { | |
519 | try { | |
520 | int msgIndex = this.index; | |
521 | List<PartWithContext> attachments = new ArrayList<>(); | |
522 | for (Message message : actual) { | |
523 | List<BodyPart> found = getAttachments(message); | |
524 |
6
1. attachment : changed conditional boundary → SURVIVED 2. attachment : changed conditional boundary → NO_COVERAGE 3. attachment : negated conditional → NO_COVERAGE 4. attachment : changed conditional boundary → KILLED 5. attachment : negated conditional → KILLED 6. attachment : negated conditional → KILLED |
BodyPart attachment = index >= found.size() ? null : found.get(index); |
525 |
3
1. attachment : negated conditional → NO_COVERAGE 2. attachment : negated conditional → SURVIVED 3. attachment : negated conditional → KILLED |
attachments.add(new PartWithContext(attachment, "attachment with index " + index + (attachment == null ? " (/!\\ not found)" : ""), new SingleMessageContext(msgIndex))); |
526 |
3
1. attachment : Changed increment from 1 to -1 → NO_COVERAGE 2. attachment : Changed increment from 1 to -1 → SURVIVED 3. attachment : Changed increment from 1 to -1 → KILLED |
msgIndex++; |
527 | } | |
528 |
3
1. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → NO_COVERAGE 2. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED 3. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED |
return new FluentPartAssert<>(attachments, this, registry); |
529 | } catch (MessagingException e) { | |
530 | throw new AssertionError("Failed to get attachment with index " + index + " of messsage", e); | |
531 | } | |
532 | } | |
533 | ||
534 | /** | |
535 | * Make assertions on a particular attachment of the message(s) using fluent | |
536 | * API. The attachment is identified using the provided finder method. | |
537 | * | |
538 | * <p> | |
539 | * If several attachments are found, it fails. If you want to make the same | |
540 | * assertions on several attachments at once, use {@link #attachments(By)} | |
541 | * instead. | |
542 | * | |
543 | * <pre> | |
544 | * .receivedMessages().message(0).attachment(By.filename("foo.pdf")) | |
545 | * .contentType(is("application/pdf")) | |
546 | * </pre> | |
547 | * | |
548 | * Will check if the content-type of the attachment named "foo.pdf" of the | |
549 | * first message is exactly "application/pdf". | |
550 | * | |
551 | * <pre> | |
552 | * .receivedMessages().every().attachment(By.filename("foo.pdf")) | |
553 | * .contentType(is("application/pdf")) | |
554 | * </pre> | |
555 | * | |
556 | * Will check that the content-type of attachment named "foo.pdf" of every | |
557 | * message is exactly "application/pdf". | |
558 | * | |
559 | * <p> | |
560 | * This is a shortcut to {@link #attachments(Predicate)} with | |
561 | * {@link FileNamePredicate}; | |
562 | * | |
563 | * @param by | |
564 | * the finder method | |
565 | * @return the fluent API for chaining assertions on received message(s) | |
566 | */ | |
567 | public FluentPartAssert<FluentEmailAssert<P>> attachment(By by) { | |
568 |
4
1. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → NO_COVERAGE 2. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED 3. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED 4. attachment : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachment → KILLED |
return attachments(by.toPredicate(), hasSize(lessThanOrEqualTo(1))); |
569 | } | |
570 | ||
571 | /** | |
572 | * Make assertions on a one or several attachments of the message(s) using | |
573 | * fluent API. The attachments are identified using provided finder method. | |
574 | * | |
575 | * <pre> | |
576 | * .receivedMessages().message(0) | |
577 | * .attachments(By.contentId("cid2")).filename(endsWith(".pdf")) | |
578 | * </pre> | |
579 | * | |
580 | * Will check if every attachment that have the Content-ID header set to | |
581 | * "cid2" of first message has a name ending with ".pdf". | |
582 | * | |
583 | * <pre> | |
584 | * .receivedMessages().every() | |
585 | * .attachments(By.contentId("cid2")).filename(endsWith(".pdf")) | |
586 | * </pre> | |
587 | * | |
588 | * Will check if every attachment that have the Content-ID header set to | |
589 | * "cid2" of every messages has a name ending with ".pdf". | |
590 | * | |
591 | * | |
592 | * @param by | |
593 | * the finder method | |
594 | * @return the fluent API for chaining assertions on received message(s) | |
595 | */ | |
596 | public FluentPartAssert<FluentEmailAssert<P>> attachments(By by) { | |
597 |
1
1. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → NO_COVERAGE |
return attachments(by.toPredicate()); |
598 | } | |
599 | ||
600 | /** | |
601 | * Make assertions on a one or several attachments of the message(s) using | |
602 | * fluent API. The attachments are identified using provided predicate. | |
603 | * | |
604 | * <pre> | |
605 | * .receivedMessages().message(0) | |
606 | * .attachments(new PdfFilter()).filename(endsWith(".pdf")) | |
607 | * </pre> | |
608 | * | |
609 | * Will check if the name of every PDF attachments of the first message are | |
610 | * named "foo.pdf". | |
611 | * | |
612 | * <pre> | |
613 | * .receivedMessages().every() | |
614 | * .attachments(new PdfFilter()).filename(endsWith(".pdf")) | |
615 | * </pre> | |
616 | * | |
617 | * Will check if the name of every PDF attachments of every message are | |
618 | * named "foo.pdf". | |
619 | * | |
620 | * | |
621 | * @param filter | |
622 | * the filter used to find attachments | |
623 | * @return the fluent API for chaining assertions on received message(s) | |
624 | */ | |
625 | public FluentPartAssert<FluentEmailAssert<P>> attachments(Predicate<Part> filter) { | |
626 |
2
1. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → NO_COVERAGE 2. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED |
return attachments(filter, null); |
627 | } | |
628 | | |
629 | private FluentPartAssert<FluentEmailAssert<P>> attachments(Predicate<Part> filter, Matcher<? super Collection<? extends Part>> singleMessageAttachmentsMatcher) { | |
630 | try { | |
631 | int msgIdx = this.index; | |
632 | List<PartWithContext> attachments = new ArrayList<>(); | |
633 | for (Message message : actual) { | |
634 | int matchingIdx = 0; | |
635 | boolean noneFound = true; | |
636 | List<Part> matchingAttachmentsForMessage = EmailUtils.<Part> getAttachments(message, filter); | |
637 |
2
1. attachments : removed call to fr/sii/ogham/testing/assertion/email/FluentEmailAssert::checkFoundAttachmentPerMessage → NO_COVERAGE 2. attachments : removed call to fr/sii/ogham/testing/assertion/email/FluentEmailAssert::checkFoundAttachmentPerMessage → SURVIVED |
checkFoundAttachmentPerMessage(filter, singleMessageAttachmentsMatcher, msgIdx, matchingAttachmentsForMessage); |
638 |
3
1. attachments : removed call to fr/sii/ogham/testing/assertion/email/FluentEmailAssert::contextualize → SURVIVED 2. attachments : removed call to fr/sii/ogham/testing/assertion/email/FluentEmailAssert::contextualize → NO_COVERAGE 3. attachments : removed call to fr/sii/ogham/testing/assertion/email/FluentEmailAssert::contextualize → KILLED |
contextualize(filter, msgIdx, attachments, matchingIdx, noneFound, matchingAttachmentsForMessage); |
639 |
3
1. attachments : Changed increment from 1 to -1 → NO_COVERAGE 2. attachments : Changed increment from 1 to -1 → SURVIVED 3. attachments : Changed increment from 1 to -1 → KILLED |
msgIdx++; |
640 | } | |
641 |
4
1. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → NO_COVERAGE 2. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED 3. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED 4. attachments : replaced return value with null for fr/sii/ogham/testing/assertion/email/FluentEmailAssert::attachments → KILLED |
return new FluentPartAssert<>(attachments, this, registry); |
642 | } catch (MessagingException e) { | |
643 | throw new AssertionError("Failed to get attachment " + filter + " of messsage", e); | |
644 | } | |
645 | } | |
646 | ||
647 | private static void contextualize(Predicate<Part> filter, int msgIdx, List<PartWithContext> attachments, int matchingIdx, boolean noneFound, List<Part> matchingAttachmentsForMessage) { | |
648 | for (Part attachment : matchingAttachmentsForMessage) { | |
649 | noneFound = false; | |
650 | attachments.add(new PartWithContext(attachment, "attachment " + filter + " (matching index: " + matchingIdx + ")", new SingleMessageContext(msgIdx))); | |
651 |
3
1. contextualize : Changed increment from 1 to -1 → NO_COVERAGE 2. contextualize : Changed increment from 1 to -1 → SURVIVED 3. contextualize : Changed increment from 1 to -1 → KILLED |
matchingIdx++; |
652 | } | |
653 |
4
1. contextualize : negated conditional → NO_COVERAGE 2. contextualize : negated conditional → KILLED 3. contextualize : negated conditional → KILLED 4. contextualize : negated conditional → KILLED |
if (noneFound) { |
654 | attachments.add(new PartWithContext(null, "attachment " + filter + " (/!\\ not found)", new SingleMessageContext(msgIdx))); | |
655 | } | |
656 | } | |
657 | ||
658 | private void checkFoundAttachmentPerMessage(Predicate<Part> filter, Matcher<? super Collection<? extends Part>> singleMessageAttachmentsMatcher, int msgIdx, | |
659 | List<Part> matchingAttachmentsForMessage) { | |
660 |
3
1. checkFoundAttachmentPerMessage : negated conditional → SURVIVED 2. checkFoundAttachmentPerMessage : negated conditional → NO_COVERAGE 3. checkFoundAttachmentPerMessage : negated conditional → KILLED |
if (singleMessageAttachmentsMatcher != null) { |
661 |
4
1. checkFoundAttachmentPerMessage : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. checkFoundAttachmentPerMessage : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → SURVIVED 3. lambda$checkFoundAttachmentPerMessage$4 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 4. lambda$checkFoundAttachmentPerMessage$4 : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE |
registry.register(() -> assertThat("message "+msgIdx+" should have only one attachment "+filter, matchingAttachmentsForMessage, singleMessageAttachmentsMatcher)); |
662 | } | |
663 | } | |
664 | } | |
Mutations | ||
84 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
85 |
1.1 2.2 3.3 |
|
87 |
1.1 2.2 3.3 4.4 |
|
130 |
1.1 2.2 3.3 4.4 |
|
132 |
1.1 2.2 3.3 4.4 5.5 |
|
181 |
1.1 2.2 3.3 |
|
183 |
1.1 2.2 3.3 |
|
231 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
232 |
1.1 2.2 3.3 |
|
234 |
1.1 2.2 3.3 |
|
289 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
290 |
1.1 2.2 3.3 |
|
292 |
1.1 2.2 3.3 4.4 |
|
329 |
1.1 2.2 3.3 |
|
331 |
1.1 2.2 3.3 4.4 |
|
370 |
1.1 2.2 3.3 |
|
372 |
1.1 2.2 3.3 4.4 |
|
411 |
1.1 2.2 3.3 |
|
413 |
1.1 2.2 3.3 |
|
454 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
455 |
1.1 2.2 3.3 |
|
457 |
1.1 2.2 3.3 4.4 |
|
492 |
1.1 2.2 3.3 4.4 |
|
524 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
525 |
1.1 2.2 3.3 |
|
526 |
1.1 2.2 3.3 |
|
528 |
1.1 2.2 3.3 |
|
568 |
1.1 2.2 3.3 4.4 |
|
597 |
1.1 |
|
626 |
1.1 2.2 |
|
637 |
1.1 2.2 |
|
638 |
1.1 2.2 3.3 |
|
639 |
1.1 2.2 3.3 |
|
641 |
1.1 2.2 3.3 4.4 |
|
651 |
1.1 2.2 3.3 |
|
653 |
1.1 2.2 3.3 4.4 |
|
660 |
1.1 2.2 3.3 |
|
661 |
1.1 2.2 3.3 4.4 |