]> 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 13:38:10 +0000 (13:38 +0000)
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 4f7a8b308d75f96266ac0ffaa1bd42ce5a0960b6..3c4a05d3c196aff21b1c16908ba47f84366591eb 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 efde74dfadf81cb24b48d9a50d80f2c58890985e..ced18781eb948cdce7a6d333cc3a96c6a1e7f2d8 100644 (file)
@@ -41,13 +41,12 @@ import static org.mockito.Mockito.verify;
 
 public class SendActionTest {
 
-
   @Rule
-  public UserSessionRule userSession = UserSessionRule.standalone();
+  public final 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 {
@@ -91,7 +90,8 @@ public class SendActionTest {
   public void throw_ForbiddenException_if_not_system_administrator() {
     userSession.logIn().setNonSystemAdministrator();
 
-    assertThatThrownBy(() -> ws.newRequest().execute())
+    var request = ws.newRequest();
+    assertThatThrownBy(request::execute)
       .isInstanceOf(ForbiddenException.class)
       .hasMessage("Insufficient privileges");
   }
@@ -109,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.");
     }
   }