import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.ce.task.CeTask;
import org.sonar.db.ce.CeTaskCharacteristicDto;
import org.sonar.server.ce.queue.ReportSubmitter;
+import org.sonar.server.exceptions.ServerException;
import org.sonar.server.ws.WsUtils;
import org.sonarqube.ws.Ce;
+import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static org.apache.commons.lang.StringUtils.abbreviate;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
public class SubmitAction implements CeWsAction {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SubmitAction.class);
+
private static final String PARAM_PROJECT_KEY = "projectKey";
private static final String PARAM_PROJECT_NAME = "projectName";
private static final String PARAM_REPORT_DATA = "report";
.setProjectId(task.getComponent().get().getUuid())
.build();
WsUtils.writeProtobuf(submitResponse, wsRequest, wsResponse);
+ } catch (IllegalStateException e) {
+ LOGGER.debug(e.getMessage(), e);
+ throw new ServerException(HTTP_INTERNAL_ERROR, e.getMessage());
}
}
import org.sonar.ce.task.CeTask;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.server.ce.queue.ReportSubmitter;
+import org.sonar.server.exceptions.ServerException;
+import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.JsonAssert;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
verify(reportSubmitter).submit(eq("my_project"), eq("my_project"), anyMap(), any());
}
+
+ @Test
+ public void handle_whenReportSubmitterThrowIllegalStateException_shouldThrowServerException() {
+ when(reportSubmitter.submit(eq("my_project"), eq("my_project"), anyMap(), any()))
+ .thenThrow(new IllegalStateException("Error message"));
+
+ TestRequest request = tester.newRequest()
+ .setParam("projectKey", "my_project")
+ .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
+ .setMediaType(MediaTypes.PROTOBUF)
+ .setMethod("POST");
+
+ assertThatThrownBy(() -> request.execute())
+ .isInstanceOf(ServerException.class)
+ .hasMessage("Error message");
+ }
}