Handle null host value in MailHealthIndicator

If both the host and port are omitted from the mail properties, the
`location` field will be omitted from the health indicator details.

Fixes gh-38007
This commit is contained in:
Scott Frederick
2023-10-25 13:52:36 -05:00
parent aaaafc6ede
commit eae95f8d17
2 changed files with 57 additions and 2 deletions

View File

@@ -44,6 +44,7 @@ import static org.mockito.Mockito.mock;
*
* @author Johannes Edmeier
* @author Stephane Nicoll
* @author Scott Frederick
*/
class MailHealthIndicatorTests {
@@ -61,6 +62,52 @@ class MailHealthIndicatorTests {
this.indicator = new MailHealthIndicator(this.mailSender);
}
@Test
void smtpOnDefaultHostAndPortIsUp() {
given(this.mailSender.getHost()).willReturn(null);
given(this.mailSender.getPort()).willReturn(-1);
given(this.mailSender.getProtocol()).willReturn("success");
Health health = this.indicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails()).doesNotContainKey("location");
}
@Test
void smtpOnDefaultHostAndPortIsDown() throws MessagingException {
given(this.mailSender.getHost()).willReturn(null);
given(this.mailSender.getPort()).willReturn(-1);
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
Health health = this.indicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails()).doesNotContainKey("location");
Object errorMessage = health.getDetails().get("error");
assertThat(errorMessage).isNotNull();
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
}
@Test
void smtpOnDefaultHostAndCustomPortIsUp() {
given(this.mailSender.getHost()).willReturn(null);
given(this.mailSender.getPort()).willReturn(1234);
given(this.mailSender.getProtocol()).willReturn("success");
Health health = this.indicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
}
@Test
void smtpOnDefaultHostAndCustomPortIsDown() throws MessagingException {
given(this.mailSender.getHost()).willReturn(null);
given(this.mailSender.getPort()).willReturn(1234);
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
Health health = this.indicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails().get("location")).isEqualTo(":1234");
Object errorMessage = health.getDetails().get("error");
assertThat(errorMessage).isNotNull();
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
}
@Test
void smtpOnDefaultPortIsUp() {
given(this.mailSender.getPort()).willReturn(-1);