From 2c0215b84f586b78ee922cd3474ed432c0ed7aeb Mon Sep 17 00:00:00 2001 From: Jacek Date: Wed, 30 Mar 2022 11:15:17 +0200 Subject: [PATCH] SONAR-16179 Fix SSF-240 (cherry picked from commit 14b32c841b83c80829df2195a555560e1a8de1ea) --- .../org/sonar/server/email/ws/SendAction.java | 16 +-------- .../sonar/server/email/ws/SendActionTest.java | 36 +++++++++---------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/email/ws/SendAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/email/ws/SendAction.java index 9ebc0949e28..209407dceb8 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/email/ws/SendAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/email/ws/SendAction.java @@ -19,10 +19,6 @@ */ package org.sonar.server.email.ws; -import com.google.common.base.Throwables; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; import org.apache.commons.mail.EmailException; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -32,7 +28,6 @@ import org.sonar.server.notification.email.EmailNotificationChannel; import org.sonar.server.user.UserSession; public class SendAction implements EmailsWsAction { - private static final String PARAM_TO = "to"; private static final String PARAM_SUBJECT = "subject"; private static final String PARAM_MESSAGE = "message"; @@ -75,18 +70,9 @@ public class SendAction implements EmailsWsAction { try { emailNotificationChannel.sendTestEmail(request.mandatoryParam(PARAM_TO), request.param(PARAM_SUBJECT), request.mandatoryParam(PARAM_MESSAGE)); } catch (EmailException emailException) { - throw createBadRequestException(emailException); + throw BadRequestException.create("Configuration invalid: please double check SMTP host, port, login and password."); } response.noContent(); } - private static BadRequestException createBadRequestException(EmailException emailException) { - List messages = Throwables.getCausalChain(emailException) - .stream() - .map(Throwable::getMessage) - .collect(Collectors.toList()); - Collections.reverse(messages); - return BadRequestException.create(messages); - } - } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/SendActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/SendActionTest.java index 899c3da6e11..1748179edcf 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/SendActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/SendActionTest.java @@ -23,7 +23,6 @@ import javax.annotation.Nullable; import org.apache.commons.mail.EmailException; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.server.ws.WebService; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; @@ -33,6 +32,7 @@ import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; @@ -42,14 +42,11 @@ import static org.mockito.Mockito.verify; public class SendActionTest { @Rule - public ExpectedException expectedException = ExpectedException.none(); + public final UserSessionRule userSession = UserSessionRule.standalone(); - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - private EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class); + private final EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class); - private WsActionTester ws = new WsActionTester(new SendAction(userSession, emailNotificationChannel)); + private final WsActionTester ws = new WsActionTester(new SendAction(userSession, emailNotificationChannel)); @Test public void send_test_email() throws Exception { @@ -73,28 +70,30 @@ public class SendActionTest { public void fail_when_to_param_is_missing() { logInAsSystemAdministrator(); - expectedException.expect(IllegalArgumentException.class); - - executeRequest(null, "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000"); + assertThatThrownBy(() -> { + executeRequest(null, "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000"); + }) + .isInstanceOf(IllegalArgumentException.class); } @Test public void fail_when_message_param_is_missing() { logInAsSystemAdministrator(); - expectedException.expect(IllegalArgumentException.class); - - executeRequest("john@doo.com", "Test Message from SonarQube", null); + assertThatThrownBy(() -> { + executeRequest("john@doo.com", "Test Message from SonarQube", null); + }) + .isInstanceOf(IllegalArgumentException.class); } @Test public void throw_ForbiddenException_if_not_system_administrator() { userSession.logIn().setNonSystemAdministrator(); - expectedException.expect(ForbiddenException.class); - expectedException.expectMessage("Insufficient privileges"); - - ws.newRequest().execute(); + TestRequest testRequest = ws.newRequest(); + assertThatThrownBy(testRequest::execute) + .isInstanceOf(ForbiddenException.class) + .hasMessage("Insufficient privileges"); } @Test @@ -110,8 +109,7 @@ public class SendActionTest { executeRequest("john@doo.com", "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000"); fail(); } catch (BadRequestException e) { - assertThat(e.errors()).containsExactly( - "root cause", "parent cause", "child cause", "last message"); + assertThat(e.errors()).containsExactly("Configuration invalid: please double check SMTP host, port, login and password."); } } -- 2.39.5