]> source.dussan.org Git - sonarqube.git/commitdiff
Move batch/upload_report to api/analysis_reports/submit_report
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 7 Jan 2015 16:11:52 +0000 (17:11 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 7 Jan 2015 16:11:57 +0000 (17:11 +0100)
server/sonar-server/src/main/java/org/sonar/server/batch/BatchWs.java
server/sonar-server/src/main/java/org/sonar/server/batch/UploadReportAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/SubmitReportWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java
server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/report/PublishReportJob.java

index ec85d3495b1c89b157fd8f7365a398702f98bb5a..ae4d87cebf3c4b8a8e873de727ad48648d9c0338 100644 (file)
@@ -35,14 +35,11 @@ public class BatchWs implements WebService {
   private final BatchIndex batchIndex;
   private final GlobalReferentialsAction globalReferentialsAction;
   private final ProjectReferentialsAction projectReferentialsAction;
-  private final UploadReportAction uploadReportAction;
 
-  public BatchWs(BatchIndex batchIndex, GlobalReferentialsAction globalReferentialsAction, ProjectReferentialsAction projectReferentialsAction,
-                 UploadReportAction uploadReportAction) {
+  public BatchWs(BatchIndex batchIndex, GlobalReferentialsAction globalReferentialsAction, ProjectReferentialsAction projectReferentialsAction) {
     this.batchIndex = batchIndex;
     this.globalReferentialsAction = globalReferentialsAction;
     this.projectReferentialsAction = projectReferentialsAction;
-    this.uploadReportAction = uploadReportAction;
   }
 
   @Override
@@ -55,7 +52,6 @@ public class BatchWs implements WebService {
     defineFileAction(controller);
     globalReferentialsAction.define(controller);
     projectReferentialsAction.define(controller);
-    uploadReportAction.define(controller);
 
     controller.done();
   }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/UploadReportAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/UploadReportAction.java
deleted file mode 100644 (file)
index 7a3e3f3..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.server.batch;
-
-import org.apache.commons.io.IOUtils;
-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.server.computation.AnalysisReportQueue;
-import org.sonar.server.computation.AnalysisReportTaskLauncher;
-
-import java.io.InputStream;
-
-public class UploadReportAction implements RequestHandler {
-
-  public static final String UPLOAD_REPORT_ACTION = "upload_report";
-
-  static final String PARAM_PROJECT_KEY = "project";
-  static final String PARAM_SNAPSHOT = "snapshot";
-  static final String PARAM_REPORT_DATA = "report";
-
-  private final AnalysisReportQueue analysisReportQueue;
-  private final AnalysisReportTaskLauncher analysisTaskLauncher;
-
-  public UploadReportAction(AnalysisReportQueue analysisReportQueue, AnalysisReportTaskLauncher analysisTaskLauncher) {
-    this.analysisReportQueue = analysisReportQueue;
-    this.analysisTaskLauncher = analysisTaskLauncher;
-  }
-
-  void define(WebService.NewController controller) {
-    WebService.NewAction action = controller.createAction(UPLOAD_REPORT_ACTION)
-      .setDescription("Update analysis report")
-      .setSince("5.0")
-      .setPost(true)
-      .setInternal(true)
-      .setHandler(this);
-
-    action
-      .createParam(PARAM_PROJECT_KEY)
-      .setRequired(true)
-      .setDescription("Project key")
-      .setExampleValue("org.codehaus.sonar:sonar");
-
-    action
-      .createParam(PARAM_SNAPSHOT)
-      .setRequired(true)
-      .setDescription("Snapshot id")
-      .setExampleValue("123");
-
-    action
-      .createParam(PARAM_REPORT_DATA)
-      .setRequired(false)
-      .setDescription("Report file");
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY);
-    String snapshotId = request.mandatoryParam(PARAM_SNAPSHOT);
-    InputStream reportData = request.paramAsInputStream(PARAM_REPORT_DATA);
-
-    try {
-      analysisReportQueue.add(projectKey, Long.valueOf(snapshotId), reportData);
-      analysisTaskLauncher.startAnalysisTaskNow();
-    } finally {
-      IOUtils.closeQuietly(reportData);
-    }
-  }
-}
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
new file mode 100644 (file)
index 0000000..7a06e6c
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.ws;
+
+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.server.computation.AnalysisReportQueue;
+import org.sonar.server.computation.AnalysisReportTaskLauncher;
+
+import java.io.InputStream;
+
+public class SubmitReportWsAction implements ComputationWsAction, RequestHandler {
+
+  public static final String ACTION = "submit_report";
+  public static final String PARAM_PROJECT_KEY = "projectKey";
+  public static final String PARAM_SNAPSHOT = "snapshot";
+  public static final String PARAM_REPORT_DATA = "report";
+
+  private final AnalysisReportQueue queue;
+  private final AnalysisReportTaskLauncher taskLauncher;
+
+  public SubmitReportWsAction(AnalysisReportQueue queue, AnalysisReportTaskLauncher taskLauncher) {
+    this.queue = queue;
+    this.taskLauncher = taskLauncher;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    WebService.NewAction action = controller.createAction(ACTION)
+      .setDescription("Submit an analysis report to the queue. Report is integrated asynchronously.")
+      .setPost(true)
+      .setInternal(true)
+      .setHandler(this);
+
+    action
+      .createParam(PARAM_PROJECT_KEY)
+      .setRequired(true)
+      .setDescription("Project key")
+      .setExampleValue("org.codehaus.sonar:sonar");
+
+    action
+      .createParam(PARAM_SNAPSHOT)
+      .setRequired(true)
+      .setDescription("Snapshot ID")
+      .setExampleValue("123");
+
+    action
+      .createParam(PARAM_REPORT_DATA)
+      .setRequired(false)
+      .setDescription("Report file. Format is not an API, it changes among SonarQube versions.");
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    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);
+      taskLauncher.startAnalysisTaskNow();
+    }
+  }
+}
index 17ccbaaba93b88bdd1447c1d515821126268c69b..5f2acb4df47d1cf370e5befb5291910a6e1c0bb5 100644 (file)
@@ -96,6 +96,7 @@ import org.sonar.server.computation.ws.QueueWsAction;
 import org.sonar.server.computation.ws.HistoryWsAction;
 import org.sonar.server.computation.ws.ComputationWebService;
 import org.sonar.server.computation.ws.IsQueueEmptyWsAction;
+import org.sonar.server.computation.ws.SubmitReportWsAction;
 import org.sonar.server.config.ws.PropertiesWs;
 import org.sonar.server.dashboard.db.DashboardDao;
 import org.sonar.server.dashboard.db.WidgetDao;
@@ -355,7 +356,7 @@ class ServerComponents {
     pico.addSingleton(GlobalReferentialsAction.class);
     pico.addSingleton(ProjectReferentialsAction.class);
     pico.addSingleton(ProjectReferentialsLoader.class);
-    pico.addSingleton(UploadReportAction.class);
+    pico.addSingleton(SubmitReportWsAction.class);
     pico.addSingleton(BatchWs.class);
 
     // update center
index 48add35421494ec0fb2f768f4ba5b0264c5fe96a..a51eee53dd0a7d7bd2f469e004a02949133889c9 100644 (file)
@@ -29,8 +29,6 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.core.properties.PropertiesDao;
-import org.sonar.server.computation.AnalysisReportQueue;
-import org.sonar.server.computation.AnalysisReportTaskLauncher;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.ws.WsTester;
 
@@ -59,8 +57,7 @@ public class BatchWsTest {
   public void before() throws IOException {
     tester = new WsTester(new BatchWs(batchIndex,
       new GlobalReferentialsAction(mock(DbClient.class), mock(PropertiesDao.class)),
-      new ProjectReferentialsAction(mock(ProjectReferentialsLoader.class)),
-      new UploadReportAction(mock(AnalysisReportQueue.class), mock(AnalysisReportTaskLauncher.class))));
+      new ProjectReferentialsAction(mock(ProjectReferentialsLoader.class))));
   }
 
   @Test
index 0d76bf4487275b9961bbdb90c04af244dd890405..4468e1b7f66ed125518b1c728a9fddf828077619 100644 (file)
@@ -59,7 +59,7 @@ public class GlobalReferentialsActionTest {
     when(dbClient.openSession(false)).thenReturn(session);
     when(dbClient.metricDao()).thenReturn(metricDao);
 
-    tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao), mock(ProjectReferentialsAction.class), mock(UploadReportAction.class)));
+    tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao), mock(ProjectReferentialsAction.class)));
   }
 
   @Test
index d9114192c76adc14132153f24f9cd334c032027a..cffed5b6aa9ade4c8b0463095f6890c6a8529e15 100644 (file)
@@ -44,7 +44,7 @@ public class ProjectReferentialsActionTest {
   @Before
   public void setUp() throws Exception {
     tester = new WsTester(new BatchWs(mock(BatchIndex.class), mock(GlobalReferentialsAction.class),
-      new ProjectReferentialsAction(projectReferentialsLoader), mock(UploadReportAction.class)));
+      new ProjectReferentialsAction(projectReferentialsLoader)));
   }
 
   @Test
diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java
deleted file mode 100644 (file)
index 9d8d68e..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.server.batch;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.tester.ServerTester;
-import org.sonar.server.ws.WsTester;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class UploadReportActionMediumTest {
-
-  @ClassRule
-  public static ServerTester tester = new ServerTester();
-
-  DbClient db;
-  DbSession session;
-
-  WsTester wsTester;
-  WebService.Controller controller;
-
-  @Before
-  public void setUp() throws Exception {
-    tester.clearDbAndIndexes();
-
-    db = tester.get(DbClient.class);
-    session = db.openSession(false);
-
-    wsTester = tester.get(WsTester.class);
-    controller = wsTester.controller(BatchWs.API_ENDPOINT);
-  }
-
-  @After
-  public void after() {
-    MyBatis.closeQuietly(session);
-  }
-
-  @Test
-  public void define() throws Exception {
-    WebService.Action action = controller.action(UploadReportAction.UPLOAD_REPORT_ACTION);
-
-    assertThat(action).isNotNull();
-    assertThat(action.params()).hasSize(3);
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionTest.java
deleted file mode 100644 (file)
index 600b159..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.server.batch;
-
-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.server.computation.AnalysisReportQueue;
-import org.sonar.server.computation.AnalysisReportTaskLauncher;
-
-import java.io.InputStream;
-
-import static org.mockito.Mockito.*;
-
-public class UploadReportActionTest {
-
-  private static final String DEFAULT_PROJECT_KEY = "123456789-987654321";
-  private UploadReportAction sut;
-
-  private AnalysisReportTaskLauncher analysisTaskLauncher;
-  private AnalysisReportQueue queue;
-
-  @Before
-  public void before() {
-    analysisTaskLauncher = mock(AnalysisReportTaskLauncher.class);
-    queue = mock(AnalysisReportQueue.class);
-
-    sut = new UploadReportAction(queue, analysisTaskLauncher);
-  }
-
-  @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(UploadReportAction.PARAM_PROJECT_KEY)).thenReturn(DEFAULT_PROJECT_KEY);
-    when(request.mandatoryParam(UploadReportAction.PARAM_SNAPSHOT)).thenReturn("123");
-    InputStream reportData = IOUtils.toInputStream("report-data");
-    when(request.paramAsInputStream(UploadReportAction.PARAM_REPORT_DATA)).thenReturn(reportData);
-
-    sut.handle(request, response);
-
-    verify(queue).add(DEFAULT_PROJECT_KEY, 123L, reportData);
-    verify(analysisTaskLauncher).startAnalysisTaskNow();
-  }
-
-}
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
new file mode 100644 (file)
index 0000000..db631fb
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+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.server.computation.AnalysisReportQueue;
+import org.sonar.server.computation.AnalysisReportTaskLauncher;
+
+import java.io.InputStream;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.*;
+
+public class SubmitReportWsActionTest {
+
+  private static final String DEFAULT_PROJECT_KEY = "123456789-987654321";
+  private SubmitReportWsAction sut;
+
+  private AnalysisReportTaskLauncher analysisTaskLauncher;
+  private AnalysisReportQueue queue;
+
+  @Before
+  public void before() {
+    analysisTaskLauncher = mock(AnalysisReportTaskLauncher.class);
+    queue = mock(AnalysisReportQueue.class);
+
+    sut = new SubmitReportWsAction(queue, analysisTaskLauncher);
+  }
+
+  @Test
+  public void define_metadata() throws Exception {
+    WebService.Context context = new WebService.Context();
+    WebService.NewController controller = context.createController("api/computation");
+    sut.define(controller);
+    controller.done();
+
+    WebService.Action action = context.controller("api/computation").action("submit_report");
+    assertThat(action).isNotNull();
+    assertThat(action.params()).hasSize(3);
+  }
+
+  @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();
+  }
+
+}
index fdaa2843e9a359d15a79a96db416a55c68cad474..12bdd6d2240e0109161644d1b1c569f93dd72497 100644 (file)
@@ -102,7 +102,7 @@ public class PublishReportJob implements BatchComponent {
     URL url;
     try {
       int snapshotId = resourceCache.get(project.getEffectiveKey()).snapshotId();
-      url = new URL(serverClient.getURL() + "/batch/upload_report?project=" + project.getEffectiveKey() + "&snapshot=" + snapshotId);
+      url = new URL(serverClient.getURL() + "/api/analysis_reports/submit_report?projectKey=" + project.getEffectiveKey() + "&snapshot=" + snapshotId);
     } catch (MalformedURLException e) {
       throw new IllegalArgumentException("Invalid URL", e);
     }