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