]> source.dussan.org Git - sonarqube.git/commitdiff
improve error handling and send report id in submit_report ws response
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 8 Jan 2015 14:20:36 +0000 (15:20 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 8 Jan 2015 15:52:53 +0000 (16:52 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java
server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java
server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/ws/SubmitReportWsActionTest/submit_report.json [new file with mode: 0644]

index 8b74f57af7b12892699587ced2189c50a427724b..2f81aa05fe726941ceab00fcce5b88039529e57b 100644 (file)
@@ -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;
     }
   }
 
index 7a06e6c98f7ad2df6defa04881a79dffe2781080..e03fd90ad84c6453ad975743bffb023f51be323c 100644 (file)
@@ -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();
     }
   }
 }
index ccbec7100666ec26f2fcbf5ce5fa7e047548e8b3..27b93180ce3a569ac79b7ec63981e5fe8cf9c384 100644 (file)
@@ -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();
+  }
 }
index db631fb58ad292f6dffc02f14a663f1ba0080a5a..9c4dbe4ccfa51833f8f420d9ebfc9b01e94ff52b 100644 (file)
 
 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 (file)
index 0000000..2e069ac
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "key": 123
+}
\ No newline at end of file