1 | package fr.sii.ogham.testing.assertion.email; | |
2 | ||
3 | import static fr.sii.ogham.testing.assertion.util.EmailUtils.getAttachment; | |
4 | import static fr.sii.ogham.testing.assertion.util.EmailUtils.getContent; | |
5 | ||
6 | import javax.mail.BodyPart; | |
7 | import javax.mail.Message; | |
8 | import javax.mail.MessagingException; | |
9 | import javax.mail.Multipart; | |
10 | ||
11 | import org.junit.Assert; | |
12 | import org.junit.internal.ArrayComparisonFailure; | |
13 | ||
14 | import fr.sii.ogham.testing.assertion.util.AssertionRegistry; | |
15 | import fr.sii.ogham.testing.assertion.util.Executable; | |
16 | import fr.sii.ogham.testing.assertion.util.FailAtEndRegistry; | |
17 | ||
18 | /** | |
19 | * Utility class for checking the attachment of the received email is as | |
20 | * expected. | |
21 | * | |
22 | * @author Aurélien Baudet | |
23 | * | |
24 | */ | |
25 | public final class AssertAttachment { | |
26 | /** | |
27 | * Shortcut for use with GreenMail. See | |
28 | * {@link #assertEquals(ExpectedAttachment, Message)}. | |
29 | * | |
30 | * @param expected | |
31 | * the expected attachment values | |
32 | * @param actual | |
33 | * the received email that must contain the attachment (the array | |
34 | * must have only one email) | |
35 | * @throws Exception | |
36 | * when access to message has failed | |
37 | */ | |
38 | public static void assertEquals(ExpectedAttachment expected, Message[] actual) throws Exception { | |
39 | AssertionRegistry registry = new FailAtEndRegistry(); | |
40 |
4
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$0 : removed call to org/junit/Assert::assertEquals → NO_COVERAGE 3. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 4. lambda$assertEquals$0 : removed call to org/junit/Assert::assertEquals → KILLED |
registry.register(() -> Assert.assertEquals("should have only one message", 1, actual.length)); |
41 |
4
1. assertEquals : negated conditional → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE 3. assertEquals : negated conditional → KILLED 4. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → KILLED |
assertEquals(expected, actual.length == 1 ? actual[0] : null, registry); |
42 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → KILLED |
registry.execute(); |
43 | } | |
44 | ||
45 | /** | |
46 | * Checks if the received email contains the expected attachment. It also | |
47 | * ensures that the values of the attachment are respected by checking: | |
48 | * <ul> | |
49 | * <li>The mimetype of the attachment</li> | |
50 | * <li>The description of the attachment</li> | |
51 | * <li>The disposition of the attachment</li> | |
52 | * <li>The content of the attachment</li> | |
53 | * </ul> | |
54 | * | |
55 | * @param expected | |
56 | * the expected attachment values | |
57 | * @param actual | |
58 | * the received email that must contain the attachment | |
59 | * @throws Exception | |
60 | * when access to message has failed | |
61 | */ | |
62 | public static void assertEquals(ExpectedAttachment expected, Message actual) throws Exception { | |
63 | AssertionRegistry registry = new FailAtEndRegistry(); | |
64 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → KILLED |
assertEquals(expected, actual, registry); |
65 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → KILLED |
registry.execute(); |
66 | } | |
67 | ||
68 | /** | |
69 | * It ensures that the values of the received attachment are respected by | |
70 | * checking: | |
71 | * <ul> | |
72 | * <li>The mimetype of the attachment</li> | |
73 | * <li>The description of the attachment</li> | |
74 | * <li>The disposition of the attachment</li> | |
75 | * <li>The content of the attachment</li> | |
76 | * </ul> | |
77 | * | |
78 | * @param expected | |
79 | * the expected attachment values | |
80 | * @param attachment | |
81 | * the received attachment | |
82 | * @throws ArrayComparisonFailure | |
83 | * when there are unexpected differences | |
84 | * @throws Exception | |
85 | * when access to part has failed | |
86 | */ | |
87 | public static void assertEquals(ExpectedAttachment expected, BodyPart attachment) throws Exception { | |
88 | AssertionRegistry registry = new FailAtEndRegistry(); | |
89 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → KILLED |
assertEquals(expected, attachment, registry); |
90 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → KILLED |
registry.execute(); |
91 | } | |
92 | ||
93 | private static void assertEquals(ExpectedAttachment expected, Message actual, AssertionRegistry registry) throws Exception { | |
94 |
2
1. assertEquals : negated conditional → NO_COVERAGE 2. assertEquals : negated conditional → KILLED |
Object content = actual == null ? null : actual.getContent(); |
95 |
4
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$1 : removed call to org/junit/Assert::assertTrue → NO_COVERAGE 3. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 4. lambda$assertEquals$1 : removed call to org/junit/Assert::assertTrue → KILLED |
registry.register(() -> Assert.assertTrue("should be multipart message", content instanceof Multipart)); |
96 | BodyPart part = getAttachmentOrNull((Multipart) content, expected.getName(), registry); | |
97 |
2
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE 2. assertEquals : removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → KILLED |
assertEquals(expected, part, registry); |
98 | } | |
99 | ||
100 | private static void assertEquals(ExpectedAttachment expected, BodyPart attachment, AssertionRegistry registry) throws Exception { | |
101 | // @formatter:off | |
102 |
2
1. assertEquals : negated conditional → NO_COVERAGE 2. assertEquals : negated conditional → KILLED |
String prefix = "attachment named '" + expected.getName() + "'" + (attachment==null ? " (/!\\ not found)" : ""); |
103 |
4
1. assertEquals : negated conditional → NO_COVERAGE 2. assertEquals : negated conditional → NO_COVERAGE 3. assertEquals : negated conditional → KILLED 4. assertEquals : negated conditional → KILLED |
String contentType = attachment == null || attachment.getContentType()==null ? null : attachment.getContentType(); |
104 |
8
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$2 : negated conditional → NO_COVERAGE 3. lambda$assertEquals$2 : negated conditional → NO_COVERAGE 4. lambda$assertEquals$2 : removed call to org/junit/Assert::assertTrue → NO_COVERAGE 5. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 6. lambda$assertEquals$2 : negated conditional → KILLED 7. lambda$assertEquals$2 : negated conditional → KILLED 8. lambda$assertEquals$2 : removed call to org/junit/Assert::assertTrue → KILLED |
registry.register(() -> Assert.assertTrue(prefix + " mimetype should match '" + expected.getMimetype() + "' but was " + (contentType==null ? "null" : "'" + contentType + "'"), |
105 |
2
1. lambda$assertEquals$2 : negated conditional → NO_COVERAGE 2. lambda$assertEquals$2 : negated conditional → KILLED |
contentType!=null && expected.getMimetype().matcher(contentType).matches())); |
106 |
4
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$3 : removed call to org/junit/Assert::assertEquals → NO_COVERAGE 3. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 4. lambda$assertEquals$3 : removed call to org/junit/Assert::assertEquals → KILLED |
registry.register(() -> Assert.assertEquals(prefix + " description should be '" + expected.getDescription() + "'", |
107 |
2
1. lambda$assertEquals$3 : negated conditional → NO_COVERAGE 2. lambda$assertEquals$3 : negated conditional → KILLED |
expected.getDescription(), |
108 | attachment == null ? null : attachment.getDescription())); | |
109 |
4
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$4 : removed call to org/junit/Assert::assertEquals → NO_COVERAGE 3. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 4. lambda$assertEquals$4 : removed call to org/junit/Assert::assertEquals → KILLED |
registry.register(() -> Assert.assertEquals(prefix + " disposition should be '" + expected.getDisposition() + "'", |
110 |
2
1. lambda$assertEquals$4 : negated conditional → NO_COVERAGE 2. lambda$assertEquals$4 : negated conditional → KILLED |
expected.getDisposition(), |
111 | attachment == null ? null : attachment.getDisposition())); | |
112 |
4
1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. lambda$assertEquals$5 : removed call to org/junit/Assert::assertArrayEquals → NO_COVERAGE 3. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED 4. lambda$assertEquals$5 : removed call to org/junit/Assert::assertArrayEquals → KILLED |
registry.register(() -> Assert.assertArrayEquals(prefix + " has invalid content", |
113 |
2
1. lambda$assertEquals$5 : negated conditional → NO_COVERAGE 2. lambda$assertEquals$5 : negated conditional → KILLED |
expected.getContent(), |
114 | attachment == null ? null : getContent(attachment))); | |
115 | // @formatter:on | |
116 | } | |
117 | ||
118 | @SuppressWarnings("squid:S2147") // false positive: merging exception | |
119 | // doesn't compile in that case or we | |
120 | // are force to throw Exception instead | |
121 | // of MessagingException | |
122 | private static BodyPart getAttachmentOrNull(Multipart multipart, final String filename, AssertionRegistry registry) throws MessagingException { | |
123 | try { | |
124 |
2
1. getAttachmentOrNull : replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::getAttachmentOrNull → NO_COVERAGE 2. getAttachmentOrNull : replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::getAttachmentOrNull → KILLED |
return getAttachment(multipart, filename); |
125 | } catch (MessagingException e) { | |
126 |
2
1. getAttachmentOrNull : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. getAttachmentOrNull : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED |
registry.register(failure(e)); |
127 | return null; | |
128 | } catch (IllegalStateException e) { | |
129 |
2
1. getAttachmentOrNull : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE 2. getAttachmentOrNull : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → KILLED |
registry.register(failure(e)); |
130 | return null; | |
131 | } | |
132 | } | |
133 | ||
134 | private static <E extends Exception> Executable<E> failure(E exception) { | |
135 |
2
1. failure : replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::failure → NO_COVERAGE 2. failure : replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::failure → KILLED |
return () -> { |
136 | throw exception; | |
137 | }; | |
138 | } | |
139 | ||
140 | private AssertAttachment() { | |
141 | super(); | |
142 | } | |
143 | } | |
Mutations | ||
40 |
1.1 2.2 3.3 4.4 |
|
41 |
1.1 2.2 3.3 4.4 |
|
42 |
1.1 2.2 |
|
64 |
1.1 2.2 |
|
65 |
1.1 2.2 |
|
89 |
1.1 2.2 |
|
90 |
1.1 2.2 |
|
94 |
1.1 2.2 |
|
95 |
1.1 2.2 3.3 4.4 |
|
97 |
1.1 2.2 |
|
102 |
1.1 2.2 |
|
103 |
1.1 2.2 3.3 4.4 |
|
104 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
105 |
1.1 2.2 |
|
106 |
1.1 2.2 3.3 4.4 |
|
107 |
1.1 2.2 |
|
109 |
1.1 2.2 3.3 4.4 |
|
110 |
1.1 2.2 |
|
112 |
1.1 2.2 3.3 4.4 |
|
113 |
1.1 2.2 |
|
124 |
1.1 2.2 |
|
126 |
1.1 2.2 |
|
129 |
1.1 2.2 |
|
135 |
1.1 2.2 |