*/
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;
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";
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);
- }
-
}
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 {
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");
}
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.");
}
}