From e45fa4541ee97b8a5008451851328c4bf0d55003 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 8 Jan 2015 15:20:36 +0100 Subject: [PATCH] improve error handling and send report id in submit_report ws response --- .../computation/AnalysisReportTask.java | 32 +++++++++--- .../computation/ws/SubmitReportWsAction.java | 8 ++- .../computation/AnalysisReportTaskTest.java | 17 +++++++ .../ws/SubmitReportWsActionTest.java | 50 ++++++++++++------- .../submit_report.json | 3 ++ 5 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/ws/SubmitReportWsActionTest/submit_report.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java index 8b74f57af7b..2f81aa05fe7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java @@ -24,6 +24,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.core.computation.db.AnalysisReportDto; +import javax.annotation.CheckForNull; + public class AnalysisReportTask implements Runnable { private static final Logger LOG = LoggerFactory.getLogger(AnalysisReportTask.class); @@ -37,15 +39,29 @@ public class AnalysisReportTask implements Runnable { @Override public void run() { - AnalysisReportDto report = queue.bookNextAvailable(); + AnalysisReportDto report = bookNextAvailableReport(); if (report != null) { - try { - service.analyzeReport(report); - } catch (Exception exception) { - LOG.error(String.format("Analysis of report %s failed", report), exception); - } finally { - removeSilentlyFromQueue(report); - } + analyzeReport(report); + } + } + + private void analyzeReport(AnalysisReportDto report) { + try { + service.analyzeReport(report); + } catch (Exception exception) { + LOG.error(String.format("Analysis of report %s failed", report), exception); + } finally { + removeSilentlyFromQueue(report); + } + } + + @CheckForNull + private AnalysisReportDto bookNextAvailableReport() { + try { + return queue.bookNextAvailable(); + } catch (Exception e) { + LOG.error("Booking next available analysis report failed", e); + return null; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java index 7a06e6c98f7..e03fd90ad84 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java @@ -24,6 +24,7 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.core.computation.db.AnalysisReportDto; import org.sonar.server.computation.AnalysisReportQueue; import org.sonar.server.computation.AnalysisReportTaskLauncher; @@ -75,8 +76,13 @@ public class SubmitReportWsAction implements ComputationWsAction, RequestHandler String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY); long snapshotId = request.mandatoryParamAsLong(PARAM_SNAPSHOT); try (InputStream reportData = request.paramAsInputStream(PARAM_REPORT_DATA)) { - queue.add(projectKey, snapshotId, reportData); + AnalysisReportDto report = queue.add(projectKey, snapshotId, reportData); taskLauncher.startAnalysisTaskNow(); + response.newJsonWriter() + .beginObject() + .prop("key", report.getId()) + .endObject() + .close(); } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskTest.java index ccbec710066..27b93180ce3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskTest.java @@ -58,4 +58,21 @@ public class AnalysisReportTaskTest { verify(queue).bookNextAvailable(); verify(service).analyzeReport(report); } + + @Test + public void when_the_analysis_throws_an_exception_it_does_not_break_the_task() throws Exception { + AnalysisReportDto report = AnalysisReportDto.newForTests(1L); + when(queue.bookNextAvailable()).thenReturn(report); + doThrow(IllegalStateException.class).when(service).analyzeReport(report); + + sut.run(); + } + + @Test + public void when_the_queue_returns_an_exception_it_does_not_break_the_task() throws Exception { + AnalysisReportDto report = AnalysisReportDto.newForTests(1L); + when(queue.bookNextAvailable()).thenThrow(IllegalStateException.class); + + sut.run(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java index db631fb58ad..9c4dbe4ccfa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java @@ -20,14 +20,13 @@ package org.sonar.server.computation.ws; -import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; -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.computation.db.AnalysisReportDto; import org.sonar.server.computation.AnalysisReportQueue; import org.sonar.server.computation.AnalysisReportTaskLauncher; +import org.sonar.server.ws.WsTester; import java.io.InputStream; @@ -39,15 +38,16 @@ public class SubmitReportWsActionTest { private static final String DEFAULT_PROJECT_KEY = "123456789-987654321"; private SubmitReportWsAction sut; - private AnalysisReportTaskLauncher analysisTaskLauncher; + private WsTester wsTester; + private AnalysisReportTaskLauncher taskLauncher; private AnalysisReportQueue queue; @Before public void before() { - analysisTaskLauncher = mock(AnalysisReportTaskLauncher.class); + taskLauncher = mock(AnalysisReportTaskLauncher.class); queue = mock(AnalysisReportQueue.class); - - sut = new SubmitReportWsAction(queue, analysisTaskLauncher); + sut = new SubmitReportWsAction(queue, taskLauncher); + wsTester = new WsTester(new ComputationWebService(sut)); } @Test @@ -64,18 +64,30 @@ public class SubmitReportWsActionTest { @Test public void add_element_to_queue_and_launch_analysis_task() throws Exception { - Response response = mock(Response.class); - Request request = mock(Request.class); - - when(request.mandatoryParam(SubmitReportWsAction.PARAM_PROJECT_KEY)).thenReturn(DEFAULT_PROJECT_KEY); - when(request.mandatoryParamAsLong(SubmitReportWsAction.PARAM_SNAPSHOT)).thenReturn(123L); - InputStream reportData = IOUtils.toInputStream("report-data"); - when(request.paramAsInputStream(SubmitReportWsAction.PARAM_REPORT_DATA)).thenReturn(reportData); - - sut.handle(request, response); - - verify(queue).add(DEFAULT_PROJECT_KEY, 123L, reportData); - verify(analysisTaskLauncher).startAnalysisTaskNow(); + AnalysisReportDto report = AnalysisReportDto.newForTests(123L); + when(queue.add(any(String.class), anyLong(), any(InputStream.class))).thenReturn(report); + + WsTester.TestRequest request = wsTester + .newGetRequest(ComputationWebService.API_ENDPOINT, "submit_report") + .setParam(SubmitReportWsAction.PARAM_PROJECT_KEY, "789-123") + .setParam(SubmitReportWsAction.PARAM_SNAPSHOT, "456") + .setParam(SubmitReportWsAction.PARAM_REPORT_DATA, null); + request.execute(); + + verify(queue).add(eq("789-123"), eq(456L), any(InputStream.class)); + verify(taskLauncher).startAnalysisTaskNow(); } + @Test + public void return_report_key() throws Exception { + AnalysisReportDto report = AnalysisReportDto.newForTests(123L); + when(queue.add(any(String.class), anyLong(), any(InputStream.class))).thenReturn(report); + + WsTester.TestRequest request = wsTester + .newPostRequest(ComputationWebService.API_ENDPOINT, "submit_report") + .setParam(SubmitReportWsAction.PARAM_PROJECT_KEY, "789") + .setParam(SubmitReportWsAction.PARAM_SNAPSHOT, "456") + .setParam(SubmitReportWsAction.PARAM_REPORT_DATA, null); + request.execute().assertJson(getClass(), "submit_report.json", false); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/SubmitReportWsActionTest/submit_report.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/SubmitReportWsActionTest/submit_report.json new file mode 100644 index 00000000000..2e069ac1028 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/SubmitReportWsActionTest/submit_report.json @@ -0,0 +1,3 @@ +{ + "key": 123 +} \ No newline at end of file -- 2.39.5