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.api.server.ws.WebService;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.Message;
import org.sonar.server.notification.email.EmailNotificationChannel;
import org.sonar.server.user.UserSession;
@Override
public void handle(Request request, Response response) throws Exception {
userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
- emailNotificationChannel.sendTestEmail(request.mandatoryParam(PARAM_TO), request.param(PARAM_SUBJECT), request.mandatoryParam(PARAM_MESSAGE));
+ try {
+ emailNotificationChannel.sendTestEmail(request.mandatoryParam(PARAM_TO), request.param(PARAM_SUBJECT), request.mandatoryParam(PARAM_MESSAGE));
+ } catch (EmailException emailException) {
+ throw createBadRequestException(emailException);
+ }
response.noContent();
}
+ private static BadRequestException createBadRequestException(EmailException emailException) {
+ List<Message> messages = Throwables.getCausalChain(emailException)
+ .stream()
+ .map(e -> Message.of(e.getMessage()))
+ .collect(Collectors.toList());
+ Collections.reverse(messages);
+ return new BadRequestException(messages);
+ }
+
}
package org.sonar.server.email.ws;
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.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.Message;
import org.sonar.server.notification.email.EmailNotificationChannel;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
ws.newRequest().execute();
}
+ @Test
+ public void fail_with_BadRequestException_when_EmailException_is_generated() throws Exception {
+ setUserAsSystemAdmin();
+ IllegalArgumentException exception1 = new IllegalArgumentException("root cause");
+ IllegalArgumentException exception2 = new IllegalArgumentException("parent cause", exception1);
+ IllegalArgumentException exception3 = new IllegalArgumentException("child cause", exception2);
+ EmailException emailException = new EmailException("last message", exception3);
+ doThrow(emailException).when(emailNotificationChannel).sendTestEmail(anyString(), anyString(), anyString());
+
+ try {
+ 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().messages()).extracting(Message::getKey).containsExactly(
+ "root cause", "parent cause", "child cause", "last message");
+ }
+ }
+
@Test
public void test_ws_definition() {
WebService.Action action = ws.getDef();