17
17
package io .micrometer .jakarta9 .instrument .mail ;
18
18
19
19
import static io .micrometer .jakarta9 .instrument .mail .MailKeyValues .UNKNOWN ;
20
- import static io .micrometer .jakarta9 .instrument .mail .MailKeyValues .EMPTY ;
20
+ import static io .micrometer .jakarta9 .instrument .mail .MailObservationDocumentation .HighCardinalityKeyNames .SMTP_MESSAGE_FROM ;
21
+ import static io .micrometer .jakarta9 .instrument .mail .MailObservationDocumentation .HighCardinalityKeyNames .SMTP_MESSAGE_SUBJECT ;
22
+ import static io .micrometer .jakarta9 .instrument .mail .MailObservationDocumentation .HighCardinalityKeyNames .SMTP_MESSAGE_ID ;
21
23
import static org .assertj .core .api .Assertions .assertThat ;
22
24
23
25
import jakarta .mail .Message ;
@@ -45,15 +47,15 @@ void smtpMessageFrom_WhenFromFails_ReturnsUnknown() throws MessagingException {
45
47
var message = Mockito .mock (Message .class );
46
48
Mockito .when (message .getFrom ()).thenThrow (new MessagingException ("test exception" ));
47
49
var result = MailKeyValues .smtpMessageFrom (message );
48
- assertThat (result . getValue ()). isEqualTo ( UNKNOWN );
50
+ assertThat (result ). isPresent (). hasValue ( SMTP_MESSAGE_FROM . withValue ( UNKNOWN ) );
49
51
}
50
52
51
53
@ Test
52
54
@ DisplayName ("Should return EMPTY when 'from' address is unset" )
53
55
void smtpMessageFrom_WhenFromUnset_ReturnsNone () {
54
56
var message = new MimeMessage ((Session ) null );
55
57
var result = MailKeyValues .smtpMessageFrom (message );
56
- assertThat (result . getValue ()). isEqualTo ( EMPTY );
58
+ assertThat (result ). isEmpty ( );
57
59
}
58
60
59
61
@ Test
@@ -62,7 +64,7 @@ void smtpMessageFrom_WhenFromEmpty_ReturnsNone() throws MessagingException {
62
64
var message = new MimeMessage ((Session ) null );
63
65
message .addFrom (new InternetAddress [0 ]);
64
66
var result = MailKeyValues .smtpMessageFrom (message );
65
- assertThat (result . getValue ()). isEqualTo ( EMPTY );
67
+ assertThat (result ). isEmpty ( );
66
68
}
67
69
68
70
@ Test
@@ -71,7 +73,7 @@ void smtpMessageFrom_WhenOneFromAddress_ReturnsAddress() throws MessagingExcepti
71
73
var message = new MimeMessage ((Session ) null );
72
74
message .
addFrom (
new InternetAddress [] {
new InternetAddress (
"[email protected] " ) });
73
75
var result = MailKeyValues .smtpMessageFrom (message );
74
- assertThat (
result . getValue ()). isEqualTo ( "[email protected] " );
76
+ assertThat (
result ). isPresent (). hasValue ( SMTP_MESSAGE_FROM . withValue ( "[email protected] " ) );
75
77
}
76
78
77
79
@ Test
@@ -81,7 +83,7 @@ void smtpMessageFrom_WhenMultipleFromAddresses_ReturnsAddresses() throws Messagi
81
83
message .
addFrom (
new InternetAddress [] {
new InternetAddress (
"[email protected] " ),
82
84
new InternetAddress (
"[email protected] " ) });
83
85
var result = MailKeyValues .smtpMessageFrom (message );
84
- assertThat (
result . getValue ()). isEqualTo ( "[email protected] , [email protected] " );
86
+ assertThat (
result ). isPresent (). hasValue ( SMTP_MESSAGE_FROM . withValue ( "[email protected] , [email protected] " ) );
85
87
}
86
88
87
89
@ ParameterizedTest
@@ -90,16 +92,20 @@ void smtpMessageFrom_WhenMultipleFromAddresses_ReturnsAddresses() throws Messagi
90
92
void smtpMessageRecipients_WhenRecipientsUnset_ReturnsNone (RecipientType recipientType ) {
91
93
var message = new MimeMessage ((Session ) null );
92
94
var result = MailKeyValues .smtpMessageRecipients (message , recipientType );
93
- assertThat (result . getValue ()). isEqualTo ( EMPTY );
95
+ assertThat (result ). isEmpty ( );
94
96
}
95
97
96
- @ Test
98
+ @ ParameterizedTest
99
+ @ MethodSource ("recipientTypes" )
97
100
@ DisplayName ("Should return UNKNOWN when recipient retrieval fails" )
98
- void smtpMessageRecipients_WhenRecipientsFail_ReturnsUnknown () throws MessagingException {
101
+ void smtpMessageRecipients_WhenRecipientsFail_ReturnsUnknown (RecipientType recipientType )
102
+ throws MessagingException {
103
+ MailObservationDocumentation .HighCardinalityKeyNames key = MailObservationDocumentation .HighCardinalityKeyNames
104
+ .valueOf ("SMTP_MESSAGE_" + recipientType .toString ().toUpperCase (Locale .ROOT ));
99
105
var message = Mockito .mock (Message .class );
100
106
Mockito .when (message .getRecipients (ArgumentMatchers .any ())).thenThrow (new MessagingException ("test exception" ));
101
- var result = MailKeyValues .smtpMessageRecipients (message , RecipientType . TO );
102
- assertThat (result . getValue ()). isEqualTo ( UNKNOWN );
107
+ var result = MailKeyValues .smtpMessageRecipients (message , recipientType );
108
+ assertThat (result ). isPresent (). hasValue ( key . withValue ( UNKNOWN ) );
103
109
}
104
110
105
111
@ ParameterizedTest
@@ -109,33 +115,35 @@ void smtpMessageRecipients_WhenRecipientsEmpty_ReturnsNone(RecipientType recipie
109
115
var message = new MimeMessage ((Session ) null );
110
116
message .addRecipients (recipientType , new InternetAddress [0 ]);
111
117
var result = MailKeyValues .smtpMessageRecipients (message , recipientType );
112
- assertThat (result . getValue ()). isEqualTo ( EMPTY );
118
+ assertThat (result ). isEmpty ( );
113
119
}
114
120
115
121
@ ParameterizedTest
116
122
@ MethodSource ("recipientTypes" )
117
123
@ DisplayName ("Should return single recipient when one is set" )
118
124
void smtpMessageRecipients_WhenOneRecipient_ReturnsRecipient (RecipientType recipientType )
119
125
throws MessagingException {
126
+ MailObservationDocumentation .HighCardinalityKeyNames key = MailObservationDocumentation .HighCardinalityKeyNames
127
+ .valueOf ("SMTP_MESSAGE_" + recipientType .toString ().toUpperCase (Locale .ROOT ));
120
128
var message = new MimeMessage ((Session ) null );
121
129
message .
addRecipients (
recipientType ,
new InternetAddress [] {
new InternetAddress (
"[email protected] " ) });
122
130
var result = MailKeyValues .smtpMessageRecipients (message , recipientType );
123
- assertThat (
result . getValue ()). isEqualTo ( "[email protected] " );
131
+ assertThat (
result ). isPresent (). hasValue ( key . withValue ( "[email protected] " ) );
124
132
}
125
133
126
134
@ ParameterizedTest
127
135
@ MethodSource ("recipientTypes" )
128
136
@ DisplayName ("Should return multiple recipients when more than one is set" )
129
137
void smtpMessageRecipients_WhenMultipleRecipients_ReturnsRecipients (RecipientType recipientType )
130
138
throws MessagingException {
139
+ MailObservationDocumentation .HighCardinalityKeyNames key = MailObservationDocumentation .HighCardinalityKeyNames
140
+ .valueOf ("SMTP_MESSAGE_" + recipientType .toString ().toUpperCase (Locale .ROOT ));
131
141
var message = new MimeMessage ((Session ) null );
132
142
message .
addRecipients (
recipientType ,
new InternetAddress [] {
new InternetAddress (
"[email protected] " ),
133
143
new InternetAddress (
"[email protected] " ) });
134
144
var result = MailKeyValues .smtpMessageRecipients (message , recipientType );
135
145
136
- var expectedKeyName = "smtp.message." + recipientType .toString ().toLowerCase (Locale .ROOT );
137
- assertThat (result .getKey ()).isEqualTo (expectedKeyName );
138
- assertThat (
result .
getValue ()).
isEqualTo (
"[email protected] , [email protected] " );
146
+ assertThat (
result ).
isPresent ().
hasValue (
key .
withValue (
"[email protected] , [email protected] " ));
139
147
}
140
148
141
149
public static Stream <Arguments > recipientTypes () {
@@ -149,7 +157,7 @@ void smtpMessageSubject_WhenSubjectSet_ReturnsSubject() throws MessagingExceptio
149
157
var message = new MimeMessage ((Session ) null );
150
158
message .setSubject ("test subject" );
151
159
var result = MailKeyValues .smtpMessageSubject (message );
152
- assertThat (result . getValue ()). isEqualTo ( "test subject" );
160
+ assertThat (result ). isPresent (). hasValue ( SMTP_MESSAGE_SUBJECT . withValue ( "test subject" ) );
153
161
}
154
162
155
163
@ Test
@@ -158,15 +166,16 @@ void smtpMessageSubject_WhenSubjectFails_ReturnsUnknown() throws MessagingExcept
158
166
var message = Mockito .mock (Message .class );
159
167
Mockito .when (message .getSubject ()).thenThrow (new MessagingException ("test exception" ));
160
168
var result = MailKeyValues .smtpMessageSubject (message );
161
- assertThat (result . getValue ()). isEqualTo ( UNKNOWN );
169
+ assertThat (result ). isPresent (). hasValue ( SMTP_MESSAGE_SUBJECT . withValue ( UNKNOWN ) );
162
170
}
163
171
164
172
@ Test
165
173
@ DisplayName ("Should return EMPTY when subject is unset" )
166
174
void smtpMessageSubject_WhenSubjectUnset_ReturnsNone () {
167
175
var message = new MimeMessage ((Session ) null );
168
176
var result = MailKeyValues .smtpMessageSubject (message );
169
- assertThat (result .getValue ()).isEqualTo (EMPTY );
177
+ assertThat (result ).isEmpty ();
178
+
170
179
}
171
180
172
181
@ Test
@@ -215,15 +224,15 @@ void smtpMessageId_WhenHeaderFails_ReturnsUnknown() throws MessagingException {
215
224
var message = Mockito .mock (Message .class );
216
225
Mockito .when (message .getHeader ("Message-ID" )).thenThrow (new MessagingException ("test exception" ));
217
226
var result = MailKeyValues .smtpMessageId (message );
218
- assertThat (result . getValue ()). isEqualTo ( UNKNOWN );
227
+ assertThat (result ). isPresent (). hasValue ( SMTP_MESSAGE_ID . withValue ( UNKNOWN ) );
219
228
}
220
229
221
230
@ Test
222
231
@ DisplayName ("Should return EMPTY when Message-ID header is missing" )
223
232
void smtpMessageId_WhenHeaderMissing_ReturnsUnknown () {
224
233
var message = new MimeMessage ((Session ) null );
225
234
var result = MailKeyValues .smtpMessageId (message );
226
- assertThat (result . getValue ()). isEqualTo ( EMPTY );
235
+ assertThat (result ). isEmpty ( );
227
236
}
228
237
229
238
@ Test
@@ -232,7 +241,7 @@ void smtpMessageId_WhenHeaderSet_ReturnsMessageId() throws MessagingException {
232
241
var message = new MimeMessage ((Session ) null );
233
242
message .
addHeader (
"Message-ID" ,
"[email protected] " );
234
243
var result = MailKeyValues .smtpMessageId (message );
235
- assertThat (
result . getValue ()). isEqualTo ( "[email protected] " );
244
+ assertThat (
result ). isPresent (). hasValue ( SMTP_MESSAGE_ID . withValue ( "[email protected] " ) );
236
245
}
237
246
238
247
@ Test
0 commit comments