]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16179 Fix SSF-240
authorJacek <jacek.poreda@sonarsource.com>
Wed, 30 Mar 2022 09:15:17 +0000 (11:15 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 30 Mar 2022 20:03:05 +0000 (20:03 +0000)
(cherry picked from commit 14b32c841b83c80829df2195a555560e1a8de1ea)

server/sonar-webserver-webapi/src/main/java/org/sonar/server/email/ws/SendAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/email/ws/SendActionTest.java

index 9ebc0949e28ddfd99b1325ff0fc56a0ad2dcc0e2..209407dceb8c3eec39993afbd910dec9bd61ac3f 100644 (file)
  */
 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<String> messages = Throwables.getCausalChain(emailException)
-      .stream()
-      .map(Throwable::getMessage)
-      .collect(Collectors.toList());
-    Collections.reverse(messages);
-    return BadRequestException.create(messages);
-  }
-
 }
index 899c3da6e11096121dd3a6fdc6fdc10760f89ff2..1748179edcf82bde0d9f8ae73a76c736a26fa210 100644 (file)
@@ -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.");
     }
   }