AssertAttachment.java

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

Mutations

42

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

2.2
Location : lambda$assertEquals$0
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertEquals → NO_COVERAGE

43

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

44

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

66

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE

67

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

91

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE

92

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

97

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

98

1.1
Location : lambda$assertEquals$1
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertTrue → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

100

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::assertEquals → NO_COVERAGE

109

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

110

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

111

1.1
Location : lambda$assertEquals$2
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertTrue → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

3.3
Location : lambda$assertEquals$2
Killed by :
negated conditional → NO_COVERAGE

4.4
Location : lambda$assertEquals$2
Killed by :
negated conditional → NO_COVERAGE

112

1.1
Location : lambda$assertEquals$2
Killed by :
negated conditional → NO_COVERAGE

113

1.1
Location : lambda$assertEquals$3
Killed by :
negated conditional → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

3.3
Location : lambda$assertEquals$3
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertEquals → NO_COVERAGE

116

1.1
Location : lambda$assertEquals$4
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertEquals → NO_COVERAGE

2.2
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

3.3
Location : lambda$assertEquals$4
Killed by :
negated conditional → NO_COVERAGE

119

1.1
Location : lambda$assertEquals$5
Killed by :
negated conditional → NO_COVERAGE

2.2
Location : lambda$assertEquals$5
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertArrayEquals → NO_COVERAGE

3.3
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

134

1.1
Location : getAttachmentOrNull
Killed by :
replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::getAttachmentOrNull → NO_COVERAGE

136

1.1
Location : lambda$getAttachmentOrNull$6
Killed by :
replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::lambda$getAttachmentOrNull$6 → NO_COVERAGE

2.2
Location : getAttachmentOrNull
Killed by :
removed call to fr/sii/ogham/testing/assertion/email/AssertAttachment::registerAndWrap → NO_COVERAGE

139

1.1
Location : getAttachmentOrNull
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

145

1.1
Location : failure
Killed by :
replaced return value with null for fr/sii/ogham/testing/assertion/email/AssertAttachment::failure → NO_COVERAGE

152

1.1
Location : registerAndWrap
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1